<template>
    <v-dialog v-model="dialogConfirmHandover" persistent width="40vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                KONFIRMASI TERIMA BARANG INVENTARIS
            </v-card-title>

            <v-card-text>
                <p>Apakah Anda yakin ingin melakukan konfirmasi terima barang inventaris ?</p>
                <v-layout pa-2 row class="green lighten-2">
                    <v-flex xs2> NO </v-flex>
                    <v-flex xs5> PENERIMA </v-flex>
                    <v-flex xs5> BARCODE </v-flex>
                </v-layout>
                <v-layout align-center pa-2 v-for="(obj, idx) in itemNeedConfirming(selected_main.itemHandedOver)"
                    :key="idx" row>
                    <v-flex xs2>{{ idx + 1 }}</v-flex>
                    <v-flex xs5>{{ obj.M_UserUsername }}</v-flex>
                    <v-flex xs5>{{ obj.T_BarcodeBarangNumber }}</v-flex>
                </v-layout>
            </v-card-text>

            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="red" flat outline @click="closeHandOver()">Batal</v-btn>
                <v-btn color="green" flat outline @click="saveConfirmHandOver()">Ya, Konfirmasi Terima</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
        }
    },
    computed: {
        dialogConfirmHandover: {
            get() {
                return this.$store.state.requester.dialogConfirmHandover
            },
            set(val) {
                this.$store.commit("requester/updateDialogConfirmHandover", val)
            }
        },
        selected_main: {
            get() {
                return this.$store.state.requester.selectedMainTable
            },
            set(val) {
                this.$store.commit("requester/updateSelectedMainTable", val)
            }
        },
    },
    methods: {
        closeHandOver() {
            this.selected_main = {}
            this.dialogConfirmHandover = false
        },
        itemNeedConfirming(data) {
            return data.filter(item => item.allHandedOver == 'N');
        },
        async saveConfirmHandOver() {
            const dataHanded = this.selected_main.itemHandedOver;
            const itemHanded = dataHanded.filter(item => item.allHandedOver == 'N');

            let prm = {
                purchaseRequestID: this.selected_main.prId,
                itemHandedOver: itemHanded
            }
            await this.$store.dispatch("requester/confirmHandOver", prm);
        },
    },
}
</script>
