<template>
  <v-layout v-if="xispreschedule === 'Y'">
    <v-flex xs12>
      <v-card class="mb-2" color="white">
        <v-toolbar color="blue lighten-3" dark height="50px">
          <v-toolbar-title>PRE SCHEDULE PROMISE : {{xschedules.name}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormPreSchedulePromise(0)" icon>
            <v-icon>library_add</v-icon>
          </v-btn>
        </v-toolbar>


        <v-layout row wrap>
          <v-flex class="border-bottom-dashed" xs12 pt-2 pl-4 pr-4 pb-4>
            <table>
              <tr>
                <th width="35%" class="text-md-center pt-2 pb-2">HARI ( DALAM MINGGU )</th>
                <th width="30%" class="text-md-center pt-2 pb-2">JAM</th>
                <th class="text-md-center pt-2 pb-2">AKSI</th>
              </tr>
              <tr v-if="preschedulepromises.length > 0 " v-for="(preschedulepromise,idx) in preschedulepromises">
                <td class="text-md-left pl-3">{{preschedulepromise.dayofweekname}}</td>
                <td class="text-md-center">{{preschedulepromise.xhour}}</td>
                <td align="center" class="text-md-center">
                  <v-btn @click="editFormPreSchedulePromise(preschedulepromise)" depressed small color="primary">
                    <v-icon>edit</v-icon>
                  </v-btn>
                  <v-btn @click="deletePreSchedulePromise(preschedulepromise)" depressed small color="error">
                    <v-icon>delete</v-icon>
                  </v-btn>
                </td>
              </tr>
              <tr v-if="preschedulepromises.length === 0">
                <td align="center" style="height:50px;text-align:center" colspan="8">Belum ada data</td>
              </tr>
            </table>
          </v-flex>
        </v-layout>



        <template>

          <v-dialog v-model="dialogdeletealertpreschedulepromise" max-width="30%">
            <v-card>
              <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                Peringatan !
              </v-card-title>
              <v-card-text class="pt-2 pb-2">
                <v-layout row>
                  <v-flex xs12 d-flex>
                    <v-layout row>
                      <v-flex pb-1 xs12>
                        <v-layout row>
                          <v-flex pt-2 pr-2 xs12>
                            {{msgalertpreschedulepromise}}
                          </v-flex>
                        </v-layout>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-card-text>
              <v-divider></v-divider>
              <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="primary" flat @click="dialogdeletealertpreschedulepromise = false">
                  Tutup
                </v-btn>
                <v-btn color="primary" flat @click="closeDeleteAlertPreSchedulePromise()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>


        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogpreschedulepromise" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form Pre Schedule Promise</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formpreschedulepromise" v-model="validpreschedulepromise" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        SCHEDULE : {{xschedules.name}}
                      </v-flex>
                      <v-flex xs12 sm12 d-flex>
                        <v-select :items="days" v-model="seletedday" item-text="name" item-value="id" return-object label="Hari dalam seminggu"></v-select>
                      </v-flex>
                      <v-flex xs12>
                        <v-text-field label="Jam" v-model="xhour"></v-text-field>
                      </v-flex>
                      <v-flex>
                        <p class="error pl-2 pr-2" style="color:#fff" v-if="xerrorname === true">Hari yang dipilih sudah ada</p>
                      </v-flex>
                    </v-layout>
                </v-card-text>
                <v-card-actions>
                  <v-spacer></v-spacer>
                  <v-btn color="blue darken-1" flat @click="updateDialogFormPreSchedulePromise()">Tutup</v-btn>
                  <v-btn color="blue darken-1" flat @click="saveFormPreSchedulePromise()">Simpan</v-btn>
                </v-card-actions>
                </v-form>
              </v-card>
            </v-dialog>
          </v-layout>
        </template>

      </v-card>
    </v-flex>



  </v-layout>
</template>

<style scoped>
  .searchbox .v-input.v-text-field .v-input__slot {
    min-height: 60px;
  }

  .searchbox .v-btn {
    min-height: 60px;
  }


  table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    background: white;
    border: 0px;
  }

  th,
  td {
    border: 1px solid black;
    border-collapse: collapse;
    padding-top: 2px;
    padding-bottom: 2px;
  }

  table>tr>td {
    padding: 8px;
  }

  table>tr>td:first {
    padding-left: 15px !important;
  }
</style>

<script>
  module.exports = {
    data() {
      return {
        query: "",
        items: [],
        headers: [{
            text: "Day Off Week",
            align: "Center",
            sortable: false,
            value: "mr",
            width: "15%",
            class: " blue lighten-4"
          },
          {
            text: "Start",
            align: "Center",
            sortable: false,
            value: "lab",
            width: "15%",
            class: "blue lighten-4"
          },
          {
            text: "End",
            align: "Center",
            sortable: false,
            value: "name",
            width: "15%",
            class: "blue lighten-4"
          },

          {
            text: "Day",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          },

          {
            text: "Hour",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          },

          {
            text: "Minute",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          },

          {
            text: "At Time",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          },

          {
            text: "Action",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          }
        ],
        isLoading: false,
        color: "success",
        validpreschedulepromise: false,
        days: [
          {
            id: "2",
            name: "Senin"
          },
          {
            id: "3",
            name: "Selasa"
          },
          {
            id: "4",
            name: "Rabu"
          },
          {
            id: "5",
            name: "Kamis"
          },
          {
            id: "6",
            name: "Jumat"
          },
          {
            id: "7",
            name: "Sabtu"
          }
        ],
        seletedday: {
          id: 1,
          name: "Minggu"
        },
        xid: 0,
        starthm: "",
        endhm: "",
        xday: "",
        xhour: "",
        xminute: "",
        flagattime: false,
        attime: "",
        readonlyattime: true,
        readonlyhour: false,
        readonlyminute: false,
        dialogdeletealertpreschedulepromise: false,
        msgalertpreschedulepromise: ""
      };
    },
    computed: {
      xerrorname() {
        return this.$store.state.preschedulepromise.error_name
      },
      preschedulepromises() {
        return this.$store.state.preschedulepromise.preschedulepromise
      },
      xschedules() {
        return this.$store.state.schedule.selected_schedule
      },
      dialogpreschedulepromise() {
        return this.$store.state.preschedulepromise.dialog_form_preschedule_promise
      },
      xispreschedule() {
        return this.$store.state.preschedulepromise.ispreschedule
      }
    },
    methods: {
      updateDialogFormPreSchedulePromise() {
        this.$store.commit("preschedulepromise/update_dialog_form_preschedule_promise", false)
      },
      openFormPreSchedulePromise(val) {
        //console.log("masuk")
        this.xid = val
        this.$store.commit("preschedulepromise/update_error_name", false)
        this.$refs.formpreschedulepromise.reset()
        this.$refs.formpreschedulepromise.resetValidation()
        this.$store.commit("preschedulepromise/update_dialog_form_preschedule_promise", true)
      },
      editFormPreSchedulePromise(data) {
        this.xid = data.id
        let fildays = _.filter(this.days, function (o) {
          return o.id === data.dayofweek
        })
        this.seletedday = fildays[0]
        this.xhour = data.xhour
        this.$store.commit("preschedulepromise/update_dialog_form_preschedule_promise", true)
        this.$store.commit("preschedulepromise/update_error_name", false)
      },
      saveFormPreSchedulePromise() {
        if (this.$refs.formpreschedulepromise.validate()) {
          this.$store.dispatch("preschedulepromise/save", {
            xid: this.xid,
            scheduleid: this.$store.state.schedule.selected_schedule.id,
            schedulename: this.$store.state.schedule.selected_schedule.name,
            dayofweek: this.seletedday.id,
            xhour: this.xhour
          })
        }
      },
      updateAlert_success(val) {
        this.$store.commit("schedule/update_alert_success", val)
      },
      changeAttime(value) {
        this.xhour = value === true ? 0 : this.xhour
        this.xminute = value === true ? 0 : this.xminute
        this.readonlyhour = value === true ? true : false
        this.readonlyminute = value === true ? true : false
        this.attime = value === true ? this.attime : ""
        this.readonlyattime = value === true ? false : true
      },
      deletePreSchedulePromise(data) {
        this.xid = data.id
        let fildays = _.filter(this.days, function (o) {
          return o.id === data.dayofweek
        })
        this.seletedday = fildays[0]
        this.msgalertpreschedulepromise = "Yakin, mau hapus preschedulepromise schedule" + this.xschedules.name + "hari " + this.seletedday
          .name + " ?"
        this.dialogdeletealertpreschedulepromise = true
      },
      closeDeleteAlertPreSchedulePromise() {
        this.$store.dispatch("preschedulepromise/delete", {
          xid: this.xid,
          scheduleid: this.$store.state.schedule.selected_schedule.id,
          schedulename: this.$store.state.schedule.selected_schedule.name
        })
        this.dialogdeletealertpreschedulepromise = false
      }
    }
  }
</script>