<template>
   <v-layout class="fill-height" column>
      <one-comment></one-comment>
      <one-req></one-req>
      <v-dialog v-model="xdialogaction" persistent  max-width="350">
            <v-card>
                <v-card-title color="warning"  class="headline">Konfirmasi</v-card-title>
                <v-card-text v-html="xmsgaction">
                    
                </v-card-text>
                <v-card-actions>
                <v-spacer></v-spacer>
                    <v-btn color="primary darken-1" flat @click="closeDialogAction()">OK</v-btn>
                    <v-btn color="error darken-1" flat @click="xdialogaction = false">Tutup</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
         <v-snackbar
            v-model="snackbar.value"
            :timeout="snackbar.timeout"
            >
            {{ snackbar.text }}
            <v-btn
                  color="blue"
                  text
                  @click="snackbar.value = false"
            >
                  Tutup
            </v-btn>
         </v-snackbar>
      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout row>
            <v-flex xs4 mt-1>
               <v-text-field
                  label="No Reg"
                  outline
                  v-model="nolab"
                  hide-details
                  v-on:keyup.enter="searchPatient"
               ></v-text-field>
            </v-flex>
            <v-flex xs4 >
               <v-autocomplete
                  label="Kel. Pelanggan"
                  v-model="selected_company"
                  class="ma-1"
                  :items="xcompanies"
                  :search-input.sync="search_company"
                  auto-select-first
                  hide-details
                  style="font-size:14px"
                  @change="companyChange"
                  outline
                  no-filter
                  item-text="M_CompanyName"
                  return-object
                  :loading="isLoading"
                  no-data-text="Semua Kel. Pelanggan"
               >
                  <template slot="item" slot-scope="{ item }">
                  <v-list-tile-content>
                     <v-list-tile-title v-text="item.M_CompanyName"></v-list-tile-title>
                  </v-list-tile-content>
                  </template>
               </v-autocomplete>
            </v-flex>
 
            <v-flex xs4 mt-1 >
               <v-text-field
                  label="Nama"
                  outline
                  v-model="name"
                  v-on:keyup.enter="searchPatient"
                  hide-details
               ></v-text-field>
            </v-flex>
         </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="xdata elevation-1">
                  <template slot="items" slot-scope="props">
                     <td width="10%"
			class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">{{ format_date(props.item.T_OrderHeaderDate) }}
                              <v-icon v-if="have_requirement(props.item)" @click="requirement(props.item)" class="red--text"  >info</v-icon>
                     </td>
                     <td width="12%" class="pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <span style="color:brick">{{ props.item.T_OrderHeaderLabNumberExt}}</span> <br/>
                        {{ props.item.T_OrderHeaderLabNumber}}</td>
                     <td width="12%"  class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">{{ props.item.M_CompanyName}}</td>
                     <td  width="25%" class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">{{ props.item.M_PatientName}}</td>
                     <td  width="18%"  class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <div v-if="status.code === 'FO.Verification' && props.item.check_status === '2'" class="text-xs-center">
                           <span v-if="btnsaveseen" @click="verify(props.item,'N')" style="display:inline-block"
                              class="pa-1 pt-1 white--text error"><v-icon dark>close</v-icon></span>
                           <span v-if="btnsaveseen" @click="verify(props.item,'Y')" style="display:inline-block"
                              class="pa-1 pt-1 white--text success"><v-icon dark>check</v-icon></span>
                           <span v-if="btnsaveseen" @click="comment(props.item)"  style="display:inline-block"
                              class="pa-1 pt-1 white--text orange">
                              <v-icon dark>message</v-icon></span>
                        </div>
                     </td>
                  </template>
               </v-data-table>
            </v-flex>
         </v-layout>
      </v-card>
      <one-dialog-alert :status="openalertverification" :msg="msgalertverification" @forget-dialog-alert="forgetAlertVerification()" @close-dialog-alert="closeAlertVerification()"></one-dialog-alert>
      <one-dialog-info :status="opendialogverification" :msg="msginfoverification" @close-dialog-info="closeInfoVerification()"></one-dialog-info>
   </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;
}
.xdata tbody{
   display:block;
   overflow:auto;
   height:650px;
   width:100%;
}
.xdata thead tr{
   display:block;
}
</style>

<script>
let ts = "?ts=" + moment().format("YYMMDDhhmmss")
module.exports = {
components : {
      'one-dialog-info':httpVueLoader('../../common/oneDialogInfo.vue' + ts),
      'one-dialog-alert':httpVueLoader('../../common/oneDialogAlert.vue' + ts),
      'one-comment':httpVueLoader('./oneComment.vue' + ts),
      'one-req':httpVueLoader('./oneRequirement.vue' + ts)
   },
   mounted() {
      this.$store.dispatch("status/lookup")
      this.$store.dispatch("delivery/lookup")
      this.$store.dispatch("patient/search",{
            name:this.name,
            nolab: this.nolab,
            //status: this.status.id
         })
   },
   methods : {
      have_requirement(item) {
         return item.T_OrderHeaderFoNote != '' || item.havingRequirement == 'Y'
      },
      requirement(item) {
         this.$store.commit("req/update_patient",item)
         this.$store.dispatch("req/load")
         this.$store.commit("req/update_show",true)
      },
      comment(item) {
         this.$store.commit("comment/update_patient",item)
         this.$store.commit("comment/update_show",true)
      },
      async companyChange(v) {
         await this.$store.commit("company/update_company",v)
         this.searchPatientV2(v.M_CompanyID)
      },
      format_date(p) {
         return moment(p).format("DD.MM.YYYY")
      },
      thr_search_company: _.debounce(function() {
            this.$store.dispatch("company/search", this.search_company);
      }, 700),
      isSelected(p) {
         if(p.iscito === 'Y')
            return false
         else
            return p.T_OrderHeaderID == this.$store.state.patient.selected_patient.T_OrderHeaderID
         
      },
      searchPatientV2(companyID) {
         if(this.status.code === 'FO.Verification'){
               this.$store.commit("patient/update_show_right",'N')
            }
            else{
               this.$store.commit("patient/update_show_right",'Y')
            }
         this.$store.dispatch("patient/search",{
            name:this.name,
            nolab: this.nolab,
            companyID : companyID
           // status: this.status.id
         })
      },
      searchPatient() {
         if(this.status.code === 'FO.Verification'){
               this.$store.commit("patient/update_show_right",'N')
            }
            else{
               this.$store.commit("patient/update_show_right",'Y')
            }
         this.$store.dispatch("patient/search",{
            name:this.name,
            nolab: this.nolab,
            companyID : this.$store.state.company.company.M_CompanyID
           // status: this.status.id
         })
      },
      selectMe(pat) {
            this.$store.commit("patient/update_selected_patient",pat)
         
      },
      closeInfoVerification(){
           this.$store.commit("patient/update_open_dialog_verification",false)
           this.searchPatient()
      },
       closeAlertVerification(){
          this.$store.commit("patient/update_open_alert_verification",false)
      },
      forgetAlertVerification(){
          this.$store.commit("patient/update_no_save",0)
          this.$store.commit("patient/update_open_alert_verification",false)
      },
       verify(value,act){
            this.$store.commit("patient/update_selected_patient",value)
            value.act = act
            this.$store.commit("patient/update_now_prm",value)
            if(act === 'Y'){
               var prm = this.$store.state.patient.now_prm
               prm.name = this.name
               prm.nolab = this.nolab
               this.$store.commit("done/update_current_page",1)
               prm.done_search = {
                  startdate:this.$store.state.done.start_date,
                  enddate:this.$store.state.done.end_date,
                  search:this.$store.state.done.search,
                  current_page:1,
                  lastid:-1

               }
               this.$store.dispatch("patient/verify",prm)
            }
            else{
               var xxver = value.verifications

               xxver.forEach((el)=>{el.chex = true})
               this.$store.commit("patient/update_verifications",xxver)
               this.$store.commit("patient/update_dialoglist",true)
               this.$store.commit("patient/update_btn_save_seen",false)
            }  
        },
        closeDialogAction(){
            var prm = this.$store.state.patient.now_prm
            this.$store.dispatch("patient/verify",prm)
            
        },
   },
   computed: {
      xcompanies() {
         return this.$store.state.company.companies
      },
      selected_company : {
         get() { return this.$store.state.company.company },
         set(v) { this.$store.commit("company/update_company",v) }
      },
      statuses() {
         return this.$store.state.status.statuses
      },
      snackbar: {
         get() {
            return this.$store.state.patient.snackbar
         },
         set(val) {
            this.$store.commit("patient/update_snackbar",val)
         }
      },
      name: {
         get() {
            return this.$store.state.patient.name
         },
         set(val) {
            this.$store.commit("patient/update_name",val)
         }
      },
      nolab: {
         get() {
            return this.$store.state.patient.nolab
         },
         set(val) {
            this.$store.commit("patient/update_nolab",val)
         }
      },
      status: {
         get() {
            return this.$store.state.status.selected_status
         },
         set(val) {
            if(val.code === 'FO.Verification'){
               this.$store.commit("patient/update_show_right",'N')
            }
            this.$store.commit("status/update_selected_status",val)
         }
      },
      isLoading() {
         return this.$store.state.patient.search_status == 1
      },
      patients() {
         return this.$store.state.patient.patients
      },
      opendialogverification: {
            get() {
                  return this.$store.state.patient.open_dialog_verification
            },
            set(val) {
                  this.$store.commit("patient/update_open_dialog_verification",false)
            }
      },
      msginfoverification(){
            return this.$store.state.patient.msg_info_verification
      },
      openalertverification: {
            get() {
                return this.$store.state.patient.open_alert_verification
            },
            set(val) {
                this.$store.commit("patient/open_alert_verification",false)
            }
        },
        msgalertverification(){
            return this.$store.state.patient.msg_alert_verification
        },
        xdialogaction:{
            get() {
                return this.$store.state.patient.dialog_action
            },
            set(val) {
                this.$store.commit("patient/update_dialog_action",val)
            }
        },
         xmsgaction:{
            get() {
                return this.$store.state.patient.msg_action
            },
            set(val) {
                this.$store.commit("patient/update_msg_action",val)
            }
        },
        btnsaveseen:{
            get() {
                return this.$store.state.patient.btn_save_seen
            },
            set(val) {
                this.$store.commit("patient/update_btn_save_seen",val)
            }
        },
   },
   data() {
      return {
         search_company: "",
         items: [],
         headers: [
            {
               text: "TANGGAL",
               align: "left",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NO REG",
               align: "left",
               sortable: false,
               value: "lab",
               width: "12%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "KEL PELANGGAN",
               align: "left",
               sortable: false,
               value: "lab",
               width: "12%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "name",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "VERIFIKASI",
               align: "center",
               sortable: false,
               value: "status",
               width: "18%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
      };
   },
   watch: {
      async search_company(val,old) {
      if (val == old) return;
      if (!val) return;
      if (val.length < 1) return;
      if (this.$store.state.company.loading == 1) return;
      this.$store.dispatch("company/search", this.search_company);
      }
   }

}
</script>
