<template>
   <v-layout class="fill-height" column>
      <!--<v-card class="pa-2 mb-2">
         <v-layout row>
            <v-flex xs12>
               <v-btn :loading="loading_download" block class="text-md-center" @click="download_data()" color="teal" dark>
                        DOWNLOAD DATA ORDER
               </v-btn>
            </v-flex>
         </v-layout>
      </v-card>-->
      <v-card class="mb-2 pa-2 searchbox">
         <v-layout align-center>
            <span title="kembali ke penyerahan hasil" v-if="show_btn_back" @click="backToFoReceiver()"
               class="icon-medium-fill-base xs1 white--text indigo darken-1"><v-icon dark>reply</v-icon></span>
            <v-menu ref="menustartdate" v-model="menustartdate" :close-on-content-click="false" :nudge-right="0" lazy
               transition="scale-transition" offset-y full-width max-width="290px" min-width="290px">
               <template v-slot:activator="{ on }">
                  <v-text-field v-model="startDateFormatted" label="Tanggal Push Order" readonly hide-details
                     class="xs3 ma-1" outline v-on="on" @blur="deFormatedDate(startDateFormatted)"></v-text-field>
               </template>
               <v-date-picker v-model="xstartdate" no-title @input="menustartdate = false"></v-date-picker>
            </v-menu>
            <!--<v-menu
                  ref="menus"
                  v-model="menuenddate"
                  :close-on-content-click="false"
                  :nudge-right="0"
                  lazy
                  transition="scale-transition"
                  offset-y
                  full-width
                  max-width="290px"
                  min-width="290px"
                  >
                  <template v-slot:activator="{ on }">
                     <v-text-field
                     v-model="endDateFormatted"
                     label="Tanggal Akhir"
                     readonly
                     hide-details
                     class="xs3 ma-1"
                     outline
                     v-on="on"
                     @blur="deFormatedDate(startDateFormatted)"
                     ></v-text-field>
                  </template>
                  <v-date-picker v-model="xenddate" no-title @input="menuenddate = false"></v-date-picker>
               </v-menu>    -->
            <v-text-field class="xs3 ma-1" label="Cari ..." placeholder="Nama / No Reg" v-on:keyup.enter="searchPatient"
               outline v-model="searchnamelab" hide-details></v-text-field>

            <v-select class="xs3 mini-select ma-1" :items="statuses" item-text="name" return-object v-model="status"
               label="Status" outline hide-details></v-select>
            <span @click="searchPatient" class="icon-medium-fill-base xs1 white--text warning iconsearch-search"></span>

         </v-layout>
      </v-card>

      <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-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                        @click="selectMe(props.item)">
                        {{ props.item.push_date }}
                     </td>
                     <td class="text-xs-left pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                        @click="selectMe(props.item)">
                        <p style="color:#800000" class="font-weight-bold mb-0">{{ props.item.T_OrderHeaderLabNumber }}
                        </p>
                        <kbd v-if="props.item.Api_PushLISLabNumber && props.item.Api_PushStatus === 'SUCCESS'"
                           class="font-weight-black caption mb-0">{{ props.item.Api_PushLISLabNumber }}</kbd>
                        <p v-if="props.item.Api_PushStatus === 'ERROR'"
                           class="font-weight-black red--text caption mb-0">Gagal push order
                           (x{{ props.item.Api_PushRetry }})</p>
                        <p v-if="props.item.Api_PushStatus === ''" class="font-weight-bold grey--text caption mb-0">
                           Belum melakukan push order</p>
                        <p v-if="props.item.Api_PushStatus === 'SUCCESS'"
                           class="font-weight-bold green--text caption mb-0">
                           Push order berhasil</p>
                           <p v-if="props.item.Api_PushStatus !== 'SUCCESS' && props.item.Api_PushStatus !== ''"
                           class="font-weight-bold red--text caption mb-0">
                           Push order gagal</p>
                     </td>
                     <td class="text-xs-left pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                        @click="selectMe(props.item)">
                        {{ props.item.M_PatientName }}
                     </td>
                     <td class="text-xs-left pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                        @click="selectMe(props.item)">
                        <p class="mb-0">{{ props.item.nik }}</p>
                        <!--<p v-if="props.item.order_nik_yankes" class="font-weight-black caption mb-0">{{ props.item.order_nik_yankes }}</p>-->
                     </td>
                     <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                        @click="selectMe(props.item)">
                        <v-btn v-if="props.item.Api_PushStatus === 'ERROR' || props.item.Api_PushStatus === '' || props.item.Api_PushStatus !== 'SUCCESS'"
                           title="push order" style="min-width:40px" small class=" text-md-center"
                           @click="rePushOrder(props.item)" color="warning" dark>
                           <v-icon small>cached</v-icon>
                        </v-btn>
                        <v-btn depressed v-if="props.item.Api_PushStatus === 'SUCCESS'" title="terkirim"
                           style="min-width:40px" small class=" text-md-center" color="success" dark>
                           <v-icon small>check</v-icon>
                        </v-btn>
                     </td>
                  </template>
               </v-data-table>
               <v-divider></v-divider>
               <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page"
                  :length="xtotal_page"></v-pagination>
            </v-flex>
         </v-layout>
      </v-card>
      <one-dialog-alert :status="openalertnopay" :msg="msgalertnopay" @forget-dialog-alert="forgetAlertNoPay()"
         @close-dialog-alert="closeAlertNoPay()"></one-dialog-alert>
   </v-layout>
</template>

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

.searchbox .v-btn {
   min-height: 40px;
}

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

table.v-table thead tr {
   height: 40px;
}

.v-messages {
   min-height: 0px !important;
}


table,
td,
th {
   border: .5px solid rgba(0, 0, 0, .12);
   text-align: left;
}

table {
   border-collapse: collapse;
   width: 100%;
}
</style>

<script>
module.exports = {
   components: {
      'one-dialog-info': httpVueLoader('../../common/oneDialogInfo.vue'),
      'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
   },
   mounted() {

      var url_string = window.location.href
      var url = new URL(url_string);
      var id = url.searchParams.get("id")
      var xdate = url.searchParams.get("xdate")
      var type = url.searchParams.get("type")
      if (id) {
         this.xstartdate = xdate
         this.xenddate = xdate
         this.searchnamelab = id
         this.status = { name: 'Semua', value: 'A' }
         this.show_btn_back = true
         this.type_back_btn = type
      }

      this.$store.dispatch("patient/search", {
         startdate: this.xstartdate,
         enddate: this.xenddate,
         search: this.searchnamelab,
         status: this.status.value,
         current_page: 1,
         lastidx: 0
      })

   },
   methods: {
      rePushOrder(pat) {
         this.$store.dispatch("patient/rePushOrder", pat);
      },
      download_data() {
         this.$store.dispatch("patient/download_data", {});
      },
      thr_search_company: _.debounce(function () {
         this.$store.dispatch("patient/searchcompany", 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')}`
      },
      isSelected(p) {
         return p.T_OrderHeaderID == this.$store.state.patient.selected_patient.T_OrderHeaderID
      },
      searchPatient() {
         this.$store.dispatch("patient/search", {
            startdate: this.xstartdate,
            enddate: this.xenddate,
            search: this.searchnamelab,
            status: this.status.value,
            current_page: 1,
            lastidx: this.$store.state.patient.lastidx
         })
      },
      selectMe(pat) {

         this.$store.commit("patient/update_selected_patient", pat)
         let patients = this.$store.state.patient.patients
         patients.forEach(function (obj, index) {
            if (obj.T_OrderHeaderID == pat.T_OrderHeaderID) {
               this.$store.commit("patient/update_lastidx", index)
            }
         })
         //this.$store.dispatch("patient/getlogs", {})

      },
      editorder(pat) {
         this.$store.commit("test/update_orderid", pat.T_OrderHeaderID)
         this.$store.commit("test/update_mouid", pat.T_OrderHeaderM_MouID)
         console.log('edit order')
         this.$store.commit("test/update_test_dialog", true)
         this.$store.commit("test/update_test_all", [])
         this.$store.dispatch("test/lookup", { orderid: pat.T_OrderHeaderID })
         this.$store.dispatch("test/lookup_promises", { orderid: pat.T_OrderHeaderID })
         this.$store.commit("test/update_test_message", "")
         this.$store.commit("test/update_save_status", 0)
         this.$store.commit("test/update_save_status_verification", 0)
         this.$store.commit("test/update_show_cito_dropdown", "N")
         this.$store.commit("test/update_selected_cito", {})
         this.$store.commit("test/update_test_dialog", true)


      },
      closeAlertNoPay() {
         this.$store.commit("patient/update_open_alert_no_pay", false)
      },
      forgetAlertNoPay() {
         var xval = this.$store.state.paymentnew.types
         var valpay = 0
         xval.forEach(function (obj) {
            obj.chex = false
            obj.leftvalue = 0
            obj.rightvalue = 0
            obj.selected_card = { id: 0, name: '' }
            obj.selected_edc = { id: 0, name: '' }
         })
         this.$store.commit("paymentnew/update_total_payment", 0)
         this.$store.commit("patient/update_open_alert_no_pay", false)
      },
      backToFoReceiver() {
         this.show_btn_back = false
         if (this.type_back_btn === 'fo')
            location.replace("/one-ui/test/vuex/one-fo-handover-patient/")
         if (this.type_back_btn === 'email')
            location.replace("/one-ui/test/vuex/one-fo-handover-email/")
      }
   },
   computed: {
      loading_download() {
         return this.$store.state.patient.loading_download;
      },
      xcompanies() {
         return this.$store.state.patient.companies;
      },
      selected_company: {
         get() {
            return this.$store.state.patient.selected_company;
         },
         set(val) {
            this.$store.commit("patient/update_selected_company", val)
            this.searchPatient()
         }
      },
      xstartdate: {
         get() {
            return this.$store.state.patient.start_date
         },
         set(val) {
            this.$store.commit("patient/update_start_date", val)
            this.searchPatient()
         }
      },
      xenddate: {
         get() {
            return this.$store.state.patient.end_date
         },
         set(val) {
            this.$store.commit("patient/update_end_date", val)
            this.searchPatient()
         }
      },
      searchnamelab: {
         get() {
            return this.$store.state.patient.search
         },
         set(val) {
            this.$store.commit("patient/update_search", val)
         }
      },
      statuses() {
         return this.$store.state.patient.statuses
      },
      status: {
         get() {
            return this.$store.state.patient.selected_status
         },
         set(val) {
            this.$store.commit("patient/update_selected_status", val)
            this.searchPatient()
         }
      },
      startDateFormatted() {
         return this.formatDate(this.xstartdate)
      },
      endDateFormatted() {
         return this.formatDate(this.xenddate)
      },
      isLoading() {
         return this.$store.state.patient.search_status == 1
      },
      patients() {
         return this.$store.state.patient.patients
      },
      openalertnopay: {
         get() {
            return this.$store.state.patient.open_alert_no_pay
         },
         set(val) {
            this.$store.commit("patient/update_open_alert_no_pay", false)
         }
      },
      msgalertnopay() {
         return this.$store.state.patient.msg_alert_no_pay
      },
      curr_page: {
         get() {
            return this.$store.state.patient.current_page
         },
         set(val) {
            this.$store.commit("patient/update_current_page", val)
            this.$store.dispatch("patient/search", {
               startdate: this.xstartdate,
               enddate: this.xenddate,
               search: this.searchnamelab,
               status: this.status.value,
               current_page: val,
               lastidx: 0
            })
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.patient.total_page
         },
         set(val) {
            this.$store.commit("patient/update_total_page", val)
         }
      },
   },

   data() {
      return {
         items: [],
         search_company: "",
         menustartdate: false,
         menuenddate: false,
         show_btn_back: false,
         type_back_btn: '',
         headers: [
            {
               text: "TGL. PUSH",
               align: "center",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "pa-2 warning white--text"
            },
            {
               text: "NO REG",
               align: "center",
               sortable: false,
               value: "lab",
               width: "20%",
               class: "pa-2 warning white--text"
            },
            {
               text: "NAMA",
               align: "center",
               sortable: false,
               value: "name",
               width: "35%",
               class: "pa-2 warning white--text"
            },

            {
               text: "NIK",
               align: "center",
               sortable: false,
               value: "status",
               width: "10%",
               class: "pa-2 warning white--text"
            },
            {
               text: "AKSI",
               align: "center",
               sortable: false,
               value: "aksi",
               width: "10%",
               class: "pa-2 warning white--text"
            }
         ],
      }
   },
   watch: {
      search_company(val, old) {
         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>
