<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-card   class="mb-2 pa-2 searchbox">
         <v-layout >
               <v-menu class="xs3 pr-2"
                          v-model="menufilterdatestart"
                          :close-on-content-click="false"
                          :nudge-right="40"
                          lazy
                          hide-details
                          transition="scale-transition"
                          offset-y
                          full-width
                          max-width="290px"
                          min-width="290px"
                          >
                          <template v-slot:activator="{ on }">
                              <v-text-field
                               class="mt-1"
                              v-model="filterComputedDateFormattedStart"
                              label="Tanggal Awal"
                              outline
                              hide-details
                              readonly
                              v-on="on"
                              @blur="date = deFormatedDate(filterComputedDateFormattedStart)"
                              ></v-text-field>
                          </template>
                          <v-date-picker v-model="xdatestart" no-title @input="menufilterdatestart = false"></v-date-picker>
                      </v-menu>

               <v-menu class="xs3 pl-2"
                          v-model="menufilterdateend"
                          :close-on-content-click="false"
                          :nudge-right="40"
                          lazy
                          hide-details
                          transition="scale-transition"
                          offset-y
                          full-width
                          max-width="290px"
                          min-width="290px"
                          >
                          <template v-slot:activator="{ on }">
                              <v-text-field
                              class="mt-1"
                              v-model="filterComputedDateFormattedEnd"
                              label="Tanggal Akhir"
                              outline
                              hide-details
                              readonly
                              v-on="on"
                              @blur="date = deFormatedDate(filterComputedDateFormattedEnd)"
                              ></v-text-field>
                          </template>
                          <v-date-picker v-model="xdateend" no-title @input="menufilterdateend = false"></v-date-picker>
                      </v-menu>
               <v-text-field class="xs4 ma-1"
                             label="Nama / No Lab"
                             placeholder="Cari ..."
                             outline
                             v-model="xnamelab"
                             hide-details
                             ></v-text-field>
               <v-select class="xs3 mini-select ma-1" :items="xstations" 
                  item-text="name"
                  return-object
                  v-model="xselectedstation"
                  label="Station" outline   hide-details></v-select>
            <!--<v-btn class="xs3 ma-1" color="success"  @click="searchTransaction" >Cari</v-btn>-->
            <span @click="searchTransaction"  class="icon-medium-fill-base xs1 white--text success iconsearch-search"></span>
         </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="xtransactions"
                  :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.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_PatientNoReg}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.patient_fullname}}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_SampleTypeName}}</td>
                  </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-flex>
         </v-layout>
      </v-card>
       <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()" @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
       <one-dialog-info :status="opendialoginfo" :msg="msginfo" @close-dialog-info="closeDialogInfo()"></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("sample/getstation",{
         namelab:this.xnamelab,
         startdate:this.xdatestart,
         enddate: this.xdateend,
         current_page:1,
         lastid:-1
      })
   },
   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.trx_id == this.$store.state.sample.selected_transaction.trx_id
      },
      searchTransaction() {
         this.$store.dispatch("sample/search",{
            namelab:this.xnamelab,
            startdate:this.xdatestart,
            enddate: this.xdateend,
            stationid:this.xselectedstation.id,
            current_page:1,
            lastid:-1
         })
      },
      selectMe(trx) {
         if(this.$store.state.sample.no_save == 0 ){
            //console.log(trx)
            this.$store.commit("sample/update_selected_transaction",trx)
            this.$store.commit("sample/update_photos",trx.photos)
            this.$store.commit("sample/update_act",'edit')
            this.$store.commit("sample/update_doctors",trx.doctors)
            if(trx.doctor_fulname !== null){
               this.$store.commit("sample/update_selected_doctor",{id:trx.So_WalkLetterCourierM_DoctorID,name:trx.doctor_fullname})
               this.$store.commit("sample/update_doctor_address",trx.doctoraddress)
               this.$store.commit("sample/update_selected_doctor_address",{id:trx.So_WalkLetterCourierM_DoctorAddressID,name:trx.doctor_address})
              // this.$store.commit("sample/update_selected_courier",{id:trx.So_WalkLetterCourierM_StaffID,name:trx.courier_name})
            }
            else{
               this.$store.commit("sample/update_selected_doctor",{})
            }
            
         }else{
            this.$store.commit("sample/update_open_alert_confirmation",true)
         }
      },
      closeAlertConfirmation(){
          this.$store.commit("sample/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("sample/update_no_save",0)
          this.$store.commit("sample/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("sample/update_alert_success",val)
      },
      setNewTransaction(){
         this.$store.commit("sample/update_selected_transaction",{})
         this.$store.commit("sample/update_act",'new')
      },
      closeDialogSuccess(){
         let arrtrx = this.$store.state.sample.transactions
         var idx = _.findIndex(arrtrx, item => item.T_sampleID === this.$store.state.sample.last_id)
         console.log(idx)
         var xcur_page = 1
         if(idx !== -1)
            xcur_page = this.$store.state.sample.current_page
         this.$store.dispatch("sample/search",{
            namelab:this.xnamelab,
               startdate:this.xdatestart,
               enddate: this.xdateend,
               current_page:xcur_page,
                stationid:this.xselectedstation.id,
               lastid:idx
         })
         this.$store.commit("sample/update_dialog_success",false)
      },
      closeDialogInfo(){
            this.$store.commit("sample/update_open_dialog_info",false)
            var trxs  = this.$store.state.sample.transactions
            var trx  = this.$store.state.sample.selected_transaction
            var idx = _.findIndex(trxs, item => item.T_OrderHeaderID === trx.T_OrderHeaderID &&  item.T_SampleTypeID === trx.T_SampleTypeID)
            var xcur_page = 1
            if(idx !== -1)
               xcur_page = this.$store.state.sample.current_page
            this.$store.dispatch("sample/search",{
               namelab:this.xnamelab,
                  startdate:this.xdatestart,
                  enddate: this.xdateend,
                   stationid:this.xselectedstation.id,
                  current_page:xcur_page,
                  lastid:idx
            })
          
      }
   },
   computed: {
      dialogsuccess: {
         get() {
            return this.$store.state.sample.dialog_success
         },
         set(val) {
            this.$store.commit("sample/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.sample.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.sample.alert_success
         },
         set(val) {
            this.$store.commit("sample/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.sample.search_status == 1
      },
      xdatestart : {
         get() {
            return this.$store.state.sample.start_date
         },
         set(val) {
            this.$store.commit("sample/update_start_date",val)
         }
      },
      xdateend : {
         get() {
            return this.$store.state.sample.end_date
         },
         set(val) {
            this.$store.commit("sample/update_end_date",val)
         }
      },
      xnamelab : {
         get() {
            return this.$store.state.sample.name_lab
         },
         set(val) {
            this.$store.commit("sample/update_name_lab",val)
         }
      },
      filterComputedDateFormattedStart () {
         return this.formatDate(this.xdatestart)
      },
      filterComputedDateFormattedEnd () {
         return this.formatDate(this.xdateend)
      },
      xstations() {
         return this.$store.state.sample.stations
      },
      xselectedstation: {
         get() {
            return this.$store.state.sample.selected_station
         },
         set(val) {
            this.$store.commit("sample/update_selected_station",val)
         }
      },
      xtransactions() {
         //console.log(this.$store.state.sample.transactions)
         return this.$store.state.sample.transactions
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.sample.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("sample/update_open_alert_confirmation",val)
         }
      },
      opendialoginfo: {
            get() {
                  return this.$store.state.sample.open_dialog_info
            },
            set(val) {
                  this.$store.commit("sample/update_open_dialog_info",false)
            }
      },
      msginfo(){
            return this.$store.state.sample.msg_info
      },
      curr_page: {
         get() {
            return this.$store.state.sample.current_page
         },
         set(val) { 
            this.$store.commit("sample/update_current_page",val)
            this.$store.dispatch("sample/search",{
               name:this.name,
               status: this.xselectedstatus.value,
               current_page:val,
               lastid:-1
            })
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.sample.total_page
         },
         set(val) {
            this.$store.commit("sample/update_total_page",val)
         }
      },
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         menufilterdatestart:false,
         menufilterdateend:false,
         date: new Date().toISOString().substr(0, 10),
         items: [],
         name: '',
         page:1,
         headers: [
            {
               text: "NO LAB",
               align: "left",
               sortable: false,
               value: "mr",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NO RM",
               align: "left",
               sortable: false,
               value: "lab",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "name",
               width: "30%",
               class: "pa-2 blue lighten-3 white--text"
            },

            {
               text: "SAMPLE",
               align: "center",
               sortable: false,
               value: "T_SampleTypeName",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         pagination:{
            descending: false,
            page: 1,
            rowsPerPage: 5,
            sortBy: 'T_sampleDate',
            totalItems: this.$store.state.sample.total_transactions
         }
      };
   }
}
</script>
