<template>
    <v-dialog v-model="dialog_detail_view" width="80vw" persistent>
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                RECEIVE ITEM INVENTORY
                <v-spacer></v-spacer>
            </v-card-title>

            <v-card-text>
                <v-layout row wrap>
                    <!-- first row -->
                    <v-flex xs6>
                        <v-menu v-model="menuReceiveDateForm" lazy offset-y transition="scale-transition"
                            max-width="290px" min-width="290px" :nudge-right="40" :close-on-content-click="false">
                            <template v-slot:activator="{ on }">
                                <v-text-field :Value="receiveDateForm" label="Tanggal" class="mr-2" hide-details outline
                                    readonly></v-text-field>
                            </template>
                            <v-date-picker no-title hide-details v-model="selected_ro.ReceiveOrderPoIDate"
                                @input="menuReceiveDateForm = false" readonly></v-date-picker>
                        </v-menu>
                    </v-flex>
                    <v-flex xs6>
                        <v-autocomplete :items="list_supplier" :value="selected_ro.SupplierID" item-text="SupplierName"
                            item-value="SupplierID" class="mr-2" label="Supplier" outline hide-details
                            readonly></v-autocomplete>
                    </v-flex>
                    <!-- <v-flex xs4>
                        <v-text-field label="Nomor Delivery Order (DO)" class="" readonly outline
                            v-model="selected_ro.ReceiveOrderPoDONumber" hide-details></v-text-field>
                    </v-flex> -->

                    <!-- second row -->
                    <v-flex xs6>
                        <v-text-field label="Referensi" class="mr-2" outline readonly hide-details
                            v-model="selected_ro.ReceiveOrderPoRefNumber" hide-details></v-text-field>
                    </v-flex>
                    <v-flex xs6>
                        <v-autocomplete :items="list_warehouse" :value="selected_ro.WarehouseID"
                            item-text="WarehouseName" item-value="WarehouseID" label="Gudang" outline hide-details
                            readonly></v-autocomplete>
                    </v-flex>

                    <!-- third row -->
                    <v-flex xs6>
                        <v-autocomplete :value="selected_ro.M_RuanganID" class="mr-2 mt-3" :items="list_lokasi" outline
                            item-value="M_RuanganID" item-text="M_RuanganName" label="Lokasi*" hide-details
                            readonly></v-autocomplete>
                    </v-flex>
                    <v-flex xs6>
                        <v-text-field label="Biaya Ekspedisi" class="mt-3" outline readonly
                            v-model="selected_ro.ReceiveOrderShippingCostAmount" hide-details></v-text-field>
                    </v-flex>

                    <!-- four row -->
                    <v-flex xs12>
                        <v-textarea v-model="selected_ro.ReceiveOrderPoNote" hide-details outline auto-grow rows="1"
                            label="Catatan" class="mt-3" readonly></v-textarea>
                    </v-flex>

                    <!-- shipping info -->
                    <v-flex xs12 class="mt-3" v-if="shipping_info.length > 0">
                        <v-layout row wrap>
                            <v-flex v-for="shp in shipping_info" :key="shp.num_po" xs3>
                                <v-card class="pa-2 ma-2">
                                    <div>
                                        <span class="font-weight-bold">{{ shp.num_po }}</span><br />
                                        <span>Rp. {{ oneMoney(shp.cost) }}</span><br />
                                        <span v-if="shp.isUpfront === 'Y'" class="green--text">
                                            Biaya Ekspedisi sudah di bayar
                                        </span>
                                        <span v-else class="red--text">
                                            Biaya Ekspedisi belum di bayar
                                        </span>
                                    </div>
                                </v-card>
                            </v-flex>
                        </v-layout>
                    </v-flex>

                    <!-- list item received -->
                    <v-flex xs12 class="my-3">
                        <v-data-table v-if="form_receive_item.length > 0" :headers="headers" :items="form_receive_item"
                            hide-actions item-key="keyID" class="elevation-1">
                            <template v-slot:items="props">
                                <tr>
                                    <td class="text-xs-left">{{ props.item.PurchaseOrderNumber }}</td>
                                    <td class="text-xs-left">
                                        <p class="p-0 mb-0">{{ props.item.M_ItemCode }}</p>
                                        <p class="p-0 mb-0">{{ props.item.M_ItemDesc }}</p>
                                        <p class="p-0 mb-0">{{ props.item.WarehouseName }}</p>
                                    </td>
                                    <td class="text-xs-center">{{ props.item.qty }} {{ props.item.ItemUnitName }}</td>
                                    <td class="text-xs-center">
                                        <div v-if="props.item.M_ItemItem_CategoryID == 2">
                                            <p class="mb-0 pb-0" v-for="batch in props.item.batchlist">
                                                Qty: {{ batch.qty || '' }}
                                            </p>
                                        </div>
                                    </td>
                                    <td class="text-xs-center"></td>
                                </tr>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-card-text>

            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="info" flat outline @click="closeRO()">Tutup</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            menuReceiveDateForm: false,
            headers: [
                {
                    text: "PO NUMBER", align: "center", sortable: false, value: "ItemUnitName",
                    width: "20%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false, value: "M_ItemDesc",
                    width: "30%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY", align: "center", sortable: false, value: "qty",
                    width: "10%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "No. Batch & Exp.Date", align: "center", sortable: false, value: "qty",
                    width: "25%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false, value: "qty",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        dialog_detail_view: {
            get() {
                return this.$store.state.ro.dialog_detail_view;
            },
            set(val) {
                this.$store.commit("ro/update_dialog_detail_view", val);
            }
        },
        receiveDateForm() {
            return this.formatDate(this.selected_ro.ReceiveOrderPoIDate)
        },
        form_receive: {
            get() {
                return this.$store.state.form.form_receive;
            },
            set(val) {
                this.$store.commit("form/update_form_receive", val)
            }
        },
        form_receive_item: {
            get() {
                return this.$store.state.form.form_receive_item;
            },
            set(val) {
                this.$store.commit("form/update_form_receive_item", val)
            }
        },
        selected_ro() {
            return this.$store.state.ro.selected_ro
        },
        list_supplier() {
            return this.$store.state.ro.list_supplier
        },
        list_warehouse() {
            return this.$store.state.ro.list_warehouse
        },
        shipping_info() {
            return this.$store.state.form.shipping_info
        },
        list_lokasi() {
            return this.$store.state.ro.list_lokasi
        },
    },
    methods: {
        checkObjectEmpty(obj) {
            if (obj === null || obj === undefined || typeof obj !== "object") {
                return true;
            }
            return Object.keys(obj).length === 0 && obj.constructor === Object;
        },
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split("-");
            return `${day}-${month}-${year}`;
        },
        oneMoney(value) {
            let cleanValue = String(value).replace(/\D/g, '');
            const num = parseInt(cleanValue, 10);
            if (isNaN(num)) {
                return '';
            }
            return num.toLocaleString('id-ID');
        },
        pilihItemRO() {
            let temp = this.form_receive_item
            if (temp.length > 0) {
                this.$store.commit("form/update_select_item_temp", temp);
            }

            this.$store.dispatch("form/getPurchaseOrder");
            this.$store.commit("form/update_dialog_select_item", true);
        },
        removeItem(props) {
            let temp = JSON.parse(JSON.stringify(this.form_receive_item));
            let newt = temp.filter((item) => item.PoItemReceiveID !== props.item.PoItemReceiveID);

            this.form_receive_item = newt;
        },
        closeRO() {
            this.$store.dispatch("form/resetForm");
            this.$store.commit("ro/update_selected_ro", {});
            this.dialog_detail_view = false;
        },
    },
}
</script>
