<template>
   <v-layout class="fill-height" column>
      <v-dialog v-model="dialogsuccess" persistent  max-width="290">
        <v-card>
            <v-card-title color="success"  class="headline">Berhasil !</v-card-title>
            <v-card-text>
                {{msgsuccess}}
            </v-card-text>
            <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn color="green darken-1" flat @click="closeDialogSuccess">OK</v-btn>
            </v-card-actions>
        </v-card>
      </v-dialog>
      <v-dialog v-model="dialog_doctor" persistent  max-width="45%">
        <v-card>
            <v-card-title color="success"  class="headline">Pilih dokter pembaca</v-card-title>
            <v-card-text>
               <v-layout row>
                  <v-flex xs12>
                     <v-switch
                        v-model="flagdoctorinoffice"
                        label="Dokter Pramita"
                     ></v-switch>
                  </v-flex>
               </v-layout>
               <v-layout row>
                  <v-flex xs6 pa-1>
                     <v-autocomplete
                        label="Dokter"
                        v-model="selected_doctor"
                        :items="doctors"
                        :search-input.sync="search_doctor"
                        auto-select-first
                        no-filter
                        item-text="name"
                        return-object
                        :loading="isLoading"
                        no-data-text="Pilih Dokter"
                        >
                        <template
                              slot="item"
                              slot-scope="{ item }"
                              >
                              <v-list-tile-content>
                                 <v-list-tile-title v-text="item.name"></v-list-tile-title>
                              </v-list-tile-content>
                        </template>
                     </v-autocomplete>
                     <p  v-if="errors.require_doctor" class="error pl-2 pr-2" style="color:#fff">Ayo pilih salah satu</p>
                  </v-flex>
                  <v-flex xs6 pa-1>
                     <v-select   
                        item-text="M_DoctorAddressDescription"
                        return-object
                        :items="doctor_address" 
                        v-model="selected_doctor_address"
                        label="Alamat Dokter"
                     ></v-select>
                     <p  v-if="errors.require_doctor_address" class="error pl-2 pr-2" style="color:#fff">Mau tau dong alamatnya</p>
                  </v-flex>
               </v-layout>
            </v-card-text>
            <v-card-actions>
            <v-spacer></v-spacer>
               <v-btn color="black" flat @click="dialog_doctor = false">Tutup</v-btn>
               <v-btn color="green darken-1" flat @click="saveDoctor">Simpan</v-btn>
               <!--<v-btn color="green darken-1" flat @click="saveRequirement()">Simpan</v-btn>-->
            </v-card-actions>
        </v-card>
      </v-dialog>
      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout >
               <v-text-field class="xs6 ma-1"
               style="font-size:14px"
                             label="No Reg"
                             outline
                              v-on:keyup.enter="searchPatient"
                             v-model="nolab"
                             hide-details
                             ></v-text-field>
               <v-select class="xs6 mini-select ma-1" :items="xstations" 
                  item-text="name"
                  return-object
                   style="font-size:14px"
                  v-model="xselectedstation"
                  label="Station" outline   hide-details></v-select>
               <v-select class="xs6 mini-select ma-1" :items="xstatuses" 
                  item-text="name"
                   style="font-size:14px"
                  return-object
                  v-model="xselectedstatus"
                  label="Status" outline   hide-details></v-select>
            <!--<v-btn class="xs3 ma-1" color="success"  @click="searchPatient" >Cari</v-btn>-->
           <!-- <span @click="searchPatient"  class="icon-medium-fill-base xs1 white--text success iconsearch-search"></span>-->
            
            <span v-if="btn_hide === false"  @click="cancel"  class="icon-medium-fill-base xs1 white--text black"><v-icon large dark>cancel</v-icon></span>
            <span v-if="btn_hide === false"  @click="opendialogdoctor"  class="icon-medium-fill-base xs1 white--text brown"><v-icon large dark>assignment_ind</v-icon></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 v-slot:headers="props">
                        <tr>
                           <th class= "text-xs-left blue lighten-3 white--text">
                              <v-checkbox style="padding-top:0;margin-top:0"
                                 hide-details
                                 color="blue lighten-1"
                                 :indeterminate="indeterminatex"
                                 @change="changeCbxAll(bar_chx_all)"
                                 v-model="bar_chx_all"
                              ></v-checkbox>
                           </th>
                           <th
                              v-for="header in props.headers"
                              :key="header.text"
                              :class="header.class"
                              :width="header.width"
                           >
                           {{ header.text }}
                           </th>
                        </tr>
                  </template>
                     
                  <template v-slot:items="props">
                        <td style="width:15px" class="text-xs-left" v-bind:class="{'amber':props.item.iscito === 'Y'}">
                           <v-checkbox style="padding-top:0;margin-top:0"
                              color="blue lighten-1"
                              hide-details
                              v-model="props.item.chex"
                              @change="checkTop()"
                           ></v-checkbox>
                        </td>
                        <td class="text-xs-left pa-2" v-bind:class="{'amber':props.item.iscito === 'Y'}">{{ props.item.T_OrderHeaderLabNumber }}</td>
                        <td class="text-xs-left pa-2" v-bind:class="{'amber':props.item.iscito === 'Y'}">{{ props.item.patient_fullname}}</td>
                        <td class="text-xs-left pa-2" v-bind:class="{'amber':props.item.iscito === 'Y'}">{{ props.item.T_BarcodeLabBarcode}}</td>
                        <td class="text-xs-left pa-2" v-bind:class="{'amber':props.item.iscito === 'Y'}">
                           <v-chip v-if="props.item.doctor_fullname !== '-' && props.item.flagdoctorinoffice === 'N'" @click="removeDoctor(props.item)" close label color="red" text-color="white">
                              {{props.item.doctor_fullname}} 
                           </v-chip>
                           <v-chip v-if="props.item.doctor_fullname !== '-' && props.item.flagdoctorinoffice === 'Y'" @click="removeDoctor(props.item)" close label color="primary" text-color="white">
                              {{props.item.doctor_fullname}} 
                           </v-chip>
                           <span v-if="props.item.doctor_fullname === '-'">-</span>
                        </td>
                        <td class="text-xs-center pa-2" v-bind:class="{'amber':props.item.iscito === 'Y'}">
                           <span @click="printSingleBarcode(props.item)" class="icon-medium-fill-base-small xs1 white--text grey darken-1  ml-1 icon-print"></span>
                        </td>
                  </template>
               </v-data-table>
            </v-flex>
         </v-layout>
      </v-card>
       <one-dialog-info :status="opendialoginfo" :msg="msginfo" @close-dialog-info="closeDialogInfo()"></one-dialog-info>
       <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()" @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
   </v-layout>
</template>

<style scoped>
.searchbox .v-input.v-text-field .v-input__slot{
   min-height:60px;
}
.searchbox .v-btn {
   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("samplecall/getstationstatus",{
            name:this.name,
            nolab:this.nolab,
            stationid: this.xselectedstation.id,
            statusid: this.xselectedstatus.id,
           // current_page:1,
            lastid: -1
      })
   },
   methods : {
      isSelected(p) {
         return p.T_BarcodeLabID == this.$store.state.samplecall.selected_patient.T_BarcodeLabID
      },
      searchPatient() {
         this.$store.dispatch("samplecall/search",{
            name:this.name,
            nolab:this.nolab,
            stationid: this.xselectedstation.id,
            statusid: this.xselectedstatus.id,
            //current_page:1,
            lastid: -1
         })
      },
      selectMe(pat) {
         if(this.$store.state.samplecall.no_save == 0 ){
            var patients = this.$store.state.samplecall.patients
            this.$store.commit("samplecall/update_selected_patient",pat)
            var idx = _.findIndex(patients, function(o) { return o.T_BarcodeLabID == pat.T_BarcodeLabID })
            this.$store.commit("samplecall/update_last_id",idx)
         }else{
            this.$store.commit("samplecall/update_open_alert_confirmation",true)
         }
            
      },
      closeAlertConfirmation(){
          this.$store.commit("samplecall/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("samplecall/update_no_save",0)
          this.$store.commit("samplecall/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("samplecall/update_alert_success",val)
      },
      setNewPatient(){
         //this.$store.commit("samplecall/update_selected_Patient",{})
         
      },
      openDialogRequirement(value){
         //console.log(value)
         var trx = this.$store.state.samplecall.patients
         var idx = _.findIndex(trx, function(o) { return o.T_BarcodeLabBarcode == value.T_BarcodeLabBarcode })
         if(value.status === 'V' && value.requirement_status === 'Y'){
               
         }
         else{
               this.$store.commit("samplecall/update_selected_patient",value)
               trx[idx].requirement_status = 'N' 
               //console.log(trx[idx].requirements)
               this.$store.commit("samplecall/update_requirements", trx[idx].requirements)
               this.$store.commit("samplecall/update_dialog_requirement", true)
         }
      },
      checkReq(val,idx){
         var xrequirements = this.requirements
         if(xrequirements[idx].status === 'D'){
               xrequirements[idx].chex = val
               this.$store.commit("samplecall/update_requirements",xrequirements)
         }
      },
      saveRequirement(){
         var sampletypes = this.$store.state.samplecall.patients
         var selected_sample = this.$store.state.samplecall.selected_patient
         var idx = _.findIndex(sampletypes, function(o) { 
               return o.T_BarcodeLabID == selected_sample.T_BarcodeLabID
         })
         if(sampletypes[idx].status === 'D'){
               sampletypes[idx].requirements = this.$store.state.samplecall.requirements
         }
         this.$store.commit("samplecall/update_dialog_requirement",false)
      },
      confirmRequirement(value){
         var sampletypes = this.$store.state.samplecall.patients
         var idx = _.findIndex(sampletypes, function(o) { 
               return o.T_BarcodeLabID == value.T_BarcodeLabID
         })
         if(value.status === 'D'){
            sampletypes[idx].requirement_status = 'Y' 
            this.$store.commit("samplecall/update_patients",sampletypes)

            sampletypes[idx].requirements.forEach((el)=>{el.chex = "N"})
         }
      },
      verify(value){
         console.log(value)
         var goaction = true
         if(value.requirement_status === 'N'){
            var req_check = _.filter(value.requirements, function(o) { return o.chex === 'Y' })
            if(req_check.length === 0){
                  goaction = false
            }
         }
         if(value.status === 'D' && value.requirement_status !== 'X' && goaction){
            console.log("in verify")
            this.$store.commit("samplecall/update_act",'verify')
            this.$store.commit("samplecall/update_selected_patient",value)
            this.closeDialogAction()
         }
         else{
                //console.log('oeey')
                if(value.status === 'D'){
                    this.$store.commit("samplecall/update_msg_info","Mobil batmen ngeluarin api, ada requirement mohon dilengkapi")
                    this.$store.commit("samplecall/update_open_dialog_info",true)
                }
                
            }
      },
      removeDoctor(value){
        //this.selected_patient = value
        let arrPatient = this.$store.state.samplecall.patients
        var idx = _.findIndex(arrPatient, item => item.T_SamplingSoID === value.T_SamplingSoID)
        arrPatient[idx]['chex'] = true 
        this.$store.commit("samplecall/update_patients",arrPatient)
        var selected =  _.filter(arrPatient, function(o) { return o.chex; })
        this.$store.commit("samplecall/update_selected_patient",selected)
        this.$store.commit("samplecall/update_act",'removedoctor')
        this.closeDialogAction()
         
      },
      receive(){
        this.$store.commit("samplecall/update_act",'receive')
        this.closeDialogAction()
         
      },
      reject(){
        this.$store.commit("samplecall/update_act",'reject')
        this.closeDialogAction()
         
      },
      cancel(){
        this.$store.commit("samplecall/update_act",'cancel')
        this.closeDialogAction()
         
      },
      closeDialogAction(){
           console.log("closedialogaction")
            this.btn_hide = true
            var act = this.$store.state.samplecall.act
            var trx  = this.$store.state.samplecall.selected_patient
            if(trx.length > 0){
               this.$store.dispatch("samplecall/doaction",{
                  act:act,
                  sample:trx,
                  nolab:this.nolab,
                  stationid: this.xselectedstation.id,
                  statusid: this.xselectedstatus.id,
                  lastid: -1
               })
            }
            else{
               this.btn_hide = false
               this.$store.commit("samplecall/update_msg_info","Tong kosong nyaring bunyinya, centang masih kosong tak ada gunanya<br/>Pilih salah satu biar ada aksi, jangan malah bengong lakukan saja")
               this.$store.commit("samplecall/update_open_dialog_info",true)
            }
            
        },
      closeDialogSuccess(){
         let arrPatient = this.$store.state.samplecall.patients
         var idx = _.findIndex(arrPatient, item => item.M_PatientID === this.$store.state.samplecall.last_id)
         console.log(idx)
         var xcur_page = 1
       //  if(idx !== -1)
         //   xcur_page = this.$store.state.samplecall.current_page
         
         this.$store.dispatch("samplecall/search",{
            name:this.name,
            nolab:this.nolab,
            stationid: this.xselectedstation.id,
            statusid: this.xselectedstatus.id,
           // current_page:xcur_page,
            lastid: idx
         })

         this.$store.commit("samplecall/update_dialog_success",false)
      },
      closeDialogInfo(){
            this.$store.commit("samplecall/update_open_dialog_info",false)
      },
      checkTop(){
        var barcodes = this.patients
        var selected =  _.filter(barcodes, function(o) { return o.chex; })
        this.bar_chx_all = false
        this.indeterminatex = false
        if(selected.length > 0 && barcodes.length === selected.length){
           this.bar_chx_all = true
           this.indeterminatex = false
        }
        if(selected.length > 0 && barcodes.length > selected.length){
           this.bar_chx_all = false
           this.indeterminatex = true
        }
         this.$store.commit("samplecall/update_selected_patient",selected)
         console.log(selected)
      },
      changeCbxAll(value){
         var arr = this.patients
         this.indeterminatex = false
         arr.forEach((el)=>{el.chex = value})
         var selected =  _.filter(arr, function(o) { return o.chex; })
         this.$store.commit("samplecall/update_selected_patient",selected)
      },
      thr_search_doctor: _.debounce( function () {
            this.$store.dispatch("samplecall/searchdoctor",{search:this.search_doctor})
        },2000),
      saveDoctor(){
         this.errors = {require_doctor:false,require_doctor_address:false}
         if(!_.isEmpty(this.selected_doctor, true) && !_.isEmpty(this.selected_doctor_address, true)){
            var barcodes = this.patients
            var selected =  _.filter(barcodes, function(o) { return o.chex; })
            var prm = {}
            prm.selected_doctor = this.selected_doctor
            prm.selected_doctor_address = this.selected_doctor_address
            prm.flagdoctorinoffice = this.flagdoctorinoffice
            prm.selected_patient = selected
            prm.search = {name:this.name,
               nolab:this.nolab,
               stationid: this.xselectedstation.id,
               statusid: this.xselectedstatus.id,
            // current_page:1,
               lastid: -1}
            this.$store.dispatch("samplecall/savedoctor",prm)
         }else{
            if(_.isEmpty(this.selected_doctor, true)){
               this.errors.require_doctor = true
            }
            if(_.isEmpty(this.selected_doctor_address, true)){
               this.errors.require_doctor_address = true
            }
         }
         
      },
      opendialogdoctor(){
         this.errors = {require_doctor:false,require_doctor_address:false}
         var trx  = this.$store.state.samplecall.selected_patient
         if(trx.length > 0){
            this.flagdoctorinoffice = false
            this.selected_doctor = {}
            this.selected_doctor_address = {}
            this.$store.commit("samplecall/update_doctors",[])
            this.$store.commit("samplecall/update_doctor_address",[])
            this.dialog_doctor = true
         }
         else{
               this.btn_hide = false
               this.$store.commit("samplecall/update_msg_info","Tong kosong nyaring bunyinya, centang masih kosong tak ada gunanya<br/>Pilih salah satu biar ada aksi, jangan malah bengong lakukan saja")
               this.$store.commit("samplecall/update_open_dialog_info",true)
            }
      },
      printSingleBarcode(value){
         one_print_barcode_amplop_so_group(value.T_OrderHeaderID)
      },
   },
   computed: {
      flagdoctorinoffice: {
         get() {
            return this.$store.state.samplecall.flag_doctor_in_office
         },
         set(val) {
            this.$store.commit("samplecall/update_flag_doctor_in_office",val)
         }
      },
      bar_chx_all: {
         get() {
            return this.$store.state.samplecall.bar_chx_all
         },
         set(val) {
            this.$store.commit("samplecall/update_bar_chx_all",val)
         }
      },
      indeterminatex: {
         get() {
            return this.$store.state.samplecall.indeterminatex
         },
         set(val) {
            this.$store.commit("samplecall/update_indeterminatex",val)
         }
      },
      btn_hide: {
         get() {
            return this.$store.state.samplecall.btn_hide
         },
         set(val) {
            this.$store.commit("samplecall/update_btn_hide",val)
         }
      },
      dialogsuccess: {
         get() {
            return this.$store.state.samplecall.dialog_success
         },
         set(val) {
            this.$store.commit("samplecall/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.samplecall.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.samplecall.alert_success
         },
         set(val) {
            this.$store.commit("samplecall/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.samplecall.search_status == 1
      },
      xstations() {
         return this.$store.state.samplecall.stations
      },
      xselectedstation: {
         get() {
            return this.$store.state.samplecall.selected_station
         },
         set(val) {
            this.$store.commit("samplecall/update_selected_station",val)
            this.searchPatient()
         }
      },
       xstatuses() {
         return this.$store.state.samplecall.statuses
      },
      xselectedstatus: {
         get() {
            return this.$store.state.samplecall.selected_status
         },
         set(val) {
            this.$store.commit("samplecall/update_selected_status",val)
            this.searchPatient()
         }
      },
      patients() {
         return this.$store.state.samplecall.patients
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.samplecall.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("samplecall/update_open_alert_confirmation",val)
         }
      },
      curr_page: {
         get() {
            return this.$store.state.samplecall.current_page
         },
         set(val) { 
            this.$store.commit("samplecall/update_current_page",val)
            this.$store.dispatch("samplecall/search",{
               name:this.name,
               nolab:this.nolab,
               stationid: this.xselectedstation.id,
               statusid: this.xselectedstatus.id,
               current_page:val,
               lastid: idx
            })
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.samplecall.total_page
         },
         set(val) {
            this.$store.commit("samplecall/update_total_page",val)
         }
      },
      opendialoginfo: {
            get() {
                return this.$store.state.samplecall.open_dialog_info
            },
            set(val) {
                this.$store.commit("samplecall/update_open_dialog_info",false)
            }
        },
        msginfo(){
            return this.$store.state.samplecall.msg_info
        },
        name: {
            get() {
                return this.$store.state.samplecall.name
            },
            set(val) {
                this.$store.commit("samplecall/update_name",val)
            }
        },
        nolab: {
            get() {
                return this.$store.state.samplecall.nolab
            },
            set(val) {
                this.$store.commit("samplecall/update_nolab",val)
            }
        },
        doctors(){
            return this.$store.state.samplecall.doctors
        },
        selected_doctor: {
            get() {
                return this.$store.state.samplecall.selected_doctor
            },
            set(val) {
                this.$store.commit("samplecall/update_selected_doctor",val)
                this.$store.commit("samplecall/update_doctor_address",val.address)
                //console.log(val.address)
                if(val.address)
                this.selected_doctor_address = val.address[0]
            }
        },
        selected_doctor_address: {
            get() {
                return this.$store.state.samplecall.selected_doctor_address
            },
            set(val) {
                this.$store.commit("samplecall/update_selected_doctor_address",val)
            }
        },
        doctor_address(){
            return this.$store.state.samplecall.doctor_address
        },
        dialog_doctor: {
            get() {
                return this.$store.state.samplecall.dialog_doctor
            },
            set(val) {
                this.$store.commit("samplecall/update_dialog_doctor",val)
            }
        },
        requirements: {
            get() {
                return this.$store.state.samplecall.requirements
            },
            set(val) {
                this.$store.commit("samplecall/update_requirements",val)
            }
        },
   },
   watch: {
      search_doctor(val,old) {
         //console.log(val)
         if (val == old ) return
         if (! val) return
         if (val.length < 1 ) return
         if (this.$store.state.samplecall.update_autocomplete_status == 1 ) return  
         this.thr_search_doctor()
      }
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         items: [],
         search_doctor:'',
         errors:{require_doctor:false,require_doctor_address:false},
         page:1,
         headers: [
            {
               text: "NO REG",
               align: "left",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "text-xs-left pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "name",
               width: "25%",
               class: "text-xs-left pa-2 blue lighten-3 white--text"
            },
            {
               text: "BARCODE",
               align: "left",
               sortable: false,
               value: "lab",
               width: "15%",
               class: "text-xs-left pa-2 blue lighten-3 white--text"
            },
            {
               text: "DOKTER",
               align: "left",
               sortable: false,
               value: "status",
               width: "20%",
               class: "text-xs-left pa-2 blue lighten-3 white--text"
            },
            {
               text: "AKSI",
               align: "center",
               sortable: false,
               value: "status",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ]
      };
   }
}
</script>
