<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Summary Productivity By Co') }}
        </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="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>
                    <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> -->
                    <!-- <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="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" 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>
                    <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="$t('Productivity by Credit Office Report')" style="display: none;"></header-report>
                    <table class="table table-bordered" id="excel-table">
                        <thead class="text-nowrap fixed_table">
                            <tr>
                                <th>#</th>
                                <th>ID Staff</th>
                                <th>Branch Code</th>
                                <th>CO Name</th>
                                <th># CLIENT</th>
                                <th>LOAN DISBUSRE</th>
                                <th># CLIENT</th>
                                <th>LOAN INSURANCE</th>
                                <th># CLIENT</th>
                                <th>LOAN PORTFOLIO</th>
                                <th># CLIENT</th>
                                <th>NPL AMONUT ≤15DAY</th>
                                <th>NPL (%)</th>
                                <th># CLIENT</th>
                                <th>NPL AMONUT >15DAY</th>
                                <th>NPL (%)</th>
                                <th># CLIENT</th>
                                <th>NPL AMONUT >30DAY</th>
                                <th>NPL (%)</th>
                                <th># CLIENT</th>
                                <th>NPL AMONUT >60DAY</th>
                                <th>NPL (%)</th>
                                <th># CLIENT</th>
                                <th>NPL AMONUT</th>
                                <th>NPL (%)</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>{{ loan.branch_code }}</td>
                                <td>{{ loan.co_name }}</td>
                                <td>{{ loan.client_disburst }}</td>
                                <td>{{ loan.loan_disburst | numberFomartValue}}</td>
                                <td>{{ loan.client_insurance }}</td>
                                <td>{{ loan.loan_insurance | numberFomartValue}}</td>
                                <td>{{ loan.client_portfolio }}</td>
                                <td>{{ loan.loan_portfolio | numberFomartValue}}</td>
                                <td>{{ loan.client_npl_less15 }}</td>
                                <td>{{ loan.npl_amount_less15 | numberFomartValue}}</td>
                                <td>{{ loan.npl_less15_percent | numberFomartValue}}%</td>
                                <td>{{ loan.client_npl_larger15 }}</td>
                                <td>{{ loan.npl_amount_larger15 | numberFomartValue}}</td>
                                <td>{{ loan.npl_larger15_percent | numberFomartValue}}%</td>
                                <td>{{ loan.client_npl_larger30 }}</td>
                                <td>{{ loan.npl_amount_larger30 | numberFomartValue}}</td>
                                <td>{{ loan.npl_larger30_percent}}%</td>
                                <td>{{ loan.client_npl_larger60  | numberFomartValue}}</td>
                                <td>{{ loan.npl_amount_larger60 | numberFomartValue}}</td>
                                <td>{{ loan.npl_larger60_percent | numberFomartValue}}%</td>
                                <td>{{ loan.total_client_npl }}</td>
                                <td>{{ loan.total_npl_amount | numberFomartValue}}</td>
                                <td>{{ loan.npl_total_percent | numberFomartValue}}%</td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr class="c_bg_tfoot fw-bold">
                                <td colspan="4" class="text-end">សរុប</td>
                                <td>{{totalClientDisburst}}</td>
                                <td>{{totalLoanDisburst | numberFomartValue}}</td>
                                <td>{{totalClientInsurance}}</td>
                                <td>{{totalLoanInsurance | numberFomartValue}}</td>
                                <td>{{totalClientPortfolio}}</td>
                                <td>{{totalLoanPortfolio | numberFomartValue}}</td>
                                <td>{{totalClientNPLLess15}}</td>
                                <td>{{totalAmountNPLLess15 | numberFomartValue}}</td>
                                <td>
                                    <span v-if="totalAmountNPLLess15>0">{{parseFloat((totalAmountNPLLess15/totalLoanPortfolio)*100).toFixed(2)}}%</span>
                                    <span v-else>0%</span>
                                </td>
                                <td>{{totalClientNPLLarger15}}</td>
                                <td>{{totalAmountNPLLarger15 | numberFomartValue}}</td>
                                <td>
                                    <span v-if="totalAmountNPLLarger15>0">{{parseFloat((totalAmountNPLLarger15/totalLoanPortfolio)*100).toFixed(2)}}%</span>
                                    <span v-else>0%</span>
                                </td>
                                <td>{{totalClientNPLLarger30}}</td>
                                <td>{{totalAmountNPLLarger30 | numberFomartValue}}</td>
                                <td>
                                    <span v-if="totalAmountNPLLarger30>0">{{parseFloat((totalAmountNPLLarger30/totalLoanPortfolio)*100).toFixed(2)}}%</span>
                                    <span v-else>0%</span>
                                </td>
                                <td>{{totalClientNPLLarger60}}</td>
                                <td>{{totalAmountNPLLarger60 | numberFomartValue}}</td>
                                <td>
                                    <span v-if="totalAmountNPLLarger60>0">{{parseFloat((totalAmountNPLLarger60/totalLoanPortfolio)*100).toFixed(2)}}%</span>
                                    <span v-else>0%</span>
                                </td>
                                <td>{{totalClientNPL}}</td>
                                <td>{{totalAmountNPL | numberFomartValue}}</td>
                                <td>
                                    <span v-if="totalAmountNPL>0">{{parseFloat((totalAmountNPL/totalLoanPortfolio)*100).toFixed(2)}}%</span>
                                    <span v-else>0%</span>
                                </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 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: [] },
                coes: [],
                termTypes: [],
                form: new Form({

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

                    month : [],
                    co_id : [],
                    date  : [],
                    term_type: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getCO();
            axios.get("admin-api/com-loan-term-type").then((res) => {this.termTypes = res.data});
        },
        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: {
            totalClientDisburst: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_disburst);
                });
                return sum;
            },
            totalLoanDisburst: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_disburst);
                });
                return sum;
            },
            totalClientInsurance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_insurance);
                });
                return sum;
            },
            totalLoanInsurance: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_insurance);
                });
                return sum;
            },
            totalClientPortfolio: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_portfolio);
                });
                return sum;
            },
            totalLoanPortfolio: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.loan_portfolio);
                });
                return sum;
            },
            totalClientNPLLess15: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_npl_less15);
                });
                return sum;
            },
            totalAmountNPLLess15: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.npl_amount_less15);
                });
                return sum;
            },
            totalClientNPLLarger15: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_npl_larger15);
                });
                return sum;
            },
            totalAmountNPLLarger15: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.npl_amount_larger15);
                });
                return sum;
            },
            totalClientNPLLarger30: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_npl_larger30);
                });
                return sum;
            },
            totalAmountNPLLarger30: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.npl_amount_larger30);
                });
                return sum;
            },
            totalClientNPLLarger60: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.client_npl_larger60);
                });
                return sum;
            },
            totalAmountNPLLarger60: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.npl_amount_larger60);
                });
                return sum;
            },
            totalClientNPL: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_client_npl);
                });
                return sum;
            },
            totalAmountNPL: function () {
                var sum = 0;
                this.dataReports.data.forEach(function (item) {
                    sum += parseFloat(item.total_npl_amount);
                });
                return sum;
            },
        },
    };
</script>
