<template>
    <v-dialog v-model="dialog_add_mapping_item" persistent width="40vw">
        <v-card>
            <v-card-title class="headline grey lighten-2">
                {{ selected_inventaris_item.itemCoaMapID > 0 
                    ? "Edit Account Item"
                    : "Add Account Item"
                }} {{ selected_inventaris_item.M_ItemDesc }}
            </v-card-title>
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="invCoa" hide-details outline label="Account Inventaris"
                            v-model="form.invCoaID" item-text="coaDescription" item-value="coaID"
                            :search-input.sync="searchInv" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="debtCoa" hide-details outline label="Account Hutang Inventaris"
                            v-model="form.debtCoaID" item-text="coaDescription" item-value="coaID"
                            :search-input.sync="searchDebt" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="bebanCoa" hide-details outline item-value="coaID"
                            label="Account Beban Penyusutan Inventaris" item-text="coaDescription"
                            v-model="form.bebanCoaID" :search-input.sync="searchBeban" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="accumCoa" hide-details outline item-value="coaID"
                            label="Account Akumulasi Penyusutan Inventaris" item-text="coaDescription"
                            v-model="form.accumCoaID" :search-input.sync="searchAccum" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="labaCoa" hide-details outline item-value="coaID"
                            label="Account Laba Pelepasan Inventaris" item-text="coaDescription"
                            v-model="form.labaCoaID" :search-input.sync="searchLaba" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex xs12 pa-1>
                        <v-autocomplete :items="rugiCoa" hide-details outline item-value="coaID"
                            label="Account Rugi Pelepasan Inventaris" item-text="coaDescription"
                            v-model="form.rugiCoaID" :search-input.sync="searchRugi" no-filter clearable>
                            <template slot="item" slot-scope="data">
                                <v-list-tile-content>
                                    <v-list-tile-title v-html="data.item.coaAccountNo"></v-list-tile-title>
                                    <v-list-tile-sub-title v-html="data.item.coaDescription"></v-list-tile-sub-title>
                                </v-list-tile-content>
                            </template>
                        </v-autocomplete>
                    </v-flex>
                </v-layout>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn @click="batalForm()" color="error" class="white--text">Batal</v-btn>
                <v-btn @click="simpanForm()" color="primary" class="white--text">Simpan</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            searchInv: null,
            searchDebt: null,
            searchBeban: null,
            searchAccum: null,
            searchLaba: null,
            searchRugi: null,
            debounceTimeout: null
        }
    },
    computed: {
        dialog_add_mapping_item: {
            get() {
                return this.$store.state.invItemMap.dialog_add_mapping_item;
            },
            set(val) {
                this.$store.commit("invItemMap/update_dialog_add_mapping_item", val);
            }
        },
        list_gol_inventaris: {
            get() {
                return this.$store.state.invMap.list_gol_inventaris;
            },
            set(val) {
                this.$store.commit("invMap/update_list_gol_inventaris", val);
            }
        },
        form: {
            get() {
                return this.$store.state.invItemMap.form;
            },
            set(val) {
                this.$store.commit("invItemMap/update_form", val);
            }
        },
        selected_inventaris_item() {
            return this.$store.state.invItemMap.selected_inventaris_item;
        },
        loading_process() {
            return this.$store.state.datacoa.loading_process;
        },
        invCoa() {
            return this.$store.state.datacoa.invCoa;
        },
        debtCoa() {
            return this.$store.state.datacoa.debtCoa;
        },
        bebanCoa() {
            return this.$store.state.datacoa.bebanCoa;
        },
        accumCoa() {
            return this.$store.state.datacoa.accumCoa;
        },
        labaCoa() {
            return this.$store.state.datacoa.labaCoa;
        },
        rugiCoa() {
            return this.$store.state.datacoa.rugiCoa;
        },
    },
    methods: {
        fetchCoa(keyword, key) {
            this.$store.dispatch("datacoa/getListCoa", { keyword: keyword, key: key });
        },
        batalForm() {
            this.form = {
                invCoaID: 0,
                debtCoaID: 0,
                bebanCoaID: 0,
                accumCoaID: 0,
                labaCoaID: 0,
                rugiCoaID: 0
            }
            this.dialog_add_mapping_item = false;
        },
        simpanForm() {
            if (!this.loading_process) {
                const form = this.form;
                const selected = this.selected_inventaris_item
                const param = {
                    M_ItemID: selected.M_ItemID,
                    M_InventarisItemCoaMappingID: selected.itemCoaMapID,
                    CoaInventarisID: form.invCoaID,
                    CoaHutangID: form.debtCoaID,
                    CoaPembelianID: form.invCoaID,
                    CoaBebanPenyusutanID: form.bebanCoaID,
                    CoaAkumulasiPenyusutanID: form.accumCoaID,
                    CoaLabaPelepasanID: form.labaCoaID,
                    CoaRugiPelepasanID: form.rugiCoaID
                }

                if (selected.itemCoaMapID > 0) {
                    this.$store.dispatch("invItemMap/editInvItemCoaMapping", param);
                } else {
                    this.$store.dispatch("invItemMap/createInvItemCoaMapping", param);
                }
            } else {
                const snac = { state: true, msg: "terdapat ongoing proses", color: "warning" };
                this.$store.commit('update_snackbar', snac);
            }
        }
    },
    watch: {
        searchInv(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'invCoa');
            }, 500)
        },
        searchDebt(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'debtCoa');
            }, 500)
        },
        searchBeban(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'bebanCoa');
            }, 500)
        },
        searchAccum(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'accumCoa');
            }, 500)
        },
        searchLaba(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'labaCoa');
            }, 500)
        },
        searchRugi(val) {
            if (!val) return;

            clearTimeout(this.debounceTimeout);
            this.debounceTimeout = setTimeout(() => {
                this.fetchCoa(val, 'rugiCoa');
            }, 500)
        }
    }
}
</script>
