<template>
   <v-layout class="fill-height" column>
      <v-dialog v-model="dialogconfirmationdelete" persistent max-width="290">
         <v-card>
         <v-card-title dark class="headline error pt-2 pb-2" primary-title style="color: white">
            <h4 dark>Konfirmasi</h4>
         </v-card-title>
         <v-card-text>
            {{ msgconfirmationdelete }}
         </v-card-text>
         <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn small color="error darken-1 text-sm-left" flat @click="doDeleteData()">Hapus</v-btn>
            <v-btn small color="primary darken-1 text-sm-right" flat
               @click="dialogconfirmationdelete = false">Batal</v-btn>
         </v-card-actions>
         </v-card>
      </v-dialog>

      <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 align-center row>
            <v-flex class="pa-1" xs4>
               <v-text-field                        
                  v-model="spk_search"
                  label="Cari"
                  outline
                  @keyup.enter="searchTransaction"
                  placeholder="Ketikkan nomor spk"
                  hide-details
               ></v-text-field>
            </v-flex>
            <v-flex class="pt-1 text-xs-right pr-0">
               <span title="buat spk baru" @click="setNewTransaction" class="pt-1 icon-medium-fill-base white--text primary mt-1 ml-1 icon-add"></span>
            </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-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}">{{ props.item.SpkHomeServiceDate }}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}">{{ props.item.SpkHomeServiceNumber }}</td>
                     <td class="text-xs-center pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}">
                        <v-chip dark v-if="props.item.SpkHomeServiceStatus === 'N'" color="orange">BARU</v-chip>
                        <v-chip dark v-if="props.item.SpkHomeServiceStatus === 'R'" color="teal">RELEASE</v-chip>
                     </td>
                     <td class="text-xs-center pa-1" v-bind:class="{'amber lighten-4':isSelected(props.item)}">
                        <v-btn title="edit/lihat data" style="min-width:20px; margin:0" @click="editRowOrder(props.item)" small color="info"><v-icon small>edit</v-icon></v-btn>
                        <span v-if="props.item.SpkHomeServiceStatus == 'N'">
                           <v-btn title="hapus" class="mr-1" style="min-width:20px; margin:0" @click="openDeleteAll(props.item)" small color="error"><v-icon small>delete</v-icon></v-btn>
                        </span>
                     </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:40px;
}
.searchbox .v-btn {
   min-height:40px;
}
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("service/search",{
         search: this.spk_search,
         current_page: 1,
         lastid: -1
      })
   },
   methods : {
      isSelected(p) {
         return p.SpkHomeServiceID == this.$store.state.service.selected_transaction.SpkHomeServiceID
      },
      searchTransaction() {
         this.$store.dispatch("service/search",{
            search: this.spk_search,
            current_page: 1,
            lastid: -1
         })
      },
      openDeleteAll(value){
         this.$store.commit("service/update_selected_transaction", value)
         let msg = "Yakin, akan menghapus data spk nomor " + this.$store.state.service.selected_transaction.SpkHomeServiceNumber + " ?"
         this.$store.commit("service/update_msg_confirmation_delete", msg)
         this.$store.commit("service/update_dialog_confirmation_delete", true)
      },
      doDeleteData() {
         var prm = {}
         prm.spkHomeServiceId = this.$store.state.service.selected_transaction.SpkHomeServiceID
         prm.number = this.$store.state.service.selected_transaction.SpkHomeServiceNumber
         console.log(prm)
         this.$store.dispatch("form/deleteOrder",prm)
      },
      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')}`
      },
      setNewTransaction(){
         this.$store.commit("service/update_act",'new')
         this.$store.commit("form/update_trx_id", -1)
         this.$store.commit("service/update_selected_transaction", {})
         this.$store.commit("form/update_liststaff", [])
         this.$store.commit("form/update_selected_orders", [])
         this.$store.commit("form/update_transaction_date", moment(new Date()).format('YYYY-MM-DD'))
         this.$store.commit("form/update_save_hide", true);
         this.$store.dispatch("form/getorder", {current_page: 1})
         this.$store.dispatch("form/getstaff",{id:''})
      },
      editRowOrder(item) {
         // console.log('edit data', item)
         this.$store.commit("service/update_act", 'edit')
         this.$store.commit("service/update_no_save", 0)
         this.$store.commit("form/update_trx_id", item.SpkHomeServiceID)
         this.$store.commit("form/update_transaction_date", this.formatDate(item.SpkHomeServiceDate))
         this.$store.commit("service/update_selected_transaction", item)
         this.$store.commit("form/update_save_hide", true);
         this.$store.dispatch("form/getorderbyid",{spkHomeServiceId: item.SpkHomeServiceID, current_page: 1})
         this.$store.dispatch("form/getstaff", {spkHomeServiceId: item.SpkHomeServiceID })
         this.$store.dispatch("form/getstaffbyid",{spkHomeServiceId: item.SpkHomeServiceID })
      },
      closeAlertConfirmation(){
          this.$store.commit("service/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("service/update_no_save",0)
          this.$store.commit("service/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("service/update_alert_success",val)
      },
      closeDialogSuccess(){
         let arrtrx = this.$store.state.service.transactions
         var idx = _.findIndex(arrtrx, item => item.SpkHomeServiceID === this.$store.state.service.last_id)
         console.log(idx)
         var xcur_page = 1
         if(idx !== -1)
            xcur_page = this.$store.state.service.current_page
            this.$store.dispatch("service/search",{
               search: this.spk_search,
               current_page: this.curr_page,
               lastid: idx
            })
         this.$store.commit("service/update_dialog_success",false)
         this.$store.commit("form/update_save_hide", false);
      },
      closeDialogInfo(){
            this.$store.commit("service/update_open_dialog_info",false)
      }
   },
   computed: {
      opendialoginfo: {
         get() {
            return this.$store.state.service.open_dialog_info
         },
         set(val) {
            this.$store.commit("service/update_open_dialog_info", false)
         }
      },
      msginfo(){
            return this.$store.state.service.msg_info
      },
      xtransactions() {
         return this.$store.state.service.transactions
      },
      curr_page: {
         get() {
            return this.$store.state.service.current_page
         },
         set(val) { 
            this.$store.commit("service/update_current_page",val)
            this.$store.dispatch("service/search",{
               search: '',
               current_page: val,
               lastid:-1
            })
         }
      },
      xtotal_page: {
         get() {
            return this.$store.state.service.total_page
         },
         set(val) {
            this.$store.commit("service/update_total_page",val)
         }
      },
      spk_search : {
         get() {
            return this.$store.state.service.spk_search
         },
         set(val) {
            this.$store.commit("service/update_spk_search",val)
            // this.searchTransaction()
         }
      },
      isLoading() {
         return this.$store.state.service.search_transaction == 1
      },
      dialogsuccess: {
         get() {
            return this.$store.state.service.dialog_success
         },
         set(val) {
            this.$store.commit("service/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.service.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.service.alert_success
         },
         set(val) {
            this.$store.commit("service/update_alert_success",val)
         }
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.service.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("service/update_open_alert_confirmation",val)
         }
      },
      dialogconfirmationdelete: {
         get() {
               return this.$store.state.service.dialog_confirmation_delete
         },
         set(val) {
               this.$store.commit("service/update_dialog_confirmation_delete", val)
         }
      },
      msgconfirmationdelete() {
         return this.$store.state.service.msg_confirmation_delete
      }
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         date: new Date().toISOString().substr(0, 10),
         name: '',
         page:1,
         headers: [
            {
               text: "TANGGAL",
               align: "center",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "NOMOR SPK",
               align: "center",
               sortable: false,
               width: "40%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "STATUS",
               align: "center",
               sortable: false,
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "AKSI",
               align: "center",
               sortable: false,
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ]
      };
   }
}
</script>
