<template>
<v-dialog v-model="dialog_confirm" persistent width="70vw">
    <v-card>
        <v-card-title class="headline grey lighten-2">KONFIRMASI PO JASA</v-card-title>
        <v-card-text style="max-height: 80vh; overflow-y: auto;">
            <!-- first row -->
            <v-layout row wrap>
                <v-flex xs4>
                    <v-text-field
                        v-model="formattedPODate" hide-details 
                        label="Tanggal PO" readonly outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs4 pl-2>
                    <v-text-field
                        v-model="selected_pojasa.SupplierName" hide-details 
                        label="Vendor" readonly outline 
                    ></v-text-field>
                </v-flex>
                <v-flex xs4 pl-2>
                    <v-text-field
                        v-model="selected_pojasa.reference" hide-details 
                        label="Referensi" readonly outline 
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- second row -->
            <v-layout row wrap mt-2>
                <v-flex xs4>
                    <v-text-field
                        v-model="selected_pojasa.M_BranchName" 
                        label="Cabang" readonly outline hide-details 
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-model="selected_pojasa.typepajak" hide-details 
                        label="Pajak (PPN)" readonly outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-if="selected_pojasa.typepajak == 'percent'"
                        v-model="selected_pojasa.valuepajak" hide-details 
                        label="Jumlah Pajak" readonly outline suffix="%"
                    ></v-text-field>
                    <v-text-field
                        v-else hide-details outline prefix="Rp."
                        v-model="formattedPajak" 
                        label="Jumlah Pajak" readonly 
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-model="selected_pojasa.typediskon" hide-details 
                        label="Diskon" readonly outline 
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-if="selected_pojasa.typediskon == 'percent'"
                        v-model="selected_pojasa.valuediskon" hide-details 
                        label="Jumlah Diskon" readonly outline suffix="%"
                    ></v-text-field>
                    <v-text-field
                        v-else hide-details outline prefix="Rp."
                        v-model="formattedDiskon" 
                        label="Jumlah Diskon" readonly 
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- third row -->
            <v-layout row wrap mt-2>
                <v-flex xs3>
                    <v-text-field
                        v-model="selected_pojasa.typekontrak" outline
                        label="Jenis Kontrak" readonly hide-details 
                    ></v-text-field>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="formattedKontrakStartDate" hide-details
                        label="Tanggal Mulai Kontrak" readonly outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="formattedKontrakEndDate" hide-details 
                        label="Tanggal Akhir Kontrak" readonly outline 
                    ></v-text-field>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="selected_pojasa.PIOutQty" hide-details 
                        label="Jumlah PI keluar" readonly outline 
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <v-textarea
                v-model="selected_pojasa.catatan" auto-grow readonly
                rows="1" outline label="Catatan" hide-details class="mt-2"
            ></v-textarea>

            <!-- detail table -->
            <v-layout row wrap class="mt-2">
                <v-flex xs12>
                    <v-data-table
                        :headers="headers" :items="selected_pojasa.detail" :expand="expand"
                        class="elevation-1" hide-actions item-key="M_ItemCode"
                    >
                        <template v-slot:items="props">
                        <tr @click="props.expanded = !props.expanded">
                            <td class="text-xs-center pa-2">
                                <v-icon>{{ props.expanded 
                                    ? "keyboard_arrow_down" 
                                    : "keyboard_arrow_right" 
                                }}</v-icon>
                            </td>
                            <td class="text-xs-center pa-2">{{ props.item.M_ItemDesc }}</td>
                            <td class="text-xs-center pa-2">{{ props.item.RequestQty }}</td>
                            <td class="text-xs-center pa-2">
                                Rp. {{ formatCurrency(props.item.SupplierPrice) }}
                            </td>
                            <td class="text-xs-center pa-2">
                                Rp. {{ formatCurrency(props.item.DiskonAmount) }}
                            </td>
                            <td class="text-xs-center pa-2">
                                Rp. {{ formatCurrency(props.item.EndPrice) }}
                            </td>
                            <td class="text-xs-center pa-2">
                                Rp. {{ formatCurrency(props.item.TempTotal) }}
                            </td>
                        </tr>
                        </template>

                        <template v-slot:expand="props">
                        <v-card flat>
                            <v-card-text>
                                <v-data-table
                                    :headers="headers_detail" class="elevation-0"
                                    :items="props.item.detail" hide-actions
                                >
                                    <template v-slot:items="obj">
                                    <tr>
                                        <td class="text-xs-center pa-2">{{ obj.item.M_ItemDesc }}</td>
                                        <td class="text-xs-center pa-2">{{ obj.item.PurchaseRequestNumber }}</td>
                                        <td class="text-xs-center pa-2">{{ obj.item.M_BranchName }}</td>
                                        <td class="text-xs-center pa-2">{{ obj.item.RequestQty }}</td>
                                        <td class="text-xs-center pa-2">{{ obj.item.ItemUnitName }}</td>
                                        <td class="text-xs-center pa-2">
                                            {{ formatCurrency(obj.item.SupplierPrice) }}
                                        </td>
                                    </tr>
                                    </template>
                                </v-data-table>
                            </v-card-text>
                        </v-card>
                        </template>

                        <template v-slot:footer>
                        <tr>
                            <td colspan="1" class="text-xs-left pa-2"></td>
                            <td colspan="5" class="text-xs-left pa-2">
                                <strong>JUMLAH</strong>
                            </td>
                            <td colspan="1" class="text-xs-center pa-2">
                                <strong>Rp. {{ formatCurrency(selected_pojasa.summary.subtotal) }}</strong>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="1" class="text-xs-left pa-2"></td>
                            <td colspan="5" class="text-xs-left pa-2">
                                <strong>DISKON</strong>
                            </td>
                            <td colspan="1" class="text-xs-center pa-2">
                                <strong>Rp. {{ formatCurrency(selected_pojasa.summary.diskon) }}</strong>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="1" class="text-xs-left pa-2"></td>
                            <td colspan="5" class="text-xs-left pa-2">
                                <strong>PAJAK</strong>
                            </td>
                            <td colspan="1" class="text-xs-center pa-2">
                                <strong>Rp. {{ formatCurrency(selected_pojasa.summary.pajak) }}</strong>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="1" class="text-xs-left pa-2"></td>
                            <td colspan="5" class="text-xs-left pa-2">
                                <strong>TOTAL</strong>
                            </td>
                            <td colspan="1" class="text-xs-center pa-2">
                                <strong>Rp. {{ formatCurrency(selected_pojasa.summary.total) }}</strong>
                            </td>
                        </tr>
                        </template>
                    </v-data-table>
                </v-flex>
            </v-layout>
        </v-card-text>
        <v-card-actions>
            <v-btn @click="viewAttachment()" color="info" class="white--text">Attachment</v-btn>
            <v-spacer></v-spacer>
            <v-btn @click="batalKonfirmasi()" color="error" class="white--text">Batal</v-btn>
            <v-btn v-if="!detail_view" @click="KonfirmasiJasa()" color="primary" class="white--text">
                {{ selected_pojasa.verifiedby == '0' ? "Verifikasi" : "Approve" }}
            </v-btn>
        </v-card-actions>
    </v-card>
</v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            expand: false,
            headers: [
                {
                    text: "", align: "center", sortable: false,
                    value: "item", width: "5%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false,
                    value: "item", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JUMLAH", align: "center", sortable: false,
                    value: "qty", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "HARGA", align: "center", sortable: false,
                    value: "price", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "DISKON", align: "center", sortable: false,
                    value: "disc", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "HARGA SETELAH DISKON", align: "center", sortable: false,
                    value: "disc", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL", align: "center", sortable: false,
                    value: "disc", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
            ],
            headers_detail: [
                {
                    text: "NAMA PRODUK", align: "center", sortable: false,
                    value: "prod", width: "25%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "NO REQUEST", align: "center", sortable: false,
                    value: "noreq", width: "20%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "REQUESTER", align: "center", sortable: false,
                    value: "req", width: "20%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "JUMLAH", align: "center", sortable: false,
                    value: "qty", width: "10%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "UNIT", align: "center", sortable: false,
                    value: "unit", width: "10%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "HARGA", align: "center", sortable: false,
                    value: "price", width: "15%", class: "pa-1 blue lighten-4 black--text",
                }
            ]
        }
    },
    computed: {
        loading_process() {
            return this.$store.state.poservice.loading_process
        },
        selected_pojasa() {
            return this.$store.state.poservice.selected_pojasa
        },
        formattedPODate() {
            return this.formatDate(this.selected_pojasa.podate)
        },
        formattedKontrakStartDate() {
            return this.formatDate(this.selected_pojasa.kontrakstart)
        },
        formattedKontrakEndDate() {
            return this.formatDate(this.selected_pojasa.kontrakend)
        },
        formattedPajak() {
            return this.formatCurrency(this.selected_pojasa.valuepajak)
        },
        formattedDiskon() {
            return this.formatCurrency(this.selected_pojasa.valuediskon)
        },
        detail_view() {
            return this.$store.state.poservice.detail_view
        },
        dialog_confirm: {
            get() {
                return this.$store.state.poservice.dialog_confirm
            },
            set(val) {
                this.$store.commit("poservice/update_dialog_confirm", val)
            }
        },
        snackbar: {
            get() {
                return this.$store.state.poservice.snackbar
            },
            set(val) {
                this.$store.commit("poservice/update_snackbar", val)
            }
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        formatCurrency(value) {
            const num = parseFloat(value);
            if (isNaN(num)) return 0;
            return num.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, ".");
        },
        viewAttachment() {
            let item = this.selected_pojasa
            let param = {
                TJasaID: item.T_KontrakJasaID, 
                poID: item.PurchaseOrderID
            }
            
            this.$store.dispatch("poservice/lookupAttachment", param)
            this.$store.commit("poservice/update_dialog_attachment", true)
        },
        batalKonfirmasi() {
            this.$store.commit("poservice/update_selected_pojasa", {
                summary: {
                    subtotal: 0.00,
                    diskon: 0.00,
                    pajak: 0.00,
                    total: 0.00
                }
            })
            this.dialog_confirm = false
        },
        KonfirmasiJasa() {
            if (!this.loading_process) {
                this.$store.dispatch("poservice/updateApprovePO");
            } else {
                let snac = {color: 'warning', msg: 'proses sedang berjalan', state: true}
                this.snackbar = snac
            }
        }
    }
}
</script>