<template>
  <b-table
    class="mb-6"
    :current-page="page"
    :data="modulos"
    :loading="isLoadingTable"
    :per-page="25"
    :row-class="(row, index) => 'pointer'"
    :selected.sync="moduloSeleccionado"
    :total="total"
    @page-change="onPageChange"
    backend-pagination
    detailed
    hoverable
    paginated
    show-detail-icon
    striped
  >
    <b-table-column field="modulo" label="Módulo" v-slot="props" centered>
      <p>{{ props.row.modulo }}</p>
    </b-table-column>

    <b-table-column field="activo" label="Activo" v-slot="props" centered>
      <p class="is-size-6 tag" :class="chooseTag(props.row.activo)">
        {{ props.row.activo ? 'Activo' : 'Inactivo' }}
      </p>
    </b-table-column>

    <template #detail="props">
      <CarritosModulo :modulo="props.row" />
    </template>
  </b-table>
</template>

<script>
import CarritosModulo from '@/components/tablas/CarritosModulo'

export default {
  components: { CarritosModulo },
  props: {
    modulos: { type: Array, required: true, default: () => [] },
    isLoadingTable: { type: Boolean, required: true, default: false },
    onPageChange: { type: Function, required: true, default: () => {} },
    page: { type: Number, required: true, default: 1 },
    total: { type: Number, required: true, default: 0 },
  },
  data() {
    return {
      moduloSeleccionado: {},
    }
  },
  methods: {
    chooseTag(activo) {
      return activo ? 'is-success' : 'is-danger'
    },
  },
  watch: {
    moduloSeleccionado() {
      this.$router.push(
        `/admin/modulos/buscar_modulo/${this.moduloSeleccionado.id_modulo}`
      )
    },
  },
}
</script>
