<template>
  <b-table
    :data="data"
    :loading="isLoadingTable"
    :selected.sync="selectedModulo"
    :row-class="(row, index) => 'pointer'"
    @details-open="(row, index) => obtenerCarritos(row.id_modulo)"
    show-detail-icon
    paginated
    detailed
  >
    <b-table-column field="modulo" label="Módulo" v-slot="props" centered>
      <span>{{ props.row.modulo }}</span>
    </b-table-column>

    <b-table-column field="activo" label="Activo" v-slot="props" centered>
      <span>{{ props.row.activo ? 'Activo' : 'Inactivo' }}</span>
    </b-table-column>

    <template #detail>
      <TablaCarritos
        :operador="admin"
        :carritos="carritos"
        :isLoadingTable="isLoadingTable"
      />
    </template>
  </b-table>
</template>

<script>
import axios from 'axios'
import BotonDesactivar from '@/components/operador/BotonDesactivar'
import TablaCarritos from '@/components/operador/TablaCarritos'
export default {
  components: {
    BotonDesactivar,
    TablaCarritos,
  },
  props: {
    admin: { typeof: Object, require: true },
    data: { type: Array, required: true },
    updateIsLoading: { type: Function, require: true },
    obtenerCatalogoModulo: { type: Function, required: true },
    updateActualizarTabla: { type: Function, required: true },
    actualizarTabla: { type: Boolean, required: true },
  },
  data() {
    return {
      selectedModulo: {},
      isLoadingTable: false,
      carritos: [],
    }
  },
  methods: {
    obtenerCarritos(id_modulo) {
      this.isLoadingTable = true
      axios
        .get(
          `${process.env.api}/carrito/carritos?pagina=1&id_institucion=${this.admin.institucion.id_institucion}&id_modulo=${id_modulo}`
        )
        .then(async (res) => {
          this.carritos = res.data[0]
          this.isLoadingTable = false
        })
        .catch((err) => {
          this.isLoadingTable = false
          this.$alertsGenericos.imprimirError(this.$buefy, err.response.data)
        })
    },
  },
  watch: {
    selectedModulo() {
      this.$router.push({
        path: 'buscar_modulo/' + this.selectedModulo.id_modulo,
      })
    },
  },
}
</script>

<style>
.pointer {
  cursor: pointer;
}
</style>
