<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Transaction') }}
        </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 tbscroll_nopaginat report_print">
                    <header-report :title="$t('Transaction')" style="display: none;"></header-report>
                    <table class="table table-bordered mb-0" id="excel-table">
                        <thead class="fixed_head sm_head">
                            <tr>
                                <th>{{ $t("Invoice Number") }}</th>
                                <th>{{ $t("Journal Date") }}</th>
                                <th>{{ $t("Chart Code") }}</th>
                                <th>{{ $t("Chart Name") }}</th>
                                <th>{{ $t("Debit") }}</th>
                                <th>{{ $t("Credit") }}</th>
                                <th>{{ $t("Created By") }}</th>
                                <th>{{ $t("Description") }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- Asset -->
                            <template v-for="asset in assets" >
                                <template v-for="(value,sub_index) in asset.array_asset">
                                    <tr >
                                        <td>{{value.invoice_number}}</td>
                                        <td>{{value.journal_date | onlyDate}}</td>
                                        <td>{{value.chart_code}}</td>
                                        <td>{{value.chart_name}}</td>
                                        <td>{{value.debit | numberFomartValue}}</td>
                                        <td>{{value.credit | numberFomartValue}}</td>
                                        <td>{{value.created_by}}</td>
                                        <td>{{value.desc}}</td>
                                    </tr>
                                </template>
                                <tr v-for="(sub,index) in asset.total" :key="sub.id" >
                                    <td colspan="4" class="text-end"><strong>Ending Balance</strong></td>
                                    <td colspan="2" class="text-center"><strong>{{sub.total_debit | numberFomartValue}}</strong></td>
                                </tr>
                            </template>
                            <!-- Liabilities -->
                            <template v-for="lb in liabilitiess">
                                <template v-for="(value,sub_index) in lb.array_liabilities">
                                    <tr>
                                        <td>{{value.invoice_number}}</td>
                                        <td>{{value.journal_date | onlyDate}}</td>
                                        <td>{{value.chart_code}}</td>
                                        <td>{{value.chart_name}}</td>
                                        <td>{{value.debit | numberFomartValue}}</td>
                                        <td>{{value.credit | numberFomartValue}}</td>
                                        <td>{{value.created_by}}</td>
                                        <td>{{value.desc}}</td>
                                    </tr>
                                </template>
                                <tr v-for="(sub, index) in lb.total" :key="index">
                                    <td colspan="4" class="text-end"><strong>Ending Balance</strong></td>
                                    <td colspan="2" class="text-center"><strong>{{sub.total_credit | numberFomartValue}}</strong></td>
                                </tr>
                            </template>
                            <!-- Equity -->
                            <template v-for="eq in equity">
                                <template v-for="(value,sub_index) in eq.array_equity">
                                    <tr>
                                        <td>{{value.invoice_number}}</td>
                                        <td>{{value.journal_date | onlyDate}}</td>
                                        <td>{{value.chart_code}}</td>
                                        <td>{{value.chart_name}}</td>
                                        <td>{{value.debit | numberFomartValue}}</td>
                                        <td>{{value.credit | numberFomartValue}}</td>
                                        <td>{{value.created_by}}</td>
                                        <td>{{value.desc}}</td>
                                    </tr>
                                </template>
                                <tr v-for="(sub, i) in eq.total" :key="i">
                                    <td colspan="4" class="text-end"><strong>Ending Balance</strong></td>
                                    <td colspan="2" class="text-center"><strong>{{sub.total_credit | numberFomartValue}}</strong></td>
                                </tr>
                            </template>
                            <!-- Income -->
                            <template v-for="inc in incomes">
                                <template v-for="(value,sub_index) in inc.array_income">
                                    <tr>
                                        <td>{{value.invoice_number}}</td>
                                        <td>{{value.journal_date | onlyDate}}</td>
                                        <td>{{value.chart_code}}</td>
                                        <td>{{value.chart_name}}</td>
                                        <td>{{value.debit | numberFomartValue}}</td>
                                        <td>{{value.credit | numberFomartValue}}</td>
                                        <td>{{value.created_by}}</td>
                                        <td>{{value.desc}}</td>
                                    </tr>
                                </template>
                                <tr v-for="sub in inc.total">
                                    <td colspan="4" class="text-end"><strong>Ending Balance</strong></td>
                                    <td colspan="2" class="text-center"><strong>{{sub.total_credit | numberFomartValue}}</strong></td>
                                </tr>
                            </template>
                            <!-- Expense -->
                            <template v-for="expense in expenses">
                                <template v-for="(value,sub_index) in expense.array_expense">
                                    <tr>
                                        <td>{{value.invoice_number}}</td>
                                        <td>{{value.journal_date | onlyDate}}</td>
                                        <td>{{value.chart_code}}</td>
                                        <td>{{value.chart_name}}</td>
                                        <td>{{value.debit | numberFomartValue}}</td>
                                        <td>{{value.credit | numberFomartValue}}</td>
                                        <td>{{value.created_by}}</td>
                                        <td>{{value.desc}}</td>
                                    </tr>
                                </template>
                                <tr v-for="sub in expense.total">
                                    <td colspan="4" class="text-end"><strong>Ending Balance</strong></td>
                                    <td colspan="2" class="text-center"><strong>{{sub.total_debit | numberFomartValue}}</strong></td>
                                </tr>
                            </template>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
        </div>
        <div slot="modal-custom">
            <custom-date-filter @chooseDate="chooseDate"></custom-date-filter>
        </div>
    </theme-table>
</template>

<script>
    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: { 
            CustomDateFilter,
            HeaderReport,
            FooterReport,
            ThemeTable
        },
        data() {
            return {
                format: "YYYY-MM-DD",
                assets: [],
                equity: [],
                incomes: [],
                expenses: [],
                liabilitiess: [],
                chart_of_account: [],
                form: new Form({}),
                params: {
                    invoice_number: "",
                    chart_of_acc_id: [],
                    journal_date: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.funGetDataChartOfAcc();
        },
        methods: {
            funGetDataChartOfAcc() {
                axios.get("accounting/com-chart-of-account").then((res) => {
                    this.chart_of_account = res.data;
                }).catch((error) => {
                    console.log(error);
                });
            },
            funGetData() {
                axios({
                    url: "accounting/general-ledger",
                    method: "GET",
                    params: this.params,
                }).then((res) => {
                    this.assets = res.data.asset;
                    this.liabilitiess = res.data.liabilities;
                    this.equity = res.data.equity;
                    this.incomes = res.data.income;
                    this.expenses = res.data.expense;
                }).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: "ExportGeneralLedger",
                        reportTitle: "ប្រតិបត្តិការទូទៅ",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
        },
    };
</script>
