<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Collateral Report') }}
        </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 mode="tags" :close-on-select="false" v-model="params.collateral_id" :placeholder="$t('Collateral')" trackBy="title" value="id" label="title" :searchable="true" :options="collaterals" />
                    </div>
                    <ul class="list-group mb-3">
                        <button type="button" class="btn btn-success" @click="funSearchData"><i class="fa fa-search"></i> {{ $t('Search') }}</button>
                    </ul>
                    <ul class="list-group mb-3">
                        <button type="button" @click="printP" class="btn btn-primary"><i class="fa fa-print"></i> {{ $t('Print') }}</button>
                    </ul>
                    <ul class="list-group mb-3">
                        <button type="button" @click="exportExcell" class="btn btn-danger"><i class="fa fa-file-export"></i> {{ $t('Export') }}</button>
                    </ul>
                </div>
            </div>
        </template>

        <div slot="body">
            <div id="show-print">
                <div class="table-responsive table_scroll report_print">
                    <header-report :title="$t('Collateral Report')" 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("Collateral Number") }}</th>
                                <th>{{ $t("Client Name") }}</th>
                                <th>{{ $t("Type Collateral") }}</th>
                                <th>{{ $t("Collateral Name") }}</th>
                                <th>{{ $t("Price Market") }}</th>
                                <th>{{ $t("Collateral Date") }}</th>
                                <th>{{ $t("Status") }}</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.number }}</td>
                                <td>{{ value.client_name }}</td>
                                <td>{{ value.pawn_type }}</td>
                                <td>{{ value.collateral_name }}</td>
                                <td>{{ value.price_market | numberFomartValue }}</td>
                                <td>{{ value.installment_date | onlyDate }}</td>
                                 <td :class="value.status==1?'text-danger':'text-success'">
                                    {{ value.status==1?'បានដាក់ធានា':'បានដកពីធានា' }}
                                </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 {
                getDatas: { data: [] },
                clients: [],
                getCollaterals: [],
                // coes: [],
                collaterals: [],
                form: new Form({

                }),
                params: {
                    search_text: "",
                    page: 1,
                    per_page: 25,
                    pagin:false,
                    collateral_id: [],
                    client_id: [],
                    co_id: [],
                },
            };
        },
        created() {
            this.funGetCollateral();
            this.getClient();
            this.funGetData();
            // this.getCO();
        },
        methods: {
            funGetCollateral() {
                axios
                    .get("admin-api/loan-has-collateral")
                    .then((res) => {
                        this.collaterals = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            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);
                        }
                    });
            },
            funSearchData(){
                this.params.pagin = 'true';
                this.funGetData();
            },
            printP() {
                this.$htmlToPaper("show-print");
            },
            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },

            async exportExcell() {
                this.$isLoading(true)
                try {
                    await exportTableToExcel({
                        tableId: "excel-table",
                        filename: "ExportCollateral",
                        reportTitle: "របាយការណ៍ទ្រព្យធានា",
                        subDate:this.params.date,
                    })
                } catch (e) {
                    this.$toast.warning(e.message);
                } finally {
                    this.$isLoading(false)
                }
            },
          
        },
    };
</script>
