<template>
    <v-dialog v-model="dialog_pick_contract" persistent width="60vw">
        <v-card class="style-limit-heightcard">
            <v-card-title class="headline grey lighten-2">PILIH ORDER ASET</v-card-title>
            <v-card-text class="style-limit-heightcontent">
                <v-layout row wrap>
                    <v-flex xs12>
                        <v-autocomplete
                            v-model="temp_selected" :loading="loading" hide-details
                            :items="list_item_order" hide-no-data outline
                            :search-input.sync="searchkey" cache-items clearable
                            label="Cari" placeholder="Nama Kontrak, nomor PO"
                            :item-text="formatOrderLabel" return-object
                        ></v-autocomplete>
                    </v-flex>
                    <v-flex xs12 mt-2>
                        <v-data-table :headers="headers" class="elevation-1" hide-actions
                            :items="temp_selected ? [temp_selected] : []"
                            :expand="expand" item-key="PurchaseOrderID">
                            <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-start pa-2">
                                        {{ props.item.PurchaseOrderNumber }}
                                    </td>
                                    <td class="text-xs-start pa-2">
                                        {{ props.item.PurchaseOrderAssetContractDate }}
                                    </td>
                                    <td class="text-xs-start pa-2">
                                        {{ props.item.PurchaseOrderAssetContractName }}
                                    </td>
                                    <td class="text-xs-start pa-2">
                                        {{ props.item.SupplierCode }} <br>
                                        {{ props.item.SupplierName }}
                                    </td>
                                    <td class="text-xs-start pa-2">
                                        {{ props.item.PurchaseOrderNote }}
                                    </td>
                                </tr>
                            </template>

                            <template v-slot:expand="props">
                            <v-card flat>
                                <v-card-text>
                                    <v-data-table
                                        :headers="b_headers" class="elevation-0"
                                        :items="props.item.detail" hide-actions
                                    >
                                    <template v-slot:items="expnd">
                                    <tr>
                                        <td class="text-xs-center pa-2">
                                            {{ expnd.item.M_ItemCode }}
                                        </td>
                                        <td class="text-xs-center pa-2">
                                            {{ expnd.item.M_ItemDesc }}
                                        </td>
                                        <td class="text-xs-center pa-2">
                                            {{ expnd.item.ItemUnitName }}
                                        </td>
                                        <td class="text-xs-center pa-2">
                                            {{ expnd.item.qty }}
                                        </td>
                                    </tr>
                                    </template>
                                    </v-data-table>
                                </v-card-text>
                            </v-card>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn @click="batalItemOrder()" color="error" class="white--text">Batal</v-btn>
                <v-btn @click="simpanItemOrder()" 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 {
            searchkey: "",
            loading: false,
            expand: false,
            temp_selected: null,
            headers: [
                {
                    text: " ", align: "center", sortable: false, value: "check",
                    width: "5%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "NO PURCHASE ORDER", align: "center", sortable: false, value: "po",
                    width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "TANGGAL KONTRAK", align: "center", sortable: false, value: "date",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "NAMA KONTRAK", align: "center", sortable: false, value: "date",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "SUPPLIER", align: "center", sortable: false, value: "unit",
                    width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "CATATAN", align: "center", sortable: false, value: "qty",
                    width: "20%", class: "pa-1 blue lighten-3 white--text",
                }
            ],
            b_headers: [
                {
                    text: "ITEM CODE", align: "center", sortable: false,
                    value: "code", width: "25%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false,
                    value: "item", width: "25%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "UNIT", align: "center", sortable: false,
                    value: "unit", width: "25%", class: "pa-1 blue lighten-4 black--text",
                },
                {
                    text: "QTY", align: "center", sortable: false,
                    value: "qty", width: "25%", class: "pa-1 blue lighten-4 black--text",
                },
            ]
        }
    },
    computed: {
        dialog_pick_contract: {
            get() {
                return this.$store.state.orderAsset.dialog_pick_contract;
            },
            set(val) {
                this.$store.commit("orderAsset/update_dialog_pick_contract", val);
            }
        },
        list_item_order: {
            get() {
                return this.$store.state.orderAsset.list_item_order;
            },
            set(val) {
                this.$store.commit("orderAsset/update_list_item_order", 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)
            }
        },
    },
    methods: {
        formatOrderLabel(item) {
            if (!item) return "";
            return `${item.PurchaseOrderNumber} - ${item.PurchaseOrderAssetContractName}`;
        },
        cariOrderAsset(newval) {
            this.loading = true;
            setTimeout(() => {
                this.$store.dispatch("orderAsset/searchOrderAsset", newval);
                this.loading = false;
            }, 500);
        },
        batalItemOrder() {
            this.temp_selected = null;
            this.dialog_pick_contract = false;
        },
        simpanItemOrder() {
            let form = JSON.parse(JSON.stringify(this.form_receive));
            const receivedOrder = JSON.parse(JSON.stringify(this.temp_selected));

            form.ponumber = receivedOrder.PurchaseOrderNumber;
            form.shipping = receivedOrder.PurchaseOrderShippingCost;
            form.shippingIsPaid = receivedOrder.PurchaseOrderShippingCostStatus

            this.form_receive = form;
            this.order_received = receivedOrder.detail;

            this.temp_selected = null;
            this.dialog_pick_contract = false;
        }
    },
    watch: {
        searchkey(newval, oldval) {
            if (newval == oldval) return;
            if (!newval) {
                this.cariOrderAsset("");
                return;
            }

            if (newval.length < 1) return;
            this.cariOrderAsset(newval);
        }
    }
}
</script>
