<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('List Approve') }}
        </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>
                    <label class="form-label">{{ $t('Installment Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" :range="true" :placeholder="$t('Installment 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="funSearchData"><i class="fa fa-search"></i> {{ $t('Search') }}</button>
                    </div>
                    <div class="list-group mb-3">
                        <button type="button" @click="printP" class="btn btn-primary"><i class="fa fa-print"></i> {{ $t('Print') }}</button>
                    </div>
                </div>
            </div>
        </template>
        <div slot="body">
            <div id="show-print">
                <div class="table-responsive table_scroll report_print">
                    <header-report :title="$t('List Approve')" style="display: none;"></header-report>
                    <table class="table table-bordered">
                        <thead class="text-nowrap fixed_table">
                            <tr>
                                <th>{{ $t("#") }}</th>
                                <th>{{ $t("Loan Code") }}</th>
                                <th>{{ $t("CO Name") }}</th>
                                <th class="fixed_start">{{ $t("Client Name") }}</th>
                                <th>{{ $t("Phone") }}</th>
                                <th>{{ $t("Address") }}</th>
                                <th>{{ $t("Installment Date") }}</th>
                                <th>{{ $t("Start Date") }}</th>
                                <th>{{ $t("Loan Out") }}</th>
                                <th>{{ $t("Total Interest") }}</th>
                                <th>{{ $t("Total Operating Services") }}</th>
                                <!-- <th>{{ $t("Total Security Services") }}</th> -->
                                <th class="text-success">{{ $t("Total Loan Amount") }}</th>
                                <!-- <th>{{ $t('Service') }} %</th> -->
                                <th>{{ $t("Total Service") }}</th>
                                <!-- <th>{{ $t("Total Insurance") }}</th> -->
                                <!-- <th>{{ $t("Total Risk") }}</th> -->
                                <th style="color:#3579c1">{{ $t('Amount To Client') }}</th>
                                <th>{{ $t("Duration") }}</th>
                                <th>{{ $t("Type Payment") }}</th>
                                <th>{{ $t("Status") }}</th>
                                <th>{{ $t("Created By") }}</th>
                                <th class="display-none">{{ $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.loan_code }}</td>
                                <td>{{ loan.co_name }}</td>
                                <td class="fixed_start">{{ loan.client_name }}</td>
                                <td>{{ loan.client_phone }}</td>
                                <td>{{ loan.address }}</td>
                                <td>{{ loan.installment_date | onlyDate }}</td>
                                <td>{{ loan.start_date | onlyDate }}</td>
                                <td>{{ loan.loan_out | numberFomartValue }}</td>
                                <td>
                                    {{ loan.total_interest | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.interest | numberFomartValue}}%)</sup></span>
                                </td>
                                <td>
                                    {{ loan.total_risk | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.risk | numberFomartValue}}%)</sup></span>
                                </td>
                                <!-- <td>
                                    {{ loan.total_maintenance | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.maintenance | numberFomartValue}}%)</sup></span>
                                </td> -->
                                <td>{{ loan.loan_receive | numberFomartValue }}</td>
                                <td>
                                    {{ loan.total_service | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.service | numberFomartValue}}%)</sup></span>
                                </td>
                                <!-- <td>
                                    {{ loan.total_insurance | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.insurance | numberFomartValue}}%)</sup></span>
                                </td>
                                <td>
                                    {{ loan.total_collection | numberFomartValue }}
                                    <span><sup class="text-success">({{loan.service_collection | numberFomartValue}}%)</sup></span>
                                </td> -->
                                <td>{{ loan.amount_client_receive | 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>{{ loan.created_by}}</td>
                                <td class="display-none">
                                    <div class="dropdown actions">
                                        <button type="button" class="btn btn-success s-btn-xs" data-bs-toggle="dropdown" aria-expanded="false">
                                            <i class="fa-solid fa-ellipsis fa-sm"></i>
                                        </button>
                                        <ul class="dropdown-menu shadow">
                                            <li class="dropdown-item bg-secondary text-white">
                                                <p><i class="fa-solid fa-user fa-sm me-2 text-white"></i> {{ loan.client_name }}</p>
                                            </li>
                                            <button type="button" v-if="$can('Loan Approve')" @click="funApprove(loan.id)" class="dropdown-item">
                                                <i class="fa-solid fa-check-to-slot fa-sm me-2 text-primary"></i> {{ $t('Approve') }}
                                            </button>
                                            <li>
                                                <router-link v-if="$can('Loan Edit')" :to="{ name: 'LoanCreate',params:{id:loan.id} }" class="dropdown-item" >
                                                    <i class="fa-solid fa-pen-to-square fa-sm me-2 text-dark"></i> {{ $t('Edit') }}
                                                </router-link>
                                            </li>
                                            <li>
                                                <router-link v-if="$can('Loan Table Schedule')" :to="{ name: 'TableSchedule',params:{id:loan.id} }" class="dropdown-item">
                                                    <i class="fa-solid fa-list fa-sm me-2 text-success"></i> {{ $t('Table Schedule') }}
                                                </router-link>
                                            </li>
                                            <!-- <li>
                                                <router-link v-if="$can('Loan Collateral Create')" :to="{ name: 'CollateralDetailCreate',params:{id:loan.id} }" class="dropdown-item">
                                                    <i class="fa-solid fa-plus fa-sm me-2 text-secondary"></i> {{ $t('Collateral Create') }}
                                                </router-link>
                                            </li> -->
                                            <li>
                                                <button type="button" v-if="$can('Loan Delete')" @click="funDelete(loan.id)" class="dropdown-item">
                                                    <i class="fa-solid fa-trash-can fa-sm me-2 text-danger"></i> {{ $t('Delete') }}
                                                </button>
                                            </li>
                                        </ul>
                                    </div>
                                </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 moment from "moment";
    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";
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable,
        },
        data() {
            return {
                moment: moment,
                dataLoans: [],
                clients: [],
                coes: [],
                form: new Form({

                }),
                params: {
                    page: 1,
                    per_page: 25,
                    search_text: "",
                    pagin : false,
                    check_list_loan:1,

                    client_id: [],
                    date     : [],
                    co_id    : [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getClient();
            this.getCO();
        },
        methods: {
            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);
                    });
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios
                    .get("admin-api/loans", {
                        params: this.params,
                    })
                    .then((res) => {
                        this.dataLoans = res.data;
                    })
                    .catch((error) => {
                        if (error.message == "Request failed with status code 403") {
                            this.$router.push({ name: "permissionTest" });
                        } else {
                            console.log(error.message);
                        }
                    });
            },
            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },
            funSearchData() {
                this.params.pagin = 'true';
                this.funGetData();
            },
            printP() {
                this.$htmlToPaper("show-print");
            },
            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);
                }
            },
            chooseDate(value) {
                this.params.date = value.data;
                this.funGetData();
            },
            funApprove(id) {
                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);
                                }
                            });
                        }
                    },
                });
            },
        },
    };
</script>
