<template>
  <v-layout>
    <v-flex xs12>
      <v-card class="mb-2" color="white">
        <v-toolbar color="blue lighten-3" dark height="50px">
          <v-toolbar-title>Test Requirement : {{xtest.name}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormTest(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="25%" class="text-md-center pt-2 pb-2">TEST</th>
                <th class="text-md-center pt-2 pb-2">AKSI</th>
              </tr>
              <tr v-if="tests.length > 0 " v-for="(test,idx) in tests">
                <td class="text-md-left pl-3">{{test.name}}</td>
                <td align="center" class="text-md-center">
                  <v-btn @click="editFormTest(test)" depressed small color="primary">
                    <v-icon>edit</v-icon>
                  </v-btn>
                  <v-btn @click="deleteTest(test)" depressed small color="error">
                    <v-icon>delete</v-icon>
                  </v-btn>
                </td>
              </tr>
              <tr v-if="tests.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="dialogdeletealerttest" 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>
                            {{msgalerttest}}
                          </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="dialogdeletealerttest = false">
                  Tutup
                </v-btn>
                <v-btn color="primary" flat @click="closeDeleteAlertTest()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>


        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogtest" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form Test</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formtesttest" v-model="validtest" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        REQUIREMENT : {{xtest.name}}
                      </v-flex>
                      <v-flex xs12>
                        <v-autocomplete label="Dokter" v-model="testreq" :items="xtestreqs" :search-input.sync="search_testreq" auto-select-first no-filter
                          item-text="M_TestRequirementNames" return-object :loading="isLoading" no-data-text="Pilih Dokter">
                          <template slot="item" slot-scope="{ item }">
                            <v-list-tile-content>
                              <v-list-tile-title v-text="item.M_TestRequirementNames"></v-list-tile-title>
                            </v-list-tile-content>
                          </template>
                        </v-autocomplete>
                      </v-flex>
                      <v-flex>
                        <p v-for="(xerror,idx) in xerrors" class="error pl-2 pr-2" style="color:#fff">{{xerror.msg}}</p>
                      </v-flex>
                    </v-layout>
                </v-card-text>
                <v-card-actions>
                  <v-spacer></v-spacer>
                  <v-btn color="blue darken-1" flat @click="updateDialogFormTest()">Tutup</v-btn>
                  <v-btn color="blue darken-1" flat @click="saveFormTest()">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 = {
    components: {
      'one-date-picker': httpVueLoader('../../common/oneDatePicker.vue')
    },
    data() {
      return {
        query: "",
        items: [],
        headers: [{
            text: "NAME",
            align: "Center",
            sortable: false,
            value: "lab",
            width: "30%",
            class: "blue lighten-4"
          },
          {
            text: "Action",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          }
        ],
        isLoading: false,
        color: "success",
        validtest: false,
        xid: 0,
        name: '',
        search_testreq: '',
        dialogdeletealerttest: false,
        msgalerttest: ""
      };
    },
    computed: {
      tests() {
        return this.$store.state.test.tests
      },
      xtest() {
        return this.$store.state.test.selected_test
      },
      dialogtest() {
        return this.$store.state.test.dialog_form_test
      },
      xerrors() {
        return this.$store.state.test.errors
      },
      xtestreqs() {
        return this.$store.state.test.testreqs
      },
      testreq: {
        get() {
          return this.$store.state.test.testreq
        },
        set(val) {
          this.$store.commit("test/update_testreq", val)
        }
      },
      startComputedDateFormatted() {
        return this.formatDate(this.xstartdate)
      },
      xstartdate: {
        get() {
          return this.$store.state.test.startdate
        },
        set(val) {
          this.$store.commit("test/update_startdate", val)
        }
      },
      endComputedDateFormatted() {
        return this.formatDate(this.xenddate)
      },
      xenddate: {
        get() {
          return this.$store.state.test.enddate
        },
        set(val) {
          this.$store.commit("test/update_enddate", val)
        }
      }
    },
    methods: {
      updateDialogFormTest() {
        this.$store.commit("test/update_dialog_form_test", false)
      },
      openFormTest(val) {
        this.xid = val
        this.name = ''
        this.search_testreq = ''
        this.$store.commit("test/update_testreqs", [])       
        this.testreq = {}
        this.$store.commit("test/update_dialog_form_test", true)
      },
      thr_search_testreq: _.debounce(function () {
        this.$store.dispatch("test/searchtestreq", this.search_testreq)
      }, 2000),
      editFormTest(val) {
        //  this.$store.commit("test/update_error_name", false)
        this.xid = val.id
        this.name = val.name
        this.$store.commit("test/update_testreqs", [{
          M_TestRequirementID: data.M_TestRequirementID,
          M_TestRequirementNames: data.M_TestRequirementNames
        }])
        this.testreq = {
          M_TestRequirementID: data.M_TestRequirementID,
          M_TestRequirementNames: data.M_TestRequirementNames
        }
        this.$store.commit("test/update_dialog_form_test", true)
        this.$store.commit("test/update_startdate", val.M_TestStartDate)
        this.$store.commit("test/update_enddate", val.M_TestEndDate)
      },
      saveFormTest() {
        this.$store.dispatch("test/save", {
          xid: this.xid,
          testreq: this.testreq.M_TestRequirementID,
        })
      },
      set_date2(x) {
        this.startdate = x.new_date
      },
      set_date3(x) {
        this.enddate = x.new_date
      },
      formatDate(date) {
        if (!date) return null

        const [year, month, day] = date.split('-')
        return `${day}-${month}-${year}`
      },
      deFormatedDate(date) {
        if (!date) return null

        const [day, month, year] = date.split('-')
        return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
      },
      updateAlert_success(val) {
        this.$store.commit("test/update_alert_success", val)
      },
      deleteTest(data) {
        this.xid = data.id

        this.msgalerttest = "Yakin, mau hapus test ?"
        this.dialogdeletealerttest = true
      },
      closeDeleteAlertTest() {
        this.$store.dispatch("test/delete", {
          xid: this.xid,
          testid: this.$store.state.test.selected_test.id,
          testname: this.$store.state.test.selected_test.name
        })
        this.dialogdeletealerttest = false
      }
    },
    watch: {
      search_testreq(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.test.update_autocomplete_status == 1) return
        this.thr_search_testreq()
      }
    }
  }
</script>