<template>
      <v-layout class="fill-height" column>
            <v-card class="mb-2 pa-2 searchbox">
                  <v-layout>
                        <v-text-field class="xs3 ma-1" label="Kel. Pelanggan" placeholder="Cari..." outline v-model="searchcompany" 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.F_BillPaymentDate}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.F_BillNo}}</td>
                                          <td class="pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.F_BillPaymentNumber}}</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}} : {{ props.item.M_MouName}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ convertMoney(props.item.F_BillTotal)}}</td>
                                          <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ convertMoney(props.item.F_BillPaymentAmount)}}</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", {
                        company: this.searchcompany,
                        search: this.searchnamelab,
                        status: this.status.value,
                        current_page: 1,
                        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.F_BillID == this.$store.state.bill.selected_bill.F_BillID
                  },
                  convertMoney(money) {
                        return one_money(money)
                  },
                  searchPatient() {
                        this.$store.dispatch("bill/search", {
                              company: this.searchcompany,
                              search: this.searchnamelab,
                              status: this.status.value,
                              current_page: 1,
                              lastidx: 0
                        })
                  },
                  selectMe(pat) {
                        if (this.xtotal_page > 0) {
                              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)
                              }
                        } else {
                              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)
                        }
                  },
                  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)
                        }
                  },
                  searchcompany: {
                        get() {
                              return this.$store.state.bill.scompany
                        },
                        set(val) {
                              this.$store.commit("bill/update_scompany", 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.xstartdate)
                  },
                  endDateFormatted() {
                        return this.formatDate(this.xenddate)
                  },
                  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", {
                                    company: this.searchcompany,
                                    search: this.searchnamelab,
                                    status: this.status.value,
                                    current_page: val,
                                    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. PEMBAYARAN",
                                    align: "left",
                                    sortable: false,
                                    value: "name",
                                    width: "20%",
                                    class: "pa-2 blue lighten-3 white--text"
                              },

                              {
                                    text: "KEL.PELANGGAN / AGREEMENT",
                                    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>