<template>
  <v-layout v-if="xisdelta === 'Y'">
    <v-flex xs12>
      <v-card class="mb-2" color="white" style="min-height:90px">
        <v-toolbar class="mb-1" color="blue lighten-3" dark height="50px">
          <v-toolbar-title>DELTA CHECK : {{xautoverificationlists.name}}</v-toolbar-title>
          <v-spacer></v-spacer>
        </v-toolbar>
        <v-layout>
          <v-flex xs18 pa-1>
            <v-layout row>
              <v-flex xs6 pa-1>
                <v-select :disabled="ishavedeltacheck === true" item-text="Nat_DeltaTypeName" return-object :items="xdifs" v-model="xdif" label="Difference"></v-select>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-text-field :disabled="ishavedeltacheck === true"  v-model="dc_validinterval" label="Valid Interval"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-select :disabled="ishavedeltacheck === true"  item-text="M_TimeName" return-object :items="xtimes" v-model="xtime" label="Rate Time Unit"></v-select>
              </v-flex>
            </v-layout>
            <v-layout row>
              <v-flex xs6 pa-1>
                <v-text-field :disabled="ishavedeltacheck === true"  v-model="dc_minvalue" label="Min Value"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-text-field :disabled="ishavedeltacheck === true"  v-model="dc_maxvalue" label="Max Value"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
              <v-checkbox v-model="ishavedeltacheck" label="Tidak Punya Delta Check"></v-checkbox>
              </v-flex>
            </v-layout>
          </v-flex>
        </v-layout>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="saveFormDeltaCheck()">Simpan</v-btn>
        </v-card-actions>
      </v-card>
    </v-flex>
  </v-layout>
</template>

<style scoped>
</style>

<script>
  module.exports = {
    data: () => ({
      color: "success",
      validtest: false,
      search: "",
      dialogdeletealerttest: false,
      xid: 0,
      msgalerttest: ""
    }),
    mounted() {
      this.$store.dispatch("deltacheck/selecttime")
      this.$store.dispatch("deltacheck/selectdif")
    },
    computed: {
      xisdelta() {
                return this.$store.state.deltacheck.isdelta
            },
      vdeltachecks() {
        return this.$store.state.deltacheck.deltachecks
      },
      dc_deltadifference: {
        get() {
          return this.$store.state.deltacheck.deltadifference
        },
        set(val) {
          this.$store.commit("deltacheck/update_deltadifference", val)
        }
      },
      dc_deltadifferencepercent: {
        get() {
          return this.$store.state.deltacheck.deltadifferencepercent
        },
        set(val) {
          this.$store.commit("deltacheck/update_deltadifferencepercent", val)
        }
      },
      dc_ratedifference: {
        get() {
          return this.$store.state.deltacheck.ratedifference
        },
        set(val) {
          this.$store.commit("deltacheck/update_ratedifference", val)
        }
      },
      dc_ratedifferencepercent: {
        get() {
          return this.$store.state.deltacheck.ratedifferencepercent
        },
        set(val) {
          this.$store.commit("deltacheck/update_ratedifferencepercent", val)
        }
      },
      dc_validinterval: {
        get() {
          return this.$store.state.deltacheck.validinterval
        },
        set(val) {
          this.$store.commit("deltacheck/update_validinterval", val)
        }
      },
      dc_minvalue: {
        get() {
          return this.$store.state.deltacheck.minvalue
        },
        set(val) {
          this.$store.commit("deltacheck/update_minvalue", val)
        }
      },
      dc_maxvalue: {
        get() {
          return this.$store.state.deltacheck.maxvalue
        },
        set(val) {
          this.$store.commit("deltacheck/update_maxvalue", val)
        }
      },
      ishavedeltacheck: {
        get() {
          return this.$store.state.deltacheck.ishavedeltacheck
        },
        set(val) {
          this.$store.commit("deltacheck/update_ishavedeltacheck", val)
          if(val === true){
            this.$store.commit("deltacheck/update_time", {})
             this.$store.commit("deltacheck/update_dif", {})
             this.$store.commit("deltacheck/update_validinterval", "")
            this.$store.commit("deltacheck/update_deltadifferencepercent", "")
            this.$store.commit("deltacheck/update_ratedifferencepercent", "")
            this.$store.commit("deltacheck/update_minvalue", "")
            this.$store.commit("deltacheck/update_maxvalue", "")
          }
        }
      },
      dialogdeltacheck() {
        return this.$store.state.deltacheck.dialog_form_delta_check
      },
      xselected_test: {
        get() {
          return this.$store.state.deltacheck.selected_test
        },
        set(val) {
          this.$store.commit("deltacheck/update_selected_test", val)
        }
      },
      xautoverificationlists() {
        return this.$store.state.autoverificationlist.selected_autoverificationlist
      },
      xtests() {
        return this.$store.state.deltacheck.tests
      },
      is_loading() {
        return this.$store.state.deltacheck.search_status == 1
      },
      xtimes() {
        return this.$store.state.deltacheck.times
      },
      xtime: {
        get() {
          return this.$store.state.deltacheck.time
        },
        set(val) {
          this.$store.commit("deltacheck/update_time", val)
        }

      },
      xdifs() {
        return this.$store.state.deltacheck.difs
      },
      xdif: {
        get() {
          return this.$store.state.deltacheck.dif
        },
        set(val) {
          this.$store.commit("deltacheck/update_dif", val)
        }

      }
    },
    methods: {
      updateDialogFormScheduleTest() {
        this.$store.commit("deltacheck/update_dialog_form_delta_check", false)
      },
      openFormScheduleTest() {
        this.$store.commit("deltacheck/update_selected_test", {})
        this.$refs.formdeltacheck.reset()
        this.$refs.formdeltacheck.resetValidation()
        this.$store.commit("deltacheck/update_dialog_form_delta_check", true)
      },
      saveFormDeltaCheck() {
        this.$store.dispatch("deltacheck/save", {
          Nat_DeltaCheckNat_TestID: this.$store.state.autoverificationlist.selected_autoverificationlist.id,
          Nat_TestName: this.$store.state.autoverificationlist.selected_autoverificationlist.name,
          Nat_DeltaCheckNat_DeltaTypeID: this.xdif.Nat_DeltaTypeID,
          Nat_DeltaCheckInterval: this.dc_validinterval,
          Nat_DeltaCheckM_TimeID: this.xtime.M_TimeID,
          Nat_DeltaCheckMinValue: this.dc_minvalue,
          Nat_DeltaCheckMaxValue: this.dc_maxvalue,
          Nat_DeltaCheckHaveDeltaCheck: this.ishavedeltacheck === true ? "N" : "Y",
          search: this.$store.state.autoverificationlist.xsearch
         
        })
      },
      updateAlert_success(val) {
        this.$store.commit("autoverificationlist/update_alert_success", val)
      },
      thr_search: _.debounce(function () {
        this.$store.dispatch("deltacheck/search", {
          id: this.$store.state.autoverificationlist.selected_autoverificationlist.id
        })
      }, 2000),
      deleteTest(data) {
        this.xid = data.id
        console.log(this.xid)
        this.$store.commit("deltacheck/update_selected_test", data)
        this.msgalerttest = "Yakin, mau hapus pemeriksaan " + this.xselected_test.name + "dari autoverificationlist " +
          this.xautoverificationlists.name + "  ?"
        this.dialogdeletealerttest = true
      },
      closeDeleteAlertTest() {
        this.$store.dispatch("deltacheck/delete", {
          xid: this.xid,
          autoverificationlistid: this.$store.state.autoverificationlist.selected_autoverificationlist.id,
          autoverificationlistname: this.$store.state.autoverificationlist.selected_autoverificationlist.name,
          testname: this.xselected_test.name
        })
        this.dialogdeletealerttest = false
      }
    },
    watch: {
      search(val, old) {
        console.log(val)
        if (val !== "" && val !== old && val && val.length > 2) {
          this.$store.commit("deltacheck/update_search", val)
          this.thr_search()
        }
      }
    }
  }
</script>