<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Journal 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 for="">{{ $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 table_scroll report_print">
                    <header-report :title="$t('Journal 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("#") }}</th>
                                <th>{{ $t("Invoice Number") }}</th>
                                <th>{{ $t("Journal Date") }}</th>
                                <th>{{ $t("Account Number") }}</th>
                                <th>{{ $t("Account Name") }}</th>
                                <th>{{ $t("Debit") }}</th>
                                <th>{{ $t("Credit") }}</th>
                                <th>{{ $t("Created By") }}</th>
                                <th>{{ $t("Description") }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(value,index) in journal_data.data">
                                <td>{{index+1}}</td>
                                <td>{{value.invoice_number}}</td>
                                <td>{{value.journal_date | onlyDate}}</td>
                                <td>{{value.chart_code}}</td>
                                <td class="text-nowrap text-start">{{value.chart_name}}</td>
                                <td class="text-nowrap">
                                    <span v-if="value.debit>0">{{value.debit | numberFomartValue}}</span>
                                </td>
                                <td class="text-nowrap">
                                    <span v-if="value.credit>0">{{value.credit | numberFomartValue}}</span>
                                </td>
                                <td>{{value.created_by}}</td>
                                <td class="text-start">{{value.desc}}</td>
                            </tr>
                            <tr class="table-active">
                                <td colspan="5" class="text-end">{{ $t("Total") }}</td>
                                <td>{{sumTotalDebit | numberFomartValue}}</td>
                                <td>{{sumTotalCredit | numberFomartValue}}</td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="journal_data" @onChangePage="funGetData($event)"> </cumstom-pagination>
        </div>
        <div slot="modal-custom">
            <custom-date-filter @chooseDate="chooseDate"></custom-date-filter>
        </div>
    </theme-table>
</template>
<script>
    import HeaderReport from "../../../components/report/header.vue";
    import CumstomPagination from "../../../components/CustomPagination.vue";
    import CustomDateFilter from "../../../components/report/CustomDateFilter.vue";
    import FooterReport from "../../../components/report/footer.vue";
    import ThemeTable from "../../../components/ThemeTable.vue";
    import { exportTableToExcel } from "../../../exportExcel.js";
    export default {
        components: {
            HeaderReport,
            CumstomPagination,
            CustomDateFilter,
            FooterReport,
            ThemeTable,
        },
        data() {
            return {
                format: "YYYY-MM-DD",
                journal_data: { data: [] },
                chart_of_account: [],
                form: new Form({}),
                params: {
                    page: 1,
                    per_page: 25,
                    search_text: "",
                    journal_list: 2,

                    invoice_number: "",
                    chart_of_acc_id: [],
                    journal_date: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.funGetDataChartOfAcc();
        },
        computed: {
            sumTotalDebit: function () {
                var sum = 0;
                this.journal_data.data?.forEach((e) => {
                    sum += e.debit;
                });
                return sum;
            },
            sumTotalCredit: function () {
                var sum = 0;
                this.journal_data.data?.forEach((e) => {
                    sum += e.credit;
                });
                return sum;
            },
        },
        methods: {
            funGetDataChartOfAcc() {
                axios
                    .get("accounting/com-chart-of-account")
                    .then((res) => {
                        this.chart_of_account = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios.get("accounting/journals", {
                    params: this.params,
                }).then((res) => {
                    this.journal_data = res.data;
                }).catch((error) => {
                    if (error.message == "Request failed with status code 403") {
                        this.$router.push({ name: "permissionTest" });
                    } else {
                        console.log(error.message);
                    }
                });
            },
            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },
            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: "ExportJournal",
                        reportTitle: "របាយការណ៍ទិនានុប្បវត្តិ",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
            
        },
    };
</script>