<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Collected 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>
                    <label class="form-label">{{ $t('Received Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" :range="true" :placeholder="$t('Received 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 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('Collected Report')" :date="params.date" :date_title="$t('Received 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("CO Name") }}</th>
                                <th>{{ $t("Client Name") }}</th>
                                <th>{{ $t("Loan Code") }}</th>
                                <th>{{ $t("Client Code") }}</th>
                                <th>{{ $t("Phone") }}</th>
                                <th>{{ $t("Address") }}</th>
                                <th>{{ $t("Received Date") }}</th>
                                <th>{{ $t("Payment Method") }}</th>
                                <th>{{ $t("Currency") }}</th>
                                <th>{{ $t("Sessions") }}</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("Penalty") }}</th>
                                <th>{{ $t("Total Received") }}</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <tr v-for="(acc, index) in dataPayments.data" :key="index">
                                <td>{{ index + 1 }}</td>
                                <td>{{ acc.co_name}}</td>   
                                <td>{{ acc.client_name}}</td>
                                <td>{{ acc.loan_code}}</td>
                                <td>{{ acc.client_code}}</td>
                                <td>{{ acc.client_phone}}</td>
                                <td>{{ acc.address}}</td>
                                <td>{{ acc.received_date | onlyDate}}</td>
                                <td>{{ acc.payment_method}}</td>
                                <td>{{ acc.currency_name }}</td>
                                <td>{{ acc.level_loan }}</td>
                                <td>{{ acc.paid_principal | numberFomartValue }}</td>
                                <td>{{ acc.paid_interest | numberFomartValue }}</td>
                                <td>{{ acc.paid_risk | numberFomartValue }}</td>
                                 <!-- <td>{{ acc.paid_maintenance | numberFomartValue }}</td> -->
                                <!-- <td>{{ acc.received | numberFomartValue }}</td> -->
                                <td>{{ acc.paid_penalty | numberFomartValue }}</td>
                                <td>{{ acc.total_collected | 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>{{totalMaintenance | numberFomartValue}}</td> -->
                                <td>{{totalPenalty | numberFomartValue}}</td>
                                <td>{{totalReceived | numberFomartValue}}</td>
                            </tr>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="dataPayments" @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 moment from "moment";
    import ThemeTable from '../../components/ThemeTable.vue';
    import { exportTableToExcel } from "../../exportExcel.js";
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable
        },
        data() {
            return {
                moment: moment,
                format: "YYYY-MM-DD",
                dataPayments: { data: [] },
                clients: [],
                coes: [],
                form: new Form({

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

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

             async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportCollected",
                        reportTitle: "របាយការណ៍បានប្រមូល",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },



        },
        computed: {
            totalPrincipal: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.paid_principal);
                });
                return sum;
            },
            totalInterest: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.paid_interest);
                });
                return sum;
            },
            totalRisk: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.paid_risk);
                });
                return sum;
            },
            totalMaintenance: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.paid_maintenance);
                });
                return sum;
            },
            totalReceived: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.total_collected);
                });
                return sum;
            },
            totalPenalty: function () {
                var sum = 0;
                this.dataPayments.data.forEach(function (item) {
                    sum += parseFloat(item.paid_penalty);
                });
                return sum;
            },
        },
    };
</script>
