<template>
  <b-table
    :data="data"
    :loading="isLoadingTable"
    class="mb-6"
    show-detail-icon
    paginated
    detailed
  >
    <b-table-column field="carrito" label="Carrito" v-slot="props" centered>
      <span>{{ props.row.carrito }}</span>
    </b-table-column>

    <b-table-column field="modulo" label="Módulo" v-slot="props" centered>
      <span>{{ props.row.modulo.modulo }}</span>
    </b-table-column>

    <b-table-column field="tipo" label="Tipo" v-slot="props" centered>
      <span>{{ props.row.tipoCarrito.tipo_carrito }}</span>
    </b-table-column>

    <b-table-column field="activo" label="Activo" v-slot="props" centered>
      <BotonDesactivar
        :admin="operador"
        :data="props.row"
        tipo="carrito"
        :cambiarStatus="cambiarStatus"
        :imprimirWarning="imprimirWarning"
      />
    </b-table-column>

    <template #detail="props">
      <TablaEquiCarritos :equipo="props.row" />
    </template>
  </b-table>
</template>

<script>
import axios from 'axios'
import BotonDesactivar from '@/components/operador/BotonDesactivar'
import TablaEquiCarritos from '@/components/operador/TablaEquiCarritos'

export default {
  components: { BotonDesactivar, TablaEquiCarritos },
  props: {
    operador: { type: Object, required: true },
    imprimirError: { type: Function, required: true },
    imprimirMensaje: { type: Function, required: true },
    imprimirWarning: { type: Function, required: true },
  },
  data() {
    return {
      isLoadingTable: false,
      data: [],
    }
  },
  methods: {
    cambiarStatus(dataSelect, status) {
      const data = { id_modulo: dataSelect.id_modulo, activo: status }
      this.updateIsLoading(true)

      axios
        .put(`${process.env.api}/modulo`, data)
        .then((res) => {
          this.obtenerCatalogoModulo(this.data[0].institucion.id_institucion)
          this.updateIsLoading(false)
          this.imprimirMensaje(res.data.message)
        })
        .catch((err) => {
          this.updateIsLoading(false)
          this.imprimirError(err.response.data)
        })
    },
    obtenerCarritos() {
      this.isLoadingTable = true
      axios
        .get(`${process.env.api}/carrito/carritos?pagina=1&id_institucion=200`)
        .then(async (res) => {
          this.data = res.data[0]
          this.isLoadingTable = false
        })
        .catch((err) => {
          this.isLoadingTable = false
          this.imprimirError(err.response.data)
        })
    },
  },
  created() {
    this.obtenerCarritos()
  },
}
</script>

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