<template>
  <v-layout class="fill-height" column>
    <v-toolbar color="blue lighten-3" dark height="50px">
      <v-toolbar-title>LEMBAGA AKREDITASI</v-toolbar-title>
      <v-spacer></v-spacer>
      <v-btn @click="openFormAkreditasi(0)" icon>
        <v-icon>library_add</v-icon>
      </v-btn>
    </v-toolbar>
    <v-snackbar v-model="snackbar">
      {{ msg_snackbar }}
      <v-btn color="pink" text @click="snackbar = false">
        tutup
      </v-btn>
    </v-snackbar>
    <v-card>
      <v-layout row>
        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
          <v-data-table :headers="headers" :items="patients" :loading="isLoading" hide-actions class="elevation-1">
            <template slot="items" slot-scope="props">
              <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                {{props.item.M_AkreditasiKode}}
              </td>
              <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                {{props.item.M_AkreditasiName}}
              </td>
              <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                {{props.item.M_AkreditasiPrefix}}
              </td>
              <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                <v-icon small class="ml-3" color="primary" @click="editFormSubgroup(props.item)">edit</v-icon>
               <v-icon small class="ml-3" color="error" @click="deleteFormSubgroup(props.item)">clear</v-icon>
              </td>
            </template>
          </v-data-table>
        </v-flex>
      </v-layout>
    </v-card>
    <template>
      <v-layout row justify-center>
        <v-dialog v-model="dialogsubgroup" persistent max-width="750px">
          <v-card>
            <v-card-title>
              <span class="headline">FORM LEMBAGA AKREDITASI</span>
            </v-card-title>
            <v-card-text class="pt-0 pb-0">
              <v-form ref="formsubgroupsubgroup" v-model="validsubgroup" lazy-validation>
                <v-layout wrap>
                  <v-flex xs12>
                    <v-text-field v-model="xcode" label="Kode*" :rules="xcodeRules" required></v-text-field>
                  </v-flex>
                  <v-flex xs12>
                    <v-text-field v-model="xname" label="Nama*" :rules="xnameRules" required></v-text-field>
                  </v-flex>
                  <v-flex xs12>
                    <v-text-field v-model="xprefix" label="Prefix*" :rules="xprefixRules" required></v-text-field>
                  </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="updateDialogFormSubgroup()">Tutup</v-btn>
              <v-btn color="blue darken-1" flat @click="saveFormSubgroup()">Simpan</v-btn>
            </v-card-actions>
            </v-form>
          </v-card>
        </v-dialog>
      </v-layout>
    </template>


    <template>

      <v-dialog v-model="dialogdeletealertsubgroup" 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>
                        {{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 color="primary" flat @click="dialogdeletealertsubgroup = false">
              Tutup
            </v-btn>
            <v-btn color="primary" flat @click="closeDeleteAlertSubgroup()">
              Yakin lah
            </v-btn>
          </v-card-actions>
        </v-card>
      </v-dialog>

    </template>

  </v-layout>
</template>



<style scoped>
  .searchbox .v-input.v-text-field .v-input__slot {
    min-height: 60px;
  }

  table.v-table tbody td,
  table.v-table tbody th {
    height: 40px;
  }

  table.v-table thead tr {
    height: 40px;
  }
</style>

<script>
  module.exports = {
    components: {
      'one-dialog-info': httpVueLoader('../../common/oneDialogInfo.vue'),
      'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue'),
      'one-x-check': httpVueLoader('../../common/onexcheck.vue')
    },
    mounted() {
      this.$store.dispatch("samplesend/search", {
        lastid: -1
      })
      this.$store.dispatch("samplesend/selectcategory")
    },
    methods: {
      isSelected(p) {
        return p.M_AkreditasiID == this.$store.state.samplesend.selected_patient.M_AkreditasiID
      },
      searchPatient() {
        this.$store.dispatch("samplesend/search", {
          lastid: -1
        })
      },
      selectMe(pat) {
        var patients = this.$store.state.samplesend.patients
        this.$store.commit("samplesend/update_selected_patient", pat)
        var idx = _.findIndex(patients, function (o) {
          return o.Nat_JpaID == pat.Nat_JpaID && o.M_AkreditasiID == pat.M_AkreditasiID
        })
        this.$store.commit("samplesend/update_last_id", idx)
        this.$store.dispatch("samplestorage/search", {
          code: this.$store.state.samplestorage.code,
          name: this.$store.state.samplestorage.name,
          status: this.$store.state.samplestorage.selected_status.id,
          jpagroup: pat.M_AkreditasiID,
          current_page: 1,
          lastid: -1
        })
      },
      openFormAkreditasi(val) {
        this.xid = val
        this.xname = ''
        this.xcode = ''
        this.xprefix = ''
        this.$store.commit("samplesend/update_dialog_form_subgroup", true)
      },
      editFormSubgroup(val) {
        this.xid = val.id
        this.xname = val.M_AkreditasiName
        this.xcode = val.M_AkreditasiKode
        this.xprefix = val.M_AkreditasiPrefix
        this.$store.commit("samplesend/update_dialog_form_subgroup", true)
      },
      updateDialogFormSubgroup() {
        this.$store.commit("samplesend/update_dialog_form_subgroup", false)
      },
      saveFormSubgroup() {
        if (this.$refs.formsubgroupsubgroup.validate()) {
          if (this.in_saving) return;
          this.in_saving = true;
          this.$store.dispatch("samplesend/save", {
            xid: this.xid,
            name: this.xname,
            kode: this.xcode,
            prefix: this.xprefix
          })
        }

      },
      deleteFormSubgroup(data) {
        this.xid = data.id

        this.msgalertsubgroup = "Yakin, mau hapus kategori ini?"
        this.dialogdeletealertsubgroup = true
      },
      closeDeleteAlertSubgroup() {
        this.$store.dispatch("samplesend/delete", {
          xid: this.xid,
          current_page: this.curr_page,
          ssubgroup: this.xsearch
        })
        this.dialogdeletealertsubgroup = false
      },
      xedit(value) {
        var patients = this.$store.state.samplesend.patients
        var idx = _.findIndex(patients, function (o) {
          return o.Nat_JpaID == pat.Nat_JpaID && o.M_AkreditasiID == pat.M_AkreditasiID
        })
        patients[idx].open_edit = 'Y'
        this.$store.commit("samplesend/update_patients", patients)
      },
      saveEdit(value) {
        var prm = value
        this.$store.dispatch("samplesend/save", prm)
      },
      xdelete(value) {
        var prm = {
          id: value.Nat_JPADetailID,
          status: 'N'
        }
        this.$store.dispatch("samplesend/save", prm)
      },
      insertNewJPA() {
        var prm = {
          id: 0,
          value: this.new_jpa
        }
        this.$store.dispatch("samplesend/savejpagroup", prm)
      },
      saveJPAGroupNew() {
        var prm = {
          id: 0,
          value: this.value_new_jpa_group
        }
        this.$store.dispatch("samplesend/savelistingjpagroup", prm)
      },
      openlisting() {
        this.$store.commit("samplesend/update_dialog_jpa_group", true)
        this.$store.dispatch("samplesend/search_listing", {})
      },
      saveEditJpaGroup(value, status) {
        var prm = {
          id: value.M_AkreditasiID,
          name: value.M_AkreditasiName,
          status: status
        }
        this.$store.dispatch("samplesend/saveEditJpaGroup", prm)
      },
      changeDetail(value) {
        var details = this.$store.state.samplesend.patients
        var idx = _.findIndex(details, function (o) {
          return o.Nat_JpaID == value.Nat_JpaID && o.M_AkreditasiID == value.M_AkreditasiID
        })
        details[idx].Nat_JPADetailDiscount = value.Nat_JPADetailDiscount
        this.$store.commit("samplesend/update_patients", details)
      },
      savedetail() {
        var prm = {
          details: this.$store.state.samplesend.patients
        }
        this.$store.dispatch("samplesend/savealljpadetail", prm)
      },
      changejpagroupinlist(value) {
        var details = this.$store.state.samplesend.jpa_groups
        var idx = _.findIndex(details, function (o) {
          return o.M_AkreditasiID == value.M_AkreditasiID
        })
        details[idx].M_AkreditasiName = value.M_AkreditasiName
        this.$store.commit("samplesend/update_jpa_groups", details)
      }
    },
    computed: {
      snackbar: {
        get() {
          return this.$store.state.samplecall.alert_success
        },
        set(val) {
          this.$store.commit("samplecall/update_alert_success", val)
        }
      },
      jpagroups() {
        return this.$store.state.samplesend.jpa_groups
      },
      value_new_jpa_group: {
        get() {
          return this.$store.state.samplesend.value_new_jpa_group
        },
        set(val) {
          this.$store.commit("samplesend/update_value_new_jpa_group", val)
        }
      },
      dialogjpagrup: {
        get() {
          return this.$store.state.samplesend.dialog_jpa_group
        },
        set(val) {
          this.$store.commit("samplesend/update_dialog_jpa_group", val)
        }
      },
      in_saving: {
        get() {
          return this.$store.state.samplesend.in_saving
        },
        set(val) {
          this.$store.commit("samplesend/update_in_saving", val)
        }
      },
      snackbar: {
        get() {
          return this.$store.state.samplesend.alert_success
        },
        set(val) {
          this.$store.commit("samplesend/update_alert_success", val)
        }
      },
      msg_snackbar: {
        get() {
          return this.$store.state.samplesend.msg_snackbar
        },
        set(val) {
          this.$store.commit("samplesend/update_msg_snackbar", val)
        }
      },
      isLoading() {
        return this.$store.state.samplesend.search_status == 1
      },
      patients() {
        return this.$store.state.samplesend.patients
      },
      xselectedpatient: {
        get() {
          return this.$store.state.samplesend.selected_patient
        },
        set(val) {
          this.$store.commit("samplesend/update_selected_patient", val)
        }
      },
      new_jpa: {
        get() {
          return this.$store.state.samplesend.new_jpa
        },
        set(val) {
          this.$store.commit("samplesend/update_new_jpa", val)
        }
      },
      xerrors() {
        return this.$store.state.samplesend.errors
      },
      dialogsubgroup() {
        return this.$store.state.samplesend.dialog_form_subgroup
      },
      xcategorys() {
        return this.$store.state.samplesend.categorys
      },
      xcategory: {
        get() {
          return this.$store.state.samplesend.category
        },
        set(val) {
          this.$store.commit("samplesend/update_category", val)
        }

      },
    },
    data() {
      return {
        open_edit: false,
        msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
        items: [],
        page: 1,
        ispcr: false,
        iskuota: false,
        isschedule: false,
        xname: '',
        xcode: '',
        xprefix: '',
        xid: 0,
        validsubgroup: false,
        dialogdeletealertsubgroup: false,
        msgalertsubgroup: "",
        xnameRules: [
          v => !!v || 'Nama harus diisi'
        ],
        xcodeRules: [
          v => !!v || 'Kode harus diisi'
        ],
        xprefixRules: [
          v => !!v || 'Prefix harus diisi'
        ],
        headers: [{
            text: "KODE",
            align: "left",
            sortable: false,
            value: "mr",
            width: "10%",
            class: "pa-2 blue lighten-3 white--text"
          },
          {
            text: "NAMA",
            align: "left",
            sortable: false,
            value: "mr",
            width: "20%",
            class: "pa-2 blue lighten-3 white--text"
          },
          {
            text: "PREFIX",
            align: "left",
            sortable: false,
            value: "mr",
            width: "50%",
            class: "pa-2 blue lighten-3 white--text"
          },
          {
            text: "AKSI",
            align: "left",
            sortable: false,
            value: "mr",
            width: "10%",
            class: "pa-2 blue lighten-3 white--text"
          }
        ]
      }
    }
  }
</script>