<template>
    <v-dialog v-model="dialog_inspeksi" persistent width="40vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                FORM INSPEKSI ASET {{ selected_item_received.M_ItemDesc }}
            </v-card-title>

            <v-card-text>
                <v-layout row>
                    <v-flex xs6 pr-1>
                        <v-text-field label="Jumlah Pesan" type="number" min="0" hide-details
                            v-model="form_inspeksi.qty_po" outline step="1"></v-text-field>
                    </v-flex>
                    <v-flex xs6 pl-1>
                        <v-text-field label="Jumlah Actual" type="number" min="0" hide-details
                            v-model="form_inspeksi.qty_ro" outline step="1"></v-text-field>
                    </v-flex>
                </v-layout>

                <v-layout row mt-2>
                    <v-flex xs6 pr-1>
                        <v-menu v-model="menuDatePo" 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="form_inspeksi.date_po" v-on="on" readonly
                                    label="Tanggal Terima Pesan" outline hide-details></v-text-field>
                            </template>
                            <v-date-picker v-model="form_inspeksi.date_po" no-title
                                @input="menuDatePo = false"></v-date-picker>
                        </v-menu>
                    </v-flex>
                    <v-flex xs6 pl-1>
                        <v-menu v-model="menuDateRo" 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="form_inspeksi.date_ro" v-on="on" readonly
                                    label="Tanggal Terima Actual" outline hide-details></v-text-field>
                            </template>
                            <v-date-picker v-model="form_inspeksi.date_ro" no-title
                                @input="menuDateRo = false"></v-date-picker>
                        </v-menu>
                    </v-flex>
                </v-layout>

                <v-layout row mt-2 wrap>
                    <v-flex xs12>
                        <v-radio-group v-model="form_inspeksi.condt_kemasan" label="Kondisi Kemasan" row hide-details>
                            <v-radio label="Baik" value="1"></v-radio>
                            <v-radio label="Tidak Baik" value="2"></v-radio>
                        </v-radio-group>
                        <v-text-field v-if="form_inspeksi.condt_kemasan == '2'" class="mt-2" label="Catatan Kemasan"
                            outline hide-details v-model="form_inspeksi.catatan_kemasan"></v-text-field>
                    </v-flex>
                    <v-flex xs12 mt-2>
                        <v-radio-group v-model="form_inspeksi.condt_pengiriman" label="Kondisi Pengiriman" row
                            hide-details>
                            <v-radio label="Sesuai" value="1"></v-radio>
                            <v-radio label="Tidak Sesuai" value="2"></v-radio>
                        </v-radio-group>
                        <v-text-field v-if="form_inspeksi.condt_pengiriman == '2'" class="mt-2" outline
                            label="Catatan Pengiriman" hide-details
                            v-model="form_inspeksi.catatan_kirim"></v-text-field>
                    </v-flex>
                    <v-flex xs12 mt-2>
                        <v-radio-group v-model="form_inspeksi.summary" row label="Kesimpulan" hide-details>
                            <v-radio label="Diterima" value="accept"></v-radio>
                            <v-radio label="Ditolak" value="reject"></v-radio>
                            <v-radio label="Diterima dengan Catatan" value="acceptnote"></v-radio>
                        </v-radio-group>
                    </v-flex>
                    <v-flex xs12 mt-2>
                        <v-textarea v-if="
                            form_inspeksi.summary == 'reject' ||
                            form_inspeksi.summary == 'acceptnote'
                        " v-model="form_inspeksi.catatan" outline auto-grow rows="1" label="Catatan"
                            hide-details></v-textarea>
                    </v-flex>
                </v-layout>

                <v-layout row mt-2>
                    <v-flex xs6 pr-1>
                        <v-autocomplete label="Staff Penerima" outline hide-details v-model="form_inspeksi.pemeriksa"
                            :items="list_staff" item-text="M_UserUsername" item-value="M_UserID"></v-autocomplete>
                    </v-flex>
                    <v-flex xs6 pl-1>
                        <v-text-field label="Petugas Pengirim" outline hide-details
                            v-model="form_inspeksi.pengirim"></v-text-field>
                    </v-flex>
                </v-layout>
            </v-card-text>

            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="red" flat outline @click="closeInspeksi()">Tutup</v-btn>
                <v-btn color="green" flat outline @click="simpanInspeksi()">Simpan</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            menuDatePo: false,
            menuDateRo: false
        };
    },
    computed: {
        dialog_inspeksi: {
            get() {
                return this.$store.state.orderAsset.dialog_inspeksi;
            },
            set(val) {
                this.$store.commit("orderAsset/update_dialog_inspeksi", val);
            }
        },
        form_inspeksi: {
            get() {
                return this.$store.state.orderAsset.form_inspeksi;
            },
            set(val) {
                this.$store.commit("orderAsset/update_form_inspeksi", val);
            }
        },
        selected_item_received: {
            get() {
                return this.$store.state.orderAsset.selected_item_received;
            },
            set(val) {
                this.$store.commit("orderAsset/update_selected_item_received", val);
            }
        },
        order_received: {
            get() {
                return this.$store.state.orderAsset.order_received;
            },
            set(val) {
                this.$store.commit("orderAsset/update_order_received", val);
            }
        },
        list_staff() {
            return this.$store.state.orderAsset.list_staff;
        }
    },
    methods: {
        defaultFormInspeksi() {
            return {
                qty_po: 0,
                qty_ro: 0,
                date_po: moment(new Date()).format("YYYY-MM-DD"),
                date_ro: moment(new Date()).format("YYYY-MM-DD"),
                condt_kemasan: "",
                catatan_kemasan: "",
                condt_pengiriman: "",
                catatan_kirim: "",
                summary: "",
                catatan: "",
                pemeriksa: "",
                pengirim: ""
            };
        },
        closeInspeksi() {
            this.form_inspeksi = this.defaultFormInspeksi();
            this.selected_item_received = {};
            this.dialog_inspeksi = false;
        },
        simpanInspeksi() {
            const dataInspeksi = JSON.parse(JSON.stringify(this.form_inspeksi));
            const selectedItem = JSON.parse(JSON.stringify(this.selected_item_received));
            const listOrder = JSON.parse(JSON.stringify(this.order_received));

            const idxToUpdate = listOrder.findIndex((obj) => obj.PODetailID == selectedItem.PODetailID);
            if (idxToUpdate != -1) {
                listOrder[idxToUpdate].inspeksi = dataInspeksi;
            }

            this.order_received = listOrder;
            this.closeInspeksi();
        }
    }
};
</script>
