<template>
    <v-dialog v-model="dialog_select_item" persistent width="60vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                TAMBAH ITEM
            </v-card-title>

            <v-card-text>
                <v-autocomplete v-model="selected_po" :items="list_po" hide-details return-object flat label="Cari PO"
                    outline class="mb-3" item-text="PurchaseOrderNumber" @input="searchItem()"></v-autocomplete>

                <!-- list item per po -->
                <v-data-table v-model="select_item_temp" :headers="headers" :items="display_list_itempo" hide-actions
                    select-all item-key="PoItemReceiveID" class="elevation-1">
                    <template v-slot:headers="props">
                        <tr>
                            <th width="10%" class="blue lighten-3 white--text">
                                <v-checkbox :input-value="props.all" :indeterminate="props.indeterminate" primary
                                    hide-details @click.stop="toggleAll(props)"></v-checkbox>
                            </th>
                            <th v-for="header in props.headers" :key="header.text" :width="header.width"
                                :class="header.class">
                                {{ header.text }}
                            </th>
                        </tr>
                    </template>
                    <template v-slot:items="props">
                        <tr :active="props.selected" @click="selectItemRow(props)">
                            <td>
                                <v-checkbox v-if="hideCheckbox(props)" :input-value="props.selected" primary
                                    hide-details></v-checkbox>
                            </td>
                            <td class="text-xs-center">{{ props.item.M_ItemCode }}</td>
                            <td class="text-xs-center">
                                <p class="p-0 mb-0">{{ props.item.M_ItemDesc }}</p>
                                <p class="p-0 m-0">{{ props.item.WarehouseName }}</p>
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.originalQty }} / {{ props.item.qtyRest }}
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">{{ props.item.qty }}</td>
                        </tr>
                    </template>
                </v-data-table>
                <div class="text-xs-center mt-2">
                    <v-pagination v-model="itempo_page" :length="total_selected_page" :total-visible="5"></v-pagination>
                </div>

                <!-- list item selected -->
                <v-divider class="my-3"></v-divider>
                <h3>ITEM DIPILIH</h3>
                <v-data-table :headers="headers_selected" :items="display_selected_item" hide-actions
                    item-key="PoItemReceiveID" class="elevation-1 mt-3">
                    <template v-slot:items="props">
                        <tr>
                            <td class="text-xs-center">{{ (props.index + 1) }}</td>
                            <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.originalQty }} / {{ props.item.qtyRest }}
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">
                                <v-text-field v-model="props.item.qty" class="my-2" hide-details outline type="number"
                                    min="0" :max="parseInt(props.item.qtyRest)" step="1"></v-text-field>
                            </td>
                            <td class="text-xs-center">
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn style="min-width: 30px; height: 30px; margin: 0;" outline small
                                            @click="removeItem(props)" color="red lighten-2">
                                            <v-icon small color="red" v-on="on">delete</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Hapus</span>
                                </v-tooltip>
                            </td>
                        </tr>
                    </template>
                </v-data-table>
                <div class="text-xs-center mt-2">
                    <v-pagination v-model="curr_sel_page" :length="total_selected_page"
                        :total-visible="5"></v-pagination>
                </div>
            </v-card-text>

            <v-divider></v-divider>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="red" flat @click="tutupDialog()">Tutup</v-btn>
                <v-btn color="green" flat @click="simpanItem()">Simpan</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            headers: [
                {
                    text: "ITEM CODE ", align: "left", sortable: false, value: "M_ItemCode",
                    width: "20%", class: "pa-2 pl-2 blue lighten-3 white--text",
                },
                {
                    text: "NAME", align: "left", sortable: false, value: "M_ItemDesc",
                    width: "30%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY", align: "left", sortable: false, value: "ItemUnitName",
                    width: "25%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY RECEIVED", align: "left", sortable: false, value: "qty",
                    width: "25%", class: "pa-2  blue lighten-3 white--text",
                },
            ],
            itempo_page: 1,
            itempo_perpage: 5,
            headers_selected: [
                {
                    text: "NO", align: "center", sortable: false, value: "no",
                    width: "5%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "No. PO", align: "center", sortable: false, value: "po",
                    width: "25%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false, value: "ITEM",
                    width: "25%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY PO", align: "center", sortable: false, value: "qty",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY RECEIVED", align: "center", sortable: false, value: "qty_ro",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "", align: "center", sortable: false, value: "qty_ro",
                    width: "5%", class: "pa-2 blue lighten-3 white--text",
                },
            ],
            curr_sel_page: 1,
            sel_perpage: 5
        }
    },
    computed: {
        dialog_select_item: {
            get() {
                return this.$store.state.form.dialog_select_item;
            },
            set(val) {
                this.$store.commit("form/update_dialog_select_item", val);
            }
        },
        selected_po: {
            get() {
                return this.$store.state.form.selected_po;
            },
            set(val) {
                this.$store.commit("form/update_selected_po", val)
            }
        },
        select_item_temp: {
            get() {
                return this.$store.state.form.select_item_temp;
            },
            set(val) {
                this.$store.commit("form/update_select_item_temp", val)
            }
        },
        list_po: {
            get() {
                return this.$store.state.form.list_po;
            },
            set(val) {
                this.$store.commit("form/update_list_po", val)
            }
        },
        list_item_po: {
            get() {
                return this.$store.state.form.list_item_po;
            },
            set(val) {
                this.$store.commit("form/update_list_item_po", val)
            }
        },
        shipping_info: {
            get() {
                return this.$store.state.form.shipping_info;
            },
            set(val) {
                this.$store.commit("form/update_shipping_info", val)
            }
        },
        total_itempo_page() {
            if (this.list_item_po.length <= 0) {
                return 1;
            }
            return Math.ceil(this.list_item_po.length / this.itempo_perpage);
        },
        display_list_itempo() {
            const start = (this.itempo_page - 1) * this.itempo_perpage;
            const end = start + this.itempo_perpage;
            return this.list_item_po.slice(start, end);
        },
        total_selected_page() {
            if (this.select_item_temp.length <= 0) {
                return 1;
            }
            return Math.ceil(this.select_item_temp.length / this.sel_perpage);
        },
        display_selected_item() {
            const start = (this.curr_sel_page - 1) * this.sel_perpage;
            const end = start + this.sel_perpage;
            return this.select_item_temp.slice(start, end);
        }
    },
    methods: {
        tutupDialog() {
            this.list_po = [];
            this.selected_po = {};
            this.list_item_po = [];
            this.select_item_temp = [];
            this.dialog_select_item = false;
        },
        toggleAll(data) {
            if (data.all) {
                // this.select_item_temp = []
                return
            } else {
                this.select_item_temp = this.list_item_po.slice()
            }
        },
        selectItemRow(data) {
            if (!data.selected) {
                data.selected = !data.selected;
            };
        },
        hideCheckbox(data) {
            return !data.selected;
        },
        searchItem() {
            this.$store.dispatch("form/getItem");
        },
        removeItem(props) {
            let temp = JSON.parse(JSON.stringify(this.select_item_temp));
            this.select_item_temp = temp.filter(ele => ele.PoItemReceiveID !== props.item.PoItemReceiveID);
        },
        simpanItem() {
            let temp = JSON.parse(JSON.stringify(this.select_item_temp));
            let shippinginfo = [];
            let isError = false
            temp.forEach(element => {
                if (Number(element.qty) > Number(element.qtyRest)) {
                    isError = true;
                    return;
                }

                let ship = {
                    num_po: element.PurchaseOrderNumber,
                    cost: element.PurchaseOrderShippingCost,
                    isUpfront: element.PurchaseOrderShippingCostStatus
                };

                const isExist = shippinginfo.some(item => item.num_po == element.PurchaseOrderNumber)
                if (!isExist) {
                    shippinginfo.push(ship);
                }

                // only for type inventory
                let withoutbatch = {
                    "qty": Number(element.qty),
                    "batchno": "",
                    "item_category": "2",
                    "menuDate": false
                }
                element.batchlist.push(withoutbatch)
            });
            if (isError) {
                this.$store.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: 'qty melebihi item yang dikirim',
                    isOpen: true
                });
                return;
            }

            this.$store.commit("form/update_form_receive_item", temp);
            this.shipping_info = shippinginfo;

            this.list_po = [];
            this.selected_po = {};
            this.list_item_po = [];
            this.select_item_temp = [];
            this.dialog_select_item = false;
        }
    },
}
</script>
