<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Loan Paid Off 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('Term Type') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.term_type" trackBy="title" value="id" label="title" :searchable="true" :options="termTypes" />
                    </div>
                    <label class="form-label">{{ $t('Completed Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" :range="true" :placeholder="$t('Completed 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>
                    <label class="form-label">{{ $t('As of day') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker :placeholder="$t('As of day')" v-model="params.as_of_date" valueType="format" format="YYYY-MM-DD"> </date-picker>
                    </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 class="list-group mb-3">
                        <button type="button" @click="exportExcell" class="btn btn-danger"><i class="fa fa-file-export"></i> {{ $t('Export') }}</button>
                    </div>
                </div>
            </div>
        </template>

        <div slot="body">
            <div id="show-print">
                <div class="table-responsive table_scroll report_print">
                    <header-report :title="$t('Loan Paid Off Report')" :date="params.date" :date_title="$t('Completed Date')" 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("Loan Code") }}</th>
                                <th>{{ $t("Client Code") }}</th>
                                <th>{{ $t("Client Name") }}</th>
                                <th>{{ $t("Phone") }}</th>
                                <th>{{ $t("Address") }}</th>
                                <th>{{ $t("CO Name") }}</th>
                                <th>{{ $t("Currency") }}</th>
                                <th>{{ $t("Sessions") }}</th>
                                <th>{{ $t("Installment Date") }}</th>
                                <th>{{ $t("Completed Date") }}</th>
                                <th>{{ $t("Loan Out") }}</th>
                                <th>{{ $t("Total Interest") }}</th>
                                <!-- <th>{{ $t("Total Operating Services") }}</th> -->
                                <th>{{ $t("Total Loan Amount") }}</th>
                                <th>{{ $t("Total Service") }}</th>
                                <!-- <th>{{ $t("Total Insurance") }}</th> -->
                                <th>{{ $t("Paid Principal") }}</th>
                                <th>{{ $t("Paid Interest") }}</th>
                                <!-- <th>{{ $t("Paid Operating Services") }}</th>
                                <th>{{ $t("Paid Security Services") }}</th> -->
                                <th>{{ $t("Total Received") }}</th>
                                <th>{{ $t("Total Penalty") }}</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <tr v-for="(loan, index) in dataReports.data" :key="index">
                                <td>{{ index + 1 }}</td>
                                <td>{{ loan.loan_code }}</td>
                                <td>{{ loan.client_code }}</td>
                                <td>{{ loan.client_name }}</td>
                                <td>{{ loan.client_phone }}</td>
                                <td>{{ loan.address }}</td>
                                <td>{{ loan.co_name }}</td>
                                <td>{{ loan.currency_name }}</td>
                                <td>{{ loan.level_loan }}</td>
                                <td>{{ loan.installment_date | onlyDate }}</td>
                                <td>{{ loan.completed_date | onlyDate }}</td>
                                <td>{{ loan.loan_out | numberFomartValue }}</td>
                                <td>{{ loan.total_interest | numberFomartValue }}</td>
                                <!-- <td>{{ loan.total_risk | numberFomartValue }}</td> -->
                                <td>{{ loan.loan_receive | numberFomartValue }}</td>
                                <td>{{ loan.total_service | numberFomartValue }}</td>
                                <!-- <td>{{ loan.total_insurance | numberFomartValue }}</td> -->
                                <td>{{ loan.paid_principal | numberFomartValue }}</td>
                                <td>{{ loan.paid_interest | numberFomartValue }}</td>
                                <!-- <td>{{ loan.paid_operating_service | numberFomartValue }}</td>
                                <td>{{ loan.paid_maintenance | numberFomartValue }}</td> -->
                                <td>{{ loan.loan_in| numberFomartValue }}</td>
                                <td>{{ loan.total_penalty| numberFomartValue }}</td>
                            </tr>
                            <tr class="table-active">
                                <td colspan="11" class="text-end">{{ $t("Total") }}</td>
                                <td>{{totalPrincipal | numberFomartValue}}</td>
                                <td>{{totalInterest | numberFomartValue}}</td>
                                <!-- <td>{{totalRisk | numberFomartValue}}</td> -->
                                <td>{{totalLoanReceived | numberFomartValue}}</td>
                                <td>{{totalService | numberFomartValue}}</td>
                                <!-- <td>{{totalInsurance | numberFomartValue}}</td> -->
                                <td>{{totalPaidPrincipal | numberFomartValue}}</td>
                                <td>{{totalPaidInterest | numberFomartValue}}</td>
                                <!-- <td>{{totalPaidRisk | numberFomartValue}}</td>
                                <td>{{totalPaidMaintenance | numberFomartValue}}</td> -->
                                <td>{{totalReceived | numberFomartValue}}</td>
                                <td>{{totalPenalty | numberFomartValue}}</td>
                            </tr>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="dataReports" @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 moment from "moment";
    import { exportTableToExcel } from "../../exportExcel.js"
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable,
        },
        data() {
            return {
                moment: moment,
                format: "YYYY-MM-DD",
                dataReports: { data: [] },
                clients: [],
                coes: [],
                termTypes: [],
                form: new Form({

                }),
                params: {
                    page: 1,
                    per_page: 25,
                    as_of_date : "",
                    client_id: [],
                    co_id: [],
                    date: [],
                    term_type:[],
                },
            };
        },
        created() {
            this.funGetData();
            this.getClient();
            this.getCO();
            axios.get("admin-api/com-loan-term-type").then((res) => {this.termTypes = res.data});
        },
        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);
                    });
            },
            chooseDate(value) {
                this.params.date = value.data;
                this.funGetData();
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios
                    .get("admin-api/completed-report", {
                        params: this.params,
                    })
                    .then((res) => {
                        this.dataReports = 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");
            },
            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportCompleted",
                        reportTitle: "របាយការណ៍ឥណទានបង់ផ្តាច់",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
        },
        computed: {
            totalPrincipal: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_out);
                });
                return sum;
            },
            totalInterest: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_interest);
                });
                return sum;
            },
            totalRisk: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_risk);
                });
                return sum;
            },
            totalLoanReceived: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_receive);
                });
                return sum;
            },
            totalService: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_service);
                });
                return sum;
            },
            totalInsurance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_insurance);
                });
                return sum;
            },
            totalMaintenance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_maintenance);
                });
                return sum;
            },
            totalCollection: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_collection);
                });

                return sum;
            },
            totalPaidPrincipal: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_principal);
                });
                return sum;
            },
            totalPaidInterest: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_interest);
                });
                return sum;
            },
            totalPaidRisk: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_operating_service);
                });
                return sum;
            },
            totalPaidMaintenance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_maintenance);
                });
                return sum;
            },
            totalReceived: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_in);
                });
                return sum;
            },
            totalPenalty: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_penalty);
                });
                return sum;
            },
        },
    };
</script>
