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

            <v-card-text>
            <v-form ref="formmutasi" lazy-validation v-model="valid_formmutasi">
                <v-layout row>
                    <v-flex xs6 pr-1>
                        <v-menu
                            v-model="menuMutasiDateForm" lazy offset-y max-width="290px"
                            full-width transition="scale-transition" min-width="290px"
                            :nudge-right="40" :close-on-content-click="false" 
                        >
                            <template v-slot:activator="{ on }">
                                <v-text-field
                                    :value="mutasiDateForm" label="Tanggal" 
                                    outline hide-details v-on="on" readonly 
                                ></v-text-field>
                            </template>
                            <v-date-picker
                                v-model="mutasi_date" no-title
                                @input="menuMutasiDateForm = false"
                            ></v-date-picker>
                        </v-menu>
                    </v-flex>
                    <v-flex xs6 px-1>
                        <v-autocomplete
                            v-model="selected_category" :rules="rules_category" disabled
                            :items="list_category" return-object outline item-value="itemCategoryID"
                            item-text="itemCategoryName" label="Kategori*" required 
                        ></v-autocomplete>
                    </v-flex>
                </v-layout>

                <v-layout row>
                    <v-flex xs6 pr-1>
                        <v-autocomplete
                            v-model="selected_branch_asal" :rules="rules_branch_asal"
                            :items="list_branch_asal" outline required disabled
                            item-text="M_BranchName" label="Cabang Asal*" return-object
                        ></v-autocomplete>
                    </v-flex>
                    <v-flex xs6 px-1>
                        <v-autocomplete
                            v-model="selected_branch_tujuan" :rules="rules_branch_tujuan"
                            :items="list_branch_tujuan" outline required 
                            item-text="M_BranchName" label="Cabang Tujuan*" return-object
                        ></v-autocomplete>
                    </v-flex>
                </v-layout>

                <v-layout row>
                    <v-flex xs6 pr-1>
                        <v-autocomplete
                            v-model="selected_warehouse" :rules="rules_warehouse"
                            :items="list_warehouse" outline required
                            item-text="WarehouseName" label="Gudang Tujuan*" return-object
                        ></v-autocomplete>
                    </v-flex>
                    <v-flex xs6 px-1>
                        <v-autocomplete
                            v-model="selected_ruangan" :rules="rules_lokasi"
                            :items="list_ruangan" outline required
                            item-text="M_RuanganName" label="Lokasi*" return-object
                        ></v-autocomplete>
                    </v-flex>
                </v-layout>

                <v-layout row wrap>
                    <v-flex xs12 pl-1>
                        <v-textarea
                            v-model="note" hide-details 
                            auto-grow rows="1" label="Catatan" outline
                        ></v-textarea>
                    </v-flex>
                </v-layout>

                <v-layout row mt-4 wrap>
                    <v-flex xs12>
                        <v-data-table
                            v-if="list_mutasi_item.length > 0"
                            :headers = "headers" :items="list_mutasi_item"
                            hide-actions item-key="MutasiHandoverDetailID" class="elevation-1"
                        >
                            <template v-slot:items="props">
                                <tr>
                                    <td class="text-xs-left">{{ props.item.MutasiRequestNumber }}</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.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_mutasi }}</td>
                                </tr>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-form>
            </v-card-text>

            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="red" flat outline @click="btnClose()">Tutup</v-btn>
                <v-btn color="green" flat outline @click="btnReceive()">Terima</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    components: {},
    data() {
        return {
            menuMutasiDateForm: false,
            valid_formmutasi: true,
            rules_category: [v => (v && Object.keys(v).length > 0) || 'Kategori harus diisi'], 
            rules_lokasi: [v => (v && Object.keys(v).length > 0) || 'Lokasi harus diisi'], 
            rules_warehouse: [v => (v && Object.keys(v).length > 0) || 'Gudang harus diisi'],
            rules_branch_asal: [v => (v && Object.keys(v).length > 0) || 'Cabang Asal harus diisi'],
            rules_branch_tujuan: [v => (v && Object.keys(v).length > 0) || 'Cabang Tujuan harus diisi'],
            headers: [
                {
                    text: "NO REQUEST MUTASI", align: "center", sortable: false, value: "ItemUnitName", 
                    width: "25%", 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: "TOTAL STOCK", align: "center", sortable: false, value: "qty",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "QTY TERSEDIA BY BARCODE", align: "center", sortable: false, value: "qty",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "QTY MUTASI", align: "center", sortable: false, value: "qty",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                }
            ]
        }
    },
    computed: {
        dialog_form_mutasi_receive: {
            get() {
                return this.$store.state.mutasi.dialog_form_mutasi_receive;
            },
            set(val) {
                this.$store.commit('mutasi/update_dialog_form_mutasi_receive', val);
            }
        },
        mutasiDateForm() {
            return this.formatDate(this.mutasi_date)
        },
        mutasi_date: {
            get() {
                return this.$store.state.form.mutasi_date;
            },
            set(val) {
                this.$store.commit('form/update_mutasi_date', val);
            }
        },
        selected_category: {
            get() {
                return this.$store.state.form.selected_category;
            },
            set(val) {
                this.$store.commit('form/update_selected_category', val);
            }
        },
        selected_branch_asal: {
            get() {
                return this.$store.state.form.selected_branch_asal;
            },
            set(val) {
                this.$store.commit('form/update_selected_branch_asal', val);
            }
        },
        selected_branch_tujuan: {
            get() {
                return this.$store.state.form.selected_branch_tujuan;
            },
            set(val) {
                this.$store.commit('form/update_selected_branch_tujuan', val);
            }
        },
        note: {
            get() {
                return this.$store.state.form.note;
            },
            set(val) {
                this.$store.commit('form/update_note', val);
            }
        },
        list_category() {
            return this.$store.state.form.list_category
        },
        list_branch_asal() {
            return this.$store.state.form.list_branch_asal
        },
        list_branch_tujuan() {
            return this.$store.state.form.list_branch_tujuan
        },
        dialog_select_item: {
            get() {
                return this.$store.state.form.dialog_select_item;
            },
            set(val) {
                this.$store.commit('form/update_dialog_select_item', val);
            }
        },
        list_mutasi_item: {
            get() {
                return this.$store.state.form.list_mutasi_item;
            },
            set(val) {
                this.$store.commit('form/update_list_mutasi_item', 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_warehouse() {
            return this.$store.state.form.list_warehouse;
        },
        selected_warehouse: {
            get() {
                return this.$store.state.form.selected_warehouse;
            },
            set(val) {
                this.$store.commit('form/update_selected_warehouse', val);
            }
        },
        list_ruangan() {
            return this.$store.state.form.list_ruangan;
        },
        selected_ruangan: {
            get() {
                return this.$store.state.form.selected_ruangan;
            },
            set(val) {
                this.$store.commit('form/update_selected_ruangan', val);
            }
        },
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split("-");
            return `${day}-${month}-${year}`;
        },
        batalApprove() {
            this.list_mutasi_item = [];
            this.dialog_form_mutasi_receive = false;
        },
        pilihItem() {
            let temp = this.list_mutasi_item
            if (temp.length > 0) {
                this.$store.commit("form/update_select_item_temp", temp);
            }

            this.dialog_select_item = true;
            this.$store.dispatch('form/getReceiveOrderPo');
        },
        removeItem(props) {
            let temp = JSON.parse(JSON.stringify(this.list_mutasi_item));
            this.list_mutasi_item = temp.filter(ele => ele.ReceiveOrderPoID !== props.item.ReceiveOrderPoID);
        },
        btnReceive() {
            if (this.$refs.formmutasi.validate()) {
                try {
                    let selected = this.$store.state.mutasi.selected_list_data
                    if (selected.MutasiHandoverID != '') {
                        this.$store.dispatch("mutasi/receive");
                    }
                } catch (error) {
                    console.error("Error receive mutasi:", error);
                    this.$store.commit("mutasi/update_snackbar", {
                        color: 'error',
                        msg: 'Gagal Receive mutasi request',
                        isOpen: true
                    });
                }
                this.$refs.formmutasi.resetValidation();
            }
        },
        btnClose() {
            this.$store.dispatch("form/resetForm");
            this.$store.commit("mutasi/update_dialog_form_mutasi_receive", false)
        }
    },
    watch: {
        dialog_form_mutasi_receive(val) {
            if (val) {
                this.$nextTick(() => {
                    this.$refs.formmutasi?.resetValidation();
                });
            }
        }
    },
}
</script>