<template>
   <v-layout class="fill-height" column>
      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout >
               <v-menu
                  ref="menustartdate"
                  v-model="menustartdate"
                  :close-on-content-click="false"
                  :nudge-right="0"
                  lazy
                  transition="scale-transition"
                  offset-y
                  full-width
                  max-width="290px"
                  min-width="290px"
                  >
                  <template v-slot:activator="{ on }">
                     <v-text-field
                     v-model="startDateFormatted"
                     label="Tanggal Awal"
                     readonly
                     v-on="on"
                     @blur="date = deFormatedDate(startDateFormatted)"
                     ></v-text-field>
                  </template>
                  <v-date-picker v-model="xstartdate" no-title @input="menustartdate = false"></v-date-picker>
               </v-menu>

               <v-menu
                  ref="menuenddate"
                  v-model="menuenddate"
                  :close-on-content-click="false"
                  :nudge-right="0"
                  lazy
                  transition="scale-transition"
                  offset-y
                  full-width
                  max-width="290px"
                  min-width="290px"
                  >
                  <template v-slot:activator="{ on }">
                     <v-text-field
                     v-model="endDateFormatted"
                     label="Tanggal Awal"
                     readonly
                     v-on="on"
                     @blur="date = deFormatedDate(endDateFormatted)"
                     ></v-text-field>
                  </template>
                  <v-date-picker v-model="xenddate" no-title @input="menuenddate = false"></v-date-picker>
               </v-menu>
               
               <v-text-field class="xs6 ma-1"
                             label=""
                             placeholder="Nama / No lab"
                             single-line
                             outline
                             v-model="searchnamelab"
                             hide-details
                             ></v-text-field>

               <v-select class="xs6 ma-1" :items="statuses" 
                  item-text="name"
                  return-object
                  v-model="status"
                  label="Status" outline   hide-details></v-select>
            <v-btn class="xs3 ma-1" color="success"  @click="searchPatient" >Search</v-btn>
         </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 slot="items" slot-scope="props">
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_PatientNoReg }}</td>
                     <td class="pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_OrderHeaderLabNumber}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_PatientName}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_StatusName}}</td>
                  </template>
               </v-data-table>
            </v-flex>
         </v-layout>
      </v-card>
      <one-dialog-alert :status="openalertverification" :msg="msgalertverification" @forget-dialog-alert="forgetAlertVerification()" @close-dialog-alert="closeAlertVerification()"></one-dialog-alert>
      <one-dialog-info :status="opendialogverification" :msg="msginfoverification" @close-dialog-info="closeInfoVerification()"></one-dialog-info>
   </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')
   },
   mounted() {
      this.$store.dispatch("patient/search",{
            startdate:this.xstartdate,
            enddate:this.xenddate,
            search: this.searchnamelab,
            status: this.status.value
         })
   },
   methods : {
      formatDate (date) {
          if (!date) return null

          const [year, month, day] = date.split('-')
          return `${day}-${month}-${year}`
      },
      deFormatedDate (date) {
          if (!date) return null

      const [ day,month, year] = date.split('-')
      return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
      },
      isSelected(p) {
         return p.T_OrderHeaderID == this.$store.state.patient.selected_patient.T_OrderHeaderID
      },
      searchPatient() {
         this.$store.dispatch("patient/search",{
            startdate:xstartdate,
            enddate:xenddate,
            search:this.searchnamelab,
            status: this.status.value
         })
      },
      selectMe(pat) {
          
         
      }
   },
   computed: {
      startDateFormatted () {
            return this.formatDate(this.xstartdate)
      },
      endDateFormatted () {
            return this.formatDate(this.xenddate)
      },
      isLoading() {
         return this.$store.state.patient.search_status == 1
      },
      patients() {
         return this.$store.state.patient.patients
      }
   },
   data() {
      return {
         items: [],
         xstartdate:moment(new Date()).format('YYYY-MM-DD'),
         xenddate:moment(new Date()).format('YYYY-MM-DD'),
         statuses:[{name:'Belum Bayar',value:'N'},{name:'Sudah Bayar',value:'Y'}],
         searchnamelab: '',
         nolab: '',
         status:{name:'Belum Bayar',value:'N'},
         menustartdate:false,
         menuenddate:false,
         headers: [
            {
               text: "PID",
               align: "left",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NO LAB",
               align: "left",
               sortable: false,
               value: "lab",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "name",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },

            {
               text: "STATUS",
               align: "left",
               sortable: false,
               value: "status",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
      }
   }
}
</script>
