<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
                          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="Filter Tanggal Awal"
                              outline
                              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
                          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="Filter Tanggal Akhir"
                              outline
                              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="Nomor Transaksi"
                             placeholder="Cari ..."
                             single-line
                             outline
                             v-model="xnamelab"
                             hide-details
                             ></v-text-field>
               <v-select class="xs3 ma-1" :items="xstations" 
                  item-text="name"
                  return-object
                  v-model="xselectedstation"
                  label="Status" outline   hide-details></v-select>
            <v-btn class="xs3 ma-1" color="success"  @click="searchTransaction" >Cari</v-btn>
            <v-btn class="xs3 ma-1" color="info"  @click="setNewTransaction" >Baru</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="xtransactions"
                  :loading="isLoading"
                  :pagination.sync="pagination"
                   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.trx_numbering }}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.trx_date_ina}}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.courier_name}}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.status_name}}</td>
                  </template>
                  <template>
                     <div class="text-xs-center">
                        <v-pagination
                           v-model="page"
                           :length="15"
                           :total-visible="7"
                        ></v-pagination>
                     </div>
                  </template>
               </v-data-table>
            </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("receivereferencedistibutioncourier/getcompanycouriers",{
         startdate:this.xdatestart,
         enddate: this.xdateend,
         search:this.xnamelab,
         stationid:this.xselectedstation.id,
         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.receivereferencedistibutioncourier.selected_transaction.trx_id
      },
      searchTransaction() {
         this.$store.dispatch("receivereferencedistibutioncourier/search",{
            startdate:this.xdatestart,
            enddate: this.xdateend,
            search:this.xnamelab,
            stationid:this.xselectedstation.id,
            lastid:-1
         })
      },
      selectMe(trx) {
         if(this.$store.state.receivereferencedistibutioncourier.no_save == 0 ){
            //console.log(trx)
            this.$store.commit("receivereferencedistibutioncourier/update_selected_transaction",trx)
            this.$store.commit("receivereferencedistibutioncourier/update_act",'edit')
            this.$store.commit("receivereferencedistibutioncourier/update_trx_note",trx.trx_note)
            this.$store.commit("receivereferencedistibutioncourier/update_trx_date",trx.trx_date)
            if(trx.company_fulname !== null){
               this.$store.commit("receivereferencedistibutioncourier/update_selected_company",{id:trx.T_ReceiverefDistributCourierM_DoctorID,name:trx.company_fullname})
               this.$store.commit("receivereferencedistibutioncourier/update_selected_courier",{id:trx.T_ReceiverefDistributCourierM_StaffID,name:trx.courier_name})
            }
            else{
               this.$store.commit("receivereferencedistibutioncourier/update_selected_company",{})
            }

             
            if(trx.details.length > 0 ){
                  this.$store.commit("receivereferencedistibutioncourier/update_details", trx.details)
            }
            else{
               this.$store.commit("receivereferencedistibutioncourier/update_details", [])
            }
           
         }else{
            this.$store.commit("receivereferencedistibutioncourier/update_open_alert_confirmation",true)
         }
      },
      closeAlertConfirmation(){
          this.$store.commit("receivereferencedistibutioncourier/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("receivereferencedistibutioncourier/update_no_save",0)
          this.$store.commit("receivereferencedistibutioncourier/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("receivereferencedistibutioncourier/update_alert_success",val)
      },
      setNewTransaction(){
         this.$store.commit("receivereferencedistibutioncourier/update_selected_transaction",{})
         this.$store.commit("receivereferencedistibutioncourier/update_details",[])
         this.$store.commit("receivereferencedistibutioncourier/update_selected_detail",{})
         this.$store.commit("receivereferencedistibutioncourier/update_selected_company",{})
         this.$store.commit("receivereferencedistibutioncourier/update_selected_courier",{})
         this.$store.commit("receivereferencedistibutioncourier/update_trx_date",moment(new Date()).format('YYYY-MM-DD'))
         this.$store.commit("receivereferencedistibutioncourier/update_trx_note",'')
         this.$store.commit("receivereferencedistibutioncourier/update_act",'new')
      },
      closeDialogSuccess(){
         let arrtrx = this.$store.state.receivereferencedistibutioncourier.transactions
         var idx = _.findIndex(arrtrx, item => item.T_ReceiverefDistributCourierID === this.$store.state.receivereferencedistibutioncourier.last_id)
         //console.log(idx)
         this.$store.dispatch("receivereferencedistibutioncourier/search",{
            startdate:this.xdatestart,
            enddate: this.xdateend,
            search:this.xnamelab,
            stationid:this.xselectedstation.id,
            lastid:idx
         })
         this.$store.commit("receivereferencedistibutioncourier/update_dialog_success",false)
      },
      closeDialogInfo(){
            this.$store.commit("receivereferencedistibutioncourier/update_open_dialog_info",false)
            var trxs  = this.$store.state.receivereferencedistibutioncourier.transactions
            var trx  = this.$store.state.receivereferencedistibutioncourier.selected_transaction
            var idx = _.findIndex(trxs, item => item.T_OrderHeaderID === trx.T_OrderHeaderID &&  item.T_SampleTypeID === trx.T_SampleTypeID)
            this.$store.dispatch("receivereferencedistibutioncourier/search",{
                startdate:this.xdatestart,
                enddate: this.xdateend,
                stationid:this.xselectedstation.id,
                lastid:idx
            })
          
      }
   },
   computed: {
      dialogsuccess: {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.dialog_success
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.receivereferencedistibutioncourier.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.alert_success
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.receivereferencedistibutioncourier.search_status == 1
      },
      xdatestart : {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.start_date
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_start_date",val)
         }
      },
      xdateend : {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.end_date
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_end_date",val)
         }
      },
      xnamelab : {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.name_lab
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_name_lab",val)
         }
      },
      filterComputedDateFormattedStart () {
         return this.formatDate(this.xdatestart)
      },
      filterComputedDateFormattedEnd () {
         return this.formatDate(this.xdateend)
      },
      xstations() {
         return this.$store.state.receivereferencedistibutioncourier.stations
      },
      xselectedstation: {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.selected_station
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_selected_station",val)
         }
      },
      xtransactions() {
         //console.log(this.$store.state.receivereferencedistibutioncourier.transactions)
         return this.$store.state.receivereferencedistibutioncourier.transactions
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.receivereferencedistibutioncourier.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("receivereferencedistibutioncourier/update_open_alert_confirmation",val)
         }
      },
      opendialoginfo: {
            get() {
                  return this.$store.state.receivereferencedistibutioncourier.open_dialog_info
            },
            set(val) {
                  this.$store.commit("receivereferencedistibutioncourier/update_open_dialog_info",false)
            }
      },
      msginfo(){
            return this.$store.state.receivereferencedistibutioncourier.msg_info
      },
   },
   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: "NOMOR",
               align: "left",
               sortable: false,
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "TANGGAL",
               align: "left",
               sortable: false,
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "KURIR",
               align: "center",
               sortable: false,
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "STATUS",
               align: "center",
               sortable: false,
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         pagination:{
            descending: false,
            page: 1,
            rowsPerPage: 5,
            sortBy: 'T_ReceiverefDistributCourierNumber DESC',
            totalItems: this.$store.state.receivereferencedistibutioncourier.total_transactions
         }
      };
   }
}
</script>
