<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 item-text="M_DifferenceName" return-object :items="xdifs" v-model="xdif" label="Difference"></v-select>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-text-field v-model="dc_validinterval" label="Valid Interval"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-select 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 v-model="dc_upperlimit" label="Upper Limit"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
                <v-text-field v-model="dc_lowerlimit" label="Lower Limit"></v-text-field>
              </v-flex>
              <v-flex xs6 pa-1>
              </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_upperlimit: {
        get() {
          return this.$store.state.deltacheck.upperlimit
        },
        set(val) {
          this.$store.commit("deltacheck/update_upperlimit", val)
        }
      },
      dc_lowerlimit: {
        get() {
          return this.$store.state.deltacheck.lowerlimit
        },
        set(val) {
          this.$store.commit("deltacheck/update_lowerlimit", val)
        }
      },
      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_DeltaCheckT_TestID: this.$store.state.autoverificationlist.selected_autoverificationlist.id,
          T_TestName: this.$store.state.autoverificationlist.selected_autoverificationlist.name,
          Nat_DeltaCheckM_DifferenceID: this.xdif.M_DifferenceID,
          Nat_DeltaCheckValidInterval: this.dc_validinterval,
          Nat_DeltaCheckM_TimeID: this.xtime.M_TimeID,
          Nat_DeltaCheckUpperLimit: this.dc_upperlimit,
          Nat_DeltaCheckLowerLimit: this.dc_lowerlimit
         
        })
      },
      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>