<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Overdue Report') }}
        </span>
        <template slot="more-button">
            <button class="btn btn-secondary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">{{$t('Filter')}}</button>
            <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
                <div class="offcanvas-header">
                    <h5 class="offcanvas-title" id="offcanvasRightLabel">{{$t('Filter')}}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                </div>
                <div class="offcanvas-body">
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Client') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.client_id" trackBy="name" value="id" label="name" :searchable="true" :options="clients" />
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Co') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.co_id" trackBy="name" value="id" label="name" :searchable="true" :options="coes" />
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Status') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.loan_status_id" trackBy="title" value="id" label="title" :searchable="true" :options="LoanStatuses" />
                    </div>
                    <label class="form-label">{{ $t('Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" :range="true" :placeholder="$t('Date')" v-model="params.date" valueType="format" format="YYYY-MM-DD"> </date-picker>
                        <button type="button" class="btn border btn-sm text-nowrap py-0" data-bs-toggle="modal" data-bs-target="#modalFilter"><i class="fas fa-sm fa-filter"></i> {{ $t('Filter') }}</button>
                    </div>
                    <div class="list-group mb-3">
                        <button type="button" class="btn btn-success" @click="funGetData"><i class="fa fa-search"></i> {{ $t('Search') }}</button>
                    </div>
                     <ul class="mb-3">
                        <button type="button" @click="exportExcell" class="btn w-100 btn-danger"><i class="fa fa-file-export"></i> {{ $t('Export') }}</button>
                    </ul>
                </div>
            </div>
        </template>
        <div slot="body">
            <div id="show-print">
                <div class="table-responsive table_scroll report_print">
                    <header-report :title="$t('Overdue Report')" style="display: none;"></header-report>
                    <table class="table table-bordered" id="excel-table">
                        <thead class="text-nowrap fixed_table">
                            <tr>
                                <th>{{ $t("#") }}</th>
                                <th>{{ $t("Client Name") }}</th>
                                <th>{{ $t("CO Name") }}</th>
                                <th>{{ $t("Loan Code") }}</th>
                                <th>{{ $t("Installment Date") }}</th>
                                <th>{{ $t("Start Date") }}</th>
                                <th>{{ $t("Depreciation") }}</th>
                                <th>{{ $t("Principal") }}</th>
                                <th>{{ $t("Interest") }}</th>
                                <th>{{ $t("Total Interest") }}</th>
                                <th>{{ $t("Operating Services") }}</th>
                                <th>{{ $t("Total Operating Services") }}</th>
                                <th>{{ $t("Loan Amount") }}</th>
                                <th>{{ $t("Total Received") }}</th>
                                <th>{{ $t("Total Balance") }}</th>
                                <th>{{ $t("Duration") }}</th>
                                <th>{{ $t("Type Payment") }}</th>
                                <th>{{ $t("Status") }}</th>
                                <th>{{ $t("Action") }}</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <tr v-for="(loan, index) in dataLoans.data" :key="index">
                                <td>{{ index + 1 }}</td>
                                <td>{{ loan.client_name }}</td>
                                <td>{{ loan.co_name }}</td>
                                <td>{{ loan.loan_code }}</td>
                                <td>{{ loan.installment_date | onlyDate }}</td>
                                <td>{{ loan.start_date | onlyDate }}</td>
                                <td>{{ loan.reschedule_amount | numberFomartValue }}</td>
                                <td>{{ loan.loan_out | numberFomartValue }}</td>
                                <td>{{ loan.interest | numberFomartValue }}%</td>
                                <td>{{ loan.total_interest | numberFomartValue }}</td>
                                <td>{{ loan.risk | numberFomartValue }}%</td>
                                <td>{{ loan.total_risk | numberFomartValue }}</td>
                                <td>{{ loan.loan_receive | numberFomartValue }}</td>
                                <td>{{ loan.loan_in | numberFomartValue }}</td>
                                <td>{{ loan.loan_balance | numberFomartValue }}</td>
                                <td>{{ loan.period}}</td>
                                <td>{{ funConditionPay(loan.condition_pay,loan.term_type)}}</td>
                                <td>
                                    <span :style="{ 'color': loan.color }">{{loan.loan_status}}</span>
                                </td>
                                <td>
                                    <router-link v-if="$can('Payment List')" :to="{ name: 'Payment',params:{id:loan.id} }" :title="$t('Payment List')" class="btn btn-success s-btn-xs"><i class="fa-solid fa-eye fa-sm"></i></router-link>
                                    <router-link v-if="$can('History Payment')" :to="{ name: 'HistoryPayment',params:{id:loan.id} }" :title="$t('History Payment')" class="btn btn-primary s-btn-xs">
                                        <i class="fa-solid fa-folder fa-sm"></i>
                                    </router-link>
                                    <router-link :to="{ name: 'TableSchedule',params:{id:loan.id} }" :title="$t('Table Schedule')" class="btn btn-primary s-btn-xs"><i class="fa-solid fa-list fa-sm"></i></router-link>
                                    <!-- <button type="button" :title="$t('Move To Corrupt')" @click="funMoveToCorrupt(loan.id)"
                                        class="btn btn-dark s-btn-xs">
                                        <i class="fa-solid fa-shield fa-sm"></i>
                                    </button> -->
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="dataLoans" @onChangePage="funGetData($event)"></cumstom-pagination>
        </div>
        <div slot="modal-custom">
            <custom-date-filter @chooseDate="chooseDate"></custom-date-filter>
        </div>
    </theme-table>
</template>
<script>
    import CumstomPagination from "../../components/CustomPagination.vue";
    import CustomDateFilter from "../../components/report/CustomDateFilter.vue";
    import HeaderReport from "../../components/report/header.vue";
    import FooterReport from "../../components/report/footer.vue";
    import ThemeTable from "../../components/ThemeTable.vue";
    import { exportTableToExcel } from "../../exportExcel.js";
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable,
        },
        data() {
            return {
                dataLoans: [],
                dataPayment: [],
                clients: [],
                coes: [],
                LoanStatuses: [],
                form: new Form({
                    check_black: "",
                }),
                params: {
                    search_text: "",
                    page: 1,
                    per_page: 25,
                    client_id: [],
                    co_id: [],
                    loan_status_id: [],
                    date: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getClient();
            this.getCO();
            this.getLoanStatus();
        },
        methods: {
            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "OverdueReport",
                        reportTitle: "របាយការណ៍អតិថិជនផុតកំណត់",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },
            getClient() {
                axios
                    .get("admin-api/com-client")
                    .then((res) => {
                        this.clients = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            getCO() {
                axios
                    .get("admin-api/com-co")
                    .then((res) => {
                        this.coes = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            getLoanStatus() {
                axios
                    .get("admin-api/com-loan-status")
                    .then((res) => {
                        this.LoanStatuses = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            chooseDate(value) {
                this.params.date = value.data;
                this.funGetData();
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios
                    .get("admin-api/overdue-report", {
                        params: this.params,
                    })
                    .then((res) => {
                        this.dataLoans = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },
            funTerm(status) {
                if (status == 1) {
                    return "ថ្ងៃ";
                } else if (status == 2) {
                    return "សប្ដាហ៍";
                } else if (status == 3) {
                    return "កន្លះខែ";
                } else if (status == 4) {
                    return "ខែ";
                }
            },
            funConditionPay(ConPay, status) {
                if (ConPay == 1) {
                    return "បង់ប្រចាំ" + this.funTerm(status) + "ថេរ";
                } else if (ConPay == 2) {
                    return "បង់រំលោះប្រចាំ" + this.funTerm(status) + "ថយ(ដើមថេរ)";
                } else if (ConPay == 3) {
                    return "បង់ប្រាក់ដើមនិងការប្រាក់ថេរ" + this.funTerm(status);
                } else if (ConPay == 4) {
                    return "បង់តែការប្រាក់ប្រចាំ" + this.funTerm(status);
                }
            },
            printP() {
                this.$htmlToPaper("show-print");
            },
            funMoveToCorrupt(id) {
                this.form.check_black = 4;
                this.$confirm({
                    message: "Are you sure?",
                    button: {
                        no: "No",
                        yes: "Yes",
                    },
                    callback: (confirm) => {
                        if (confirm) {
                            this.form.put("admin-api/loans/" + id).then((res) => {
                                if (res.data.status == 1) {
                                    this.$toast.success(res.data.message);
                                    this.funGetData();
                                } else {
                                    this.$toast.warning(res.data.message);
                                }
                            });
                        }
                    },
                });
            },
            funDelete(id) {
                this.$confirm({
                    message: "Are you sure?",
                    button: {
                        no: "No",
                        yes: "Yes",
                    },
                    callback: (confirm) => {
                        if (confirm) {
                            this.form.delete("admin-api/loans/" + id).then((res) => {
                                if (res.data.status == 1) {
                                    this.$toast.success(res.data.message);
                                    this.funGetData();
                                } else {
                                    this.$toast.warning(res.data.message);
                                }
                            });
                        }
                    },
                });
            },
            submitData() {
                this.form
                    .post("admin-api/loans")
                    .then((res) => {
                        if (res.data.status == 1) {
                            this.form.reset();
                            this.$toast.success(res.data.message);
                            $("#LoanForm").modal("hide");
                            this.funGetData();
                        } else {
                            this.$toast.warning(res.data.message);
                        }
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
        },
    };
</script>
