<template>
  <section>
    <b-table :data="data">
      <b-table-column field="dia" label="Día" centered v-slot="props">
        {{ props.row.dia.dia }}
      </b-table-column>

      <b-table-column
        field="hora_inicio"
        label="Hora Inicio"
        centered
        v-slot="props"
      >
        {{ props.row.hora_inicio }}
      </b-table-column>

      <b-table-column field="hora_fin" label="Hora Fin" centered v-slot="props">
        {{ props.row.hora_fin }}
      </b-table-column>

      <b-table-column field="editar" label="Editar" v-slot="props"
        ><b-button type="is-primary" @click="editarHora(props.row)">
          <b-icon icon="pencil" size="is-small" />
        </b-button>
      </b-table-column>
    </b-table>

    <div class="columns has-text-centered" v-if="mostrar">
      <div class="column">
        <p class="subtitle is-5">Selecciona la hora de inicio</p>
        <b-clockpicker
          type="is-info"
          v-model="horaInicio"
          inline
          :min-time="minTime"
          :max-time="maxTime"
          :locale="locale"
          :hour-format="hourFormat"
        ></b-clockpicker>
      </div>

      <p class="subtitle is-2">{{ dia }}</p>

      <div class="column">
        <p class="subtitle is-5">Selecciona la hora de fin</p>
        <b-clockpicker
          type="is-info"
          v-model="horaFin"
          inline
          :min-time="minTime"
          :max-time="maxTime"
          :locale="locale"
          :hour-format="hourFormat"
        ></b-clockpicker>
      </div>
    </div>

    <div class="has-text-centered">
      <b-button
        class="my-5"
        type="is-success"
        v-if="mostrar"
        @click="cambiarHora"
        >Guardar</b-button
      >
    </div>
  </section>
</template>

<script>
import axios from 'axios'
import moment from 'moment'
export default {
  props: {
    admin: { type: Object, require: true },
    imprimirMensaje: { type: Function, required: true },
    imprimirWarning: { type: Function, required: true },
    imprimirError: { type: Function, required: true },
    updateIsLoading: { type: Function, required: true },
    actualizarTabla: { type: Boolean, required: true },
    updateActualizarTabla: { type: Function, required: true },
  },
  data() {
    const min = new Date()
    min.setHours(7)
    min.setMinutes(0)
    const max = new Date()
    max.setHours(20)
    max.setMinutes(0)
    return {
      data: [],
      horaInicio: new Date(`01-01-2022 09:00`),
      horaFin: new Date(`01-01-2022 17:45`),
      minTime: min,
      maxTime: max,
      mostrar: false,

      dia: '',
      diaSeleccion: {},
      hourFormat: '24', // Browser locale
      locale: 'es-ES', // Browser locale
    }
  },
  methods: {
    obtenerDias() {
      this.updateIsLoading(true)
      axios
        .get(
          `${process.env.api}/institucion-dia/?id_institucion=${this.admin.institucion.id_institucion}`
        )
        .then((res) => {
          this.data = res.data
          this.updateIsLoading(false)
        })
        .catch((err) => {
          this.updateIsLoading(false)
          this.imprimirError(err.response.data)
        })
    },
    editarHora(diaSeleccion) {
      this.dia = diaSeleccion.dia.dia
      this.mostrar = true
      this.diaSeleccion = diaSeleccion
    },
    cambiarHora() {
      const data = {
        id_institucion_dia: this.diaSeleccion.id_institucion_dia,
        hora_inicio: moment(this.horaInicio).format('HH:mm'),
        hora_fin: moment(this.horaFin).format('HH:mm'),
        activo: true,
      }
      this.updateIsLoading(true)
      axios
        .put(`${process.env.api}/institucion-dia`, data)
        .then((res) => {
          this.updateIsLoading(false)
          this.imprimirMensaje(res.data.message)
          this.updateActualizarTabla(true)
        })
        .catch((err) => {
          this.error = 'is-danger'
          this.updateIsLoading(false)
          this.imprimirError(err.response.data)
        })
    },
  },
  watch: {
    actualizarTabla() {
      if (this.actualizarTabla) {
        this.obtenerDias()
        this.updateActualizarTabla(false)
      }
    },
  },
  created() {
    this.obtenerDias()
  },
}
</script>
