import * as api from '../api/api-presentase.js';

export default {
    namespaced: true,
    state: {
        tabel_precab: {},
        pagination_precab: 1,
        loading_precab: false,
        selected_precab: {},
        input_precab: {
            typeid: 0,
            typename: ''
        },
        tabel_detail: {},
        pagination_detail: 1,
        loading_detail: false,
        loading_api: false,
        snackbar: {
            isOpen: false,
            color: "error",
            msg: ""
        },
        dialogFormType: false,
        dialogFormDet: false,
        list_regional: {},
        selected_input_reg: {},
        regional_branches: {},
        list_coa: {},
        selected_coa: {},
    },
    mutations: {
        update_tabel_precab(state, data) {
            state.tabel_precab = data
        },
        update_pagination_precab(state, data) {
            state.pagination_precab = data
        },
        update_loading_precab(state, data) {
            state.loading_precab = data
        },
        update_selected_precab(state, data) {
            state.selected_precab = data
        },
        update_tabel_detail(state, data) {
            state.tabel_detail = data
        },
        update_pagination_detail(state, data) {
            state.pagination_detail = data
        },
        update_loading_detail(state, data) {
            state.loading_detail = data
        },
        update_input_precab(state, data) {
            state.input_precab = data
        },
        update_loading_api(state, data) {
            state.loading_api = data
        },
        update_snackbar(state, data) {
            state.snackbar = data
        },
        update_dialog_form(state, data) {
            state.dialogFormType = data
        },
        update_dialog_form_det(state, data) {
            state.dialogFormDet = data
        },
        update_list_regional(state, data) {
            state.list_regional = data
        },
        update_selected_input_reg(state, data) {
            state.selected_input_reg = data
        },
        update_regional_branches(state, data) {
            state.regional_branches = data
        },
        update_list_coa(state, data) {
            state.list_coa = data
        },
        update_selected_coa(state, data) {
            state.selected_coa = data
        },
        update_item_listcoa(state, data) {
            state.regional_branches.records = data
        }
    },
    actions: {
        async get_listing_type(context, params) {
            context.commit("update_loading_precab", true)
            try {
                params.token = one_token()
                let resp = await api.get_listing_type(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_precab", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_tabel_precab", resp.data)
                    context.commit("update_loading_precab", false)
                }
            } catch (error) {
                context.commit("update_loading_precab", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async get_listing_reg(context, params) {
            context.commit("update_loading_detail", true)
            try {
                params.token = one_token()
                let resp = await api.get_list_detail_type(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_detail", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_tabel_detail", resp.data)
                    context.commit("update_loading_detail", false)
                }
            } catch (error) {
                context.commit("update_loading_detail", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async insert_type_presentase(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.insert_type_presentase(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_loading_api", false)
                    context.dispatch("get_listing_type", {currpages: context.state.pagination_precab})
                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_dialog_form", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
                context.commit("update_dialog_form", false)
            }
        },
        async edit_type_presentase(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.edit_type_presentase(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_loading_api", false)
                    context.dispatch("get_listing_type", {currpages: context.state.pagination_precab})
                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_dialog_form", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async delete_type_presentase(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.delete_type_presentase(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_loading_api", false)
                    context.dispatch("get_listing_type", {currpages: context.state.pagination_precab})

                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_dialog_form", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async get_regional_input(context) {
            context.commit("update_loading_api", true)
            try {
                let resp = await api.get_list_regional({ token: one_token() })
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    let ndata = resp.data.records
                    let exist = context.state.tabel_detail.records
                    let set_exist = new Set(exist.map(item => item.S_RegionalID))
                    let res = ndata.filter(item => !set_exist.has(item.S_RegionalID))

                    let data = {
                        total: resp.data.total,
                        records: res
                    }
                    context.commit("update_loading_api", false)
                    context.commit("update_list_regional", data)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async get_regional_branches(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.get_regional_branch(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    resp.data.records.forEach(item => {
                        item.listCoa = []    
                    });

                    context.commit("update_loading_api", false)
                    context.commit("update_regional_branches", resp.data)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async insert_detail_precab(context) {
            context.commit("update_loading_api", true)
            try {
                let precab = context.state.selected_precab
                let input_reg = context.state.selected_input_reg
                let input_branch = context.state.regional_branches.records

                let final_input = []
                input_branch.forEach( item => {
                    let a = {
                        branchpercentid: precab.BranchPercentID,
                        regionalid: input_reg.S_RegionalID,
                        branchid: item.M_BranchID,
                        value: item.branchValue,
                        noacc: item.accountNumber
                    }
                    final_input.push(a)
                });

                let params = {
                    branchvalue: final_input,
                    token: one_token()
                }

                let resp = await api.insert_detail_precab(params)
                if (resp.status != "OK") {
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_loading_api", false)
                } else {
                    let page = context.state.pagination_detail
                    context.dispatch("get_listing_reg", {currpages: page, presentasecabangid: precab.BranchPercentID, token: one_token()})

                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_loading_api", false)
                    context.commit("update_dialog_form_det", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
                context.commit("update_dialog_form_det", false)
            }
        },
        async edit_detail_precab(context) {
            context.commit("update_loading_api", true)
            try {
                let precab = context.state.selected_precab
                let input_branch = context.state.regional_branches.records

                let final_input = []
                input_branch.forEach( item => {
                    let a = {
                        precabdetailid: item.BranchPercentDetailID,
                        value: item.branchValue,
                        noacc: item.accountNumber
                    }
                    final_input.push(a)
                })

                let params = {
                    branchvalue: final_input,
                    token: one_token()
                }

                let resp = await api.edit_detail_precab(params)
                if (resp.status != "OK") {
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_loading_api", false)
                } else {
                    let page = context.state.pagination_detail
                    context.dispatch("get_listing_reg", {currpages: page, presentasecabangid: precab.BranchPercentID, token: one_token()})

                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_loading_api", false)
                    context.commit("update_dialog_form_det", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
                context.commit("update_dialog_form_det", false)
            }
        },
        async get_current_value_detail(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.get_current_value_detail(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    resp.data.records.forEach(item => {
                        item.branchValue = parseFloat(item.branchValue)
                        item.listCoa = []    
                    });

                    
                    context.commit("update_regional_branches", resp.data)
                    context.commit("update_loading_api", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async get_list_coa(context, params) {
            context.commit("update_loading_api", true)
            try {
                params.token = one_token()
                let resp = await api.search_coa(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    let branc = context.state.regional_branches.records
                    if (branc) {
                        // let data = {
                        //     data: resp.data.records,
                        //     code: params.code
                        // }

                        branc.forEach(item => {
                            if (item.M_BranchCode == params.code) {
                                item.listCoa = resp.data.records
                            }
                        });
                        context.commit("update_item_listcoa", branc)
                    }
                    
                    context.commit("update_loading_api", false)
                    // context.commit("update_list_coa", resp.data)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
        async delete_detail_precab(context, params) {
            context.commit("update_loading_api", true)
            try {
                let precab = context.state.selected_precab
                params.token = one_token()
                let resp = await api.delete_detail_precab(params)
                if (resp.status != "OK") {
                    context.commit("update_loading_api", false)
                    let snac = {
                        isOpen: true,
                        color: "error",
                        msg: resp.message
                    }
                    context.commit("update_snackbar", snac)
                } else {
                    let page = context.state.pagination_detail
                    context.dispatch("get_listing_reg", {currpages: page, presentasecabangid: precab.BranchPercentID, token: one_token()})
                    let snac = {
                        isOpen: true,
                        color: "success",
                        msg: "Sukses"
                    }
                    context.commit("update_snackbar", snac)
                    context.commit("update_loading_api", false)
                    context.commit("update_dialog_form_det", false)
                }
            } catch (error) {
                context.commit("update_loading_api", false)
                let snac = {
                    isOpen: true,
                    color: "error",
                    msg: error.message
                }
                context.commit("update_snackbar", snac)
            }
        },
    }
}