import * as api from "../api/bank.js"

export default {
    namespaced: true,
    state: {
        autocomplete_status: 0,
        search_error_message: "",
        regionals: [],
        selected_regional: {},
        branchs: [],
        selected_branch: {},
        search_status: "",
        bankcoas: [],
        selected_bankcoa: {},
        total_page: 0,
        current_page: 1,
        open_dialog_info: false,
        msg_info: "",
        xsearch: "",
        dialog_bankCoa: false,
        coa_bank_list: [],
        selected_bank_coa: {},
        search_coa_bank: "",
        coa_edc_list: [],
        selected_edc_coa: {},
        search_coa_edc: "",
        isEDC: false,
        save_status: 0,
        save_error_message: "",
        alert_error: false,
        alert_success: false,
        errors_save_bankcoa: [],
        msg_success: ""
    },
    mutations: {
        update_autocomplete_status(state, val) {
            state.autocomplete_status = val
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_regionals(state, val) {
            state.regionals = val
        },
        update_selected_regional(state, val) {
            state.selected_regional = val
        },
        update_branchs(state, val) {
            state.branchs = val
        },
        update_selected_branch(state, val) {
            state.selected_branch = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_bankcoas(state, val) {
            state.bankcoas = val
        },
        update_selected_bankcoa(state, val) {
            state.selected_bankcoa = val
        },
        update_total_page(state, val) {
            state.total_page = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_open_dialog_info(state, val) {
            state.open_dialog_info = val
        },
        update_msg_info(state, val) {
            state.msg_info = val
        },
        update_xsearch(state, val) {
            state.xsearch = val
        },
        update_dialog_bankCoa(state, val) {
            state.dialog_bankCoa = val
        },
        update_coa_bank_list(state, val) {
            state.coa_bank_list = val
        },
        update_selected_bank_coa(state, val) {
            state.selected_bank_coa = val
        },
        update_search_coa_bank(state, val) {
            state.search_coa_bank = val
        },
        update_coa_edc_list(state, val) {
            state.coa_edc_list = val
        },
        update_selected_edc_coa(state, val) {
            state.selected_edc_coa = val
        },
        update_search_coa_edc(state, val) {
            state.search_coa_edc = val
        },
        update_isEDC(state, val) {
            state.isEDC = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_alert_error(state, val) {
            state.alert_error = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_errors_save_bankcoa(state, val) {
            state.errors_save_bankcoa = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        }
    },
    actions: {
        async getRegional(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getRegional({ token: one_token() });
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };
    
                    context.commit("update_regionals", data.records);
                }   
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async getBranch(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getBranch({ token: one_token(), regionalId: context.state.selected_regional.S_RegionalID});
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };
        
                    context.commit("update_branchs", data.records);
                } 
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async search(context, prm) {
            context.commit("update_search_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.search(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_search_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_search_status", 2)
                    let data = {
                        records: resp.data.records,
                        total_page: resp.data.total_page,
                    };
        
                    context.commit("update_bankcoas", data.records);
                    context.commit("update_total_page", data.total_page);
                } 
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_search_status", 3)
            }
        },
        async searchCoaBank(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.searchCoaBank(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_autocomplete_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records
                    }

                    context.commit("update_coa_bank_list", data.records)
                }
            } catch (e) {
                context.commit("update_autocomplete_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async searchCoaEDC(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.searchCoaEDC(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_autocomplete_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records
                    }

                    context.commit("update_coa_edc_list", data.records)
                }
            } catch (e) {
                context.commit("update_autocomplete_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async saveupdate(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.saveupdate(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    if (data.total !== -1) {
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_bankCoa", false)
                        var msg = prm.mapBankName + " berhasil di update"
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            branchCode: context.state.selected_branch.M_BranchCode,
                            regionalId: context.state.selected_regional.S_RegionalID
                        })
                    } else {
                        context.commit("update_errors_save_bankcoa", [])
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                context.commit("update_alert_error", true)
            }
        },
        async deletebankcoa(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deletebankcoa(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    if (data.total !== -1) {
                        context.commit("update_alert_success", true)
                        var msg = prm.mapBankName + " berhasil di hapus"
                        context.commit("update_msg_success", msg)
                        context.commit("update_alert_success", true)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            branchCode: context.state.selected_branch.M_BranchCode,
                            regionalId: context.state.selected_regional.S_RegionalID
                        })
                    } else {
                        context.commit("update_errors_save_bankcoa", [])
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
                context.commit("update_save_error_message", e.message)
            }
        },
        async doImportBank(context, prm) {
            context.commit("update_save_status", 1)
            try {
                let branchCode = prm.branchCode;
                let resp = await api.importBank(branchCode)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                        context.commit("update_alert_success", true)
                        var msg = resp.data.msg
                        var countInsert = resp.data.inserted_rows.length
                        var pesan = msg + ", jumlah insert : " + countInsert
                        context.commit("update_msg_success", pesan)
                        context.commit("update_alert_success", true)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            branchCode: context.state.selected_branch.M_BranchCode,
                            regionalId: context.state.selected_regional.S_RegionalID
                        })
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
                context.commit("update_save_error_message", e.message)
            }
        },
    },
}