<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 class="black--text">Pemeriksaan : {{xtest.name}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormSpecimenLocal(0)" icon>
            <v-icon class="black--text">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="75%" class="text-md-center pt-2 pb-2">SPECIMEN LOCAL</th>
                <th class="text-md-center pt-2 pb-2">AKSI</th>
              </tr>
              <tr v-if="specimenlocals.length > 0 " v-for="(specimenlocal,idx) in specimenlocals">
                <td class="text-md-left pl-3">{{specimenlocal.description}}</td>
                <td align="center" class="text-md-center">
                  <v-btn @click="editFormSpecimenLocal(specimenlocal)" depressed small color="primary">
                    <v-icon>edit</v-icon>
                  </v-btn>
                  <v-btn @click="deleteSpecimenLocal(specimenlocal)" depressed small color="error">
                    <v-icon>delete</v-icon>
                  </v-btn>
                </td>
              </tr>
              <tr v-if="specimenlocals.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="dialogdeletealertspecimenlocal" max-width="30%">
            <v-card>
              <v-card-title class="headline blue lighten-3 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>
                            {{msgalertspecimenlocal}}
                          </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   flat @click="dialogdeletealertspecimenlocal = false">
                  Tutup
                </v-btn>
                <v-btn  flat @click="closeDeleteAlertSpecimenLocal()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>


        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogspecimenlocal" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form SpecimenLocal</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formtestspecimenlocal" v-model="validspecimenlocal" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        SPECIMEN LOKAL : {{xtest.name}}
                      </v-flex>
                     <v-flex xs12>
                        <v-autocomplete label="Specimen" v-model="sampletype" :items="xsampletypes" :search-input.sync="search_sampletype" auto-select-first no-filter
                          item-text="T_SampleTypeNames" return-object :loading="isLoading" no-data-text="Pilih Specimen">
                          <template slot="item" slot-scope="{ item }">
                            <v-list-tile-content>
                              <v-list-tile-title v-text="item.T_SampleTypeNames"></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="grey darken-1" flat @click="updateDialogFormSpecimenLocal()">Tutup</v-btn>
                  <v-btn color="grey darken-1" flat @click="saveFormSpecimenLocal()">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: "",
        search_sampletype: '',
        items: [],
        headers: [{
            text: "CHILD",
            align: "Center",
            sortable: false,
            value: "lab",
            width: "70%",
            class: "blue lighten-4"
          },
          {
            text: "Action",
            align: "Center",
            sortable: false,
            value: "status",
            width: "15%",
            class: "blue lighten-4"
          }
        ],
        isLoading: false,
        color: "success",
        validspecimenlocal: false,
        xid: 0,
        name: "",
        startdate: "",
        enddate: "",
        isbill: "",
         menustartdate: false,
      menuenddate: false,
      date: new Date().toISOString().substr(0, 10),

        dialogdeletealertspecimenlocal: false,
        msgalertspecimenlocal: ""
      };
    },
    computed: {
      specimenlocals() {
        return this.$store.state.specimenlocal.specimenlocals
      },
      xtest() {
        return this.$store.state.test.selected_test
      },
      dialogspecimenlocal() {
        return this.$store.state.specimenlocal.dialog_form_specimenlocal
      },
      xerrors() {
        return this.$store.state.specimenlocal.errors
      },
      xsampletypes() {
        return this.$store.state.specimenlocal.sampletypes
      },
      sampletype: {
        get() {
          return this.$store.state.specimenlocal.sampletype
        },
        set(val) {
          this.$store.commit("specimenlocal/update_sampletype", val)
        }
      },
    },
    methods: {
      updateDialogFormSpecimenLocal() {
        this.$store.commit("specimenlocal/update_dialog_form_specimenlocal", false)
      },
      openFormSpecimenLocal(val) {
        this.xid = val
        this.search_sampletype = ''
        this.$store.commit("specimenlocal/update_sampletypes", [])
        this.sampletype = {}
        this.$store.commit("specimenlocal/update_dialog_form_specimenlocal", true)
      },
      editFormSpecimenLocal(val) {
      //  this.$store.commit("specimenlocal/update_error_name", false)
        this.xid = val.id
        this.$store.commit("specimenlocal/update_sampletypes", [{
          T_SampleTypeID: val.T_SampleTypeID,
          T_SampleTypeNames: val.T_SampleTypeNames
        }])
        this.sampletype = {
          T_SampleTypeID: val.T_SampleTypeID,
          T_SampleTypeNames: val.T_SampleTypeNames
        }
        this.$store.commit("specimenlocal/update_dialog_form_specimenlocal", true)
      },
      saveFormSpecimenLocal() {
        this.$store.dispatch("specimenlocal/save", {
          xid: this.xid,
          testid: this.$store.state.test.selected_test.id,
          testname: this.$store.state.test.selected_test.name,
          sampletypeid: this.sampletype.T_SampleTypeID
        })
      },
      thr_search_sampletype: _.debounce(function () {
        this.$store.dispatch("specimenlocal/searchsampletype", this.search_sampletype)
      }, 2000),
      updateAlert_success(val) {
        this.$store.commit("test/update_alert_success", val)
      },
      deleteSpecimenLocal(data) {
        this.xid = data.id

        this.msgalertspecimenlocal = "Yakin, mau hapus specimenlocal ?"
        this.dialogdeletealertspecimenlocal = true
      },
      closeDeleteAlertSpecimenLocal() {
        this.$store.dispatch("specimenlocal/delete", {
          xid: this.xid,
          testid: this.$store.state.test.selected_test.id,
          testname: this.$store.state.test.selected_test.name,
          name: this.name
        })
        this.dialogdeletealertspecimenlocal = false
      }
    },
    watch: {
      search_sampletype(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.specimenlocal.update_autocomplete_status == 1) return
        this.thr_search_sampletype()
      }
    }
  }
</script>
