<template>
  <div>
    <b-field v-if="alumno.Status.idStatus === 5 && alumno.Programa.acatlan">
      <b-checkbox type="is-info" size="is-medium" v-model="vistoBuenoAcatlan">
        Visto bueno Acatlán
      </b-checkbox>
    </b-field>

    <b-field
      class="mt-5"
      v-if="alumno.Status.idStatus === 1 || alumno.Status.idStatus === 5"
    >
      <b-button
        class="is-success"
        :disabled="
          alumno.Status.idStatus === 5 &&
          alumno.Programa.acatlan &&
          !vistoBuenoAcatlan
        "
        @click="
          imprimirWarning(
            '¿Seguro(a) que quieres confirmar este servicio?',
            confirmarServicio
          )
        "
      >
        Confirmar
      </b-button>
    </b-field>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  props: {
    idServicio: { type: Number, required: true },
    admin: { type: Object, required: true },
    alumno: { type: Object, required: true },
    imprimirError: { type: Function, required: true },
    imprimirMensaje: { type: Function, required: true },
    imprimirWarning: { type: Function, required: true },
    updateIsLoading: { type: Function, required: true },
  },
  data() {
    return { vistoBuenoAcatlan: false }
  },
  methods: {
    confirmarServicio() {
      if (this.alumno.Status.idStatus === 1)
        this.confirmar(this.confirmarPreRegistro)
      else if (this.alumno.Status.idStatus === 5)
        this.confirmar(this.confirmarLiberacion)
    },
    confirmar(funcConfirmar) {
      const data = { idServicio: this.idServicio }

      this.updateIsLoading(true)
      funcConfirmar(data)
        .then((res) => {
          this.updateIsLoading(false)
          this.imprimirMensaje(res.data.message)
          this.$router.push('/admin')
        })
        .catch((err) => {
          this.updateIsLoading(false)
          this.imprimirError(err.response.data)
        })
    },
    confirmarPreRegistro(data) {
      return axios.put(
        `${process.env.api}/servicio/registro`,
        data,
        this.admin.token
      )
    },
    confirmarLiberacion(data) {
      if (this.vistoBuenoAcatlan)
        data.vistoBuenoAcatlan = this.vistoBuenoAcatlan
      return axios.put(
        `${process.env.api}/servicio/liberacion`,
        data,
        this.admin.token
      )
    },
  },
}
</script>

<style scoped></style>
