<template>
  <b-table
    class="pb-6"
    :data="programas"
    :loading="isLoadingTable"
    :per-page="25"
    :total="total"
    hoverable
    paginated
    striped
  >
    <b-table-column field="programa" label="Software" v-slot="props" centered>
      <p>{{ props.row.programa.programa }}</p>
    </b-table-column>

    <b-table-column
      field="activo"
      label="Status"
      width="400"
      v-slot="props"
      centered
    >
      <BotonDesactivar
        :activarDesactivar="activarDesactivar"
        :row="props.row"
        :msjWarning="`¿Estás segur@ de querer ${
          props.row.mostrar ? 'desactivar' : 'activar'
        } este tipo de software?`"
      />
    </b-table-column>
  </b-table>
</template>

<script>
import axios from 'axios'
import BotonDesactivar from '@/components/botones/BotonDesactivar'

export default {
  components: { BotonDesactivar },
  props: {
    programas: { type: Array, required: true, default: () => [] },
    isLoadingTable: { type: Boolean, required: true, default: false },
    obtenerProgramas: {
      type: Function,
      required: true,
      default: () => {},
    },
    updateIsLoading: { type: Function, required: true, default: () => {} },
    total: { type: Number, required: true, default: 0 },
  },
  methods: {
    activarDesactivar(institucionPrograma) {
      const data = {
        id_institucion_programa: institucionPrograma.id_institucion_programa,
        mostrar: !institucionPrograma.mostrar,
      }

      this.updateIsLoading(true)
      return axios
        .put(
          `${process.env.api}/institucion-programa/`,
          data,
          this.$getToken.token()
        )
        .then((res) => {
          this.obtenerProgramas()
          this.updateIsLoading(false)
          this.$alertsGenericos.imprimirMensaje(this.$buefy, res.data.message)
        })
        .catch((err) => {
          this.updateIsLoading(false)
          this.$alertsGenericos.imprimirError(
            this.$buefy,
            this.$router,
            err.response.data
          )
        })
    },
  },
}
</script>
