<template>
  <section class="mb-6">
    <h3 class="is-size-4 mb-3">Crear módulo</h3>

    <div class="box">
      <div class="columns is-align-items-flex-end pl-0 pb-4">
        <b-field class="column mb-0 pb-0" label="Módulo">
          <b-input
            icon="storefront-outline"
            placeholder="Nombre del módulo"
            type="text"
            @keyup.enter.native="warning()"
            v-model="modulo"
            rounded
          />
        </b-field>

        <BotonCrear columnSize="is-3" :disabled="!modulo" :crear="warning" />
      </div>
    </div>
  </section>
</template>

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

export default {
  components: { BotonCrear },
  props: {
    updateActualizarTabla: { type: Function, required: true },
    updateIsLoading: { type: Function, required: true },
    idInstitucion: { type: Number, required: true, default: 0 },
  },
  data() {
    return {
      modulo: '',
    }
  },
  methods: {
    crearModulo() {
      const data = {
        id_institucion: this.idInstitucion,
        modulo: this.modulo,
      }

      this.updateIsLoading(true)
      axios
        .post(`${process.env.api}/modulo`, data, this.$getToken.token())
        .then((res) => {
          this.modulo = ''
          this.updateActualizarTabla(true)
          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
          )
        })
    },
    warning() {
      if (this.modulo)
        this.$alertsGenericos.imprimirWarning(
          this.$buefy,
          '¿Esta segur@ de querer crear este módulo?',
          this.crearModulo
        )
    },
  },
}
</script>
