<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Trial Balance') }}
        </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('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('Trial Balance')" style="display: none;"></header-report>
                    <table class="table table-bordered mb-0" id="excel-table">
                        <thead class="fixed_head sm_head">
                            <tr>
                                <th>{{ $t("Chart Name") }}</th>
                                <th>{{ $t("Debit") }}</th>
                                <th>{{ $t("Credit") }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- Asset -->
                            <tr v-for="(value,index) in assets" :key="index">
                                <td>{{value.chart_code_name}}</td>
                                <td>{{value.total | numberFomartValue}}</td>
                                <td></td>
                            </tr>
                            <!-- Liabilities -->
                            <tr v-for="(value,index) in liabilitiess" :key="index">
                                <td>{{value.chart_code_name}}</td>
                                <td></td>
                                <td>{{value.total | numberFomartValue}}</td>
                            </tr>
                            <!-- Equity -->
                            <tr v-for="(value,index) in equity" :key="index">
                                <td>{{value.chart_code_name}}</td>
                                <td></td>
                                <td>{{value.total | numberFomartValue}}</td>
                            </tr>
                            <!-- Income -->
                            <tr v-for="(value,index) in incomes" :key="index">
                                <td>{{value.chart_code_name}}</td>
                                <td></td>
                                <td>{{value.total | numberFomartValue}}</td>
                            </tr>
                            <!-- Expense -->
                            <tr v-for="(value,index) in expenses" :key="index">
                                <td>{{value.chart_code_name}}</td>
                                <td>{{value.total | numberFomartValue}}</td>
                                <td></td>
                            </tr>
                        </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",
                chart_of_account: [],
                assets: [],
                equity: [],
                incomes: [],
                expenses: [],
                liabilitiess: [],
                form: new Form({}),
                params: {
                    chart_of_acc_id: [],
                    journal_date: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.funGetDataChartOfAcc();
        },
        methods: {
            funGetData() {
                axios({
                    url: "accounting/trial-balance",
                    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");
            },
            funGetDataChartOfAcc() {
                axios
                    .get("accounting/com-chart-of-account")
                    .then((res) => {
                        this.chart_of_account = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },

            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportTrialBalance",
                        reportTitle: "តុល្យភាពសាកល្បង",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },
        },
    };
</script>