<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Collaterals') }}
        </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('Client') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.client_id" trackBy="name" value="id" label="name" :searchable="true" :options="clients" />
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Co') }}</label>
                        <Multiselect mode="tags" :close-on-select="false" v-model="params.co_id" trackBy="name" value="id" label="name" :searchable="true" :options="coes" />
                    </div>
                    <!-- <div class="mb-3">
                        <label class="form-label">{{ $t('Collateral') }}</label>
                        <Multiselect v-model="params.collateral_id" :placeholder="$t('Collateral')" label="title" :searchable="true" :options="getCollaterals" />
                    </div> -->
                    <div class="mb-3">
                        <label class="form-label">{{ $t('Loan Code') }}</label>
                        <input type="text" class="form-control" v-model="params.search_text" />
                    </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('Collaterals')" style="display: none;"></header-report>
                    <table class="table table-bordered" id="excel-table">
                        <thead class="text-nowrap fixed_table">
                            <tr>
                                <th>{{ $t("#") }}</th>
                                <th>{{ $t("Loan Code") }}</th>
                                <th>{{ $t("Client Name") }}</th>
                                <th>{{ $t("Collateral Number") }}</th>
                                <th>{{ $t("Type Collateral") }}</th>
                                <th>{{ $t("Collateral Name") }}</th>
                                <th>កាលបរិច្ឆេទដាក់ទ្រព្យ</th>
                                <th>កាលបរិច្ឆេទដកទ្រព្យ</th>
                                <th>{{ $t("Status") }}</th>
                                <th>{{ $t("Branch") }}</th>
                                <th class="d-print-none" v-show="user_branch != -1">{{ $t("Action") }}</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <template v-for="(value,index) in getCollaterals.data">
                                <template v-for="(sub,sub_index) in value.CollateralDetailResource">
                                    <tr>
                                        <td v-if="sub_index === 0" :rowspan="value.CollateralDetailResource.length">{{ index + 1 }}</td>
                                        <td v-if="sub_index === 0" :rowspan="value.CollateralDetailResource.length">{{value.loan_code}} </td>
                                        <td v-if="sub_index === 0" :rowspan="value.CollateralDetailResource.length">{{value.client_name}}</td>
                                        <td class="text-nowrap">{{sub.number}}</td>
                                        <td class="text-nowrap">{{sub.cat_name}}</td>
                                        <td class="text-nowrap">{{sub.collateral_name}}</td>
                                        <td>{{ sub.created_at |onlyDate }}</td>
                                        <td v-if="sub.status == 2">{{ sub.updated_at |onlyDate }}</td><td v-else></td>
                                        <td :class="sub.status==1?'text-danger':'text-success'">{{ sub.status==1?'ដាក់ធានា':'បានដក' }}</td>
                                        <td>{{ value.branch_name}}</td>
                                        <td class="d-print-none" v-show="user_branch != -1">
                                            <button v-if="sub.status==1" type="button" :title="$t('Add')" @click="funSub(sub.id)" class="btn btn-danger s-btn-xs" :disabled="sub.loan_status_id == 2">
                                                <i class="fa-solid fa-minus fa-sm"></i>
                                            </button>
                                            <button v-else type="button" :title="$t('Sub')" class="btn btn-success s-btn-xs" disabled>
                                                <!-- <i class="fa-solid fa-add fa-sm"></i> -->
                                                <i class="fa-solid fa-check-to-slot fa-sm"></i>
                                            </button>
                                        </td>
                                    </tr>
                                </template>
                            </template>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="collaterals" @onChangePage="funGetData($event)"></cumstom-pagination>
        </div>
    </theme-table>
</template>

<script>
    import CumstomPagination from "../../components/CustomPagination.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: {
            CumstomPagination,
            HeaderReport,
            FooterReport,
            ThemeTable
        },
        data() {
            return {
                collaterals: { data: [] },
                getCollaterals: [],
                clients: [],
                coes: [],
                user_branch  : '',
                form: new Form({

                }),
                params: {
                    search_text: "",
                    page: 1,
                    per_page: 25,
                    check: "",
                    collateral_id: "",
                    client_id: [],
                    co_id: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getClient();
            this.funComUserBranch();
            this.getCO();
        },
        methods: {
            getClient() {
                axios
                    .get("admin-api/com-client")
                    .then((res) => {
                        this.clients = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            getCO() {
                axios
                    .get("admin-api/com-co")
                    .then((res) => {
                        this.coes = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios.get("admin-api/collateral-list", {
                        params: this.params,
                    }).then((res) => {
                        this.getCollaterals = 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();
            },
            funSub(id) {
                this.$confirm({
                    message: "Are you sure?",
                    button: {
                        no: "No",
                        yes: "Yes",
                    },
                    callback: (confirm) => {
                        if (confirm) {
                            this.params.check = 2;
                            this.form
                                .put("admin-api/collaterals/" + id, {
                                    params: this.params,
                                })
                                .then((res) => {
                                    if (res.data.status == 1) {
                                        this.$toast.success(res.data.message);
                                        this.funGetData();
                                    } else {
                                        this.$toast.warning(res.data.message);
                                    }
                                });
                        }
                    },
                });
            },
            // funAdd(id) {
            //     this.$confirm({
            //         message: "Are you sure?",
            //         button: {
            //             no: "No",
            //             yes: "Yes",
            //         },
            //         callback: (confirm) => {
            //             if (confirm) {
            //                 this.params.check = 1;
            //                 this.form
            //                     .put("admin-api/collaterals/" + id, {
            //                         params: this.params,
            //                     })
            //                     .then((res) => {
            //                         if (res.data.status == 1) {
            //                             this.$toast.success(res.data.message);
            //                             this.funGetData();
            //                         } else {
            //                             this.$toast.warning(res.data.message);
            //                         }
            //                     });
            //             }
            //         },
            //     });
            // },
            printP() {
                this.$htmlToPaper('show-print');
            },
            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "CollateralList",
                        reportTitle: "ប្រភេទទ្រព្យធានា",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            }
        },
    };
</script>
