<template>

    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Balance Sheet') }}
        </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">
                    <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 custom_report">

                    <header-report :title="$t('Balance Sheet')" style="display:none"></header-report>

                    <table class="table table-custom" id="excel-table">
                        <tbody>
                            <tr class="text-center">
                                <th>{{$t('#')}}</th>
                                <th>{{$t('Account Number')}}</th>
                                <th>{{$t('Account Name')}}</th>
                                <th>{{$t('Debit')}}</th>
                                <th>{{$t('Credit')}}</th>
                            </tr>
                            <tr>
                                <th colspan="5"><i><u>I-{{$t('ASSETS')}}</u></i></th>
                            </tr>
                            <tr v-for="(obj_asset,index) in assets" :key="index">
                                <td class="text-center">{{ index +1}}</td>
                                <td class="text-center">{{obj_asset.chart_code}}</td>
                                <td>{{obj_asset.chart_name}}</td>
                                <td class="text-end">
                                    <span v-show="obj_asset.total>0">{{(obj_asset.total) | numberFomartValue}}</span>
                                </td>
                                <td>
                                    <span v-show="obj_asset.total<0">{{(-obj_asset.total) | numberFomartValue}}</span>
                                </td>
                            </tr>
                            <tr>
                                <th colspan="3">{{$t('Total Assets')}}</th>
                                <th class="text-end">{{ (allData.total_asset) | numberFomartValue}}</th>
                                <th></th>
                            </tr>
                            <tr>
                                <th colspan="5"><i><u>II-{{$t('LIABILITIES')}}</u></i></th>
                            </tr>
                            <tr v-for="(obj_liability,index) in liabilities" :key="index">
                                <td class="text-center">{{ index +1}}</td>
                                <td class="text-center">{{obj_liability.chart_code}}</td>
                                <td>{{obj_liability.chart_name}}</td>
                                <td class="text-end">
                                    <span v-show="obj_liability.total<0">{{(obj_liability.total) | numberFomartValue}}</span>
                                </td>
                                <td class="text-end">
                                    <span v-show="obj_liability.total>0">{{(obj_liability.total) | numberFomartValue}}</span>
                                </td>
                            </tr>
                            <tr>
                                <th colspan="3">{{$t('Total Liabilities')}}</th>
                                <th class="text-end"></th>
                                <th class="text-end">{{(allData.total_liabilities) | numberFomartValue}}</th>
                            </tr>
                            <tr>
                                <th colspan="5"><i><u>III-{{$t('EQUITY')}}</u></i></th>
                            </tr>
                            <tr v-for="(obj_equity,index) in equities" :key="index">
                                <td class="text-center">{{ index +1}}</td>
                                <td class="text-center">{{obj_equity.chart_code}}</td>
                                <td>{{obj_equity.chart_name}}</td>
                                <td class="text-end">
                                    <span v-show="obj_equity.total<0">{{(obj_equity.total) | numberFomartValue}}</span>
                                </td>
                                <td class="text-end">
                                    <span v-show="obj_equity.total>0">{{(obj_equity.total) | numberFomartValue}}</span>
                                </td>
                            </tr>
                            <!-- <tr>
                                <td class="text-center">{{ equities.length+1}}</td>
                                <td class="text-center">40004</td>
                                <td colspan="3">Retained Earnings</td>
                                <td class="text-end"></td>
                                <td class="text-end">{{allData.return_earning | numberFomartValue}}</td>
                            </tr> -->
                            <tr>
                                <th colspan="3">Retained Earnings</th>
                                <th></th>
                                <th class="text-end">{{allData.return_earning | numberFomartValue}}</th>
                            </tr>
                            <tr>
                                <th colspan="3">{{$t('Total Equity')}}</th>
                                <th class="text-end"></th>
                                <th class="text-end">{{(allData.total_equity) | numberFomartValue}}</th>
                            </tr>
                            <tr class="table-success">
                                <th colspan="3"><i>Total Liabilities & Equity</i></th>
                                <th class="text-end"></th>
                                <th class="text-end">{{ (allData.total_liabilities + allData.total_equity) | numberFomartValue}}</th>
                            </tr>
                            <tr>
                                <th class="border-0" colspan="3"></th>
                                <th class="text-end">Check Balance</th>
                                <th class="text-end">
                                    {{ allData.check_balance | numberFomartValue}}
                                </th>
                            </tr>
                        </tbody>
                    </table>
                    <div class="footer mt-3" style="display:none">
                        <div class="report-footer page-wrap">
                            <div class="items">
                                <p> {{funToday(today)}}</p>
                                <p><strong>ពិនិត្យ និងឯកភាពដោយ</strong></p>
                            </div>
                            <div class="items">

                            </div>
                            <div class="items">
                                <p> {{funToday(today)}}</p>
                                <p><strong>រៀបចំដោយ</strong></p>
                            </div>
                        </div>
                    </div>
                </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 ThemeTable from "../../../components/ThemeTable.vue"
    import { exportTableToExcel } from "../../../exportExcel.js"
    import moment from "moment";

    export default {
        components:{
            HeaderReport,
            CustomDateFilter,
            ThemeTable
        },
        data() {
            return {
                moment : moment,
                today  : new Date().toISOString(),
                format : 'YYYY-MM-DD',
                    allData    : '',
                    assets     : [],
                    liabilities: [],
                    equities   : [],
                form: new Form({
                }),
                params: {
                    journal_date:[],
                },
            }
        },
        created() {
            this.funGetData();
        },
        methods: {
            funGetData() {
                axios.get("accounting/balance-sheet", {
                    params: this.params,
                }).then((res) => {
                    this.allData     = res.data.all;
                    this.assets      = res.data.assets;
                    this.liabilities = res.data.liabilities;
                    this.equities    = res.data.equities;
                });
            },
            chooseDate(value){
                this.params.journal_date = value.data;
                this.funGetData();
            },
            printP() {
                this.$htmlToPaper('show-print');
            },
            funDay(day) {
                if (day == "Mo") {
                    return "ចន្ទ";
                } else if (day == "Tu") {
                    return "អង្គារ";
                } else if (day == "We") {
                    return "ពុធ";
                } else if (day == "Th") {
                    return "ព្រហស្បតិ៍";
                } else if (day == "Fr") {
                    return "សុក្រ";
                } else if (day == "Sa") {
                    return "សៅរ៍";
                } else {
                    return "អាទិត្យ";
                }
            },
            funToday(date) {
                if (date) {
                    return "ថ្ងៃ" + this.funDay(moment(date).format("dd")) + " ទី" + moment(date).format("DD") + " ខែ" + this.funMonth(moment(date).format("MMM")) + " ឆ្នាំ" + moment(date).format("YYYY");
                }
            },
            funMonth(mon) {
                if (mon == "Jan") {
                    return "មករា";
                } else if (mon == "Feb") {
                    return "កុម្ភៈ";
                } else if (mon == "Mar") {
                    return "មីនា";
                } else if (mon == "Apr") {
                    return "មេសា";
                } else if (mon == "May") {
                    return "ឧសភា";
                } else if (mon == "Jun") {
                    return "មិថុនា";
                } else if (mon == "Jul") {
                    return "កក្កដា";
                } else if (mon == "Aug") {
                    return "សីហា";
                } else if (mon == "Sep") {
                    return "កញ្ញា";
                } else if (mon == "Oct") {
                    return "តុលា";
                } else if (mon == "Nov") {
                    return "វិច្ឆិកា";
                } else if (mon == "Dec") {
                    return "ធ្នូ";
                }
            },
            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportBalanceSheet",
                        reportTitle: "តារាងតុល្យការ",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
        }
    }
</script>


<style>


.table-custom tr th,
.table-custom tr td{
    border: 1px solid var(--bs-border-color);
}


</style>
