<template>
      <v-layout class="fill-height" column>
            <v-card class="mb-2 pa-2 searchbox">
                  <v-layout>
                        <v-menu class="xs3 pr-2" v-model="menustartdate" :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="startDateFormatted" label="Tgl. Awal" outline readonly v-on="on" @blur="date = deFormatedDate(startDateFormatted)"></v-text-field>
                    </template>
                    <v-date-picker v-model="init_sdate" no-title @input="menustartdate = false"></v-date-picker>
                </v-menu>
                <v-menu class="xs3 pl-2" v-model="menuenddate" :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="endDateFormatted" label="Tgl. Akhir" outline readonly v-on="on" @blur="date = deFormatedDate(endDateFormatted)"></v-text-field>
                    </template>
                    <v-date-picker v-model="init_edate" no-title @input="menuenddate = false"></v-date-picker>
                </v-menu>
                        <v-text-field class="xs3 ma-1" label="Supplier" placeholder="Cari..." outline v-model="searchsupplier" hide-details></v-text-field>

                        <v-text-field class="xs3 ma-1" label="No. Bayar / No. Tagihan" placeholder="Cari..." outline v-model="searchnamelab" hide-details></v-text-field>

                        <v-select class="xs3 mini-select ma-1" :items="statuses" item-text="name" return-object v-model="status" label="Status" outline
                              hide-details></v-select>
                        <span @click="searchPatient" class="icon-medium-fill-base xs1 white--text success iconsearch-search"></span>
                       <!-- <v-btn @click="changeBtnFlagCourier(xisauto)" v-if="xisauto === 'N'" small color="error">
                              Manual</v-btn>
                        <v-btn @click="changeBtnFlagCourier(xisauto)" v-if="xisauto === 'Y'" small color="success">
                              Otomatis</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="bills" :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.tanggalbayar}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.SupplierInvoiceNumber}}
                                          <p><v-btn v-if="props.item.SupplierInvoiceIsLunas === 'Y'" small color="success">Lunas</v-btn></p></td>
                                          <td class="pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.SupplierPaymentNumber}}
                                          <p style="color:#800000" class="mb-0 font-weight-bold caption">{{props.item.SupplierPaymentCashierNumber}}</p></td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.SupplierName}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ convertMoney(props.item.totalbill)}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ convertMoney(props.item.paid)}}</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="openalertnopay" :msg="msgalertnopay" @forget-dialog-alert="forgetAlertNoPay()" @close-dialog-alert="closeAlertNoPay()"></one-dialog-alert>
      </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;
      }

      .v-messages {
            min-height: 0px !important;
      }
</style>

<script>
      module.exports = {
            components: {
                  'one-dialog-info': httpVueLoader('../../common/oneDialogInfo.vue'),
                  'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
            },
            mounted() {

                  this.$store.dispatch("bill/search", {
                        supplier: this.searchsupplier,
                        search: this.searchnamelab,
                        status: this.status.value,
                        current_page: 1,
                        startdate: this.init_sdate,
                        enddate: this.init_edate,
                        lastidx: 0
                  })

            },
            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.SupplierInvoiceID == this.$store.state.bill.selected_bill.SupplierInvoiceID
                  },
                  convertMoney(money) {
                        return one_money(money)
                  },
                  searchPatient() {
                        this.$store.dispatch("bill/search", {
                              supplier: this.searchsupplier,
                              search: this.searchnamelab,
                              status: this.status.value,
                              current_page: 1,
                              startdate: this.init_sdate,
                              enddate: this.init_edate,
                              lastidx: 0
                        })
                  },
                  selectMe(pat) {
                        this.$store.commit("paymentmanual/update_paymenttype", '')
                        if (this.xtotal_page > 0) {
                              // alert(pat.tanggalbayar)
                              this.$store.commit("paymentmanual/update_xbayar", pat.totalbill)
                              this.$store.commit("paymentmanual/update_xtanggalbayar", pat.SupplierInvoiceDraftPaymentDate)
                              let totpay = this.$store.state.paymentnew.total_payment
                              if (totpay === 0) {
                                    let xobj = this.$store.state.paymentnew.types
                                    console.log(xobj)
                                    xobj.forEach(function (obj) {
                                          obj.chex = false
                                          obj.leftvalue = 0
                                          obj.rightvalue = 0
                                          obj.selected_card = {
                                                id: 0,
                                                name: ''
                                          }
                                          obj.selected_edc = {
                                                id: 0,
                                                name: ''
                                          }
                                    })
                                    this.$store.commit("paymentnew/update_types", {
                                          records: xobj,
                                          total: xobj.length
                                    })
                                    this.$store.commit("bill/update_selected_bill", pat)
                                    this.$store.commit("paymentnew/update_total_payment", 0)
                                    this.$store.commit("paymentnew/update_notes", pat.notes)
                                    this.$store.commit("paymentmanual/update_notes", pat.notes)
                                    if (pat.flaglunas === 'N') {
                                          this.$store.commit("paymentmanual/update_tagihans", pat.tagihans)
                                    } else {
                                          this.$store.commit("paymentmanual/update_tagihans", '')
                                    }

                              } else {
                                    this.$store.commit("bill/update_open_alert_no_pay", true)
                              }

                  
            var arr = this.$store.state.paymentmanual.tagihans
            var value = true
            this.$store.commit("paymentmanual/update_indeterminatex", false)
            this.$store.commit("paymentmanual/update_bar_chx_all", value)
                

                arr.forEach((el) => {
                    el.chex = value
                    el.tagihan_bayar = value == true ? el.tagihan_tagihan : 0
                })
                
                var selected = _.filter(arr, function (o) {
                    return o.chex;
                })
                this.$store.commit("paymentmanual/update_selected_tagihan", selected)

                var xval = this.$store.state.paymentmanual.selected_tagihan
                var valx = 0
                xval.forEach(function (obj) {
                    valx += parseInt(obj.tagihan_bayar)
                })
                let totpaid = valx
                this.$store.commit("paymentmanual/update_total_payment", totpaid)

                
                        } else {
                              // alert(pat.tanggalbayar)
                              this.$store.commit("paymentmanual/update_xbayar", pat.totalbill)
                              this.$store.commit("paymentmanual/update_xtanggalbayar", pat.tanggalbayar)
                              let totpay = 0
                              if (totpay === 0) {
                                    let xobj = this.$store.state.paymentnew.types
                                    console.log(xobj)
                                    xobj.forEach(function (obj) {
                                          obj.chex = false
                                          obj.leftvalue = 0
                                          obj.rightvalue = 0
                                          obj.selected_card = {
                                                id: 0,
                                                name: ''
                                          }
                                          obj.selected_edc = {
                                                id: 0,
                                                name: ''
                                          }
                                    })
                                    this.$store.commit("paymentnew/update_types", {
                                          records: '',
                                          total: 0
                                    })
                                    this.$store.commit("bill/update_selected_bill", '')
                                    this.$store.commit("paymentnew/update_total_payment", 0)
                                    this.$store.commit("paymentmanual/update_notes", '')
                                    this.$store.commit("paymentnew/update_notes", '')
                                    if (pat.flaglunas === 'N') {
                                          this.$store.commit("paymentmanual/update_tagihans", pat.tagihans)
                                    } else {
                                          this.$store.commit("paymentmanual/update_tagihans", '')
                                    }
                              } else {
                                    this.$store.commit("bill/update_open_alert_no_pay", true)
                              }
                        }


                  },
                  closeAlertNoPay() {
                        this.$store.commit("bill/update_open_alert_no_pay", false)
                  },
                  forgetAlertNoPay() {
                        var xval = this.$store.state.paymentnew.types
                        var valpay = 0
                        xval.forEach(function (obj) {
                              obj.chex = false
                              obj.leftvalue = 0
                              obj.rightvalue = 0
                              obj.selected_card = {
                                    id: 0,
                                    name: ''
                              }
                              obj.selected_edc = {
                                    id: 0,
                                    name: ''
                              }
                        })
                        this.$store.commit("paymentnew/update_total_payment", 0)
                        this.$store.commit("bill/update_open_alert_no_pay", false)
                  },
                  changeBtnFlagCourier(value) {
                        var newval = (value === 'Y') ? 'N' : 'Y'
                        this.xisauto = newval
                  },
                  changeBtnFlagDefaultCourier(value) {
                        var newval = (value === 'Y') ? 'N' : 'Y'
                        this.xdisauto = newval
                  },
            },
            computed: {
                  xstartdate: {
                        get() {
                              return this.$store.state.bill.start_date
                        },
                        set(val) {
                              this.$store.commit("bill/update_start_date", val)
                        }
                  },
                  xenddate: {
                        get() {
                              return this.$store.state.bill.end_date
                        },
                        set(val) {
                              this.$store.commit("bill/update_end_date", val)
                        }
                  },
                  init_sdate: {
                get() {
                    return this.$store.state.bill.init_sdate
                },
                set(val) {
                    this.$store.commit("bill/update_init_sdate", val)
                }
            },
            init_edate: {
                get() {
                    return this.$store.state.bill.init_edate
                },
                set(val) {
                    this.$store.commit("bill/update_init_edate", val)
                }
            },
                  xisauto: {
                        get() {
                              return this.$store.state.bill.isauto
                        },
                        set(val) {
                              this.$store.commit("bill/update_isauto", val)
                              this.$store.commit("paymentmanual/update_isauto", val)
                              this.$store.commit("paymentnew/update_isauto", val)
                        }
                  },
                  searchsupplier: {
                        get() {
                              return this.$store.state.bill.ssupplier
                        },
                        set(val) {
                              this.$store.commit("bill/update_ssupplier", val)
                        }
                  },
                  searchnamelab: {
                        get() {
                              return this.$store.state.bill.search
                        },
                        set(val) {
                              this.$store.commit("bill/update_search", val)
                        }
                  },
                  statuses() {
                        return this.$store.state.bill.statuses
                  },
                  status: {
                        get() {
                              return this.$store.state.bill.selected_status
                        },
                        set(val) {
                              this.$store.commit("bill/update_selected_status", val)
                        }
                  },
                  startDateFormatted() {
                        return this.formatDate(this.init_sdate)
                  },
                  endDateFormatted() {
                        return this.formatDate(this.init_edate)
                  },
                  isLoading() {
                        return this.$store.state.bill.search_status == 1
                  },
                  bills() {
                        return this.$store.state.bill.bills
                  },
                  openalertnopay: {
                        get() {
                              return this.$store.state.bill.open_alert_no_pay
                        },
                        set(val) {
                              this.$store.commit("bill/update_open_alert_no_pay", false)
                        }
                  },
                  msgalertnopay() {
                        return this.$store.state.bill.msg_alert_no_pay
                  },
                  curr_page: {
                        get() {
                              return this.$store.state.bill.current_page
                        },
                        set(val) {
                              this.$store.commit("bill/update_current_page", val)
                              this.$store.dispatch("bill/search", {
                                    supplier: this.searchsupplier,
                                    search: this.searchnamelab,
                                    status: this.status.value,
                                    current_page: val,
                                    startdate: this.init_sdate,
                                    enddate: this.init_edate,
                                    lastidx: 0
                              })
                        }
                  },
                  xtotal_page: {
                        get() {
                              return this.$store.state.bill.total_page
                        },
                        set(val) {
                              this.$store.commit("bill/update_total_page", val)
                        }
                  },
            },
            data() {
                  return {
                        items: [],
                        menustartdate: false,
                        menuenddate: false,
                        headers: [{
                                    text: "TGL. BAYAR",
                                    align: "left",
                                    sortable: false,
                                    value: "mr",
                                    width: "15%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },
                              {
                                    text: "NO. TAGIHAN",
                                    align: "left",
                                    sortable: false,
                                    value: "lab",
                                    width: "15%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },
                              {
                                    text: "NO. INSTRUKSI",
                                    align: "left",
                                    sortable: false,
                                    value: "name",
                                    width: "20%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },

                              {
                                    text: "SUPPLIER",
                                    align: "left",
                                    sortable: false,
                                    value: "status",
                                    width: "20%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },
                              {
                                    text: "TOTAL TAGIHAN",
                                    align: "left",
                                    sortable: false,
                                    value: "status",
                                    width: "10%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },
                              {
                                    text: "TOTAL BAYAR",
                                    align: "left",
                                    sortable: false,
                                    value: "status",
                                    width: "10%",
                                    class: "pa-2 blue lighten-3 white--text"
                              }
                        ],
                  }
            }
      }
</script>