<template>
    <div style="width: 100%;" class="pa-1">
        <v-card class="pa-2">
            <v-layout row>
                <v-flex xs class="pr-1">
                    <p class="mb-0">
                        Nomor: 
                        <strong>{{ selected_voucher.PaymentVoucherNumber ?? "-" }}</strong>
                    </p>
                    <p class="mb-0">
                        Sumber Dana: 
                        <strong>{{ selected_voucher.coaDescriptionSource ?? "-" }}</strong>
                    </p>
                    <p class="mb-0">
                        Tampungan Sementara: 
                        <strong>{{ selected_voucher.coaDescriptionTemporary ?? "-" }}</strong>
                    </p>
                </v-flex>
                <v-flex shrink class="pl-1">
                    <v-btn
                        v-if="selected_voucher.PaymentVoucherRealitationApproved == 'N'"
                        color="primary" outline 
                        @click="approveRealisasi()"
                    >
                        Verifikasi
                    </v-btn>
                    <v-btn v-else dark color="primary" @click="{}">Verfified</v-btn>
                </v-flex>
            </v-layout>

            <v-data-table 
                :items="list_detail_voucher.records" :headers="headers"
                class="elevation-1 mt-2" hide-actions
            >
                <template slot="items" slot-scope="props">
                    <tr>
                        <td class="pa-2 text-xs-center">
                            {{ props.index + 1 }}
                        </td>
                        <td class="pa-2 text-xs-center">
                            {{ props.item.PurchaseRequestDirectNumber }}
                        </td>
                        <td class="pa-2 text-xs-center">
                            Rp. {{ one_money(props.item.PaymentVoucherDetailTotal) }}
                        </td>
                        <td class="pa-2 text-xs-center">
                            Rp. {{ one_money(props.item.PurchaseRequestDirectAdjustment) }}
                        </td>
                        <td class="pa-2 text-xs-center">
                            <v-btn 
                                style="min-width: 25px; height: 25px; margin: 0;" outline
                                small color="blue lighten-2" @click="editDetailVoucher(props.item)"
                                :disabled="selected_voucher.PaymentVoucherRealitationApproved == 'Y'"
                            >
                                <v-icon small color="info">edit</v-icon>
                            </v-btn>
                        </td>
                    </tr>
                </template>
            </v-data-table>
            <div class="mt-2 text-xs-center">
                <v-pagination 
                    v-model="detail_table_page" :total-visible="10" 
                    :length="calcTableLength()"
                ></v-pagination>
            </div>
        </v-card>

        <dialog-edit></dialog-edit>

        <v-snackbar
            v-model="snackbar.isOpen" :timeout="3000" :multi-line="false"
            :vertical="false" :top="true" :color="snackbar.color"
        >
            {{ snackbar.message }}
            <v-btn class="px-2" flat @click="snackbar.isOpen = false">Tutup</v-btn>
        </v-snackbar>
    </div>
</template>

<script>

module.exports = {
    components: {
        'dialog-edit': httpVueLoader('./adjustment_form.vue'),
    },
    data() {
        return {
            headers: [
                {
                    text: "NO", align: "center", sortable: false,
                    value: "no", width: "10%", class: "blue lighten-3 white--text",
                },
                {
                    text: "NOMOR REQUEST", align: "center", sortable: false, 
                    value: "supplier", width: "35%", class: "blue lighten-3 white--text",
                },
                {
                    text: "TOTAL", align: "center", sortable: false,
                    value: "total", width: "20%", class: "blue lighten-3 white--text",
                },
                {
                    text: "TAMBAHAN", align: "center", sortable: false,
                    value: "total", width: "20%", class: "blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false,
                    value: "status", width: "15%", class: "blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        selected_voucher() {
            return this.$store.state.voucher.selected_voucher;
        },
        list_detail_voucher() {
            return this.$store.state.voucher.list_detail_voucher;
        },
        detail_table_page: {
            get() {
                return this.$store.state.voucher.detail_table_page;
            },
            set(val) {
                this,$store.commit("voucher/update_detail_table_page", val);
                this.$store.dispatch("voucher/searchDetail");
            }
        },
        snackbar: {
            get() {
                return this.$store.state.voucher.snackbar
            },
            set(val) {
                this.$store.commit("voucher/update_snackbar", val);
            }
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split("-");
            return `${day}-${month}-${year}`;
        },
        one_money(value) {
            let splitValue = value.toString().split(".");

            if(splitValue.length > 1) {
                let val = splitValue[0]
                return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".") + "," + this.roundtoThreeDecimal(splitValue[1]);
            } else {
                return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
            }
        },
        roundtoThreeDecimal(digit) {
            const num = parseInt(digit, 10)
            if (isNaN(num) || num <= 0) {
                return '00';
            }

            const scale = 10 ** digit.length;
            const decimal = num / scale;
            return Math.round(decimal * 100);
        },
        calcTableLength() {
            const total = this.list_detail_voucher.total;
            if (total == undefined || total == 0) return 1;
            return Math.ceil(total/10);
        },
        editDetailVoucher(item) {
            this.$store.commit("voucher/update_selected_detail_voucher", item)
            this.$store.commit("voucher/update_dialog_adjustment", true);
        },
        approveRealisasi() {
            this.$store.dispatch("voucher/approveRealisasi")
        }
    },
}
</script>