<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-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)">
                  {{ resultPromise(props.item.T_OrderPromiseDateTime) }}
               </td>
               <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)"
                   @click="select(props.item)">
                  <v-btn small :color="btn.color" dark v-for="btn in props.item.buttons" v-bind:key="btn.id" class="ml-1 mr-1 ma-0" @click="printMe(btn.url)">{{btn.label}}</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: "NO LAB",
               align: "left",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "mr",
               width: "45%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "JANJI HASIL",
               align: "left",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "CETAK HASIL",
               align: "left",
               sortable: false,
               value: "mr",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],


         isLoading: false
      };
   },

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

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

      is_selected (item) {
         let x = this.$store.state.rp_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('rp_patient/search')
      },

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

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

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

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

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

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

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

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

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