<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Net Profit / Loss') }}
        </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('Net Profit / Loss')" style="display: none;"></header-report>
              

                    <table class="table mb-0" id="excel-table">
                        <tbody>
                            <!-- Revenues Section -->
                            <tr style="border-bottom: 2px solid #2076ba;">
                                <td colspan="2" class="border-0">
                                    <strong>{{ $t('Revenues') }}</strong>
                                </td>
                            </tr>
                            
                            <tr v-for="(income, index) in incomes" :key="'inc-' + index" style="border-bottom: 1px solid #eeeeee6b;">
                                <td class="ps-4 border-0">
                                    <label>({{ income.chart_code }}) {{ income.chart_name }}</label>
                                </td>
                                <td class="text-end border-0">
                                    <label>{{ income.total | numberFomartValue }}</label>
                                </td>
                            </tr>
                            
                            <tr>
                                <td class="ps-4 border-0 pb-4">
                                    <strong><i>{{ $t('Total Revenues') }}</i></strong>
                                </td>
                                <td class="text-end border-0 pb-4">
                                    <strong>{{ total_income | numberFomartValue }}</strong>
                                </td>
                            </tr>

                            <!-- Expense Section -->
                            <tr style="border-bottom: 2px solid #2076ba;">
                                <td colspan="2" class="border-0">
                                    <strong>{{ $t('EXPENSE') }}</strong>
                                </td>
                            </tr>
                            
                            <tr v-for="(expense, index) in expenses" :key="'exp-' + index" style="border-bottom: 1px solid #eeeeee6b;">
                                <td class="ps-4 border-0">
                                    <label>({{ expense.chart_code }}) {{ expense.chart_name }}</label>
                                </td>
                                <td class="text-end border-0">
                                    <label>{{ expense.total | numberFomartValue }}</label>
                                </td>
                            </tr>
                            
                            <tr>
                                <td class="ps-4 border-0 pb-4">
                                    <strong><i>{{ $t('Total Expense') }}</i></strong>
                                </td>
                                <td class="text-end border-0 pb-4">
                                    <strong>{{ total_expense | numberFomartValue }}</strong>
                                </td>
                            </tr>

                            <!-- Net Income Section -->
                            <tr style="border-bottom: 2px solid #2076ba;">
                                <td class="border-0">
                                    <strong>{{ $t('Net Income') }}</strong>
                                </td>
                                <td class="text-end border-0">
                                    <strong>{{ net_profit | numberFomartValue }}</strong>
                                </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: {
            FooterReport,
            HeaderReport,
            CustomDateFilter,
            ThemeTable,
        },
        data() {
            return {
                format: "YYYY-MM-DD",
                incomes: [],
                expenses: [],
                net_profit: 0,
                total_income: 0,
                total_expense: 0,
                total_cost_ogs: 0,
                form: new Form({}),
                params: {
                    journal_date: [],
                },
            };
        },
        created() {
            this.funGetData();
        },
        methods: {
             async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportNetProfitLoss",
                        reportTitle: "ចំណេញ / ខាតសុទ្ធ",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },
            funGetData() {
                axios
                    .get("accounting/netprofit-loss", {
                        params: this.params,
                    })
                    .then((res) => {
                        this.incomes = res.data.incomes;
                        this.total_income = res.data.total_income;
                        this.total_cost_ogs = res.data.total_cost_ogs;
                        this.expenses = res.data.glexspenses;
                        this.total_expense = res.data.total_glexpense;
                        this.net_profit = res.data.net_profit;
                    });
            },
            chooseDate(value) {
                this.params.journal_date = value.data;
                this.funGetData();
            },
            printP() {
                this.$htmlToPaper("show-print");
            },
        },
    };
</script>