<template>
   <v-layout class="fill-height" column>
      <v-dialog v-model="dialogsuccess" persistent  max-width="350">
        <v-card>
            <v-card-title color="success"  class="headline">Berhasil !</v-card-title>
            <v-card-text v-html="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 row >
            <v-flex class="xs3 mt-1 pr-2">
               <v-select class="mini-select" :items="typeresults" 
                  item-text="name"
                  style="font-size:12px"
                  return-object
                  v-model="typeresult"
                  label="Tipe hasil" outline   hide-details></v-select>
            </v-flex>
            <v-flex class="xs2">
               <v-menu 
                          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="Tanggal Tindakan"
                              style="font-size:12px"
                              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-flex>
            <v-flex class="xs5 pr-2">
               <v-layout align-center row>
                  <v-flex class="text-xs-center" xs3>
                     <span  title="tidak termasuk" v-if="switch_exclude" @click="switch_exclude = !switch_exclude"  class="icon-medium-fill-base-small xs1 white--text red"><v-icon dark>assignment_late</v-icon></span>
                     <span  title="termasuk" v-if="!switch_exclude" @click="switch_exclude = !switch_exclude"  class="icon-medium-fill-base-small xs1 white--text blue"><v-icon dark>assignment_turned_in</v-icon></span>
                  </v-flex>
                  <v-flex xs9>
                     <v-autocomplete
                        label="Kel. Pelanggan"
                        v-model="selected_company"
                        class="mt-1"
                        :items="xcompanies"
                        :search-input.sync="search_company"
                        auto-select-first
                        hide-details
                        style="font-size:14px"
                        outline
                        no-filter
                        item-text="name"
                        return-object
                        :loading="isLoading"
                        no-data-text="Semua Kel. Pelanggan"
                     >
                        <template slot="item" slot-scope="{ item }">
                        <v-list-tile-content>
                           <v-list-tile-title v-text="item.name"></v-list-tile-title>
                        </v-list-tile-content>
                        </template>
                     </v-autocomplete>
                  </v-flex>
               </v-layout>
               </v-flex>
               <v-flex class="xs3 pr-2 mt-1">
                  <v-text-field 
                        label="Cari..."
                        placeholder="Nama / No Reg"
                        style="font-size:12px"
                        outline
                        v-model="xnamelab"
                        v-on:keyup.enter="searchTransaction"
                        hide-details
                  ></v-text-field>
               </v-flex>
         </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="{'purple lighten-3':props.item.status_name === 'PRINTED','amber':props.item.iscito === 'Y','orange lighten-3':props.item.status_name === 'VERIFIKASI','success lighten-3':props.item.status_name === 'VALIDASI'}" @click="selectMe(props.item)">
                        <p class="mb-0 font-weight-black">{{ props.item.ordernumber }}</p>
                        <p class="mb-0 font-weight-bold mono" style="color:#852222">{{ props.item.ordernumber_ext }}</p>
                     </td>
                     <td class="text-xs-left pa-2" v-bind:class="{'purple lighten-3':props.item.status_name === 'PRINTED','amber':props.item.iscito === 'Y','orange lighten-3':props.item.status_name === 'VERIFIKASI','success lighten-3':props.item.status_name === 'VALIDASI'}" @click="selectMe(props.item)">{{ props.item.orderdate}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'purple lighten-3':props.item.status_name === 'PRINTED','amber':props.item.iscito === 'Y','orange lighten-3':props.item.status_name === 'VERIFIKASI','success lighten-3':props.item.status_name === 'VALIDASI'}" @click="selectMe(props.item)">{{ props.item.patient_fullname}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'purple lighten-3':props.item.status_name === 'PRINTED','amber':props.item.iscito === 'Y','orange lighten-3':props.item.status_name === 'VERIFIKASI','success lighten-3':props.item.status_name === 'VALIDASI'}" @click="selectMe(props.item)">
                        {{ props.item.company_name}}
                       
                     </td>
                     <td class="text-xs-left pa-2" v-bind:class="{'purple lighten-3':props.item.status_name === 'PRINTED','amber':props.item.iscito === 'Y','orange lighten-3':props.item.status_name === 'VERIFIKASI','success lighten-3':props.item.status_name === 'VALIDASI'}" @click="selectMe(props.item)">
                        <v-icon v-if="isSelected(props.item) && props.item.status_name == 'BARU'" dark color="blue lighten-3">pan_tool</v-icon>
                        <v-icon v-if="isSelected(props.item) && props.item.status_name !== 'BARU'" dark color="white">pan_tool</v-icon>
                     </td>
                  </template>
               </v-data-table>
               <v-divider></v-divider>
               <v-pagination
                  style="margin-top:10px;margin-bottom:5px"
                  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')
   },
   async mounted() {
      var prm = {
         startdate:this.xdatestart,
         enddate: this.xdateend,
         search:this.xnamelab,
         stationid:this.xselectedstation.id,
         companyid:this.selected_company.id,
         switch_exclude:this.switch_exclude,
         groupid:this.$store.state.sample.select_item_group.id,
         subgroupid:this.$store.state.sample.select_item_subgroup.id,
         current_page:1,
         typeresult:this.$store.state.sample.typeresult.id,
         lastid:-1
      }
      
      await this.$store.dispatch("sample/search",prm)
      if (this.xtransactions.length > 0 ) {
            let trx = this.xtransactions[0]
            await this.$store.dispatch("email/get",trx)
      }
   },
   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
      },
      async searchTransaction() {
         this.$store.commit("sample/update_last_id",-1)
         this.$store.commit("sample/update_current_page",1)
         await this.$store.dispatch("sample/search",{
            startdate:this.xdatestart,
            enddate: this.xdateend,
            search:this.xnamelab,
            stationid:this.xselectedstation.id,
            companyid:this.selected_company.id,
            switch_exclude:this.switch_exclude,
            groupid:this.$store.state.sample.select_item_group.id,
            subgroupid:this.$store.state.sample.select_item_subgroup.id,
            typeresult:this.$store.state.sample.typeresult.id,
            current_page:1,
            lastid:-1
         })
         if (this.xtransactions.length > 0 ) {
            let trx = this.xtransactions[0]
            await this.$store.dispatch("email/get",trx)
         }
      },
      async selectMe(trx) {
         if(this.$store.state.sample.no_save == 0 ){
            this.$store.commit("sample/update_last_id",trx.trx_id)
            this.$store.commit("sample/update_selected_transaction",trx)
            this.$store.commit("sample/update_act",'edit')
            await this.$store.dispatch("email/get",trx)
            if(trx.details.length > 0 ){
                  this.$store.commit("sample/update_details", trx.details)
            }
            else{
               this.$store.commit("sample/update_details", [])
            }
           
         }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_details",[])
         this.$store.commit("sample/update_selected_detail",{})
         this.$store.commit("sample/update_selected_doctor",{})
         this.$store.commit("sample/update_selected_doctor_address",{})
         this.$store.commit("sample/update_selected_courier",{})
         this.$store.commit("sample/update_trx_date",moment(new Date()).format('YYYY-MM-DD'))
         this.$store.commit("sample/update_trx_note",'')
         this.$store.commit("sample/update_act",'new')
      },
      closeDialogSuccess(){
         this.$store.dispatch("sample/search",{
            startdate:this.xdatestart,
            enddate: this.xdateend,
            search:this.xnamelab,
            stationid:this.xselectedstation.id,
            companyid:this.selected_company.id,
            switch_exclude:this.switch_exclude,
            groupid:this.$store.state.sample.select_item_group.id,
            subgroupid:this.$store.state.sample.select_item_subgroup.id,
            typeresult:this.$store.state.sample.typeresult.id,
            current_page:this.$store.state.sample.current_page
         })
         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)
            this.$store.dispatch("sample/search",{
                startdate:this.xdatestart,
               enddate: this.xdateend,
               search:this.xnamelab,
               stationid:this.xselectedstation.id,
               groupid:this.$store.state.sample.select_item_group.id,
               subgroupid:this.$store.state.sample.select_item_subgroup.id,
                lastid:idx
            })*/
          
      },
      selectgroup(value){
         this.selected_itemgroup = value
      },
      selectsubgroup(value){
         this.selected_itemsubgroup = value
      },
      thr_search_company: _.debounce(function() {
         this.$store.dispatch("sample/searchcompany", this.search_company)
      }, 2000),
   },
   computed: {
      switch_exclude: {
         get() {
            return this.$store.state.sample.switch_exclude
         },
         set(val) {
            this.$store.commit("sample/update_switch_exclude",val)
            this.searchTransaction()
         }
      },
      typeresult: {
         get() {
            return this.$store.state.sample.typeresult
         },
         set(val) {
            this.$store.commit("sample/update_typeresult",val)
            this.searchTransaction()
         }
      },
      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
      },
      itemgroups() {
         return this.$store.state.sample.item_groups
      },
      selected_itemgroupname() {
         return this.$store.state.sample.selected_itemgroupname
      },
      selected_itemgroup: {
         get() {
            //console.log(this.$store.state.sample.select_item_group)
            return this.$store.state.sample.select_item_group
         },
         set(val) {
            this.$store.commit("sample/update_select_item_group",val)
            this.$store.commit("sample/update_selected_itemgroupname",val.fulltitle)
            this.$store.commit("sample/update_item_subgroups",val.childrens)
            this.$store.commit("sample/update_select_item_subgroup",val.childrens[0])
            this.$store.commit("sample/update_selected_itemsubgroupname",val.childrens[0].fulltitle)
         }
      },
      itemsubgroups() {
         return this.$store.state.sample.item_subgroups
      },
      selected_itemsubgroupname() {
         return this.$store.state.sample.selected_itemsubgroupname
      },
      selected_itemsubgroup: {
         get() {
            return this.$store.state.sample.select_item_subgroup
         },
         set(val) {
            this.$store.commit("sample/update_select_item_subgroup",val)
             this.$store.commit("sample/update_selected_itemsubgroupname",val.fulltitle)
         }
      },
      xdatestart : {
         get() {
            return this.$store.state.sample.start_date
         },
         set(val) {
            this.$store.commit("sample/update_start_date",val)
            this.searchTransaction()
         }
      },
      xdateend : {
         get() {
            return this.$store.state.sample.end_date
         },
         set(val) {
            this.$store.commit("sample/update_end_date",val)
            this.searchTransaction()
         }
      },
      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.commit("sample/update_last_id",-1)
            this.$store.dispatch("sample/search",{
               startdate:this.xdatestart,
               enddate: this.xdateend,
               search:this.xnamelab,
               stationid:this.xselectedstation.id,
               companyid:this.selected_company.id,
               switch_exclude:this.switch_exclude,
               groupid:this.$store.state.sample.select_item_group.id,
               subgroupid:this.$store.state.sample.select_item_subgroup.id,
               typeresult:this.$store.state.sample.typeresult.id,
               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)
         }
      },
      xcompanies() {
         return this.$store.state.sample.companies
      },
      selected_company: {
         get() {
            return this.$store.state.sample.selected_company
         },
         set(val) {
            this.$store.commit("sample/update_selected_company", val)
            this.searchTransaction()
         }
      },
   },
   watch: {
      search_company(val, old) {
         if (val == old) return;
         if (!val) return;
         if (val.length < 1) return;
         if (this.$store.state.sample.update_autocomplete_status == 1) return;
         this.thr_search_company();
      }
   },
   data() {
      return {
         typeresults:[{id:'Rontgen',name:'Radiodiagnostik'},{id:'USG',name:'Ultrasonografi'},{id:'BMD',name:'BMD'},{id:'MRI',name:'MRI'}],
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         menufilterdatestart:false,
         menufilterdateend:false,
         search_company:'',
         date: new Date().toISOString().substr(0, 10),
         items: [],
         name: '',
         page:1,
         headers: [
            {
               text: "NO REG",
               align: "left",
               sortable: false,
               width: "8%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "TANGGAL TINDAKAN",
               align: "left",
               sortable: false,
               width: "12%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PASIEN",
               align: "left",
               sortable: false,
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "KEL. PASIEN",
               align: "left",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "",
               align: "left",
               sortable: false,
               width: "5%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         pagination:{
            descending: false,
            page: 1,
            rowsPerPage: 5,
            sortBy: 'trx_id ASC',
            totalItems: this.$store.state.sample.total_transactions
         }
      };
   }
}
</script>
