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

      <option
        v-for="(p, index) in programas"
        :key="index"
        :value="p.id_programa"
      >
        {{ p.programa }}
      </option>
    </b-select>
  </b-field>
</template>

<script>
import axios from 'axios'

export default {
  props: {
    deshabilitarOptVacia: { typeof: Boolean, required: false, default: true },
    idProgramaPadre: { type: Number, required: true, default: 0 },
    columnSize: { typeof: String, required: false, default: '' },
  },
  data: () => {
    return {
      programas: [],
      isLoadingSelect: false,
      idPrograma: 0,
    }
  },
  methods: {
    obtenerProgramas() {
      this.isLoadingSelect = true
      this.programas = []
      axios
        .get(`${process.env.api}/institucion-programa`, this.$getToken.token())
        .then((res) => {
          this.programas = res.data
          this.$emit('catalogo-programas', this.programas)
          this.isLoadingSelect = false
        })
        .catch((err) => {
          this.isLoadingSelect = false
        })
    },
  },
  watch: {
    idPrograma(programaSeleccionado) {
      this.$emit('programa-seleccionado', programaSeleccionado)
    },
    idProgramaPadre(nuevoPrograma) {
      this.idPrograma = nuevoPrograma
    },
  },
  created() {
    this.obtenerProgramas()
  },
}
</script>

<style scoped></style>
