<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-autocomplete
               class="mt-1"
                  label="FILTER PERUSAHAAN"
                  v-model="filter_selected_company"
                  :items="xcompanies"
                  :search-input.sync="filter_search_company"
                  auto-select-first
                  no-filter
                  outline
                  hide-details
                  item-text="name"
                  placeholder="Cari..."
                  return-object
                  :loading="isLoading"
                  no-data-text="Tidak ada data perusahaan"
               >
                  <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-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>
            <span  @click="setNewTransaction"  class="icon-medium-fill-base xs1 white--text primary  ml-1 icon-add"></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="transactions"
                  :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.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}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_CompanyName}}</td>
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_MouName}}</td>
                  </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>
   </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("samplecall/getstationstatus",{
            filter_company:{id:0,name:'Semua Perusahaan'},
           // current_page:1,
            lastid: -1
      })
      this.$store.dispatch("samplecall/getagreementtype")

   },
   methods : {
      isSelected(p) {
         return p.trx_id == this.$store.state.samplecall.selected_transaction.trx_id
      },
      searchtransaction() {
         this.$store.dispatch("samplecall/search",{
            filter_company:this.filter_selected_company,
            //current_page:1,
            lastid: -1
         })
      },
      selectMe(trx) {
         if(this.$store.state.samplecall.no_save == 0 ){
            this.$store.commit("samplecall/update_selected_transaction",trx)
            this.$store.commit("samplecall/update_trx_id",trx.trx_id)
            this.$store.commit("samplecall/update_trx_numbering",trx.trx_numbering)
            this.$store.commit("samplecall/update_companies",[{id:trx.Mcu_AgreementCustomerM_CompanyID,name:trx.M_CompanyName}])
            this.$store.commit("samplecall/update_selected_company", {id:trx.Mcu_AgreementCustomerM_CompanyID,name:trx.M_CompanyName})
            this.$store.commit("samplecall/update_selected_agreement_type", {id:trx.Mcu_AgreementScopeWorkMcu_AgreementTypeID,name:trx.Mcu_AgreementTypeName})
            this.$store.commit("samplecall/update_mou", trx.mou)
            this.$store.commit("samplecall/update_selected_mou", {id:trx.Mcu_AgreementCustomerM_MouID,name:trx.M_MouName})
            this.$store.commit("samplecall/update_company_cpname", trx.Mcu_AgreementCustomerCpName)
            this.$store.commit("samplecall/update_company_cphp", trx.Mcu_AgreementCustomerCpHp)
            this.$store.commit("samplecall/update_participants", trx.Mcu_AgreementScopeWorkParticipants)
            this.$store.commit("samplecall/update_resulttype_report", trx.Mcu_AgreementScopeWorkResultTypeReport)
            this.$store.commit("samplecall/update_special_requirement", trx.Mcu_AgreementScopeWorkSpecialRequirements)
            this.$store.commit("samplecall/update_implementation_date", trx.Mcu_AgreementImplementationDate)
            this.$store.commit("samplecall/update_implementation_time", trx.Mcu_AgreementImplementationTime)
            this.$store.commit("samplecall/update_implementation_place", trx.Mcu_AgreementImplementationPlace)
            this.$store.commit("samplecall/update_setupinstrument_time", trx.Mcu_AgreementImplementationSetupInstumentTime)
            this.$store.commit("samplecall/update_normal_price", trx.Mcu_AgreementPriceNormalPrice)
            this.$store.commit("samplecall/update_agreement_price", trx.Mcu_AgreementPriceAgreementPrice)
            this.$store.commit("samplecall/update_discount_price", trx.Mcu_AgreementPriceDiscount)
            this.$store.commit("samplecall/update_duedate", trx.Mcu_AgreementPriceDueDate)
            this.$store.commit("samplecall/update_billing_system", trx.Mcu_AgreementPriceBillingSystem)
            this.$store.commit("samplecall/update_sendresult_date", trx.Mcu_AgreementSendResultDate)
            this.$store.commit("samplecall/update_sendresult_place", trx.Mcu_AgreementSendResultPlace)
            this.$store.commit("samplecall/update_sendresult_cpname", trx.Mcu_AgreementSendResultCPName)
            this.$store.commit("samplecall/update_sendresult_cphp", trx.Mcu_AgreementSendResultCPHP)
            this.$store.commit("samplecall/update_marketing_staff", trx.Mcu_AgreementMarketingName)
            this.$store.commit("samplecall/update_notes", trx.Mcu_AgrrementNotesValue)
            this.$store.commit("samplecall/update_foods", trx.Mcu_AgreementFacilityFood)
    
         }else{
            this.$store.commit("samplecall/update_open_alert_confirmation",true)
         }
            
      },
      closeAlertConfirmation(){
          this.$store.commit("samplecall/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("samplecall/update_no_save",0)
          this.$store.commit("samplecall/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("samplecall/update_alert_success",val)
      },
      setNewtransaction(){
         //this.$store.commit("samplecall/update_selected_transaction",{})
         
      },
      closeDialogSuccess(){
         this.$store.commit("samplecall/update_dialog_success",false)
         this.$store.dispatch("samplecall/search",{
            filter_company:this.filter_selected_company,
            //current_page:1,
            lastid: -1
         })
      },
      setNewTransaction(){
         this.$store.commit("samplecall/update_selected_transaction",{})
            this.$store.commit("samplecall/update_trx_id",'0')
            this.$store.commit("samplecall/update_trx_numbering",'No. TRANSAKSI')
            this.$store.commit("samplecall/update_companies",[])
            this.$store.commit("samplecall/update_selected_company", {})
            this.$store.commit("samplecall/update_selected_agreement_type", {})
            this.$store.commit("samplecall/update_mou", [])
            this.$store.commit("samplecall/update_selected_mou", {})
            this.$store.commit("samplecall/update_company_cpname", "")
            this.$store.commit("samplecall/update_company_cphp", "")
            this.$store.commit("samplecall/update_participants", "")
            this.$store.commit("samplecall/update_resulttype_report", "")
            this.$store.commit("samplecall/update_special_requirement", "")
            this.$store.commit("samplecall/update_implementation_date", "")
            this.$store.commit("samplecall/update_implementation_time", "")
            this.$store.commit("samplecall/update_implementation_place", "")
            this.$store.commit("samplecall/update_setupinstrument_time", "")
            this.$store.commit("samplecall/update_normal_price", "")
            this.$store.commit("samplecall/update_agreement_price","")
            this.$store.commit("samplecall/update_discount_price", "")
            this.$store.commit("samplecall/update_duedate", "")
            this.$store.commit("samplecall/update_billing_system", "")
            this.$store.commit("samplecall/update_sendresult_date", "")
            this.$store.commit("samplecall/update_sendresult_place", "")
            this.$store.commit("samplecall/update_sendresult_cpname", "")
            this.$store.commit("samplecall/update_sendresult_cphp", "")
            this.$store.commit("samplecall/update_marketing_staff", "")
            this.$store.commit("samplecall/update_notes", "")
            this.$store.commit("samplecall/update_foods", "")
      },
      thr_filter_search_company: _.debounce(function() {
         this.$store.dispatch("samplecall/filtersearchcompany", this.filter_search_company);
      }, 2000),
   },
   computed: {
      xcompanies() {
         return this.$store.state.samplecall.filter_companies;
      },
      filter_selected_company: {
         get() {
         return this.$store.state.samplecall.filter_selected_company;
         },
         set(val) {
         this.$store.commit("samplecall/update_filter_selected_company", val);
         }
      },
      dialogsuccess: {
         get() {
            return this.$store.state.samplecall.dialog_success
         },
         set(val) {
            this.$store.commit("samplecall/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.samplecall.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.samplecall.alert_success
         },
         set(val) {
            this.$store.commit("samplecall/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.samplecall.search_status == 1
      },
      xstations() {
         return this.$store.state.samplecall.stations
      },
      xselectedstation: {
         get() {
            return this.$store.state.samplecall.selected_station
         },
         set(val) {
            this.$store.commit("samplecall/update_selected_station",val)
         }
      },
       xstatuses() {
         return this.$store.state.samplecall.statuses
      },
      xselectedstatus: {
         get() {
            return this.$store.state.samplecall.selected_status
         },
         set(val) {
            this.$store.commit("samplecall/update_selected_status",val)
         }
      },
      transactions() {
         return this.$store.state.samplecall.transactions
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.samplecall.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("samplecall/update_open_alert_confirmation",val)
         }
      },
      curr_page: {
         get() {
            return this.$store.state.samplecall.current_page
         },
         set(val) { 
            this.$store.commit("samplecall/update_current_page",val)
            this.$store.dispatch("samplecall/search",{
               name:this.name,
               nolab:this.nolab,
               stationid: this.xselectedstation.id,
               statusid: this.xselectedstatus.id,
               current_page:val,
               lastid: idx
            })
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.samplecall.total_page
         },
         set(val) {
            this.$store.commit("samplecall/update_total_page",val)
         }
      },
   },
   watch: {
    filter_search_company(val, old) {
      if (val == old) return;
      if (!val) return;
      if (val.length < 1) return;
      if (this.$store.state.samplecall.update_autocomplete_status == 1) return;
      this.thr_filter_search_company();
    }
  },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         items: [],
         name: '',
         nolab:'',
         page:1,
         filter_search_company: "",
         headers: [
            {
               text: "NO TRANSAKSI",
               align: "left",
               sortable: false,
               value: "mr",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "TANGGAL TRANSAKSI",
               align: "left",
               sortable: false,
               value: "lab",
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PERUSAHAAN",
               align: "left",
               sortable: false,
               value: "name",
               width: "35%",
               class: "pa-2 blue lighten-3 white--text"
            },

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