<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="dialogrequirement" persistent  max-width="45%">
        <v-card>
            <v-card-title color="success"  class="headline">Pilih yang tidak terpenuhi</v-card-title>
            <v-card-text>
               <v-layout wrap>
                   <v-flex v-for="(req,idx) in requirements" :key="req.id" xs6>
                       <one-x-check
                            :xdatalabel="req.name"
                            :xdatacbx="req.chex"
                            @update-data-cbx="(val) =>checkReq(val,idx)"
                        ></one-x-check>
                   </v-flex>
               </v-layout>
            </v-card-text>
            <v-card-actions>
            <v-spacer></v-spacer>
               <v-btn color="green darken-1" flat @click="saveRequirement">Tutup</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 row>
            <v-flex xs3>
               <v-text-field
                        class="ma-1"
                        style="font-size:14px"
                             label="No Reg / Nama"
                             outline
                             v-model="nolab"
                             hide-details
                             ></v-text-field>
            </v-flex>
            <!--<v-flex xs2>
               <v-text-field 
                class="ma-1"
                style="font-size:14px"
                             label="Nama"
                             outline
                             v-on:keyup.enter="searchPatient"
                             v-model="name"
                             hide-details
                             ></v-text-field>
            </v-flex>-->
            <v-flex xs4>
               <v-select class="ma-1 mini-select" :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 xs1>
               <span @click="searchPatient"  class="icon-medium-fill-base white--text success iconsearch-search"></span>
            </v-flex>
            <!--<v-flex xs4>
               <v-select class="ma-1 mini-select" :items="xstatuses" 
                  item-text="name"
                   style="font-size:14px"
                  return-object
                  v-model="xselectedstatus"
                  label="Status" outline   hide-details></v-select>
            </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 fixed_header">
                  <template slot="items" slot-scope="props">
                     <td style="width:10%" class="text-xs-center pa-1" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">{{ props.item.order_date}}</td>
                     <td style="width:10%" class="text-xs-center pa-1" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <span class="font-weight-black caption">{{ props.item.T_OrderHeaderLabNumber }}</span>
                        <span style="color:#800000" class="font-weight-bold caption">{{props.item.T_OrderHeaderLabNumberExt}}</span>
                     </td>
                     <td style="width:17%" class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <p class="mb-0">{{ props.item.patient_fullname}}</p>
                        <p style="cursor:pointer" class="mb-0" :title="props.item.umur">{{ props.item.patient_dob}}</p>
                     </td>
                     <td style="width:17%" class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        
                        <p v-if="props.item.status === 'D'" class="mb-0">{{ props.item.testname}}</p>
                        <p v-if="props.item.status === 'Z'" style="text-decoration:line-through" class="red--text mb-0">{{props.item.testname}}</p>
                        <p class="mb-0">{{ props.item.doctor_fullname}}</p>
                     </td>
                     <td style="width:10%" class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <div>
                           <span @click="openDialogRequirement(props.item)" v-bind:class="{ 'white': props.item.requirement_status === 'X', 'error': props.item.requirement_status === 'N' }" class="icon-medium-fill-base-small white"><v-icon :dark="props.item.requirement_status === 'N'">close</v-icon></span>
                           <span @click="confirmRequirement(props.item)" v-bind:class="{ 'white': props.item.requirement_status === 'X', 'success': props.item.requirement_status === 'Y' }" class="icon-medium-fill-base-small white"><v-icon :dark="props.item.requirement_status === 'Y'">check</v-icon></span>
                        </div>
                     </td>
                     <td style="width:10%" class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item),'amber':props.item.iscito === 'Y'}" @click="selectMe(props.item)">
                        <div>
                           <span v-if="btn_hide === false && (props.item.status === 'D' || props.item.status === 'Z')" @click="reject(props.item)" class="icon-medium-fill-base-small black"><v-icon dark>replay</v-icon></span>
                           <span v-if="btn_hide === false && (props.item.status === 'D' || props.item.status === 'Z')" @click="verify(props.item)"  class="icon-medium-fill-base-small teal"><v-icon dark>done_all</v-icon></span>
                           <span v-if="btn_hide === false && props.item.status === 'V'"  @click="unverify(props.item)" class="icon-medium-fill-base-small black"><v-icon dark>cancel</v-icon></span>
                        </div>
                     </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;
}

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

    table {
    border-collapse: collapse;
    width: 100%;
    }
table.v-table tbody td,table.v-table tbody th {
   height: 40px;
}

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

.fixed_header tbody {
  display: block;
  max-height: 630px;
  overflow-y: scroll;
}

.fixed_header thead,
tbody tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}

</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_SamplingSoID == this.$store.state.samplecall.selected_patient.T_SamplingSoID
      },
      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_SamplingSoID == pat.T_SamplingSoID })
            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_SamplingSoID == value.T_SamplingSoID })
         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].status === 'Z'){
               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_SamplingSoID == selected_sample.T_SamplingSoID
         })
         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_SamplingSoID == value.T_SamplingSoID
         })
         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.status === 'Z') && 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' || value.status === 'Z'){
                    this.$store.commit("samplecall/update_msg_info","Mobil batmen ngeluarin api, ada requirement mohon dilengkapi")
                    this.$store.commit("samplecall/update_open_dialog_info",true)
                }
                
            }
      },
      unverify(value){
         if(value.status === 'V'){
            console.log("in unverify")
            this.$store.commit("samplecall/update_act",'unverify')
            this.$store.commit("samplecall/update_selected_patient",value)
            this.closeDialogAction()
         }
      },
      reject(value){
         if(value.status === 'D' || value.status === 'Z'){
            console.log("in reject")
            this.$store.commit("samplecall/update_act",'reject')
            this.$store.commit("samplecall/update_selected_patient",value)
            this.closeDialogAction()
         }
      },
      closeDialogAction(){
           console.log("closedialogaction")
            this.btn_hide = true
            var act = this.$store.state.samplecall.act
            //var statusnextid = 4
            var trxs  = this.$store.state.samplecall.patients
            var trx  = this.$store.state.samplecall.selected_patient
            var idx = _.findIndex(trxs, item => item.T_SamplingSoID === trx.T_SamplingSoID)
            this.$store.dispatch("samplecall/doaction",{
               act:act,
               orderid:trx.T_OrderHeaderID,
               sampleid:trx.T_SampleTypeID,
               sample:trx,
               name:this.name,
               nolab:this.nolab,
               stationid: this.xselectedstation.id,
               statusid: this.xselectedstatus.id,
               //current_page:1,
               lastid: -1
            })
            
        },
      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)
        }
   },
   computed: {
      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)
                //////this.searchPatient()
            }
        },
        nolab: {
            get() {
                return this.$store.state.samplecall.nolab
            },
            set(val) {
                this.$store.commit("samplecall/update_nolab",val)
                //////this.searchPatient()
            }
        },
        dialogrequirement: {
            get() {
                return this.$store.state.samplecall.dialog_requirement
            },
            set(val) {
                this.$store.commit("samplecall/update_dialog_requirement",val)
            }
        },
        requirements: {
            get() {
                return this.$store.state.samplecall.requirements
            },
            set(val) {
                this.$store.commit("samplecall/update_requirements",val)
            }
        },
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         items: [],
         page:1,
         headers: [
            {
               text: "TGL. TRANSAKSI",
               align: "center",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NO REG",
               align: "center",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "center",
               sortable: false,
               value: "name",
               width: "17%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PEMERIKSAAN",
               align: "center",
               sortable: false,
               value: "lab",
               width: "17%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "REQUIREMENT",
               align: "center",
               sortable: false,
               value: "status",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "AKSI",
               align: "center",
               sortable: false,
               value: "status",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ]
      };
   }
}
</script>
