<template>
  <div>
    <BuscarModulo
      :updateIsLoading="updateIsLoading"
      :updateModulo="updateModulo"
      :admin="admin"
      :modulo="modulo"
    />

    <TablaCarritos
      :carritos="carritos"
      :isLoadingTable="isLoadingTable"
      :onPageChange="onPageChange"
      :page="page"
      :total="total"
    />
  </div>
</template>

<script>
import axios from 'axios'
import BuscarModulo from '@/components/buscar/BuscarModulo'
import TablaCarritos from '@/components/tablas/TablaCarritos'

export default {
  components: { BuscarModulo, TablaCarritos },
  props: {
    admin: { type: Object, required: true },
    updateIsLoading: { type: Function, required: true },
  },
  data() {
    return {
      carritos: [],
      isLoadingTable: false,
      page: 1,
      total: 0,
      modulo: { institucion: {} },
    }
  },
  methods: {
    obtenerCarritos() {
      this.isLoadingTable = true
      axios
        .get(
          `${process.env.api}/carrito/carritos?pagina=1&id_institucion=${this.admin.institucion.id_institucion}&id_modulo=${this.modulo.id_modulo}`,
          this.$getToken.token()
        )
        .then(async (res) => {
          this.carritos = res.data[0]
          this.isLoadingTable = false
        })
        .catch((err) => {
          this.isLoadingTable = false
          this.$alertsGenericos.imprimirError(
            this.$buefy,
            this.$router,
            err.response.data
          )
        })
    },
    onPageChange(page) {
      this.page = page
      this.obtenerCarritos()
    },
    updateModulo(valorObject) {
      this.modulo = valorObject
    },
  },
  watch: {
    modulo() {
      this.obtenerCarritos()
    },
  },
}
</script>
