<template>
  <b-field class="column mb-0 pb-0" label="Módulo" :class="columnSize">
    <b-select
      icon="store"
      :loading="isLoadingSelect"
      v-model="modulo"
      expanded
      rounded
    >
      <option :disabled="deshabilitarOptVacia" :value="objVacio">
        Selecciona una opción
      </option>

      <option v-for="(m, index) in modulos" :key="index" :value="m">
        {{ m.modulo }}
      </option>
    </b-select>
  </b-field>
</template>

<script>
import axios from 'axios'

export default {
  props: {
    deshabilitarOptVacia: { typeof: Boolean, required: false, default: true },
    idInstitucion: { type: Number, required: true, default: 0 },
    moduloPadre: { type: Object, required: true, default: () => ({}) },
    columnSize: { typeof: String, required: false, default: '' },
  },
  data: () => {
    return {
      modulos: [],
      isLoadingSelect: false,
      modulo: {},
      objVacio: {},
    }
  },
  methods: {
    obtenerModulos() {
      this.isLoadingSelect = true
      this.modulos = []
      axios
        .get(
          `${process.env.api}/modulo/modulos?id_institucion=${this.idInstitucion}`
        )
        .then((res) => {
          this.modulos = res.data
          this.$emit('catalogo-modulos', this.modulos)
          this.isLoadingSelect = false
        })
        .catch((err) => {
          this.isLoadingSelect = false
        })
    },
  },
  watch: {
    idInstitucion(nuevoIdInstitucion) {
      if (nuevoIdInstitucion) this.obtenerModulos()
    },
    modulo(moduloSeleccionado) {
      this.$emit('modulo-seleccionado', moduloSeleccionado)
    },
    moduloPadre(nuevoModulo) {
      if (this.$objIsEmpty(nuevoModulo)) this.modulo = this.objVacio
    },
  },
  created() {
    this.modulo = this.objVacio
  },
}
</script>

<style scoped></style>
