<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Summary Co 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('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="form-group mb-3">
                        <label>{{ $t('Month') }}</label>
                        <date-picker type="month" @blur="funMonth" v-model="params.month" :bootstrap-styling="true" @input="funGetData"></date-picker>
                    </div>
                    <label class="form-label">{{ $t('Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" @blur="funDate" :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>
                    <ul class="list-group mb-3">
                        <button type="button" class="btn btn-success" @click="funSearchData"><i class="fa fa-search"></i> {{ $t('Search') }}</button>
                    </ul>
                    <ul class="list-group mb-3">
                        <button type="button" @click="printP" class="btn btn-primary"><i class="fa fa-print"></i> {{ $t('Print') }}</button>
                    </ul>
                    <!-- <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> -->
                    <ul class="list-group mb-3">
                        <button type="button" @click="exportExcell" class="btn 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="'របាយការណ៍សរុបរបស់មន្ត្រីឥណទាន'" style="display: none;"></header-report>
                    <table class="table table-bordered" id="excel-table">
                        <thead class="text-nowrap fixed_table">
                            <!-- <tr>
                                <th colspan="14"></th>
                                <th colspan="2" class="text-center">តូចជាង៣០ថ្ងៃ</th>
                                <th colspan="2" class="text-center">ធំជាង៣០ថ្ងៃ</th>
                                <th colspan="4"></th>
                            </tr> -->
                            <tr>
                                <th>ល.រ</th>
                                <th>កូដមន្រ្តីឥណទាន</th>
                                <th class="fixed_start">មន្រ្តីឥណទាន</th>
                                <th>ចំនួនបញ្ចេញ</th>
                                <th>អតិថិជនថ្មី</th>
                                <th>ទឹកប្រាក់បញ្ចេញ</th>
                                <th>ទឹកប្រាក់បញ្ចេញអតិ.ថ្មី</th>
                                <th>ចំនួនសកម្ម</th>
                                <!-- <th>ចំនួនអសកម្ម</th> -->
                                <th>ចំនួនបង់ផ្តាច់</th>
                                <th>ទឹកប្រាក់ក្នុងដៃអតិថិជន</th>
                                <th>ចំនួនយឺត</th>
                                <th>ទឹកប្រាក់យឺត</th>
                                <th>PAR</th>
                                <!-- <th>ទឹកប្រាក់យឺត</th>
                                <th>PAR</th>
                                <th>ទឹកប្រាក់យឺត</th>
                                <th>PAR</th> -->
                                <th>ការប្រាក់បានប្រមូល</th>
                                <th>សេវាប្រតិបត្តិការបានប្រមូល</th>
                                <th>ប្រាក់ដើមបានប្រមូល</th>
                                <th>ប្រាក់ពិន័យបានប្រមូល</th>
                                <th>បា្រក់បានប្រមូលសរុប</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <tr v-for="(loan, index) in dataReports.data" :key="index">
                                <td>{{ index + 1 }}</td>
                                <td>{{ loan.code }}</td>
                                <td class="fixed_start">{{ loan.co_name }}</td>
                                <td>{{ loan.total_client }}</td>
                                <td>{{ loan.total_client_new }}</td>
                                <td>{{ loan.sum_principal | numberFomartValue }}</td>
                                <td>{{ loan.sum_client_new | numberFomartValue }}</td>
                                <td>{{ loan.client_active }}</td>
                                <!-- <td>{{ loan.client_inactive }}</td> -->
                                <td>{{ loan.client_complete }}</td>
                                <td>{{ loan.balance_principal | numberFomartValue }}</td>
                                <td>{{ loan.count_day_late | numberFomartValue }}</td>
                                <td>{{ loan.amount_day_late | numberFomartValue }}</td>
                                <td>{{ (loan.amount_day_late/loan.balance_principal)*100 | numberFomartValue }} %</td>
                                <!-- <td>{{ loan.amount_late_small_30 | numberFomartValue }}</td>
                                <td>{{(loan.amount_late_small_30/loan.balance_principal)*100 | numberFomartValue}} %</td>
                                <td>{{ loan.amount_late_big_30 | numberFomartValue }}</td>
                                <td>{{(loan.amount_late_big_30/loan.balance_principal)*100 | numberFomartValue}} %</td> -->
                                <td>{{ loan.paid_interest | numberFomartValue }}</td>
                                <td>{{ loan.paid_risk | numberFomartValue }}</td>
                                <td>{{ loan.paid_principal | numberFomartValue }}</td>
                                <td>{{ loan.paid_penalty | numberFomartValue }}</td>
                                <td>{{loan.total_paid | numberFomartValue}}</td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr class="c_bg_tfoot">
                                <td colspan="3" class="text-end">សរុប</td>
                                <td>{{totalClient}}</td>
                                <td>{{totalClientNew}}</td>
                                <td>{{totalSumPrincipal | numberFomartValue}}</td>
                                <td>{{totalPrincipalNew | numberFomartValue}}</td>
                                <td>{{totalActive}}</td>
                                <!-- <td>{{totalInActive}}</td> -->
                                <td>{{totalInActive}}</td>
                                <td>{{totalBalancePrincipal | numberFomartValue}}</td>
                                <td>{{totalLate | numberFomartValue}}</td>
                                <td>{{totalAmountLate | numberFomartValue}}</td>
                                <td>{{(totalAmountLate/totalBalancePrincipal)*100 | numberFomartValue}}</td>
                                <!-- <td></td>
                                <td>{{(totalAmountLateSmall/totalBalancePrincipal)*100 | numberFomartValue}}</td>
                                <td></td>
                                <td>{{(totalAmountLateBig/totalBalancePrincipal)*100 | numberFomartValue}}</td> -->
                                <td>{{totalPaidInterest | numberFomartValue}}</td>
                                <td>{{totalPaidRisk | numberFomartValue}}</td>
                                <td>{{totalPaidPrincipal | numberFomartValue}}</td>
                                <td>{{totalPaidPenalty | numberFomartValue}}</td>
                                <td>{{totalAll |numberFomartValue}}</td>
                            </tr>
                        </tfoot>
                    </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 { exportTableToExcel } from "../../exportExcel.js";
    import moment from "moment";
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable
        },
        data() {
            return {
                moment: moment,
                format: "YYYY-MM-DD",
                dataReports: { data: [] },
                coes: [],
                form: new Form({

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

                    month : [],
                    co_id : [],
                    date  : [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getCO();
        },
        methods: {
            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.params.month = [];
                this.funGetData();
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios.get("admin-api/summary-co-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);
                    }
                });
            },
            funMonth(){
                this.params.date = [];
            },
            funDate(){
                this.params.month = [];
            },
            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: "ExportSummaryCo",
                        reportTitle: "របាយការណ៍សង្ខេបមន្រ្តីឥណទាន",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },
        },
        computed: {
            totalSumPrincipal: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.sum_principal);
                });
                return sum;
            },
            totalSumInterest: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.sum_interest);
                });
                return sum;
            },
            totalSumRisk: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.sum_risk);
                });
                return sum;
            },
            totalSumMaintenance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.sum_maintenance);
                });
                return sum;
            },
            totalBalancePrincipal: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.balance_principal);
                });
                return sum;
            },
            totalBalanceInterest: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.balance_interest);
                });
                return sum;
            },
            totalBalanceRisk: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.balance_risk);
                });
                return sum;
            },
            totalBalanceMaintenance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.balance_maintenance);
                });
                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_risk);
                });
                return sum;
            },
            totalPaidPenalty: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_penalty);
                });
                return sum;
            },
            totalAll: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_paid);
                });
                return sum;
            },
            totalPaidMaintenance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.paid_maintenance);
                });
                return sum;
            },
            totalAmountLate: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.amount_day_late);
                });
                return sum;
            },
            totalAmountLateSmall: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.amount_late_small_30);
                });
                return sum;
            },
            totalAmountLateBig: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.amount_late_big_30);
                });
                return sum;
            },
            totalLate: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.count_day_late);
                });
                return sum;
            },
            totalActive: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_active);
                });
                return sum;
            },
            totalInActive: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_inactive);
                });
                return sum;
            },
            totalClient: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_client);
                });
                return sum;
            },
            totalClientNew: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_client_new);
                });
                return sum;
            },
            totalPrincipalNew: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.sum_client_new);
                });
                return sum;
            },
        },
    };
</script>
