<template>
    <v-dialog v-model="dialog_handover_asset" persistent width="50vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                SERAH TERIMA BARANG ASSET
            </v-card-title>
            <v-card-text>
                <p>Apakah Anda yakin ingin melakukan serah terima barang inventaris kepada user pemohon</p>
                <v-form ref="formhandover" lazy-validation v-model="valid_formhandover">
                    <v-data-table :headers="headers" :items="list_item_not_handed" hide-actions class="elevation-1">
                        <template v-slot:items="props">
                            <tr>
                                <td class="text-xs-center">
                                    <v-checkbox v-if="props.item.isHandoverDone == 'N'" v-model="props.item.checked"
                                        hide-details @change="onChangeCheckbox(props.item)">
                                    </v-checkbox>
                                </td>
                                <td class="text-xs-center">{{ props.item.M_ItemDesc }}</td>
                                <td class="text-xs-center">{{ props.item.originalQty }}</td>
                                <td class="text-xs-center">
                                    <v-text-field v-if="props.item.checked" v-model="props.item.serahQty" type="number"
                                        solo :rules="qtyRules(props.item.sisaQty)"></v-text-field>
                                    <div v-else>
                                        {{ props.item.serahQty }}
                                    </div>
                                </td>
                            </tr>
                        </template>
                    </v-data-table>
                    <v-layout row mt-2>
                        <v-flex xs6 pr-1>
                            <v-autocomplete v-model="selected_ruangan" :items="list_lokasi" label="Lokasi Penerima*"
                                :rules="ruang_rules" outline item-value="M_RuanganID"
                                item-text="M_RuanganName"></v-autocomplete>
                        </v-flex>
                        <v-flex xs6 pl-1>
                            <v-autocomplete label="Staff Penerima*" outline :rules="rules_staff"
                                v-model="selected_staff" :items="list_staff" item-value="M_UserID"
                                item-text="M_UserUsername"></v-autocomplete>
                        </v-flex>
                    </v-layout>
                </v-form>
            </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="simpanHandOver()">Ya, Serahkan Barang</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            selected_ruangan: 0,
            selected_staff: 0,
            valid_formhandover: true,
            ruang_rules: [v => (v && Object.keys(v).length > 0) || 'Lokasi ruangan harus diisi'],
            rules_staff: [v => (v && Object.keys(v).length > 0) || 'Staff harus diisi'],
            headers: [
                {
                    text: "", align: "center", sortable: false, value: "checked",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false, value: "itemname",
                    width: "50%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY DITERIMA", align: "center", sortable: false, value: "qtyitem",
                    width: "20%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "QTY HANDOVER", align: "center", sortable: false, value: "qtysend",
                    width: "20%", class: "pa-2 blue lighten-3 white--text",
                },
            ],
        }
    },
    computed: {
        dialog_handover_asset: {
            get() {
                return this.$store.state.roasset.dialog_handover_asset;
            },
            set(val) {
                this.$store.commit("roasset/update_dialog_handover_asset", val)
            }
        },
        selected_roasset: {
            get() {
                return this.$store.state.roasset.selected_roasset
            },
            set(val) {
                this.$store.commit("roasset/update_selected_roasset", val)
            }
        },
        list_item_not_handed: {
            get() {
                return this.$store.state.roasset.list_item_not_handed;
            },
            set(val) {
                this.$store.commit("roasset/update_list_item_not_handed", val)
            }
        },
        list_staff() {
            return this.$store.state.orderAsset.list_staff;
        },
        list_lokasi() {
            return this.$store.state.roasset.list_lokasi;
        }
    },
    methods: {
        onChangeCheckbox(item) {
            if (item.checked) {
                item.serahQty = item.sisaQty
            } else {
                item.serahQty = item.originalQty - item.sisaQty
            }
        },
        qtyRules(originalQty) {
            return [
                v => Number(v) > 0 || "Jumlah harus diatas 0",
                v => Number(v) <= Number(originalQty) || "Jumlah melebihi inventaris yang diterima"
            ];
        },
        closeHandOver() {
            this.selected_roasset = {};
            this.selected_staff = 0;
            this.selected_ruangan = 0;
            this.$refs.formhandover.resetValidation();
            this.dialog_handover_asset = false;
        },
        async simpanHandOver() {
            if (this.$refs.formhandover.validate()) {
                let checked = this.list_item_not_handed.filter((ele) => ele.checked);
                if (checked.length < 1) {
                    const snac = { color: 'error', msg: 'tidak ada item yg diterima', state: true };
                    this.$store.commit("roasset/update_snackbar", snac);
                    return;
                }

                let param = {
                    staffID: this.selected_staff,
                    receiveOrderPoID: this.selected_roasset.ReceiveOrderPoID,
                    ruanganID: this.selected_ruangan,
                    item: checked
                }

                await this.$store.dispatch("roasset/saveHandoverAsset", param);
                this.selected_staff = 0;
                this.selected_ruangan = 0;
                this.$refs.formhandover.resetValidation();
            }
        }
    },
    watch: {}
}
</script>
