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

export default {
    namespaced: true,
    state: {
        autocomplete_status: 0,
        search_error_message: "",
        regionals: [],
        selected_regional: {},
        search_regional: "",
        autocomplete_status_regional: 0,
        regional_filters: [],
        selected_regional_filter: {},
        branchs: [],
        selected_branch: {},
        branch_filters: [],
        selected_branch_filter: {},
        search_status: "",
        rkcabangs: [],
        selected_rkcabang: {},
        total_page: 0,
        current_page: 1,
        open_dialog_info: false,
        msg_info: "",
        xsearch: "",
        dialog_form: false,
        coa_edc_list: [],
        selected_edc_coa: {},
        search_coa_edc: "",
        autocomplete_status_type: 0,
        isEDC: false,
        save_status: 0,
        save_error_message: "",
        alert_error: false,
        alert_success: false,
        errors_save: [],
        msg_success: "",
        act: "new",
        type_list: [],
        selected_type: {},
        autocomplete_status_type: 0,
        search_type: ""
    },
    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_search_regional(state, val) {
            state.search_regional = val
        },
        update_autocomplete_status_regional(state, val) {
            state.autocomplete_status_regional = val
        },
        update_regional_filters(state, val) {
            state.regional_filters = val
        },
        update_selected_regional_filter(state, val) {
            state.selected_regional_filter = val
        },
        update_branch_filters(state, val) {
            state.branch_filters = val
        },
        update_selected_branch_filter(state, val) {
            state.selected_branch_filter = 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_rkcabangs(state, val) {
            state.rkcabangs = val
        },
        update_selected_rkcabang(state, val) {
            state.selected_rkcabang = 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_form(state, val) {
            state.dialog_form = 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_autocomplete_status_edc(state, val) {
            state.autocomplete_status_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(state, val) {
            state.errors_save = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_type_list(state, val) {
            state.type_list = val
        },
        update_selected_type(state, val) {
            state.selected_type = val
        },
        update_autocomplete_status_type(state, val) {
            state.autocomplete_status_type = val
        },
        update_search_type(state, val) {
            state.search_type = val
        }
    },
    actions: {
        async getRegional(context, prm) {
            context.commit("update_autocomplete_status_regional", 1)
            try {
                let resp = await api.getRegional(one_token(), prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status_regional", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status_regional", 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_regional", 3)
            }
        },
        async getRegionalFilter(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getRegionalFilter({ 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_regional_filters", 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 getBranchFilter(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getBranchFilter({ token: one_token(), regionalId: context.state.selected_regional_filter.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_branch_filters", 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_rkcabangs", 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 getType(context, prm) {
            context.commit("update_autocomplete_status_type", 1)
            try {
                let resp = await api.getType(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status_type", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_autocomplete_status_type", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records
                    }

                    context.commit("update_type_list", data.records)
                }
            } catch (e) {
                context.commit("update_autocomplete_status_type", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async searchCoaEDC(context, prm) {
            context.commit("update_autocomplete_status_edc", 1)
            try {
                let resp = await api.searchCoaEDC(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status_edc", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_autocomplete_status_edc", 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_edc", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async saveRkCabang(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.saveRkCabang(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_form", false)
                        var msg = prm.rkCabangCoaDescription + " berhasil di tambahkan"
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            branchCode: prm.branchCode,
                            regionalId: prm.regionalId
                        })
                    } else {
                        context.commit("update_errors_save", "error save")
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                context.commit("update_alert_error", true)
            }
        },
        async updateRkCabang(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.updateRkCabang(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_form", false)
                        var msg = prm.rkCabangCoaDescription + " berhasil di update"
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            branchCode: prm.branchCode,
                            regionalId: prm.regionalId
                        })
                    } else {
                        context.commit("update_errors_save", "update error")
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                context.commit("update_alert_error", true)
            }
        },
        async deleteRkCabang(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deleteRkCabang(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.rkCabangName + " 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: prm.branchCode,
                            regionalId: prm.regionalId
                        })
                    } else {
                        context.commit("update_errors_save", "delete error")
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
                context.commit("update_save_error_message", e.message)
            }
        },
    },
}