<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 text-center mb-0" id="excel-table">
                        <thead class="fixed_head sm_head">
                            <tr>
                                <th>{{ $t("#") }}</th>
                                <th>{{ $t("Account Number") }}</th>
                                <th>{{ $t("Account Name") }}</th>
                                <th>{{ $t("Debit") }}</th>
                                <th>{{ $t("Credit") }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(value,index) in datas" :key="index">
                                <td>{{ index +1 }}</td>
                                <td>{{value.chart_code}}</td>
                                <td class="text-sart">{{value.chart_name}}</td>
                                <td class="text-end">
                                    <span v-if="value.total_debit>0 || value.total_debit<0">{{value.total_debit | numberFomartValue}}</span>
                                </td>
                                <td class="text-end">
                                    <span v-if="value.total_credit>0 || value.total_credit<0">{{value.total_credit | numberFomartValue}}</span>
                                </td>
                            </tr>
                            <tr class="table-active">
                                <th colspan="3" class="text-end">{{ $t("Total") }}</th>
                                <th class="text-end"> {{ sumDebit | numberFomartValue}} </th>
                                <th class="text-end"> {{ sumCredit | numberFomartValue}} </th>
                            </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",
                datas: [],
                chart_of_account: [], 
                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.datas = res.data;
                }).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)
                }
            }
        },
        computed:{
            sumDebit: function () {
                var sum = 0;
                this.datas?.forEach(function (item) {
                    if(item.total_debit>0||item.total_debit<0){
                        sum += parseFloat(item.total_debit);
                    }
                });
                return sum;
            },
            sumCredit: function () {
                var sum = 0;
                this.datas?.forEach(function (item) {
                    if(item.total_credit>0||item.total_credit<0){
                        sum += parseFloat(item.total_credit);
                    }
                });
                return sum;
            },
        }
    };
</script>