<template>
	<div v-show="visible" class="block">
		<div class="field">
			<label class="label" :class="isAdmin ? 'has-text-white' : ''">Seleccioná tu barrio o grupo de compra</label>
			<div class="control">
				<div class="select">
		        	<select @change="onGDCSelected" v-model="gdc" name="name">
				    	<option :disabled="isDefaultDisabled==1" value=null>Seleccionar</option>
						<option v-for="gdc in gdcs" v-text="gdc.nombre + (isAdmin ? '_admin' : '')"
                                :name="gdc.nombre + (isAdmin ? '_admin' : '')">
                        </option>
					</select>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				region: null,
				gdcs: [],
				isDefaultDisabled: 0,
				gdc: null,
                isAdmin: this.admin == null ? false : this.admin
			}
		},
		mounted() {
	  		Event.$on('region-seleccionada', (region)=>  {
				this.region = region;
	  			this.fillGDC(region);
	  			this.visible = true;
	  		});
		},
		methods : {
			fillGDC(region) {
		    	axios.get("/api/grupos-de-compra").then(response => {
		    		this.gdcs = response.data[this.region];
		    	});
			},
			onGDCSelected() {
				this.isDefaultDisabled = 1;
	  			Event.$emit("gdc-seleccionado",this.gdc);
	  		}
		},
        props: {'admin': Boolean}
	}
</script>
