<template>

    <div class="modal fade" id="showDisbursementVoucher" aria-hidden="true" role="dialog" data-backdrop="static">
        <div class="modal-dialog modal-xl modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title"></h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-span="Close"></button>
                </div>
                <div class="modal-body">
                    <div id="print-disbursement-voucher">
                        <div v-for="index in 2" :key="index" :class="customClass(index)">
                            <div class="custom-reciept">
                                <div class="header-receipt mb-2">
                                    <img src="images/logo.png" alt="" width="70px" class="position-absolute" style="top: 8px">
                                    <div class="text-center py-3">
                                        <h6 class="mb-1"><u class="text_moul">បង្កាន់ដៃបើកប្រាក់</u></h6>
                                        <h6><strong>(Loan Disburement Voucher)</strong></h6>
                                    </div>
                                </div>
                                <div class="row mb-2">
                                    <div class="col-6">
                                        <table>
                                            <tr>
                                                <td>*ឈ្មោះអ្នកខ្ចីប្រាក់</td>
                                                <th class="text-primary ps-3">{{ obj.client?.name }}</th>
                                            </tr>
                                            <tr>
                                                <td>*ឈ្មោះអ្នករួមខ្ចីប្រាក់</td>
                                                <th class="text-primary ps-3">{{ obj.garuantor1?.name || '............' }}</th>
                                            </tr>
                                        </table>
                                    </div>
                                    <div class="col-6">
                                        <table>
                                            <tr>
                                                <td>*កាលបរិច្ឆេទបើកប្រាក់កម្ចី</td>
                                                <th class="text-primary ps-3">{{ obj.loan?.installment_date | onlyDate }}</th>
                                            </tr>
                                            <tr>
                                                <td>*លេខសម្គាល់អតិថិជន (CID)</td>
                                                <th class="text-primary ps-3">{{ obj.client?.code}}</th>
                                            </tr>
                                        </table>

                                    </div>
                                </div>


                                <table class="table text-center table-custom mb-1">
                                    <tbody>
                                        <tr class="table-warning">
                                            <td>ល.រ</td>
                                            <td>បរិយាយ</td>
                                            <td>ចំនួនទឹកប្រាក់</td>
                                        </tr>
                                        <tr>
                                            <td>1</td>
                                            <td class="text-start">ចំនួនទឹកប្រាក់កម្ចី (Loan Amount)</td>
                                            <td>{{ obj.loan?.loan_out | numberFomartValue }} </td>
                                        </tr>
                                        <tr>
                                            <td>2</td>
                                            <td class="text-start">សេវារដ្ឋបាល ( Admin Fee) & សេវាធានារ៉ាប់រងកម្ចី (Loan Insurance)</td>
                                            <td> {{ totalSum | numberFomartValue}} </td>
                                        </tr>
                                        <tr>
                                            <td>3</td>
                                            <td class="text-start">ទឹកប្រាក់ដែលនៅជំពាក់ស្នើសុំកាត់កង</td>
                                            <td></td>
                                        </tr>
                                        <tr>
                                            <td>4</td>
                                            <td class="text-start">ចំនួនទឹកប្រាក់ផ្តល់កម្ចី(Loan Disbursement Amount)</td>
                                            <td>{{ obj.loan?.amount_client_receive | numberFomartValue }}</td>
                                        </tr>
                                        <tr>
                                            <td>5</td>
                                            <td class="text-start" colspan="2">ចំនួនទឹកប្រាក់ជាអក្សរ ({{ totalToword(obj.loan?.amount_client_receive, obj.loan?.currency_id) }})</td>
                                        </tr>

                                    </tbody>
                                </table>

                                <div class="row text-center">
                                    <div class="col-6">
                                        <p>ហត្ថលេខា និងឈ្មោះ</p>
                                        <p>មន្រ្តីឥណទាន/បេឡាធិការ</p>
                                        <br>
                                        <br>
                                        <br>
                                        <br>
                                        <p>{{ obj.loan?.co_name }}</p>
                                    </div>
                                    <div class="col-6">
                                        <p>ស្នាមមេដៃ និងឈ្មោះ</p>
                                        <p>អ្នកទទួលប្រាក់</p>
                                        <br>
                                        <br>
                                        <br>
                                        <br>
                                        <p>{{ obj.client?.name }}</p>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-bs-dismiss="modal"> {{ $t("Close") }}</button>
                    <button type="button" class="btn btn-primary" @click="printP">
                        {{$t('Print')}}
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    var moment = require("moment");
    import {currencyToWord } from "number_to_khmer_word";
    export default {
        props:['obj'],
        data(){
            return{
                company: '',
                moment : moment,
                currencyToWord : currencyToWord,
            }
        },
        created() {
            this.funGetCompany();
        },
        computed: {
            totalSum() {
                return (Number(this.obj.loan?.total_insurance) || 0) + (Number(this.obj.loan?.total_service) || 0);
            }
        },
        methods: {
            customClass(index) {
                if (index === 1) return 'custom-reciept-top';
                if (index === 2) return 'custom-reciept-bottom';
                return '';
            },
            funGetCompany(){
                axios.get('admin-api/company')
                .then((res) => {
                    this.company = res.data;
                });
            },
            funDate(date) {
                if(date){
                    return moment(date).format("DD") + "/" + this.funMonth(moment(date).format("MMM")) + "/" + moment(date).format("YYYY");
                } else {
                    return 'ថ្ងៃទី................ ខែ................ ឆ្នាំ................'
                }
            },
            funMonth(mon) {
                if (mon == "Jan") {
                    return "មករា";
                } else if (mon == "Feb") {
                    return "កុម្ភៈ";
                } else if (mon == "Mar") {
                    return "មីនា";
                } else if (mon == "Apr") {
                    return "មេសា";
                } else if (mon == "May") {
                    return "ឧសភា";
                } else if (mon == "Jun") {
                    return "មិថុនា";
                } else if (mon == "Jul") {
                    return "កក្កដា";
                } else if (mon == "Aug") {
                    return "សីហា";
                } else if (mon == "Sep") {
                    return "កញ្ញា";
                } else if (mon == "Oct") {
                    return "តុលា";
                } else if (mon == "Nov") {
                    return "វិច្ឆិកា";
                } else if (mon == "Dec") {
                    return "ធ្នូ";
                }
            },
            totalToword(total_amount_receive,currency_id) {
                if (currency_id == 1) {
                    if (total_amount_receive) {
                        return currencyToWord(total_amount_receive, "KHR")
                            .slice(1)
                            .replace(/-/g, "")
                            .replace(/ រៀល គត់/, "រៀលគត់");
                    } else {
                        return currencyToWord(0, "KHR").slice(1);
                    }
                } else {
                    if (total_amount_receive) {
                        return currencyToWord(total_amount_receive, "USD")
                            .slice(1)
                            .replace(/-/g, " ")
                            .replace(/ ដុល្លារ គត់/, " ដុល្លារសហរដ្ឋអាមេរិចគត់");
                    } else {
                        return currencyToWord(0, "USD").slice(1);
                    }
                }
            },
            printP() {
                this.$htmlToPaper("print-disbursement-voucher", {
                    styles: ["/css/app.css", "/css/print/print-reciept.css"],
                });
            },
        }
    }
</script>
<style lang="scss" scoped>
    .custom-reciept{
        width:210mm;
        min-height: 148mm;
        margin: auto;
        padding: 30px;
        border: 1px solid #dee2e6;
    }
    table.table-custom tr td{
        border: 1px solid #000;
        padding: 2px 0px;
    }
</style>
