<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">Sub Grup : {{xsubgrup.description}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormDoctor(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="65%" class="text-md-center pt-2 pb-2">DOKTER</th>
                <th class="text-md-center pt-2 pb-2">AKSI</th>
              </tr>
              <tr v-if="doctors.length > 0 " v-for="(doctor,idx) in doctors">
                <td class="text-md-left pl-3">{{doctor.M_DoctorNames}}</td>
                <td align="center" class="text-md-center">
                  <v-btn @click="editFormDoctor(doctor)" depressed small color="primary">
                    <v-icon>edit</v-icon>
                  </v-btn>
                  <v-btn @click="deleteDoctor(doctor)" depressed small color="error">
                    <v-icon>delete</v-icon>
                  </v-btn>
                </td>
              </tr>
              <tr v-if="doctors.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="dialogdeletealertdoctor" 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>
                            {{msgalertdoctor}}
                          </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="dialogdeletealertdoctor = false">
                  Tutup
                </v-btn>
                <v-btn  flat @click="closeDeleteAlertDoctor()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>


        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogdoctor" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form Doctor</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formsubgrupdoctor" v-model="validdoctor" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        SUB GRUP : {{xsubgrup.name}}
                      </v-flex>
                     <v-flex xs12>
                        <v-autocomplete label="Doctor" v-model="mdoctor" :items="xmdoctors" :search-input.sync="search_mdoctor" auto-select-first no-filter
                          item-text="M_DoctorNames" 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_DoctorNames"></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="updateDialogFormDoctor()">Tutup</v-btn>
                  <v-btn color="grey darken-1" flat @click="saveFormDoctor()">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_mdoctor: '',
        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",
        validdoctor: false,
        xid: 0,
        name: "",
        startdate: "",
        enddate: "",
        isbill: "",
         menustartdate: false,
      menuenddate: false,
      date: new Date().toISOString().substr(0, 10),

        dialogdeletealertdoctor: false,
        msgalertdoctor: ""
      };
    },
    computed: {
      doctors() {
        return this.$store.state.doctor.doctors
      },
      xsubgrup() {
        return this.$store.state.subgrup.selected_subgrup
      },
      dialogdoctor() {
        return this.$store.state.doctor.dialog_form_doctor
      },
      xerrors() {
        return this.$store.state.doctor.errors
      },
      xmdoctors() {
        return this.$store.state.doctor.mdoctors
      },
      mdoctor: {
        get() {
          return this.$store.state.doctor.mdoctor
        },
        set(val) {
          this.$store.commit("doctor/update_mdoctor", val)
        }
      },
    },
    methods: {
      updateDialogFormDoctor() {
        this.$store.commit("doctor/update_dialog_form_doctor", false)
      },
      openFormDoctor(val) {
        this.xid = val
        this.search_mdoctor = ''
        this.$store.commit("doctor/update_mdoctors", [])
        this.mdoctor = {}
        this.$store.commit("doctor/update_dialog_form_doctor", true)
      },
      editFormDoctor(val) {
      //  this.$store.commit("doctor/update_error_name", false)
        this.xid = val.id
        this.$store.commit("doctor/update_mdoctors", [{
          M_DoctorID: val.M_DoctorID,
          M_DoctorNames: val.M_DoctorNames
        }])
        this.mdoctor = {
          M_DoctorID: val.M_DoctorID,
          M_DoctorNames: val.M_DoctorNames
        }
        this.$store.commit("doctor/update_dialog_form_doctor", true)
      },
      saveFormDoctor() {
        this.$store.dispatch("doctor/save", {
          xid: this.xid,
          subgrupid: this.$store.state.subgrup.selected_subgrup.id,
          subgrupname: this.$store.state.subgrup.selected_subgrup.name,
          mdoctorid: this.mdoctor.M_DoctorID
        })
      },
      thr_search_mdoctor: _.debounce(function () {
        this.$store.dispatch("doctor/searchmdoctor", this.search_mdoctor)
      }, 2000),
      updateAlert_success(val) {
        this.$store.commit("subgrup/update_alert_success", val)
      },
      deleteDoctor(data) {
        this.xid = data.id

        this.msgalertdoctor = "Yakin, mau hapus doctor ?"
        this.dialogdeletealertdoctor = true
      },
      closeDeleteAlertDoctor() {
        this.$store.dispatch("doctor/delete", {
          xid: this.xid,
          subgrupid: this.$store.state.subgrup.selected_subgrup.id,
          subgrupname: this.$store.state.subgrup.selected_subgrup.name,
          name: this.name
        })
        this.dialogdeletealertdoctor = false
      }
    },
    watch: {
      search_mdoctor(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.doctor.update_autocomplete_status == 1) return
        this.thr_search_mdoctor()
      }
    }
  }
</script>
