<template>
  <div>
    <v-snackbar v-model="snackbar" bottom timeout="2000">
      Data berhasil diubah
      <v-btn color="pink" flat @click="snackbar = false"> Close </v-btn>
    </v-snackbar>
    <v-toolbar  >
      <v-toolbar-title><b>Setup Grafik</b></v-toolbar-title></v-toolbar-title>
      <v-spacer></v-spacer>
    </v-toolbar>
    <v-card>
      <v-container fluid grid-list-lg>
        <v-layout row>
          <v-flex xs12>
            <v-alert :value="_.isEmpty(selected_patient)" type="warning">
              Pilih Proyek MCU yang ada di listing sebelah kiri
            </v-alert>
          </v-flex>
        </v-layout>
        <v-card class="mb-2" v-if="!_.isEmpty(selected_patient) && (show_generate || show_generate_global)" >
          <v-layout row>
          <v-flex  xs6>
            <v-btn v-if="show_generate" :disabled="process_generate" color="error" dark @click="gengraph()" large>Generate Grafik</v-btn>
          </v-flex>
          <v-flex xs6 class="text-xs-right">
            <v-btn v-if="show_generate_global"  :disabled="process_generate" @click="gengraph_global()" color="warning" dark large>Generate Grafik Global</v-btn>
          </v-flex>
        </v-layout>
        </v-card>
        
        <v-divider></v-divider>
        <v-layout v-if="grafiks.length > 0" wrap row>
          <v-flex v-for="graph in grafiks" xs6>
            <v-card class="pt-3">
              <v-img
                :src="'/one-api/tools/mcu_chartgenerator/get_graph_image/'+graph.Mcu_ImageGrafikID"
                aspect-ratio="1.7" contain
                height="200px"
              >
              </v-img>

              <v-card-title primary-title>
                <div>
                  <div class="subheading">{{ graph.Mcu_ImageGrafikName }}</div>
                </div>
              </v-card-title>
              <v-divider></v-divider>
              <v-card-actions>
                <v-switch
                  v-model="graph.selected"
                  :label="`${graph.selected?'Tampilkan':'Tidak Ditampilkan'}`"
                  @change="changeShow(graph,$event)"
                ></v-switch>
                <v-spacer></v-spacer>
              </v-card-actions>
            </v-card>
          </v-flex>
        </v-layout>
      </v-container>
    </v-card>
  </div>
</template>

<style scoped>
table,
td,
th {
  border: 1px solid #ddd;
  text-align: left;
}

table {
  border-collapse: collapse;
  width: 100%;
}

th,
td {
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 8px;
  padding-right: 5px;
}

.mini-input .v-input {
  margin-top: 0px;
}

.mini-input .v-input,
.mini-input .v-input--selection-controls,
.mini-input .v-input__slot {
  margin-top: 0px;
  margin-bottom: 0px;
  margin-left: 3px;
}

.mini-input .v-messages {
  min-height: 0px;
}

input.fhm-input {
  border: 1px solid black;
  border-radius: 2px;
  -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1), 0 0 4px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1), 0 0 4px rgba(0, 0, 0, 0.1);
  box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1), 0 0 4px rgba(0, 0, 0, 0.1);
  padding: 2px 4px;
  background: rgba(255, 255, 255, 0.5);
  margin: 0 0 1px 0;
  width: 30px;
  text-align: center;
}

.v-date-picker-table {
  height: auto !important;
}
</style>

<script>
module.exports = {
  components: {
    "one-dialog-print": httpVueLoader("../../common/oneDialogPrintX.vue"),
  },
  mounted() {
    this.$store.dispatch("patient/getTemplateFisikMapping");
    this.$store.dispatch("patient/getBranch");
    this.$store.dispatch("patient/getTPriceHeaderCurrent");
  },
  data: () => ({
    // cpone
    search_corporate: "",
    // cpone
    menufilterdatestart: false,
    menufilterdateend: false,
    date: new Date().toISOString().substr(0, 10),
    search_company: "",
    search_doctor: "",
    is_loading: false,
    requiredcompany: false,
    requiredmou: false,
    requireddoctor: false,
    openprint: false,
    urlprint: "",
    clicks: 0,
    result: [],
    delay: 300,
    // show1: true,
    indeterminatex: false,
    headers: [
      {
        text: "EMAIL USER MCU",
        align: "center",
        sortable: false,
        width: "90%",
        class: "pl-2 pt-1 pb-1 pr-2 blue-grey text-xs-center white--text",
      },
      {
        text: "AKSI",
        align: "center",
        sortable: false,
        width: "10%",
        class: "pl-2 pt-1 pb-1 pr-2 blue-grey text-xs-center white--text",
      },
    ],
  }),
  computed: {
    process_generate: {
      get() {
        return this.$store.state.patient.process_generate;
      },
      set(val) {
        this.$store.commit("patient/update_process_generate", val);
      },
    },
    show_generate: {
      get() {
        return this.$store.state.patient.show_generate;
      },
      set(val) {
        this.$store.commit("patient/update_show_generate", val);
      },
    },
    show_generate_global: {
      get() {
        return this.$store.state.patient.show_generate_global;
      },
      set(val) {
        this.$store.commit("patient/update_show_generate_global", val);
      },
    },
    snackbar: {
      get() {
        return this.$store.state.patient.snackbar;
      },
      set(val) {
        this.$store.commit("patient/update_snackbar", val);
      },
    },
    grafiks: {
      get() {
        return this.$store.state.patient.grafiks;
      },
      set(val) {
        this.$store.commit("patient/update_grafiks", val);
      },
    },
    selected_patient() {
      return this.$store.state.patient.selected_patient;
    },
    // cpone
    xgeneratepasswordby() {
      return this.$store.state.patient.generatepasswordby;
    },
    selected_generatepasswordby: {
      get() {
        return this.$store.state.patient.selected_generatepasswordby;
      },
      set(val) {
        this.$store.commit("patient/update_selected_generatepasswordby", val);
      },
    },
    reportresult: {
      get() {
        return this.$store.state.patient.reportresult;
      },
      set(val) {
        this.$store.commit("patient/update_reportresult", val);
      },
    },
    show_delete: {
      get() {
        return this.$store.state.patient.show_delete;
      },
      set(val) {
        this.$store.commit("patient/update_show_delete", val);
      },
    },
    bar_chx_allx: {
      get() {
        return this.$store.state.patient.bar_chx_allx;
      },
      set(val) {
        this.$store.commit("patient/update_bar_chx_allx", val);
      },
    },
    xUserMculists() {
      return this.$store.state.patient.userMcuLists;
    },
    dialogerror: {
      get() {
        return this.$store.state.patient.dialog_error;
      },
      set(val) {
        this.$store.commit("patient/update_dialog_error", val);
      },
    },
    msgerror() {
      return this.$store.state.patient.msg_error;
    },
    xtpriceheaderformCb() {
      return this.$store.state.patient.tpriceheaderformCb;
    },
    selected_tpriceheaderformCb: {
      get() {
        return this.$store.state.patient.selected_tpriceheaderformCb;
      },
      set(val) {
        this.$store.commit("patient/update_selected_tpriceheaderformCb", val);
      },
    },
    selected_detail_paketx: {
      get() {
        return this.$store.state.patient.selected_detail_paketx;
      },
      set(val) {
        this.$store.commit("patient/update_selected_detail_paketx", val);
      },
    },
    dialogconfirmationdetailpaket: {
      get() {
        return this.$store.state.patient.dialog_confirmation_paket;
      },
      set(val) {
        this.$store.commit("patient/update_dialog_confirmation_paket", val);
      },
    },
    xdetailpackets() {
      return this.$store.state.patient.detailpackets;
    },
    inp_Mgm_McuPicEmailValidate: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuPicEmailValidate;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuPicEmailValidate", val);
      },
    },
    inp_Mgm_McuUserEmailValidate: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuUserEmailValidate;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuUserEmailValidate", val);
      },
    },
    xshow1: {
      get() {
        return this.$store.state.patient.show1;
      },
      set(val) {
        this.$store.commit("patient/update_show1", val);
      },
    },
    xcorporates() {
      return this.$store.state.patient.corporates;
    },
    xcorporate: {
      get() {
        return this.$store.state.patient.corporate;
      },
      set(val) {
        this.$store.commit("patient/update_corporate", val);
        // this.$store.dispatch("patient/getmou",this.$store.state.patient.corporate)
      },
    },
    xbranchCb() {
      return this.$store.state.patient.branchs;
    },
    selected_branchCb: {
      get() {
        return this.$store.state.patient.selected_branch;
      },
      set(val) {
        this.$store.commit("patient/update_selected_branch", val);
      },
    },
    xtemplatefisikCb() {
      return this.$store.state.patient.templateFisikCb;
    },
    selected_templateFisikCb: {
      get() {
        return this.$store.state.patient.selected_templateFisikCb;
      },
      set(val) {
        this.$store.commit("patient/update_selected_templateFisikCb", val);
      },
    },
    inp_Mgm_McuNote: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuNote;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuNote", val);
      },
    },
    inp_Mgm_McuFlagRelasiBayarSendiri: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuFlagRelasiBayarSendiri;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuFlagRelasiBayarSendiri", val);
      },
    },
    inp_Mgm_McuBisaTambahPemeriksaan: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuBisaTambahPemeriksaan;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuBisaTambahPemeriksaan", val);
      },
    },
    inp_Mgm_McuLabel: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuLabel;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuLabel", val);
      },
    },
    inp_Mgm_McuPicName: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuPicName;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuPicName", val);
      },
    },
    inp_Mgm_McuPicEmail: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuPicEmail;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuPicEmail", val);
      },
    },
    inp_Mgm_McuUserEmail: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuUserEmail;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuUserEmail", val);
      },
    },
    inp_Mgm_McuPicPassword: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuPicPassword;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuPicPassword", val);
      },
    },
    inp_Mgm_McuUserPassword: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuUserPassword;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuUserPassword", val);
      },
    },
    inp_Mgm_McuTotalParticipant: {
      get() {
        return this.$store.state.patient.inp_Mgm_McuTotalParticipant;
      },
      set(val) {
        this.$store.commit("patient/update_inp_Mgm_McuTotalParticipant", val);
      },
    },
    // cpone
    current_page_test: {
      get() {
        return this.$store.state.patient.current_page_test;
      },
      set(val) {
        this.$store.commit("patient/update_current_page_test", val);
        var prm = this.default_mou;
        prm.search = this.search_test;
        prm.current_page = val;
        this.$store.dispatch("patient/getdatatests", prm);
      },
    },
    current_page_packet: {
      get() {
        return this.$store.state.patient.current_page_packet;
      },
      set(val) {
        this.$store.commit("patient/update_current_page_packet", val);
        // var prm = this.default_mou;
        // prm.T_PriceHeaderID = this.selected_tpriceheaderformCb.T_PriceHeaderID;
        // prm.search = this.search_packet;
        // prm.current_page = val;
        // this.$store.dispatch("patient/getdatapackets", prm);
        this.$store.dispatch("patient/getPackets", {
          T_PriceHeaderID: this.selected_tpriceheaderformCb.T_PriceHeaderID,
          searchPacket: this.search_packet,
          current_page: val,
          lastid: -1,
        });
      },
    },
    packets() {
      return this.$store.state.patient.packets;
    },
    tests() {
      return this.$store.state.patient.tests;
    },
    is_progress() {
      return this.$store.state.patient.is_progress;
    },
    total_patient_packet() {
      return this.$store.state.patient.total_patient_packet;
    },
    total_patient_test() {
      return this.$store.state.patient.total_patient_test;
    },
    search_test: {
      get() {
        return this.$store.state.patient.search_test;
      },
      set(val) {
        this.$store.commit("patient/update_search_test", val);
        this.$store.commit("patient/update_no_save", 1);
      },
    },
    search_packet: {
      get() {
        return this.$store.state.patient.search_packet;
      },
      set(val) {
        this.$store.commit("patient/update_search_packet", val);
        this.$store.commit("patient/update_no_save", 1);
      },
    },
    selected_packets: {
      get() {
        return this.$store.state.patient.selected_packets;
      },
      set(val) {
        this.$store.commit("patient/update_selected_packets", val);
      },
    },
    selected_tests: {
      get() {
        return this.$store.state.patient.selected_tests;
      },
      set(val) {
        this.$store.commit("patient/update_selected_tests", val);
      },
    },
    status() {
      return this.$store.state.patient.status;
    },
    deliveries: {
      get() {
        return this.$store.state.patient.deliveries;
      },
      set(val) {
        this.$store.commit("patient/update_deliveries", val);
      },
    },
    default_mou: {
      get() {
        return this.$store.state.patient.default_mou;
      },
      set(val) {
        this.$store.commit("patient/update_default_mou", val);

        this.$store.commit("patient/update_packets", []);
        this.$store.commit("patient/update_tests", []);
        this.$store.commit("patient/update_selected_packets", []);
        this.$store.commit("patient/update_selected_tests", []);
        this.$store.commit("patient/update_current_page_test", 1);
        this.$store.commit("patient/update_current_page_packet", 1);
        this.$store.commit("patient/update_total_patient_packet", 0);
        this.$store.commit("patient/update_total_patient_test", 0);
        var prm = val;
        prm.T_PriceHeaderID = this.selected_tpriceheaderformCb.T_PriceHeaderID;
        prm.current_page = 1;
        prm.search = "";
        // this.$store.dispatch("patient/getdatapackets", prm);
        this.$store.dispatch("patient/getPackets", {
          T_PriceHeaderID: this.selected_tpriceheaderformCb.T_PriceHeaderID,
          searchPacket: "",
          current_page: 1,
        });
        this.$store.dispatch("patient/getdatatests", prm);

        this.$store.dispatch("patient/getdeliveries", val);
      },
    },
    default_doctor: {
      get() {
        return this.$store.state.patient.default_doctor;
      },
      set(val) {
        this.$store.commit("patient/update_default_doctor", val);
        this.$store.commit("patient/update_doctor_address", val.xaddress);
        this.$store.commit("patient/update_default_doctor_address", {});
      },
    },
    default_doctor_address: {
      get() {
        return this.$store.state.patient.default_doctor_address;
      },
      set(val) {
        this.$store.commit("patient/update_default_doctor_address", val);
      },
    },
    picker_date: {
      get() {
        return this.$store.state.patient.promise_date;
      },
      set(val) {
        this.$store.commit("patient/update_promise_date", val);
      },
    },
    picker_time: {
      get() {
        return this.$store.state.patient.promise_time;
      },
      set(val) {
        this.$store.commit("patient/update_promise_time", val);
      },
    },
    doctor_address() {
      return this.$store.state.patient.doctor_address;
    },
    xseldoctors() {
      return this.$store.state.patient.selected_doctors;
    },
    xselmous() {
      return this.$store.state.patient.selected_mous;
    },
    xordercompanies() {
      return this.$store.state.patient.companies;
    },
    xordercompany: {
      get() {
        return this.$store.state.patient.company;
      },
      set(val) {
        this.$store.commit("patient/update_company", val);
        this.$store.dispatch("patient/getmou", this.$store.state.patient.company);
      },
    },
    xdoctors() {
      return this.$store.state.patient.doctors;
    },
    xdoctor: {
      get() {
        return this.$store.state.patient.doctor;
      },
      set(val) {
        this.$store.commit("patient/update_doctor", val);
      },
    },
    filterComputedDateFormattedStart() {
      return this.formatDate(this.xdatestart);
    },
    filterComputedDateFormattedEnd() {
      return this.formatDate(this.xdateend);
    },
    dialogconfirmationdelete: {
      get() {
        return this.$store.state.patient.dialog_confirmation_delete;
      },
      set(val) {
        this.$store.commit("patient/update_dialog_confirmation_delete", val);
      },
    },
    msgconfirmationdelete() {
      return this.$store.state.patient.msg_confirmation_delete;
    },
    xact() {
      return this.$store.state.patient.act;
    },
    detail() {
      return this.$store.state.patient.selected_patient;
    },
    xdatestart: {
      get() {
        return this.$store.state.patient.start_date;
      },
      set(val) {
        this.$store.commit("patient/update_start_date", val);
        //this.searchTransaction()
      },
    },
    xdateend: {
      get() {
        return this.$store.state.patient.end_date;
      },
      set(val) {
        this.$store.commit("patient/update_end_date", val);
        //this.searchTransaction()
      },
    },
    xmous() {
      return this.$store.state.patient.mous;
    },
    xmou: {
      get() {
        return this.$store.state.patient.mou;
      },
      set(val) {
        this.$store.commit("patient/update_mou", val);
        this.$store.commit("patient/update_no_save", 1);
      },
    },
  },
  methods: {
    gengraph_global(){
      this.process_generate = true
      this.$store.dispatch("patient/gengraph_global", this.selected_patient)
    },
    gengraph(){
      this.process_generate = true
      this.$store.dispatch("patient/gengraph", this.selected_patient)
    },
    changeShow: function(item, value){
      let valnew = value
      this.$store.dispatch("patient/changeShow", {id:item.Mcu_ImageGrafikID, mgm_mcuid:item.Mcu_ImageGrafikMgm_McuID, newval:valnew});
    },
    changeCbxAllX(value) {
      console.log("cek value", value);
      this.show_delete = false;
      var arr = this.xUserMculists;
      this.indeterminatex = false;
      // this.btn_hide = true
      // var filtered_arr =  _.filter(arr, function(o) { return o.status === 'N' })
      arr.forEach((el) => {
        el.chex = value;
      });
      var selected = _.filter(arr, function (o) {
        return o.chex;
      });
      if (selected.length > 0) {
        this.show_delete = true;
      }
    },
    checkTopX() {
      this.show_delete = false;
      var barcodes = this.xUserMculists;
      var selected = _.filter(barcodes, function (o) {
        return o.chex;
      });
      this.bar_chx_allx = false;
      this.indeterminatex = false;
      if (selected.length > 0 && barcodes.length === selected.length) {
        this.bar_chx_allx = true;
        this.indeterminatex = false;
        this.show_delete = true;
      }
      if (selected.length > 0 && barcodes.length > selected.length) {
        this.bar_chx_allx = false;
        this.indeterminatex = true;
        this.show_delete = true;
      }
    },
    deleteUserMcu() {
      var arr = this.$store.state.patient.userMcuLists;
      var selected = _.filter(arr, function (o) {
        return o.chex;
      });
      var deleted = this.$store.state.patient.deleted_usermcus;
      selected.forEach(function (value, index) {
        if (parseInt(value.Mgm_McuUserID) > 0) {
          var idx_del = _.findIndex(
            deleted,
            (itemx) => parseInt(itemx.Mgm_McuUserID) === parseInt(value.Mgm_McuUserID)
          );
          if (idx_del === -1) deleted.push(value);
        }
      });

      this.$store.commit("patient/update_deleted_usermcus", deleted);
      this.show_delete = false;
    },
    changePriceHeaderForm(selectedItem) {
      console.log("Selected item price header:", selectedItem);
      this.$store.commit("patient/update_selected_tpriceheaderformCb", {
        T_PriceHeaderID: selectedItem.T_PriceHeaderID,
        T_PriceHeaderName: selectedItem.T_PriceHeaderName,
        T_PriceHeaderStartDate: selectedItem.T_PriceHeaderStartDate,
        T_PriceHeaderEndDate: selectedItem.T_PriceHeaderEndDate,
        T_PriceHeaderIsActive: selectedItem.T_PriceHeaderIsActive,
        T_PriceHeaderUserID: selectedItem.T_PriceHeaderUserID,
        T_PriceHeaderCreated: selectedItem.T_PriceHeaderCreated,
        T_PriceHeaderCraetdUserID: selectedItem.T_PriceHeaderCraetdUserID,
        T_PriceHeaderLastUpdated: selectedItem.T_PriceHeaderLastUpdated,
        T_PriceHeaderLastUpdatedUserID: selectedItem.T_PriceHeaderLastUpdatedUserID,
        T_PriceHeaderDeleted: selectedItem.T_PriceHeaderDeleted,
        T_PriceHeaderDeletedUserID: selectedItem.T_PriceHeaderDeletedUserID,
      });
      this.$store.commit("patient/update_current_page_packet", 1);

      this.$store.dispatch("patient/getPackets", {
        T_PriceHeaderID: this.selected_tpriceheaderformCb.T_PriceHeaderID,
        searchPacket: this.search_packet,
        current_page: 1,
        lastid: -1,
      });
    },
    closeDialogError() {
      this.$store.commit("patient/update_dialog_error", false);
    },
    convertMoney(money) {
      return one_money(money);
    },
    getDetailPacket(row) {
      // this.clicks++
      // if(this.clicks === 1) {
      //     var self = this
      //     this.timer = setTimeout(function() {
      //         self.result.push(event.type);
      //         self.clicks = 0
      //     }, this.delay);
      //     // this.clicks = 0;
      // } else{
      //     console.log('get detail packet',row)
      //     clearTimeout(this.timer);
      //     this.result.push('dblclick');
      //     this.clicks = 0;

      //     // dispatch

      // }

      console.log("detail", row);
      this.$store.commit("patient/update_selected_detail_paketx", row);

      this.$store.dispatch("patient/getDetailPacketByID", {
        T_PacketID: row.T_PacketID,
      });
    },
    validateEmail() {
      // this.$refs.form.validate();
      console.log("value", this.inp_Mgm_McuPicEmail);
      const value = this.inp_Mgm_McuPicEmail;

      if (value.length > 0) {
        const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        // return pattern.test(value) || 'Invalid e-mail.';
        if (!pattern.test(value)) {
          this.$store.commit(
            "patient/update_inp_Mgm_McuPicEmailValidate",
            "Email Tidak Valid"
          );
          console.log("not valid email");
        } else {
          this.$store.commit("patient/update_inp_Mgm_McuPicEmailValidate", "");
          console.log("valid email");
        }
      }
    },
    validateUserEmail() {
      // this.$refs.form.validate();
      console.log("value", this.inp_Mgm_McuUserEmail);
      const value = this.inp_Mgm_McuUserEmail;

      if (value.length > 0) {
        const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        // return pattern.test(value) || 'Invalid e-mail.';
        if (!pattern.test(value)) {
          this.$store.commit(
            "patient/update_inp_Mgm_McuUserEmailValidate",
            "Email Tidak Valid"
          );
          console.log("not valid email");
        } else {
          this.$store.commit("patient/update_inp_Mgm_McuUserEmailValidate", "");
          console.log("valid email");
        }
      }
    },
    validateNumber() {
      if (this.inp_Mgm_McuTotalParticipant < 0) {
        this.inp_Mgm_McuTotalParticipant = 0;
      }
    },
    thr_search_corporate: _.debounce(function () {
      console.log(this.search_corporate);
      this.$store.dispatch("patient/searchcorporate", {
        search: this.search_corporate,
      });
    }, 2000),
    deleteTempUserMcuRow(idx) {
      this.$store.dispatch("patient/removeUserMcuTemp", idx);
    },
    resetUserMcu() {
      this.$store.commit("patient/update_inp_Mgm_McuUserEmail", "");
      this.$store.commit("patient/update_inp_Mgm_McuUserPassword", "");
    },
    saveUserMcu() {
      this.$store.commit("patient/update_errors", []);
      var errors = this.$store.state.patient.errors;

      if (_.isEmpty(this.inp_Mgm_McuUserEmail)) {
        errors.push("requireusermcuemail");
      }
      if (_.isEmpty(this.inp_Mgm_McuUserPassword)) {
        errors.push("requireusermcupassword");
      }

      if (errors.length === 0) {
        let arrTempUserMcu = this.xUserMculists;

        this.$store.commit("patient/update_usermcuid", 0);

        arrTempUserMcu.push({
          Mgm_McuUserID: this.$store.state.patient.usermcuid,
          Mgm_McuUserEmail: this.inp_Mgm_McuUserEmail,
          Mgm_McuUserPassword: this.inp_Mgm_McuUserPassword,
        });

        if (this.xUserMculists.length > 0) {
          this.resetUserMcu();
        }
      }
    },

    savesetupX() {
      var go_save = true;
      if (go_save) {
        // var prm = {
        //             xid:this.$store.state.patient.xid,
        //             act:this.$store.state.patient.act,
        //             startdate:this.$store.state.patient.start_date,
        //             enddate:this.$store.state.patient.end_date,
        //             company : this.$store.state.patient.company,
        //             mous:this.$store.state.patient.selected_mous,
        //             doctors:this.$store.state.patient.selected_doctors,
        //             promise_date:this.$store.state.patient.promise_date,
        //             promise_time:this.$store.state.patient.promise_time,
        //             default_mou:this.$store.state.patient.default_mou,
        //             default_doctor:this.$store.state.patient.default_doctor,
        //             default_doctor_address:this.$store.state.patient.default_doctor_address,
        //             deliveries:this.$store.state.patient.deliveries,
        //             selected_packets:this.$store.state.patient.selected_packets,
        //             selected_tests:this.$store.state.patient.selected_tests,
        //         }
        // this.$store.dispatch("patient/savesetup",prm)

        var prm = {
          Mgm_McuGeneratePasswordBY: this.selected_generatepasswordby
            .generatepasswordbycode,
          Mgm_McuReportHasil: this.reportresult,
          arrUserMcu: this.xUserMculists,
          deleted_usermcus: this.$store.state.patient.deleted_usermcus,
          xid: this.$store.state.patient.xid,
          act: this.$store.state.patient.act,
          Mgm_McuLabel: this.$store.state.patient.inp_Mgm_McuLabel,
          Mgm_McuPicName: this.$store.state.patient.inp_Mgm_McuPicName,
          Mgm_McuCorporateID: this.$store.state.patient.corporate,
          Mgm_McuStartDate: this.$store.state.patient.start_date,
          Mgm_McuEndDate: this.$store.state.patient.end_date,
          Mgm_McuFlagRelasiBayarSendiri: this.$store.state.patient
            .inp_Mgm_McuFlagRelasiBayarSendiri,
          Mgm_McuBisaTambahPemeriksaan: this.$store.state.patient
            .inp_Mgm_McuBisaTambahPemeriksaan,
          Mgm_McuNote: this.$store.state.patient.inp_Mgm_McuNote,
          selected_packets: this.$store.state.patient.selected_packets,
          Mgm_McuTemplateFisikTemplateMappingID: this.$store.state.patient
            .selected_templateFisikCb,
          Mgm_McuPicEmail: this.$store.state.patient.inp_Mgm_McuPicEmail,
          Mgm_McuPicPassword: this.$store.state.patient.inp_Mgm_McuPicPassword,
          Mgm_McuTotalParticipant: this.$store.state.patient.inp_Mgm_McuTotalParticipant,
          Mgm_McuT_PriceHeaderID: this.$store.state.patient.selected_tpriceheaderformCb
            .T_PriceHeaderID,
          Mgm_McuM_BranchID: this.$store.state.patient.selected_branch.M_BranchID,
          search: this.$store.state.patient.search_mgmmcu,
          current_page: this.$store.state.patient.current_page,
          lastid: -1,
        };

        console.log("inputan simpan or edit", prm);
        this.$store.dispatch("patient/savesetupv0", prm);
      }
    },
    // cpone
    checkNatTest(item) {
      var now_natest = item.nat_test;
      var rtn = true;
      var selected_packets = this.$store.state.patient.selected_packets;
      var selected_tests = this.$store.state.patient.selected_tests;
      selected_packets.forEach(function (entry) {
        var nat_test = entry.nat_test;
        now_natest.forEach(function (itemnattest) {
          var idx = _.findIndex(nat_test, function (o) {
            return o == itemnattest;
          });
          if (idx > -1) rtn = false;
        });
      });
      selected_tests.forEach(function (entry) {
        var nat_test = entry.nat_test;
        now_natest.forEach(function (itemnattest) {
          var idx = _.findIndex(nat_test, function (o) {
            return o == itemnattest;
          });
          if (idx > -1) rtn = false;
        });
      });
      return rtn;
    },
    testExist(item) {
      var selected_tests = this.$store.state.patient.selected_tests;
      var idx = _.findIndex(selected_tests, function (o) {
        return parseInt(o.id) === parseInt(item.id);
      });
      if (idx === -1) {
        return true;
      } else {
        return false;
      }
    },
    addToTests(item) {
      var selected_tests = this.$store.state.patient.selected_tests;
      selected_tests.push(item);
    },
    removeFromTests(item) {
      var selected_tests = this.$store.state.patient.selected_tests;
      var idx = _.findIndex(selected_tests, function (o) {
        return parseInt(o.id) === parseInt(item.id);
      });
      selected_tests.splice(idx, 1);
    },
    searchTest() {
      var prm = this.default_mou;
      prm.search = this.search_test;
      prm.current_page = 1;
      this.$store.commit("patient/update_current_page_test", 1);
      this.$store.dispatch("patient/getdatatests", prm);
    },
    packetExist(item) {
      var selected_packets = this.$store.state.patient.selected_packets;
      var idx = _.findIndex(selected_packets, function (o) {
        return parseInt(o.id) === parseInt(item.id);
      });
      if (idx === -1) {
        return true;
      } else {
        return false;
      }
    },
    addToPackets(item) {
      var selected_packets = this.$store.state.patient.selected_packets;
      selected_packets.push(item);
    },
    removeFromPackets(item) {
      var selected_packets = this.$store.state.patient.selected_packets;
      var idx = _.findIndex(selected_packets, function (o) {
        return parseInt(o.id) === parseInt(item.id);
      });
      selected_packets.splice(idx, 1);
    },
    searchPacket() {
      // var prm = this.default_mou
      // prm.search = this.search_packet
      // prm.current_page = 1
      this.$store.commit("patient/update_current_page_packet", 1);
      // this.$store.dispatch("patient/getdatapackets", prm)
      this.$store.dispatch("patient/getPackets", {
        T_PriceHeaderID: this.selected_tpriceheaderformCb.T_PriceHeaderID,
        searchPacket: this.search_packet,
        current_page: this.current_page_packet,
        lastid: -1,
      });
    },
    thr_search_company: _.debounce(function () {
      console.log(this.search_company);
      this.$store.dispatch("patient/searchcompany", {
        search: this.search_company,
      });
    }, 2000),
    openPrint(item) {
      this.urlprint =
        "/birt/run?__report=report/one/rekap/rpt_r_073.rptdesign&__format=pdf&PMouID=" +
        item.id +
        "&username=admin";
      this.openprint = true;
    },
    changeBoxRight(value, idx) {
      var deliveries = this.$store.state.patient.deliveries;
      console.log(deliveries);
      deliveries[idx].chex = value.chex === "N" ? "Y" : "N";
      console.log(deliveries[idx].chex);
      this.$store.commit("patient/update_deliveries", deliveries);
    },
    reHTMLAddress(entry) {
      var rtn = "";
      if (entry) {
        var perpart = entry.split("^");
        rtn += "<p class='mb-0' style='font-size:12px'>" + perpart[0] + "</p>";
        if (perpart[1])
          rtn += "<p class='mb-0' style='font-size:12px'>" + perpart[1] + "</p>";
      }

      return rtn;
    },
    getColorFont(value, type) {
      var xcolor = "black--text";
      if (value.chex === "Y") {
        xcolor = "teal--text";
      }

      return xcolor;
    },
    downloadxapp() {
      var start = Date.now();
      location.replace("/install-mcu.zip?tm=" + start);
    },
    delmou(row) {
      var arr = this.$store.state.patient.selected_mous;
      idx = _.findIndex(arr, row);
      arr.splice(idx, 1);
      this.$store.commit("patient/update_default_mou", {});

      var arr_mous = this.$store.state.patient.mous;
      arr_mous.push(row);
      this.$store.commit("patient/update_mous", arr_mous);
    },
    deldoctor(row) {
      var arr = this.$store.state.patient.selected_doctors;
      idx = _.findIndex(arr, row);
      arr.splice(idx, 1);

      var newx = this.$store.state.patient.selected_doctors;
      if (newx.length === 0) {
        this.$store.commit("patient/update_default_doctor", {});
      }
    },
    savesetup() {
      var go_save = true;
      if (go_save) {
        var prm = {
          xid: this.$store.state.patient.xid,
          act: this.$store.state.patient.act,
          startdate: this.$store.state.patient.start_date,
          enddate: this.$store.state.patient.end_date,
          company: this.$store.state.patient.company,
          mous: this.$store.state.patient.selected_mous,
          doctors: this.$store.state.patient.selected_doctors,
          promise_date: this.$store.state.patient.promise_date,
          promise_time: this.$store.state.patient.promise_time,
          default_mou: this.$store.state.patient.default_mou,
          default_doctor: this.$store.state.patient.default_doctor,
          default_doctor_address: this.$store.state.patient.default_doctor_address,
          deliveries: this.$store.state.patient.deliveries,
          selected_packets: this.$store.state.patient.selected_packets,
          selected_tests: this.$store.state.patient.selected_tests,
        };
        this.$store.dispatch("patient/savesetup", prm);
      }
    },
    adddoctor() {
      var selected = this.$store.state.patient.doctor;
      var arr = this.$store.state.patient.doctors;
      var xsel = this.$store.state.patient.selected_doctors;
      xsel.push(selected);
      this.$store.commit("patient/update_selected_doctors", xsel);
      this.$store.commit("patient/update_doctors", []);
      this.$store.commit("patient/update_doctor", {});
    },
    addmou() {
      var selected = this.$store.state.patient.mou;
      var arr = this.$store.state.patient.mous;
      var xsel = this.$store.state.patient.selected_mous;
      xsel.push(selected);
      this.$store.commit("patient/update_selected_mous", xsel);
      var filtered = arr.filter(
        (o1) => xsel.filter((o2) => o2.id === o1.id).length === 0
      );
      this.$store.commit("patient/update_mous", filtered);
      this.$store.commit("patient/update_mou", {});
    },
    thr_search_doctor: _.debounce(function () {
      //console.log('thr doctor')
      this.$store.dispatch("patient/searchdoctor", this.search_doctor);
    }, 2000),
    thr_search_company: _.debounce(function () {
      console.log(this.search_company);
      this.$store.dispatch("patient/searchcompany", {
        search: this.search_company,
      });
    }, 2000),
    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")}`;
    },
    checkError(value) {
      var errors = this.$store.state.patient.errors;
      if (errors.includes(value)) {
        return true;
      } else {
        return false;
      }
    },
    saveData() {
      this.$store.commit("patient/update_errors", []);
      var errors = this.$store.state.patient.errors;
      var aDOB = moment(this.xdob, "DD-MM-YYYY", true);
      console.log(this.xdob);
      console.log(aDOB);
      var isValidDOB = aDOB.isValid();
      if (!isValidDOB) {
        errors.push("wrongformatdob");
      }
      if (this.xpatientname === "") {
        errors.push("requirename");
      }
      if (this.xdob === "") {
        errors.push("requiredob");
      }
      if (_.isEmpty(this.xsex)) {
        errors.push("requiresex");
      }
      if (_.isEmpty(this.xtitle)) {
        errors.push("requiretitle");
      }
      if (errors.length === 0) {
        var prm = {};
        prm.M_PatientID = this.$store.state.patient.selected_patient.M_PatientID;
        prm.M_PatientM_TitleID = this.$store.state.patient.selected_title.M_TitleID;
        prm.M_PatientPrefix = this.xpatientprefix;
        prm.M_PatientName = this.xpatientname;
        prm.M_PatientSuffix = this.xpatientsuffix;
        prm.M_PatientDOB = this.xdob;
        prm.M_PatientM_SexID = this.$store.state.patient.selected_sex.M_SexID;
        prm.M_PatientM_ReligionID = this.$store.state.patient.selected_religion.M_ReligionID;
        prm.M_PatientEmail = this.xemail;
        prm.M_PatientPOB = this.xpob;
        prm.M_PatientHP = this.xhp;
        prm.M_PatientPhone = this.xphone;
        prm.M_PatientM_IdTypeID = this.$store.state.patient.selected_kartuidentitas.M_IdTypeID;
        prm.M_PatientIDNumber = this.xnoidentitas;
        prm.M_PatientNote = this.xnote;
        prm.M_PatientNIK = this.xnik;
        prm.M_PatientJabatan = this.xjabatan;
        prm.M_PatientKedudukan = this.xkedudukan;
        prm.M_PatientPJ = this.xpj;
        prm.M_PatientLocation = this.xlocation;
        prm.M_PatientJob = this.xjob;
        this.$store.dispatch("patient/save", prm);
      }
    },
    saveNewPatient() {
      this.$store.commit("patient/update_errors", []);
      var errors = this.$store.state.patient.errors;
      var aDOB = moment(this.xdob, "DD-MM-YYYY", true);
      console.log(this.xdob);
      console.log(aDOB);
      var isValidDOB = aDOB.isValid();
      if (!isValidDOB) {
        errors.push("wrongformatdob");
      }
      if (this.xpatientname === "") {
        errors.push("requirename");
      }
      if (this.xdob === "") {
        errors.push("requiredob");
      }
      if (_.isEmpty(this.xsex)) {
        errors.push("requiresex");
      }
      if (_.isEmpty(this.xtitle)) {
        errors.push("requiretitle");
      }
      if (errors.length === 0) {
        var prm = {};
        prm.M_PatientID = 0;
        prm.M_PatientM_TitleID = this.$store.state.patient.selected_title.M_TitleID;
        prm.M_PatientPrefix = this.xpatientprefix;
        prm.M_PatientName = this.xpatientname;
        prm.M_PatientSuffix = this.xpatientsuffix;
        prm.M_PatientDOB = this.xdob;
        prm.M_PatientM_SexID = this.$store.state.patient.selected_sex.M_SexID;
        prm.M_PatientM_ReligionID = this.$store.state.patient.selected_religion.M_ReligionID;
        prm.M_PatientEmail = this.xemail;
        prm.M_PatientPOB = this.xpob;
        prm.M_PatientHP = this.xhp;
        prm.M_PatientPhone = this.xphone;
        prm.M_PatientM_IdTypeID = this.$store.state.patient.selected_kartuidentitas.M_IdTypeID;
        prm.M_PatientIDNumber = this.xnoidentitas;
        prm.M_PatientNote = this.xnote;
        prm.M_PatientNIK = this.xnik;
        prm.M_PatientJabatan = this.xjabatan;
        prm.M_PatientKedudukan = this.xkedudukan;
        prm.M_PatientPJ = this.xpj;
        prm.M_PatientLocation = this.xlocation;
        prm.M_PatientJob = this.xjob;
        this.$store.dispatch("patient/newpatient", prm);
      }
    },
    deleteData() {
      let msg = "Yakin, akan menghapus data pasien " + this.xpatientname + " ?";
      this.$store.commit("patient/update_msg_confirmation_delete", msg);
      this.$store.commit("patient/update_dialog_confirmation_delete", true);
    },
    doDeleteData() {
      var prm = {};
      prm.id = this.$store.state.patient.selected_mgmmcu.Mgm_McuID;
      prm.code = this.$store.state.patient.selected_mgmmcu.Mgm_McuNumber;
      console.log(prm);
      this.$store.dispatch("patient/deletev0", prm);
    },
  },
  watch: {
    // cpone
    search_corporate(val, old) {
      //console.log(val)
      if (val == old) return;
      if (!val) return;
      if (val.length < 1) return;
      if (this.$store.state.patient.update_autocomplete_status == 1) return;
      this.thr_search_corporate();
    },
    // cpone
    search_doctor(val, old) {
      console.log(val);
      if (val == old) return;
      if (!val) return;
      if (val.length < 1) return;
      if (this.$store.state.patient.update_autocomplete_status == 1) return;
      this.thr_search_doctor();
    },
    search_company(val, old) {
      //console.log(val)
      if (val == old) return;
      if (!val) return;
      if (val.length < 1) return;
      if (this.$store.state.patient.update_autocomplete_status == 1) return;
      this.thr_search_company();
    },
  },
};
</script>
