<template>
    <v-dialog v-model="dialog_receive_aset" persistent width="70vw">
        <v-card class="style-limit-heightcard">
            <v-card-title class="headline grey lighten-2">FORM TERIMA ASET</v-card-title>
            <v-card-text class="style-limit-heightcontent">
                <v-layout row wrap>
                    <v-flex xs4 pa-1>
                        <v-menu offset-y lazy min-width="290px" transition="scale-transition" max-width="290px"
                            v-model="menuROdate" full-width :nudge-right="40" :close-on-content-click="false">
                            <template v-slot:activator="{ on }">
                                <v-text-field label="Tanggal RO" hide-details readonly v-on="on" outline
                                    v-model="formattedRODate"></v-text-field>
                            </template>
                            <v-date-picker no-title @input="menuROdate = false" :readonly="readonly_mode"
                                v-model="form_receive.date"></v-date-picker>
                        </v-menu>
                    </v-flex>
                    <v-flex xs4 pa-1>
                        <v-autocomplete :items="list_supplier" hide-details outline label="Vendor"
                            v-model="form_receive.supplier" item-text="SupplierName" :readonly="readonly_mode"
                            item-value="SupplierID"></v-autocomplete>
                    </v-flex>
                    <v-flex xs4 pa-1>
                        <v-text-field hide-details outline placeholder="Nomor Delivery / Supplier Invoice"
                            v-model="form_receive.reference" label="Referensi" :readonly="readonly_mode"
                        ></v-text-field>
                    </v-flex>
                </v-layout>

                <v-layout row wrap>
                    <v-flex xs4 pa-1>
                        <v-text-field hide-details outline label="No PO" placeholder="nomor po"
                            v-model="form_receive.ponumber" readonly></v-text-field>
                    </v-flex>
                    <v-flex xs2 pa-1>
                        <v-autocomplete
                            :items="list_cabang" hide-details outline
                            v-model="form_receive.branchID" label="Cabang"
                            item-text="M_BranchName" item-value="M_BranchID"
                            @change="onChangeCabang" :readonly="readonly_mode"
                        ></v-autocomplete>
                    </v-flex>
                    <v-flex xs2 pa-1>
                        <v-autocomplete
                            :items="list_gudang" hide-details outline
                            v-model="form_receive.gudangID" label="Gudang"
                            item-text="WarehouseName" item-value="WarehouseID"
                            :readonly="readonly_mode"
                        ></v-autocomplete>
                    </v-flex>
                    <v-flex xs4 pa-1>
                        <v-text-field hide-details outline label="Biaya Pengiriman" placeholder="0"
                            v-model="form_receive.shipping" :readonly="readonly_mode"></v-text-field>
                    </v-flex>
                </v-layout>

                <v-layout row wrap>
                    <v-flex xs12 pa-1>
                        <v-textarea auto-grow rows="1" outline label="Catatan" hide-details
                            v-model="form_receive.catatan" :readonly="readonly_mode"></v-textarea>
                    </v-flex>
                    <v-flex xs3 pa-1 mt-1>
                         <v-btn block color="info" @click="pickKontrakAset()"
                            :disabled="isFormEmpty()" v-show="!readonly_mode">
                            PILIH ORDER ASSET
                        </v-btn>
                    </v-flex>
                </v-layout>

                <v-layout row wrap class="mt-3">
                     <v-flex xs12 v-if="order_received.length > 0">
                        <v-data-table :headers="headers" :items="order_received"
                            item-key="PODetailID" class="elevation-1" hide-actions>
                            <template v-slot:items="props">
                                <tr>
                                    <td class="text-xs-center pa-1">
                                        <v-chip label small>{{ props.item.M_ItemCode }}</v-chip> <br>
                                        {{ props.item.M_ItemDesc }}
                                    </td>
                                    <td class="text-xs-center pa-1">
                                        {{ props.item.qty }}
                                    </td>
                                    <td class="text-xs-center pa-1">
                                        {{ props.item.ItemUnitName }}
                                    </td>
                                    <td class="text-xs-center pa-1">
                                        <div v-if="props.item.inspeksi">
                                            <p v-if="props.item.inspeksi.summary == 'accept'" class="mb-0">
                                                Diterima
                                            </p>
                                            <p v-else-if="props.item.inspeksi.summary == 'reject'" class="mb-0">
                                                Ditolak <br>
                                                {{ props.item.inspeksi.catatan }}
                                            </p>
                                            <p v-else class="mb-0">
                                                Diterima dengan catatan <br>
                                                {{ props.item.inspeksi.catatan }}
                                            </p>
                                        </div>
                                    </td>
                                    <td class="text-xs-center pa-1">
                                        <v-tooltip bottom pr-2>
                                            <template v-slot:activator="{ on }">
                                                <v-btn
                                                    color="green" dark @click="openInspeksi(props.item)"
                                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                                >
                                                    <v-icon small>note_add</v-icon>
                                                </v-btn>
                                            </template>
                                            <span>Inspeksi Jasa</span>
                                        </v-tooltip>

                                        <v-tooltip bottom v-show="!readonly_mode">
                                            <template v-slot:activator="{ on }">
                                                <v-btn
                                                    color="warning" dark @click="cancelOrder(props.item)"
                                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                                >
                                                    <v-icon small>cancel</v-icon>
                                                </v-btn>
                                            </template>
                                            <span>Batal Order</span>
                                        </v-tooltip>
                                    </td>
                                </tr>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn @click="batalReceiveOrder()" color="error" class="white--text">Batal</v-btn>
                <v-btn :disabled="readonly_mode" @click="simpanReceivedOrder()" color="primary" class="white--text">Simpan</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<style scoped>
.style-limit-heightcard {
  max-height: 60vh;
  display: flex;
  flex-direction: column;
}

.style-limit-heightcontent {
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}
</style>

<script>
module.exports = {
    data() {
        return {
            menuROdate: false,
            headers: [
                {
                    text: "ITEM", align: "center", sortable: false, value: "item",
                    width: "40%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "QTY", align: "center", sortable: false, value: "qty",
                    width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "UNIT ITEM", align: "center", sortable: false, value: "unit",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "INSPEKSI", align: "center", sortable: false, value: "inspec",
                    width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false, value: "act",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        dialog_receive_aset: {
            get() {
                return this.$store.state.orderAsset.dialog_receive_aset
            },
            set(val) {
                this.$store.commit("orderAsset/update_dialog_receive_aset", val)
            }
        },
        form_receive: {
            get() {
                return this.$store.state.orderAsset.form_receive
            },
            set(val) {
                this.$store.commit("orderAsset/update_form_receive", val)
            }
        },
        order_received: {
            get() {
                return this.$store.state.orderAsset.order_received
            },
            set(val) {
                this.$store.commit("orderAsset/update_order_received", val)
            }
        },
        readonly_mode() {
            return this.$store.state.orderAsset.readonly_mode
        },
        formattedRODate() {
            return this.formatDate(this.form_receive.date);
        },
        list_cabang() {
            return this.$store.state.orderAsset.list_cabang;
        },
        list_gudang() {
            return this.$store.state.orderAsset.list_gudang;
        },
        list_supplier() {
            return this.$store.state.orderAsset.list_supplier;
        },
        loading_process() {
            return this.$store.state.roasset.loading_process;
        }
    },
    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, ".");
        },
        onChangeCabang(branchID) {
            if (!branchID) return;
            this.$store.dispatch("orderAsset/listingGudang", branchID);
        },
        isFormEmpty() {
            const form = this.form_receive;
            if (form.supplier == "" || form.gudangID == "") return true;
            return false;
        },
        pickKontrakAset() {
            this.$store.dispatch("orderAsset/searchOrderAsset", "");
            this.$store.commit("orderAsset/update_dialog_pick_contract", true);
        },
        openInspeksi(item) {
            const selectedItem = JSON.parse(JSON.stringify(item));
            const formInspeksi = item.inspeksi
                ? JSON.parse(JSON.stringify(item.inspeksi))
                : {
                    qty_po: item.qty || 0,
                    qty_ro: item.qty || 0,
                    date_po: moment(new Date()).format("YYYY-MM-DD"),
                    date_ro: moment(new Date()).format("YYYY-MM-DD"),
                    condt_kemasan: "",
                    catatan_kemasan: "",
                    condt_pengiriman: "",
                    catatan_kirim: "",
                    summary: "",
                    catatan: "",
                    pemeriksa: "",
                    pengirim: ""
                };

            this.$store.dispatch("orderAsset/listingStaff");
            this.$store.commit("orderAsset/update_selected_item_received", selectedItem);
            this.$store.commit("orderAsset/update_form_inspeksi", formInspeksi);
            this.$store.commit("orderAsset/update_dialog_inspeksi", true);
        },
        cancelOrder(item) {
            const current = JSON.parse(JSON.stringify(this.order_received));
            const neworder = current.filter((obj) => obj.PODetailID != item.PODetailID);
            this.order_received = neworder;
        },
        simpanReceivedOrder() {
            if (this.order_received.length <= 0) {
                let snac = {color: 'warning', msg: "item tidak boleh kosong", state: true}
                this.$store.commit("roasset/update_snackbar", snac)
                return;
            }

            if (!this.loading_process) {
                if (!this.form_receive.ReceiveOrderPoID) {
                    this.$store.dispatch("orderAsset/createReceiveOrder");
                } else {
                    this.$store.dispatch("orderAsset/editReceiveOrder");
                }
            } else {
                let snac = {color: 'warning', msg: "proses masih berjalan", state: true}
                this.$store.commit("roasset/update_snackbar", snac)
            }
        },
        batalReceiveOrder() {
            const formReceive = {
                date: moment(new Date()).format("YYYY-MM-DD"),
                supplier: "",
                reference: "",
                ponumber: "",
                branchID: "",
                gudangID: "",
                shipping: 0,
                shippingIsPaid: "N",
                catatan: ""
            };

            const formInspeksi = {
                qty_po: 0,
                qty_ro: 0,
                date_po: moment(new Date()).format("YYYY-MM-DD"),
                date_ro: moment(new Date()).format("YYYY-MM-DD"),
                condt_kemasan: "",
                catatan_kemasan: "",
                condt_pengiriman: "",
                catatan_kirim: "",
                summary: "",
                catatan: "",
                pemeriksa: "",
                pengirim: ""
            };

            this.$store.commit("orderAsset/update_form_receive", formReceive);
            this.$store.commit("orderAsset/update_order_received", []);
            this.$store.commit("orderAsset/update_list_item_order", []);
            this.$store.commit("orderAsset/update_selected_item_received", {});
            this.$store.commit("orderAsset/update_form_inspeksi", formInspeksi);
            this.$store.commit("orderAsset/update_readonly_mode", false)

            this.dialog_receive_aset = false;
        }
    }
}
</script>
