<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Cash Flow') }}
        </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('Invoice Number') }}</label>
                        <input type="text" class="form-control border-1" v-model="params.invoice_number" :placeholder="$t('Invoice Number')" />
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Chart Code') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.chart_of_acc_id" trackBy="chart_code_name" value="id" label="chart_code_name" :searchable="true" :options="chart_of_account" />
                    </div>
                    <label class="form-label">{{ $t('Journal Date') }}</label>
                    <div class="mb-3 d-flex">
                        <date-picker class="me-1" :range="true" :placeholder="$t('Journal Date')" v-model="params.journal_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="funGetData"><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('Cash Flow')" style="display: none;"></header-report>
                    <table class="table table-bordered mb-0" id="excel-table">
                        <thead class="fixed_head sm_head">
                            <tr>
                                <th class="text-nowrap">{{ $t("#") }}</th>
                                <th class="text-nowrap">{{ $t("Invoice Number") }}</th>
                                <th class="text-nowrap">{{ $t("Journal Date") }}</th>
                                <th class="text-nowrap">{{ $t("Chart Code") }}</th>
                                <th class="text-nowrap">{{ $t("Chart Name") }}</th>
                                <th class="text-nowrap">{{ $t("Debit") }}</th>
                                <th class="text-nowrap">{{ $t("Credit") }}</th>
                                <th class="text-nowrap">{{ $t("Balance") }}</th>
                                <th class="text-nowrap">{{ $t("Created By") }}</th>
                                <th class="text-nowrap">{{ $t("Description") }}</th>
                            </tr>
                        </thead>
                        <tbody v-for="(value,index) in cash_flows.data" :key="index">
                            <tr>
                                <td>{{index+1}}</td>
                                <td>{{value.invoice_number}}</td>
                                <td>{{value.journal_date | onlyDate}}</td>
                                <td>{{value.chart_code}}</td>
                                <td>{{value.chart_name}}</td>
                                <td>{{value.debit==0?0: value.debit | numberFomartValue}}</td>
                                <td>{{value.credit==0?0: value.credit | numberFomartValue}}</td>
                                <td>{{value.balance | numberFomartValue}}</td>
                                <td>{{value.created_by}}</td>
                                <td>{{value.desc}}</td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr class="table-active">
                                <th colspan="5" class="text-end">{{ $t("Total") }}:</th>
                                <th>{{ totalDebit | numberFomartValue}}</th>
                                <th>{{ totalCredit | numberFomartValue}}</th>
                                <th>{{ totalBalance | numberFomartValue}}</th>
                                <th></th>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="data_paginate" @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";
    export default {
        components: {
            CumstomPagination,
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable
        },
        data() {
            return {
                format: "YYYY-MM-DD",
                cash_flows: { data: [] },
                data_paginate: {},
                chart_of_account: [],
                form: new Form({}),
                params: {
                    page: 1,
                    per_page: 25,
                    search_text: "",

                    invoice_number: "",
                    chart_of_acc_id: [],
                    journal_date: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.funGetDataChartOfAcc();
        },
        methods: {
            funGetData(page = 1) {
                this.params.page = page;
                var array_push = [];
                var balance = 0;
                axios({
                    url: "accounting/cash-flow",
                    method: "GET",
                    params: this.params,
                }).then((res) => {
                    res.data.data.forEach(function (item) {
                        balance = balance + item.debit - item.credit;
                        array_push.push({
                            desc: item.desc,
                            created_by: item.created_by,
                            chart_name: item.chart_name,
                            journal_date: item.journal_date,
                            invoice_number: item.invoice_number,
                            chart_code: item.chart_code,
                            debit: item.debit,
                            credit: item.credit,
                            balance: balance,
                        });
                    });
                    this.cash_flows.data = array_push;
                    this.data_paginate = res.data;
                }).catch((error) => {
                    if (error.message == "Request failed with status code 403") {
                        this.$router.push({ name: "permissionTest" });
                    } else {
                        console.log(error.message);
                    }
                });
            },
            funGetDataChartOfAcc() {
                axios
                    .get("accounting/get-current-asset")
                    .then((res) => {
                        this.chart_of_account = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            chooseDate(value) {
                this.params.journal_date = value.data;
                this.funGetData();
            },
            printP() {
                this.$htmlToPaper("show-print");
            },
            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportCashFlow",
                        reportTitle: "លំហូរសាច់ប្រាក់",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
        },
        computed: {
            totalDebit: function () {
                return this.cash_flows.data.reduce((sum, item) => sum + item.debit, 0);
            },
            totalCredit: function () {
                return this.cash_flows.data.reduce((sum, item) => sum + item.credit, 0);
            },
            totalBalance: function () {
                return this.totalDebit - this.totalCredit;
            },
        },
    };
</script>
