<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">Dokter : {{xdoctor.description}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormSubGroup(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">SUB GRUP</th>
                <th class="text-md-center pt-2 pb-2">AKSI</th>
              </tr>
              <tr v-if="subgroups.length > 0 " v-for="(subgroup,idx) in subgroups">
                <td class="text-md-left pl-3">{{subgroup.description}}</td>
                <td align="center" class="text-md-center">
                  <v-btn @click="editFormSubGroup(subgroup)" depressed small color="primary">
                    <v-icon>edit</v-icon>
                  </v-btn>
                  <v-btn @click="deleteSubGroup(subgroup)" depressed small color="error">
                    <v-icon>delete</v-icon>
                  </v-btn>
                </td>
              </tr>
              <tr v-if="subgroups.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="dialogdeletealertsubgroup" 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>
                            {{msgalertsubgroup}}
                          </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="dialogdeletealertsubgroup = false">
                  Tutup
                </v-btn>
                <v-btn  flat @click="closeDeleteAlertSubGroup()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>


        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogsubgroup" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form SubGroup</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formdoctorsubgroup" v-model="validsubgroup" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        SUB GRUP : {{xdoctor.name}}
                      </v-flex>
                     <v-flex xs12>
                        <v-autocomplete label="Subgroup" v-model="natsubgroup" :items="xnatsubgroups" :search-input.sync="search_natsubgroup" auto-select-first no-filter
                          item-text="Nat_SubGroupNames" return-object :loading="isLoading" no-data-text="Pilih Sub Grup">
                          <template slot="item" slot-scope="{ item }">
                            <v-list-tile-content>
                              <v-list-tile-title v-text="item.Nat_SubGroupNames"></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="updateDialogFormSubGroup()">Tutup</v-btn>
                  <v-btn color="grey darken-1" flat @click="saveFormSubGroup()">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_natsubgroup: '',
        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",
        validsubgroup: false,
        xid: 0,
        name: "",
        startdate: "",
        enddate: "",
        isbill: "",
         menustartdate: false,
      menuenddate: false,
      date: new Date().toISOString().substr(0, 10),

        dialogdeletealertsubgroup: false,
        msgalertsubgroup: ""
      };
    },
    computed: {
      subgroups() {
        return this.$store.state.subgroup.subgroups
      },
      xdoctor() {
        return this.$store.state.doctor.selected_doctor
      },
      dialogsubgroup() {
        return this.$store.state.subgroup.dialog_form_subgroup
      },
      xerrors() {
        return this.$store.state.subgroup.errors
      },
      xnatsubgroups() {
        return this.$store.state.subgroup.natsubgroups
      },
      natsubgroup: {
        get() {
          return this.$store.state.subgroup.natsubgroup
        },
        set(val) {
          this.$store.commit("subgroup/update_natsubgroup", val)
        }
      },
    },
    methods: {
      updateDialogFormSubGroup() {
        this.$store.commit("subgroup/update_dialog_form_subgroup", false)
      },
      openFormSubGroup(val) {
        this.xid = val
        this.search_natsubgroup = ''
        this.$store.commit("subgroup/update_natsubgroups", [])
        this.natsubgroup = {}
        this.$store.commit("subgroup/update_dialog_form_subgroup", true)
      },
      editFormSubGroup(val) {
      //  this.$store.commit("subgroup/update_error_name", false)
        this.xid = val.id
        this.$store.commit("subgroup/update_natsubgroups", [{
          Nat_SubGroupID: val.Nat_SubGroupID,
          Nat_SubGroupNames: val.Nat_SubGroupNames
        }])
        this.natsubgroup = {
          Nat_SubGroupID: val.Nat_SubGroupID,
          Nat_SubGroupNames: val.Nat_SubGroupNames
        }
        this.$store.commit("subgroup/update_dialog_form_subgroup", true)
      },
      saveFormSubGroup() {
        this.$store.dispatch("subgroup/save", {
          xid: this.xid,
          doctorid: this.$store.state.doctor.selected_doctor.id,
          doctorname: this.$store.state.doctor.selected_doctor.name,
          natsubgroupid: this.natsubgroup.Nat_SubGroupID
        })
      },
      thr_search_natsubgroup: _.debounce(function () {
        this.$store.dispatch("subgroup/searchnatsubgroup", this.search_natsubgroup)
      }, 2000),
      updateAlert_success(val) {
        this.$store.commit("doctor/update_alert_success", val)
      },
      deleteSubGroup(data) {
        this.xid = data.id

        this.msgalertsubgroup = "Yakin, mau hapus subgroup ?"
        this.dialogdeletealertsubgroup = true
      },
      closeDeleteAlertSubGroup() {
        this.$store.dispatch("subgroup/delete", {
          xid: this.xid,
          doctorid: this.$store.state.doctor.selected_doctor.id,
          doctorname: this.$store.state.doctor.selected_doctor.name,
          name: this.name
        })
        this.dialogdeletealertsubgroup = false
      }
    },
    watch: {
      search_natsubgroup(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.subgroup.update_autocomplete_status == 1) return
        this.thr_search_natsubgroup()
      }
    }
  }
</script>
