<template>
    <theme-table :newButton="true" @click="funshowForm" :permisName="'ChartOfAccount Create'" @input="Searching">
        <span slot="main-title">
            {{ $t('Chat Of Account') }}
        </span>
        <div slot="body">
            <div class="table-responsive table_scroll">
                <table class="table  table-bordered">
                    <thead>
                        <tr class="border-top border-bottom text-nowrap">
                            <th >{{ $t("#") }}</th>
                            <th >{{ $t("Chart Code") }}</th>
                            <th >{{ $t("Account Type") }}</th>
                            <th >{{ $t("Chart Name") }}</th>
                            <th >{{ $t("Description") }}</th>
                            <th >{{ $t("Create By") }}</th>
                            <th >{{ $t("Create Date") }}</th>
                            <th >{{ $t("Action") }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="text-nowrap" v-for="(value, index) in datas.data" :key="index">
                            <td> {{ index + 1 }} </td>
                            <td>{{ value.chart_code }}</td>
                            <td>{{ value.name }}</td>
                            <td>{{ value.chart_name }}</td>
                            <td>{{ value.desc }}</td>
                            <td>{{ value.created_by }}</td>
                            <td>{{ value.created_at | onlyDate }}</td>
                            <td v-if="![1,2,3,4,6,19,21,23,24,25,66].includes(value.id)">
                                <button v-if="$can('ChartOfAccount Edit')" type="button" @click="funshowForm(value.id)" :title="$t('Edit')" class="btn btn-dark s-btn-xs">
                                    <i class="fa-solid fa-pen-to-square fa-sm"></i>
                                </button>
                                <button v-if="$can('ChartOfAccount Delete')" type="button" @click="funDelete(value.id)" :title="$t('Delete')" class="btn btn-danger s-btn-xs">
                                    <i class="fa-solid fa-trash-can fa-sm"></i>
                                </button>
                            </td>
                            <td v-else></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="datas" @onChangePage="funGetData($event)"></cumstom-pagination>
        </div>
        <div slot="modal-custom">
            <ChartOfAccountForm :form="form" @submitData="submitData" @interface="getChildInterface"></ChartOfAccountForm>
        </div>
    </theme-table>
</template>

<script>
    import CumstomPagination from '../../../components/CustomPagination.vue'
    import ChartOfAccountForm from './ChartOfAccountForm.vue'
    import ThemeTable from '../../../components/ThemeTable.vue'
    export default {
        components: {
            CumstomPagination,
            ChartOfAccountForm,
            ThemeTable
        },
        data() {
            return {
                datas:{},
                form: new Form({
                    'id':'',
                    'desc': '',
                    'chart_code': '', 
                    'chart_name': '',
                    'type_of_acc_id':'',
                    'type_of_acc_detail_id':'',
                }),
                params: {
                    page: 1,
                    per_page: 25,
                    search_text: ''
                }
            }
        },
        created() {
            this.funGetData();
        },
        methods: {
            funGetData(page = 1) {
                this.params.page = page;
                axios.get('accounting/chart-of-account', {
                    params: this.params
                }).then((res) => {
                    this.datas = res.data;
                }).catch((error) => {
                    if(error.message=='Request failed with status code 403') 
                    {
                        this.$router.push({ name: 'permissionTest' });
                    }
                    else
                    {
                        console.log(error.message);
                    }
                    console.log(error);
                });
            },
            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },
            Searching(e) {
                this.params.search_text = e.target.value;
                this.funGetData();
            },
            funshowForm(id) {
                axios.get('accounting/chart-of-account/create', {
                    params: { 'id': id }
                }).then((res) => {
                    if (res.data.status == 1) {
                        this.form.fill(res.data.data);
                        this.$options.childInterface.getTypeOfAccDetail();
                    } else {
                        this.form.reset();
                    }
                    $("#modalForm").modal("show");
                });
            },
            getChildInterface(childInterface) {
                this.$options.childInterface = childInterface;
            },
            submitData() {
                this.form.post("accounting/chart-of-account").then((res) => {
                    if (res.data.status == 1) {
                        this.form.reset();
                        this.$toast.success(res.data.message);
                        $("#modalForm").modal("hide");
                        this.funGetData();
                    } else {
                        this.$toast.warning(res.data.message);
                    }
                }).catch((error) => {

                    console.log(error);
                })
            },
            funDelete(id) {
                this.$confirm({
                    message: 'Are you sure?',
                    button: {
                        no: 'No',
                        yes: 'Yes'
                    },
                    callback: confirm => {
                        if (confirm) {
                            this.form.delete("accounting/chart-of-account/" + id).then((res) => {
                                if (res.data.status == 1) {
                                    this.$toast.success(res.data.message);
                                    this.funGetData();
                                } else {
                                    this.$toast.warning(res.data.message);
                                }
                            })
                        }
                    }
                });
            },
        }
    }
</script>