<template>
   <v-layout class="fill-height" column>
      <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 slot="items" slot-scope="props">
               <td class="text-xs-center pa-2 green--text" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                  <v-checkbox false-value="N" true-value="Y" :input-value="checked[props.index]" hide-details @change="checkedChange($event, props.index)" v-show="!isSentAll(props.item)"></v-checkbox>
               </td>
               <td class="text-xs-left pa-2 green--text" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                  {{ props.item.T_OrderHeaderDate.substr(0, 10).split('-').reverse().join('-') }}
               </td>
               <td class="text-xs-left pa-2 green--text" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                  {{ props.item.T_OrderHeaderLabNumber }}
               </td>
               <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                  {{ props.item.M_PatientName }}
               </td>
               <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                   <v-btn small :color="test.sent == 'Y' ? 'green' : 'blue'" dark v-for="(test, n) in props.item.T_TestName" v-bind:key="n" class="mr-1 ma-0">{{test.T_TestName}}</v-btn>
               </td>
               <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                   <v-btn small color="primary" class="mr-1 ma-0" @click="sendMe(props.item)" :disabled="isSentAll(props.item)">Kirim</v-btn>
               </td>
            </template>

         </v-data-table>
         <v-pagination
            v-model="curr_patient_page"
            :length="total_patient_page"
            :total-visible="5"
            @input="change_page"
         ></v-pagination>
      </v-card>
   </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 = {
   data() {
      return {
         query: "",
         items: [],
         headers: [
            {
               text: "X",
               align: "center",
               sortable: false,
               value: "mr",
               width: "5%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "TANGGAL",
               align: "left",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NO LAB",
               align: "left",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "mr",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PEMERIKSAAN",
               align: "left",
               sortable: false,
               value: "mr",
               width: "45%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "RUJUKAN",
               align: "left",
               sortable: false,
               value: "mr",
               width: "5%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],


         isLoading: false
      };
   },

   methods : {
      oneMoment : function(d) {
         return window.oneMoment(d)
      },

      select (item) {
         this.$store.commit('ro_patient/update_selected_patient', item)
      },

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

         return ''
      },

      change_page(x) {
         this.curr_patient_page = x
         this.$store.dispatch('ro_patient/search')
      },

      resultPromise(p) {
         
         let x = p.split(' ')
         let y = x[0].split('-').reverse().join('-')

         return y + ' ' + x[1]
      },

      printMe(url) {
         this.$store.commit('ro_patient/update_rpt_url', url)
         this.$store.commit('ro_patient/update_print_dialog', true)
      },

      sendMe(id) {
         this.$store.commit('ro_send/update_dialog_main', true)
      },

      checkedChange(e, idx) {
         this.$store.commit('ro_patient/update_checked', {idx:idx, val:e})
      },

      isSentAll(item) {
         let x = item.T_TestName
         for (let i in x)
            if (x[i].sent != "Y")
               return false

         return true
      }
   },

   computed : {
      patients () {
         return this.$store.state.ro_patient.patients
      },

      total_patient () {
         return this.$store.state.ro_patient.total_patient
      },

      total_patient_page () {
         return this.$store.state.ro_patient.total_patient_page
      },

      curr_patient_page : {
         get () { return this.$store.state.ro_patient.curr_patient_page },
         set (v) { this.$store.commit('ro_patient/update_curr_patient_page', v) }
      },

      button_print () {
         return [
            {id:1, label:"Lab", color:"blue"},
            {id:2, label:"MDT", color:"green"}
         ]
      },

      checked : {
         get () { return this.$store.state.ro_patient.checked },
         set (v) { this.$store.commit('ro_patient/update_checked', v) }
      }
   },

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