<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_ro" :items="list_ro" hide-details 
                    return-object flat label="Cari GR" outline class="mb-3"
                    item-text="ReceiveOrderPoNumber" @input="searchItem()"
                ></v-autocomplete>

                <!-- list item -->
                <v-data-table
                    v-model="select_item_temp" :headers="headers" :items="list_item_receive" 
                    hide-actions select-all item-key="ReceiveOrderPoID" 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>
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.barcode_total }} 
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.stock_total }} 
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.qty_available }} 
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.qty_locked }} 
                            </td>
                        </tr>
                    </template>
                </v-data-table>
                <div class="text-xs-center mt-2">
                    <v-pagination v-model="current_page" :length="total_pages" :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="ReceiveOrderPoID" 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.ReceiveOrderPoNumber }}</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>
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.barcode_total }} 
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.stock_total }} 
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.qty_available }} 
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center">
                                {{ props.item.qty_locked }} 
                            </td>
                            <td class="text-xs-center">
                                <v-text-field
                                    v-model="props.item.qty_mutasi" class="my-2" hide-details outline
                                    type="number" min="0" :max="parseInt(props.item.qty_available)" 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>
            </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: "15%", class: "pa-2 pl-2 blue lighten-3 white--text",
                },
                {
                    text: "NAME", align: "left", sortable: false, value: "M_ItemDesc",
                    width: "25%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL BARCODE", align: "left", sortable: false, value: "ItemUnitName",
                    width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL STOCK", align: "left", sortable: false, value: "ItemUnitName",
                    width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY TERSEDIA BY BARCODE", align: "left", sortable: false, value: "ItemUnitName",
                    width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY LOCKED", align: "left", sortable: false, value: "ItemUnitName",
                    width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
            ],
            headers_selected: [
                {
                    text: "NO", align: "center", sortable: false, value: "no",
                    width: "5%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "NO GR", align: "center", sortable: false, value: "po",
                    width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false, value: "ITEM",
                    width: "20%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL BARCODE", align: "left", sortable: false, value: "ItemUnitName",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL STOCK", align: "left", sortable: false, value: "ItemUnitName",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY TERSEDIA BY BARCODE", align: "left", sortable: false, value: "ItemUnitName",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY LOCKED", align: "left", sortable: false, value: "ItemUnitName",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY MUTASI", align: "center", sortable: false, value: "qty_ro",
                    width: "15%", 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",
                },
            ],
        }
    },
    computed: {
        dialog_select_item: {
            get() {
                return this.$store.state.form.dialog_select_item;
            },
            set(val) {
                this.$store.commit("form/update_dialog_select_item", val);
            }
        },
        list_ro: {
            get() {
                return this.$store.state.form.list_ro;
            },
            set(val) {
                this.$store.commit("form/update_list_ro", val)
            }
        },
        selected_ro: {
            get() {
                return this.$store.state.form.selected_ro;
            },
            set(val) {
                this.$store.commit("form/update_selected_ro", val)
            }
        },
        list_item_receive: {
            get() {
                return this.$store.state.form.list_item_receive;
            },
            set(val) {
                this.$store.commit("form/update_list_item_receive", val)
            }
        },
        total_pages: {
            get() {
                return this.$store.state.form.total_pages;
            },
            set(val) {
                this.$store.commit("form/update_total_pages", val)
            }
        },
        current_page: {
            get() {
                return this.$store.state.form.current_page;
            },
            set(val) {
                this.$store.commit("form/update_current_page", val);
                this.$store.dispatch("form/getItemReceive");
            }
        },
        xsearch: {
            get() {
                return this.$store.state.form.x_search;
            },
            set(val) {
                this.$store.commit("form/update_x_search", val)
            }
        },
        select_item_temp: {
            get() {
                return this.$store.state.form.select_item_temp;
            },
            set(val) {
                this.$store.commit("form/update_select_item_temp", val)
            }
        },
        display_selected_item() {
            return this.select_item_temp;
        }
    },
    methods: {
        thr_search: _.debounce(function () {
            this.$store.dispatch("form/getItemReceive");
        }, 1000),
        tutupDialog() {
            this.selected_ro = {};
            this.list_ro = [];
            this.list_item_receive = [];
            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_receive
            }
        },
        selectItemRow(data) {
            if (!data.selected) {
                data.selected = !data.selected;   
            };
        },
        hideCheckbox(data) {
            return !data.selected;
        },
        searchItem() {
            this.$store.dispatch("form/getItemReceive");
        },
        removeItem(props) {
            let temp = JSON.parse(JSON.stringify(this.select_item_temp));
            let list_item_receive = JSON.parse(JSON.stringify(this.list_item_receive));

            let find_index = list_item_receive.findIndex(ele => ele.M_ItemID === props.item.M_ItemID);
            if (find_index !== -1) {
                list_item_receive[find_index].qty_mutasi = 0;
                this.list_item_receive = list_item_receive;
            }
            
            this.select_item_temp = temp.filter(ele => ele.ReceiveOrderPoID !== props.item.ReceiveOrderPoID);
        }, 
        simpanItem() {
            let temp = JSON.parse(JSON.stringify(this.select_item_temp));
            let isError = false
            temp.forEach(element => {
                if (Number(element.qty_mutasi) > Number(element.qty_available)) {
                    isError = true;
                    return;
                }
            });
            if (isError) {
                this.$store.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: 'qty mutasi melebihi item tersedia',
                    isOpen: true
                });
                return;
            }

            this.$store.commit("form/update_list_mutasi_item", temp);
            
            this.list_ro = [];
            this.selected_ro = {};
            this.list_item_receive = [];
            this.select_item_temp = [];
            this.dialog_select_item = false;
        }
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val;
            this.thr_search();
        },
    }
}
</script>