<template>
   <v-layout class="fill-height" column>
      <v-snackbar
         v-model="snackbar.status"
         :color="snackbar.color"
         :timeout="snackbar.timeout"
         :vertical="snackbar.mode === 'vertical'"
      >
         {{ snackbar.text }}
         <v-btn
         dark
         flat
         @click="snackbar.status = false"
         >
         Tutup
         </v-btn>
      </v-snackbar>
      <v-card class="grow">
         <!-- <v-subheader>
            <h3 class="title">DAFTAR PASIEN</h3>
         </v-subheader> -->
            <hr style="border-top:0px solid #c8c8c8;" />
         <v-data-table
            :headers="headers" :items="patients"
            :loading="isLoading"
            hide-actions class="xelevation-1">
            <template v-slot:headers="props">
                  <tr>
                     <th style="width:20px"  class= "text-xs-left blue lighten-3 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 slot="items" slot-scope="props">
            <tr :class="{
               'susulan' : is_susulan(props.item),
               'verif_done': is_verif_done(props.item), 'verif_partial': is_verif_partial(props.item), 
               'valid_done': is_valid_done(props.item), 'valid_partial':is_valid_partial(props.item) }"
            >
               <td style="width:20px" v-bind:class="[is_selected(props.item), is_cito(props.item)]" class="text-xs-left">
                           <v-checkbox v-if="props.item.ready_print === 'Y'" style="padding-top:0;margin-top:0"
                              color="white"
                              hide-details
                              :readonly="bar_chx_all"
                              v-model="props.item.chex"
                              @change="checkTop(props.item)"
                           ></v-checkbox>
                           <span v-if="props.item.ready_print === 'N'">
                              <v-icon>lock</v-icon>
                           </span>
                           <span v-if="props.item.ready_print === 'P'">
                              <v-icon>receipt</v-icon>
                           </span>
                        </td>
               <td 
                   class="text-xs-left pa-2" v-bind:class="[is_selected(props.item), is_cito(props.item)]"
                   @click="select(props.item)">
                   <span v-bind:class="{'font-weight-bold red--text':props.item.ready_print === 'N','font-weight-bold white--text':props.item.ready_print === 'Y'}">{{props.item.T_OrderHeaderLabNumber}}</span> <br/>
                   <span >{{ props.item.T_OrderHeaderLabNumberExt }}</span>
               </td>
               <td 
                   class="text-xs-left pa-2" v-bind:class="[is_selected(props.item), is_cito(props.item)]"
                   @click="select(props.item)">
                   <span v-bind:class="{'red--text':props.item.ready_print === 'N','white--text':props.item.ready_print === 'Y'}">{{props.item.M_PatientName}}</span> <br/>
                  {{ props.item.order_date }}
               </td>
            </tr>
           </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-card>
   </v-layout>
</template>

<style scoped>
   div.v-table__overflow {
      height:560px!important;
      overflow-y:scroll;
   }
   tr.susulan {
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%, white 5%);
   }
   tr.susulan.verif_partial{
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%, white 5%, white 40%,yellow 70%);
   }
   tr.susulan.verif_done {
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%, yellow 5%);
   }
   tr.susulan.verif_partial.valid_partial {
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%,#99e699 5%, #99e699 20%,white 30%,  white 70%, yellow 80%);
   }
   tr.susulan.verif_done.valid_partial {
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%,#99e699 5%,  #99e699 40%,yellow 60%);
   }
   tr.susulan.valid_done{
      background-image: linear-gradient(to right, rgba(255, 0, 0,0.9)  5%,#99e699 5%,  #99e699,#99e699, #99e699, #99e699);
   }



   tr.verif_partial{
      background-image: linear-gradient(to right, white 40%,yellow 70%);
   }
   tr.verif_done {
      background-image: linear-gradient(to right, yellow,yellow, yellow, yellow);
   }
   tr.verif_partial.valid_partial {
      background-image: linear-gradient(to right, #99e699 20%,white 30%,  white 70%, yellow 80%);
   }
   tr.verif_done.valid_partial {
      background-image: linear-gradient(to right, #99e699 40%,yellow 60%);
   }
   tr.valid_done{
      background-image: linear-gradient(to right, #99e699,#99e699, #99e699, #99e699);
   }

   .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 = {
   data() {
      return {
         query: "",
         items: [],
        // selected_patients:[],
         headers: [
            {
               text: "NO REG",
               align: "left",
               sortable: false,
               value: "mr",
               width: "30%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "mr",
               width: "70%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],


         isLoading: false
      };
   },
   methods : {
      is_susulan(i) {
         let cur_date = moment().format('DD.MM.YYYY')
         let o_date = moment(i.T_OrderHeaderDate).format('DD.MM.YYYY')

         return cur_date != o_date
      },
      oneMoment : function(d) {
         return window.oneMoment(d)
      },
      is_cito (item) {
            let x = this.$store.state.re_patient.selected_patient
            if (!x)
               return ''
            
            if (item.T_OrderHeaderIsCito == "Y")
               return 'amber'

            return ''
      },
      is_valid_partial(i) {
         return i.T_OrderHeaderAddOnValidationDone == 'P'
      },
      is_valid_done(i) {
         return i.T_OrderHeaderAddOnValidationDone == 'Y'
      },
      is_verif_partial(i) {
         return i.T_OrderHeaderAddOnVerificationDone == 'P'
      },
      is_verif_done(i) {
         return i.T_OrderHeaderAddOnVerificationDone == 'Y'
      },
      format_date(p) {
         return moment(p).format("DD.MM.YYYY")
      },
      select (item) {
         let user = one_user()
         this.$store.commit('re_patient/update_selected_patient', item)
         let rpt = "rpt_test"
         console.log(item.ready_print)
         if(item.ready_print === 'N')
            rpt = "rpt_test_not_print"
         var xurl = "/birt/frameset?__report=report/one/lab/"+rpt+".rptdesign&__format=pdf&username="+user.M_UserUsername+"&PID="+item.T_OrderHeaderID+"&tm="+Date.now()
         this.$store.commit('re_patient/update_url_print', xurl)
         this.$store.commit('re_px/update_id', item.T_OrderHeaderID)
         this.$store.dispatch('re_patient/getdeliveries',{id:item.T_OrderHeaderID})
         //this.$store.dispatch('re_patient/info_req')
         // this.$store.commit('ver_verification/update_selected_sent_sample', item)
      },

      is_selected (item) {
         let x = this.$store.state.re_patient.selected_patient
         if (!x)
            return ''
         
         if (x.T_OrderHeaderID == item.T_OrderHeaderID  && item.ready_print !== 'Y')
            return 'green lighten-4'

         if (item.ready_print === 'Y')
            return 'purple white--text'

         return ''
      },

      change_page(x) {
         this.curr_patient_page = x
         this.$store.dispatch('re_patient/search')
      },
      checkTop(value){
         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
         }
           var arrx = this.selected_patients
           var idx = _.findIndex(arrx, function(o) { return o.T_OrderHeaderID == value.T_OrderHeaderID && o.T_OrderPromiseID == value.T_OrderPromiseID })
           if(idx == -1 && value.chex)
               arrx.push({T_OrderHeaderID:value.T_OrderHeaderID,T_OrderPromiseID:value.T_OrderPromiseID})
         
            if(idx > -1 && !value.chex)
               arrx.splice(idx, 1)

            this.selected_patients = arrx
         },
      changeCbxAll(value){
         var arr = this.patients
         this.indeterminatex = false
         arr.forEach((el)=>{
            if(el.ready_print == 'Y')
               el.chex = value
            else{
               el.chex = false
            }
         })
         var selected =  _.filter(arr, function(o) { return o.chex; })
         this.selected_patients = []
      }
   },

   computed : {
      snackbar: {
         get() {
            return this.$store.state.re_patient.snackbar
         },
         set(val) { 
            this.$store.commit("re_patient/update_snackbar",val)
         }
      },
      patients () {
         return this.$store.state.re_patient.patients
      },
      selected_patients: {
         get() {
            return this.$store.state.re_patient.selected_patients
         },
         set(val) { 
            this.$store.commit("re_patient/update_selected_patients",val)
            //this.$store.dispatch('re_patient/search')
         }
      },
      total_patient () {
         return this.$store.state.re_patient.total_patient
      },

      total_patient_page () {
         return this.$store.state.re_patient.total_patient_page
      },
      curr_patient_page : {
         get () { return this.$store.state.re_patient.curr_patient_page },
         set (v) { this.$store.commit('re_patient/update_curr_patient_page', v) }
      },
      curr_page: {
         get() {
            return this.$store.state.re_patient.current_page
         },
         set(val) { 
            this.$store.commit("re_patient/update_current_page",val)
            this.$store.dispatch('re_patient/search')
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.re_patient.total_patient_page
         },
         set(val) {
            this.$store.commit("re_patient/update_total_patient_page",val)
         }
      },
      bar_chx_all: {
         get() {
            return this.$store.state.re_patient.bar_chx_all
         },
         set(val) {
            this.$store.commit("re_patient/update_bar_chx_all",val)
         }
      },
      indeterminatex: {
         get() {
            return this.$store.state.re_patient.indeterminatex
         },
         set(val) {
            this.$store.commit("re_patient/update_indeterminatex",val)
         }
      },
   },

   mounted () {
      this.$store.dispatch('re_patient/search')
     //this.$store.dispatch('re_patient/get_deliverytype')
      
   }
}
</script>
