<template>
   <v-layout class="fill-height" column>
      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout row>
            <v-flex xs10>
               <v-layout row>
                  <v-flex xs4>
                     <v-text-field class="ma-1"
                        style="font-size:14px"
                             label="No Reg"
                             outline
                             v-model="nolab"
                             hide-details
                             ></v-text-field>
                  </v-flex>
                  <v-flex xs6>
                     <v-select class="ma-1 mini-select" :items="filter_sampletype" 
                        item-text="name"
                        return-object
                        style="font-size:14px"
                        v-model="selected_filter_sampletype"
                        label="Specimen" outline   hide-details></v-select>
                  </v-flex>
                  <!--<v-flex xs8>
                     <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-flex>-->
                  <!-- <v-flex xs8>
               <v-select class="ma-1 mini-select" :items="filter_sampletype" 
                  item-text="name"
                  return-object
                   style="font-size:14px"
                  v-model="selected_filter_sampletype"
                  label="Specimen" outline   hide-details></v-select>
            </v-flex>-->
               </v-layout>
            </v-flex>
            <v-flex xs2 v-if="btn_hide === false" text-md-right>
                  <v-tooltip  left>
                  <template v-slot:activator="{ on }">
                     <span v-on="on"   @click="send"  class="icon-medium-fill-base xs1 white--text primary"><v-icon large dark>send</v-icon></span>
                  </template>
                  <span>Kirim</span>
               </v-tooltip>
            </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="elevation-1">
                  <template v-slot:headers="props">
                        <tr>
                           <th style="width:20px" class= "text-xs-left blue-grey white--text">
                              <v-checkbox style="padding-top:0;margin-top:0"
                                 hide-details
                                 color="white"
                                 :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:20px" class="text-xs-left">
                           <v-checkbox style="padding-top:0;margin-top:0"
                              color="blue-grey"
                              hide-details
                              v-model="props.item.chex"
                              @change="checkTop()"
                           ></v-checkbox>
                        </td>
                        <td class="text-xs-left pa-2">{{ props.item.T_OrderHeaderLabNumber }}</td>
                        <td class="text-xs-left pa-2">{{ props.item.patient_fullname}}</td>
                        <td class="text-xs-left pa-2">
                           <p class="mb-0">{{ props.item.T_SampleTypeName}}</p>
                           <p class="mb-0">{{ props.item.T_BarcodeLabBarcode}}</p>
                        </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("samplesend/getstationstatus",{
            name:this.name,
            nolab:this.nolab,
            sampletypeid: 0,
            lastid: -1
      })
   },
   methods : {
      isSelected(p) {
         return p.T_BarcodeLabID == this.$store.state.samplesend.selected_patient.T_BarcodeLabID
      },
      searchPatient() {
         this.$store.dispatch("samplesend/search",{
            name:this.name,
            nolab:this.nolab,
            sampletypeid: this.selected_filter_sampletype.id,
            lastid: -1
         })
      },
      selectMe(pat) {
         if(this.$store.state.samplesend.no_save == 0 ){
            var patients = this.$store.state.samplesend.patients
            this.$store.commit("samplesend/update_selected_patient",pat)
            var idx = _.findIndex(patients, function(o) { return o.T_BarcodeLabID == pat.T_BarcodeLabID })
            this.$store.commit("samplesend/update_last_id",idx)
         }else{
            this.$store.commit("samplesend/update_open_alert_confirmation",true)
         }
            
      },
      closeAlertConfirmation(){
          this.$store.commit("samplesend/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("samplesend/update_no_save",0)
          this.$store.commit("samplesend/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("samplesend/update_alert_success",val)
      },
      setNewPatient(){
         //this.$store.commit("samplesend/update_selected_Patient",{})
         
      },
      openDialogRequirement(value){
         //console.log(value)
         var trx = this.$store.state.samplesend.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("samplesend/update_selected_patient",value)
               trx[idx].requirement_status = 'N' 
               //console.log(trx[idx].requirements)
               this.$store.commit("samplesend/update_requirements", trx[idx].requirements)
               this.$store.commit("samplesend/update_dialog_requirement", true)
         }
      },
      checkReq(val,idx){
         var xrequirements = this.requirements
         if(xrequirements[idx].status === 'D'){
               xrequirements[idx].chex = val
               this.$store.commit("samplesend/update_requirements",xrequirements)
         }
      },
      saveRequirement(){
         var sampletypes = this.$store.state.samplesend.patients
         var selected_sample = this.$store.state.samplesend.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.samplesend.requirements
         }
         this.$store.commit("samplesend/update_dialog_requirement",false)
      },
      confirmRequirement(value){
         var sampletypes = this.$store.state.samplesend.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("samplesend/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("samplesend/update_act",'verify')
            this.$store.commit("samplesend/update_selected_patient",value)
            this.closeDialogAction()
         }
         else{
                //console.log('oeey')
                if(value.status === 'D'){
                    this.$store.commit("samplesend/update_msg_info","Mobil batmen ngeluarin api, ada requirement mohon dilengkapi")
                    this.$store.commit("samplesend/update_open_dialog_info",true)
                }
                
            }
      },
      send(){
        this.$store.commit("samplesend/update_act",'send')
        this.closeDialogAction()
         
      },
      closeDialogAction(){
           console.log("closedialogaction")
            this.btn_hide = true
            var act = this.$store.state.samplesend.act
            var trx  = this.$store.state.samplesend.selected_patient
            this.$store.dispatch("samplesend/doaction",{
               act:act,
               sample:trx,
               nolab:this.nolab,
               sampletypeid: this.selected_filter_sampletype.id,
               lastid: -1
            })
        },
      closeDialogSuccess(){
         let arrPatient = this.$store.state.samplesend.patients
         var idx = _.findIndex(arrPatient, item => item.M_PatientID === this.$store.state.samplesend.last_id)
         console.log(idx)
         var xcur_page = 1
       //  if(idx !== -1)
         //   xcur_page = this.$store.state.samplesend.current_page
         
         this.$store.dispatch("samplesend/search",{
            name:this.name,
            nolab:this.nolab,
            stationid: this.xselectedstation.id,
            statusid: this.xselectedstatus.id,
           // current_page:xcur_page,
            lastid: idx
         })

         this.$store.commit("samplesend/update_dialog_success",false)
      },
      closeDialogInfo(){
            this.$store.commit("samplesend/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("samplesend/update_selected_patient",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("samplesend/update_selected_patient",selected)
    },
   },
   computed: {
      filter_sampletype(){
         return this.$store.state.samplesend.filter_sampletype
      },
      selected_filter_sampletype: {
         get() {
            return this.$store.state.samplesend.selected_filter_sampletype
         },
         set(val) {
            this.$store.commit("samplesend/update_selected_filter_sampletype",val)
            this.searchPatient()
         }
      },
       bar_chx_all: {
         get() {
            return this.$store.state.samplesend.bar_chx_all
         },
         set(val) {
            this.$store.commit("samplesend/update_bar_chx_all",val)
         }
      },
      indeterminatex: {
         get() {
            return this.$store.state.samplesend.indeterminatex
         },
         set(val) {
            this.$store.commit("samplesend/update_indeterminatex",val)
         }
      },
      btn_hide: {
         get() {
            return this.$store.state.samplesend.btn_hide
         },
         set(val) {
            this.$store.commit("samplesend/update_btn_hide",val)
         }
      },
      dialogsuccess: {
         get() {
            return this.$store.state.samplesend.dialog_success
         },
         set(val) {
            this.$store.commit("samplesend/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.samplesend.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.samplesend.alert_success
         },
         set(val) {
            this.$store.commit("samplesend/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.samplesend.search_status == 1
      },
      xstations() {
         return this.$store.state.samplesend.stations
      },
      xselectedstation: {
         get() {
            return this.$store.state.samplesend.selected_station
         },
         set(val) {
            this.$store.commit("samplesend/update_selected_station",val)
            this.searchPatient()
         }
      },
       xstatuses() {
         return this.$store.state.samplesend.statuses
      },
      xselectedstatus: {
         get() {
            return this.$store.state.samplesend.selected_status
         },
         set(val) {
            this.$store.commit("samplesend/update_selected_status",val)
         }
      },
      patients() {
         return this.$store.state.samplesend.patients
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.samplesend.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("samplesend/update_open_alert_confirmation",val)
         }
      },
      curr_page: {
         get() {
            return this.$store.state.samplesend.current_page
         },
         set(val) { 
            this.$store.commit("samplesend/update_current_page",val)
            this.$store.dispatch("samplesend/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.samplesend.total_page
         },
         set(val) {
            this.$store.commit("samplesend/update_total_page",val)
         }
      },
      opendialoginfo: {
            get() {
                return this.$store.state.samplesend.open_dialog_info
            },
            set(val) {
                this.$store.commit("samplesend/update_open_dialog_info",false)
            }
        },
        msginfo(){
            return this.$store.state.samplesend.msg_info
        },
        name: {
            get() {
                return this.$store.state.samplesend.name
            },
            set(val) {
                this.$store.commit("samplesend/update_name",val)
            }
        },
        nolab: {
            get() {
                return this.$store.state.samplesend.nolab
            },
            set(val) {
                this.$store.commit("samplesend/update_nolab",val)
            }
        },
        dialogrequirement: {
            get() {
                return this.$store.state.samplesend.dialog_requirement
            },
            set(val) {
                this.$store.commit("samplesend/update_dialog_requirement",val)
            }
        },
        requirements: {
            get() {
                return this.$store.state.samplesend.requirements
            },
            set(val) {
                this.$store.commit("samplesend/update_requirements",val)
            }
        },
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         items: [],
         on:false,
         page:1,
         //indeterminatex:false,
         checkednotall:false,
         //bar_chx_all:false,
         selected: [],
         headers: [
            {
               text: "NO REG",
               align: "left",
               sortable: false,
               value: "mr",
               width: "17%",
               class: "text-xs-left pa-2 blue-grey white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "name",
               width: "43%",
               class: "text-xs-left pa-2 blue-grey white--text"
            },
            {
               text: "BARCODE",
               align: "left",
               sortable: false,
               value: "lab",
               width: "25%",
               class: "text-xs-left pa-2 blue-grey white--text"
            }
         ]
      };
   }
}
</script>
