<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('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="mb-3">
                        <button type="button" @click="exportExcell" class="btn w-100 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("Type Collateral") }}</th>
                                <th>{{ $t("Collateral Name") }}</th>
                                <th>{{ $t("Collateral Number") }}</th>
                                <th>{{ $t("Status") }}</th>
                                <th class="d-print-none">{{ $t("Action") }}</th>
                            </tr>
                        </thead>
                        <tbody class="text-nowrap">
                            <tr v-for="(value,index) in getCollaterals.data">
                                <td>{{ index+1 }}</td>
                                <td>{{ value.loan_code }}</td>
                                <td>{{ value.client_name }}</td>
                                <td>{{ value.pawn_type }}</td>
                                <td>{{ value.collateral_name }}</td>
                                <td>{{ value.number }}</td>
                                 <td :class="value.status==1?'text-danger':'text-success'">
                                    {{ value.status==1?'បានដាក់ធានា':'បានដកពីធានា' }}
                                </td>
                                <td class="d-print-none">
                                    <button v-show="value.status==1" type="button" :title="$t('Add')" @click="funSub(value.id)" class="btn btn-danger s-btn-xs">
                                        <i class="fa-solid fa-minus fa-sm"></i>
                                    </button>
                                    <button :disabled="true" v-show="value.status==2" type="button" :title="$t('Sub')" class="btn btn-success s-btn-xs">
                                        <i class="fa-solid fa-check-to-slot fa-sm"></i>
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <footer-report style="display: none;"></footer-report>
                </div>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="getCollaterals" @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: [],
                form: new Form({

                }),
                params: {
                    search_text: "",
                    page: 1,
                    per_page: 25,
                    check: "",
                    collateral_id: "",
                    client_id: [],
                },
            };
        },
        created() {
            this.funGetData();
            this.getClient();
        },

        methods: {
             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)
                }
            },
            getClient() {
                axios
                    .get("admin-api/com-client")
                    .then((res) => {
                        this.clients = 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');
            },
        },
    };
</script>
