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

export default {
    namespaced: true,
    state: {
        MapNatSub_ID: 0,
        a_errors_save_nat_subgroup: [],
        a_save_status: 0,
        a_loading_nat_group: false,
        e_loading_nat_group: false,
        a_loading_nat_subgroup: false,
        a_alert_success: false,
        a_msg_success: '',
        a_alert_error: false,
        a_save_error_message: '',
        dialog_add_coa: false,
        dialog_edit_coa: false,
        a_msg_info: '',
        a_open_dialog_info: false,
        a_loading_coa_pendapatan: false,
        e_loading_coa_pendapatan: false,
        a_loading_coa_biaya: false,
        e_loading_coa_biaya: false,
        a_search_status: 0,
        a_coas: [],
        a_errors_save_coa: [],
        a_selected_a_coa: {},
        // search table
        a_x_search: '',
        a_current_page: 1,
        a_last_id: -1,
        a_total_coa: 0,
        // search table
        // dropdown add nat_group
        a_nat_group_list: [],
        a_nat_group: {},
        e_nat_group_list: [],
        e_nat_group: {},
        // dropdown add nat_subgroup
        a_nat_subgroup_list: [],
        a_nat_subgroup: {},
        e_nat_subgroup_list: [],
        e_nat_subgroup: {},
        // ac pendapatan
        a_coa_pendapatan_list: [],
        a_coa_pendapatan: {},
        a_coa_search_pendapatan: '',

        e_coa_pendapatan_list: [],
        e_coa_pendapatan: {},
        e_coa_search_pendapatan: '',
        // ac biaya
        a_coa_biaya_list: [],
        a_coa_biaya: {},
        a_coa_search_biaya: '',

        e_coa_biaya_list: [],
        e_coa_biaya: {},
        e_coa_search_biaya: '',
        // ac persediaan
        a_coa_persediaan_list: [],
        a_coa_persediaan: {},
        a_coa_search_persediaan: '',

        e_coa_persediaan_list: [],
        e_coa_persediaan: {},
        e_coa_search_persediaan: '',
    },
    mutations: {
        update_dialog_edit_coa(state, val) {
            state.dialog_edit_coa = val
        },
        update_e_loading_nat_group(state, val) {
            state.e_loading_nat_group = val
        },
        update_a_selected_a_coa(state, val) {
            state.a_selected_a_coa = val
        },
        update_a_errors_save_coa(state, val) {
            state.a_errors_save_coa = val
        },
        update_MapNatSub_ID(state, val) {
            state.MapNatSub_ID = val
        },
        update_a_open_dialog_info(state, val) {
            state.a_open_dialog_info = val
        },
        update_a_msg_info(state, val) {
            state.a_msg_info = val
        },
        update_a_alert_success(state, val) {
            state.a_alert_success = val
        },
        update_a_msg_success(state, val) {
            state.a_msg_success = val
        },
        update_a_alert_error(state, val) {
            state.a_alert_error = val
        },
        update_dialog_add_coa(state, val) {
            state.dialog_add_coa = val
        },
        //table
        update_a_search_error_message(state, val) {
            state.a_search_error_message = val
        },
        update_a_search_status(state, val) {
            state.a_search_status = val
        },
        update_a_x_search(state, val) {
            state.a_x_search = val
        },
        update_a_current_page(state, val) {
            state.a_current_page = val
        },
        update_a_last_id(state, val) {
            state.a_last_id = val
        },
        update_a_total_coa(state, val) {
            state.a_total_coa = val
        },
        update_a_a_coas(state, val) {
            state.a_coas = val
        },

        // nat_group
        update_a_loading_nat_group(state, val) {
            state.a_loading_nat_group = val
        },
        update_a_nat_group_list(state, val) {
            state.a_nat_group_list = val
        },
        update_a_nat_group(state, val) {
            state.a_nat_group = val
        },
        update_e_nat_group_list(state, val) {
            state.e_nat_group_list = val
        },
        update_e_nat_group(state, val) {
            state.e_nat_group = val
        },

        // nat_subgroup
        update_a_loading_nat_subgroup(state, val) {
            state.a_loading_nat_subgroup = val
        },
        update_a_nat_subgroup_list(state, val) {
            state.a_nat_subgroup_list = val
        },
        update_a_nat_subgroup(state, val) {
            state.a_nat_subgroup = val
        },

        update_e_nat_subgroup_list(state, val) {
            state.e_nat_subgroup_list = val
        },
        update_e_nat_subgroup(state, val) {
            state.e_nat_subgroup = val
        },

        // pendapatan
        update_a_loading_coa_pendapatan(state, val) {
            state.a_loading_coa_pendapatan = val
        },
        update_e_loading_coa_pendapatan(state, val) {
            state.e_loading_coa_pendapatan = val
        },
        update_a_coa_pendapatan_list(state, val) {
            state.a_coa_pendapatan_list = val
        },
        update_e_coa_pendapatan_list(state, val) {
            state.e_coa_pendapatan_list = val
        },
        update_a_coa_pendapatan(state, val) {
            state.a_coa_pendapatan = val
        },
        update_e_coa_pendapatan(state, val) {
            state.e_coa_pendapatan = val
        },
        update_a_coa_search_pendapatan(state, val) {
            state.a_coa_search_pendapatan = val
        },
        update_e_coa_search_pendapatan(state, val) {
            state.e_coa_search_pendapatan = val
        },
        // biaya
        update_a_loading_coa_biaya(state, val) {
            state.a_loading_coa_biaya = val
        },
        update_a_coa_biaya_list(state, val) {
            state.a_coa_biaya_list = val
        },
        update_a_coa_biaya(state, val) {
            state.a_coa_biaya = val
        },
        update_a_coa_search_biaya(state, val) {
            state.a_coa_search_biaya = val
        },

        update_e_loading_coa_biaya(state, val) {
            state.e_loading_coa_biaya = val
        },
        update_e_coa_biaya_list(state, val) {
            state.e_coa_biaya_list = val
        },
        update_e_coa_biaya(state, val) {
            state.e_coa_biaya = val
        },
        update_e_coa_search_biaya(state, val) {
            state.e_coa_search_biaya = val
        },
        // persediaan
        update_a_loading_coa_persediaan(state, val) {
            state.a_loading_coa_persediaan = val
        },
        update_e_loading_coa_persediaan(state, val) {
            state.e_loading_coa_persediaan = val
        },
        update_a_coa_persediaan_list(state, val) {
            state.a_coa_persediaan_list = val
        },
        update_e_coa_persediaan_list(state, val) {
            state.e_coa_persediaan_list = val
        },
        update_a_coa_persediaan(state, val) {
            state.a_coa_persediaan = val
        },
        update_e_coa_persediaan(state, val) {
            state.e_coa_persediaan = val
        },
        update_a_coa_search_persediaan(state, val) {
            state.a_coa_search_persediaan = val
        },
        update_e_coa_search_persediaan(state, val) {
            state.e_coa_search_persediaan = val
        },


        update_a_save_status(state, val) {
            state.a_save_status = val
        },
        update_a_save_error_message(state, val) {
            state.a_save_error_message = val
        },
        update_a_errors_save_nat_subgroup(state, val) {
            state.a_errors_save_nat_subgroup = val
        },
    },
    actions: {
        // get nat_group from api
        async open_add_nat_group(context) {
            context.commit("update_a_loading_nat_group", true)
            try {
                let prm = {};
                prm.token = one_token()
                let resp = await api.getNatGroup(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_nat_group", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_nat_group", false)
                    context.commit("update_a_nat_group_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_nat_group", false)
            }
        },

        // get nat_subgroup from api
        async open_add_nat_subgroup(context) {
            context.commit("update_a_loading_nat_subgroup", true)
            try {
                let prm = {};
                prm.token = one_token()
                let resp = await api.getNatSubGroup(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_nat_subgroup", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_nat_subgroup", false)
                    context.commit("update_a_nat_subgroup_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_nat_subgroup", false)
            }
        },

        // get ac pendapatan from api
        async open_add_coa_pendapatan(context, payload) {
            context.commit("update_a_loading_coa_pendapatan", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcPendapatan(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_pendapatan", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_pendapatan", false)
                    context.commit("update_a_coa_pendapatan_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_pendapatan", false)
            }
        },

        async open_edit_coa_pendapatan(context, payload) {
            context.commit("update_a_loading_coa_pendapatan", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcPendapatan(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_pendapatan", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_pendapatan", false)
                    context.commit("update_e_coa_pendapatan_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_pendapatan", false)
            }
        },

        // get ac biaya from api
        async open_add_coa_biaya(context, payload) {
            context.commit("update_a_loading_coa_biaya", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcBiaya(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_biaya", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_biaya", false)
                    context.commit("update_a_coa_biaya_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_biaya", false)
            }
        },

        async open_edit_coa_biaya(context, payload) {
            context.commit("update_a_loading_coa_biaya", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcBiaya(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_biaya", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_biaya", false)
                    context.commit("update_e_coa_biaya_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_biaya", false)
            }
        },

        // get ac persediaan from api
        async open_add_coa_persediaan(context, payload) {
            context.commit("update_a_loading_coa_persediaan", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcPersediaan(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_persediaan", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_persediaan", false)
                    context.commit("update_a_coa_persediaan_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_persediaan", false)
            }
        },

        async open_edit_coa_persediaan(context, payload) {
            context.commit("update_a_loading_coa_persediaan", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcPersediaan(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_persediaan", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_persediaan", false)
                    context.commit("update_e_coa_persediaan_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_persediaan", false)
            }
        },

        // add data
        async add_data(context, payload) {
            context.commit("update_a_save_status", 1)
            try {
                let prm = {};
                prm.token = one_token()
                prm.a_nat_group = payload.a_nat_group_param
                prm.a_nat_subgroup = payload.a_nat_subgroup_param
                prm.a_coa_pendapatan = payload.a_coa_pendapatan_param
                prm.a_coa_biaya = payload.a_coa_biaya_param
                prm.a_coa_persediaan = payload.a_coa_persediaan_param

                let resp = await api.addData(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_save_status", 3)
                    context.commit("update_a_save_error_message", resp.message)
                    context.commit("update_a_alert_error", true)
                } else {
                    context.commit("update_a_save_status", 2)
                    context.commit("update_a_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    if (data.total !== -1) {
                        context.commit("update_a_alert_success", true)
                        var msg = " Add Data Nat Sub Group COA Mapping Sukses"
                        context.commit("update_a_msg_success", msg)
                        context.commit('update_dialog_add_coa', false)
                        context.dispatch("search", {
                            current_page: context.state.a_current_page,
                            search: context.state.a_x_search,
                            last_id: -1
                        })

                        context.commit("update_a_nat_group", {});
                        context.commit("update_a_nat_subgroup", {});
                        context.commit("update_a_coa_pendapatan", {});
                        context.commit("update_a_coa_biaya", {});
                        context.commit("update_a_coa_persediaan", {});
                    } else {
                        context.commit("update_a_errors_save_nat_subgroup", [])
                    }

                }
            } catch (e) {
                context.commit("update_a_save_status", 3)
                context.commit("update_a_save_error_message", e.message)
                context.commit("update_a_alert_error", true)
            }
        },

        // search
        async search(context, prm) {
            context.commit("update_a_search_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_a_search_status", 3)
                    context.commit("update_a_search_error_message", resp.message)
                    context.commit("update_a_msg_info", resp.message)
                    context.commit("update_a_open_dialog_info", true)
                } else {
                    context.commit("update_a_search_status", 2)
                    context.commit("update_a_search_error_message", "")
                    context.commit("update_a_msg_info", resp.message)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }

                    context.commit("update_a_a_coas", data.records)
                    context.commit("update_a_total_coa", data.total)
                }
            } catch (e) {
                context.commit("update_a_search_status", 3)
                context.commit("update_a_search_error_message", e.message)
                context.commit("update_a_msg_info", e.message)
                context.commit("update_a_open_dialog_info", true)
            }
        },

        // get_by_id
        async get_by_id(context, payload) {
            context.commit("update_e_loading_nat_group", true)
            try {
                let prm = {};
                prm.token = one_token()
                // map group
                prm.MapNatSub_ID = payload.MapNatSub_ID
                // group
                prm.MapNatSub_NatGroupID = payload.MapNatSub_NatGroupID
                // sub_grup
                prm.MapNatSub_NatSubGroupID = payload.MapNatSub_NatSubGroupID
                // pendapatan
                prm.MapNatSub_PendapatanCoaID = payload.MapNatSub_PendapatanCoaID
                // biaya
                prm.MapNatSub_BiayaCoaID = payload.MapNatSub_BiayaCoaID
                // persediaan
                prm.MapNatSub_PersediaanCoaID = payload.MapNatSub_PersediaanCoaID

                let resp = await api.getByID(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_e_loading_nat_group", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_e_loading_nat_group", true)

                    // MapNatGroup_ID
                    context.commit("update_MapNatSub_ID", resp.data.record_map_nat_subgroup[0].MapNatSub_ID)

                    // nat_group
                    context.commit("update_e_nat_group_list", resp.data.records_group_all)
                    context.commit("update_e_nat_group", {
                        Nat_GroupCode: resp.data.records_group[0].Nat_GroupCode,
                        Nat_GroupID: resp.data.records_group[0].Nat_GroupID,
                        Nat_GroupName: resp.data.records_group[0].Nat_GroupName
                    })

                    // nat_subgroup
                    context.commit("update_e_nat_subgroup_list", resp.data.records_sub_group_all)
                    context.commit("update_e_nat_subgroup", {
                        Nat_SubGroupCode: resp.data.records_sub_group[0].Nat_SubGroupCode,
                        Nat_SubGroupID: resp.data.records_sub_group[0].Nat_SubGroupID,
                        Nat_SubGroupName: resp.data.records_sub_group[0].Nat_SubGroupName,
                        display: resp.data.records_sub_group[0].display,
                    })

                    // pendapatan
                    context.commit("update_e_coa_pendapatan_list", resp.data.records_pendapatan)
                    context.commit("update_e_coa_pendapatan", {
                        coaID: resp.data.records_pendapatan[0].coaID,
                        coaAccountNo: resp.data.records_pendapatan[0].coaAccountNo,
                        coaDescription: resp.data.records_pendapatan[0].coaDescription,
                        coaSubDescription: resp.data.records_pendapatan[0].coaSubDescription,
                        coaAccountType: resp.data.records_pendapatan[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_pendapatan[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_pendapatan[0].coaIsInput,
                        coaReportSchedule: resp.data.records_pendapatan[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_pendapatan[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_pendapatan[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_pendapatan[0].coaCreated,
                        coaLastUpdated: resp.data.records_pendapatan[0].coaLastUpdated,
                        coaIsActive: resp.data.records_pendapatan[0].coaIsActive,
                        coaLevel: resp.data.records_pendapatan[0].coaLevel
                    })

                    // biaya
                    context.commit("update_e_coa_biaya_list", resp.data.records_biaya)
                    context.commit("update_e_coa_biaya", {
                        coaID: resp.data.records_biaya[0].coaID,
                        coaAccountNo: resp.data.records_biaya[0].coaAccountNo,
                        coaDescription: resp.data.records_biaya[0].coaDescription,
                        coaSubDescription: resp.data.records_biaya[0].coaSubDescription,
                        coaAccountType: resp.data.records_biaya[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_biaya[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_biaya[0].coaIsInput,
                        coaReportSchedule: resp.data.records_biaya[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_biaya[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_biaya[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_biaya[0].coaCreated,
                        coaLastUpdated: resp.data.records_biaya[0].coaLastUpdated,
                        coaIsActive: resp.data.records_biaya[0].coaIsActive,
                        coaLevel: resp.data.records_biaya[0].coaLevel
                    })

                    // persediaan
                    context.commit("update_e_coa_persediaan_list", resp.data.records_persediaan)
                    context.commit("update_e_coa_persediaan", {
                        coaID: resp.data.records_persediaan[0].coaID,
                        coaAccountNo: resp.data.records_persediaan[0].coaAccountNo,
                        coaDescription: resp.data.records_persediaan[0].coaDescription,
                        coaSubDescription: resp.data.records_persediaan[0].coaSubDescription,
                        coaAccountType: resp.data.records_persediaan[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_persediaan[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_persediaan[0].coaIsInput,
                        coaReportSchedule: resp.data.records_persediaan[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_persediaan[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_persediaan[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_persediaan[0].coaCreated,
                        coaLastUpdated: resp.data.records_persediaan[0].coaLastUpdated,
                        coaIsActive: resp.data.records_persediaan[0].coaIsActive,
                        coaLevel: resp.data.records_persediaan[0].coaLevel
                    })

                    context.commit("update_dialog_edit_coa", true)
                }
            } catch (e) {
                context.commit("update_e_loading_nat_group", false)
            }
        },

        // edit data
        async edit_data(context, payload) {
            context.commit("update_a_save_status", 1)
            try {
                let prm = {};
                prm.token = one_token()
                prm.MapNatSub_ID = payload.MapNatSub_ID
                prm.e_nat_group = payload.e_nat_group_param
                prm.e_nat_subgroup = payload.e_nat_subgroup_param
                prm.e_coa_biaya = payload.e_coa_biaya_param
                prm.e_coa_pendapatan = payload.e_coa_pendapatan_param
                prm.e_coa_persediaan = payload.e_coa_persediaan_param

                let resp = await api.editData(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_save_status", 3)
                    context.commit("update_a_save_error_message", resp.message)
                    context.commit("update_a_alert_error", true)
                } else {
                    context.commit("update_a_save_status", 2)
                    context.commit("update_a_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    if (data.total !== -1) {
                        context.commit("update_a_alert_success", true)
                        var msg = " Edit Data National SUb Grup COA Mapping Sukses"
                        context.commit("update_a_msg_success", msg)
                        context.commit('update_dialog_edit_coa', false)
                        context.dispatch("search", {
                            current_page: context.state.a_current_page,
                            search: context.state.a_x_search,
                            last_id: -1
                        })
                    } else {
                        context.commit("update_a_errors_save_coa", [])
                    }

                }
            } catch (e) {
                context.commit("update_a_save_status", 3)
                context.commit("update_a_save_error_message", e.message)
                context.commit("update_a_alert_error", true)
            }
        },

        // delete data
        async delete_data(context, prm) {
            context.commit("update_a_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deleteData(prm)
                if (resp.status != "OK") {
                    context.commit("update_a_save_status", 3)
                    context.commit("update_a_save_error_message", resp.message)
                    context.commit("update_a_alert_error", true)
                } else {
                    context.commit("update_a_save_status", 2)
                    context.commit("update_a_save_error_message", resp.message)
                    let data = {
                        total: resp.data.total
                    }

                    if (data.total !== -1) {
                        context.commit("update_a_alert_success", true)
                        var msg = " Coa " + prm.Nat_GroupName + " berhasil dihapus"
                        context.commit("update_a_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.a_current_page,
                            search: context.state.a_x_search,
                            last_id: -1
                        })
                    } else {
                        context.commit("update_a_errors_save_coa", [])
                    }

                }
            } catch (e) {
                context.commit("update_a_save_status", 3)
                context.commit("update_a_save_error_message", e.message)
                context.commit("update_a_alert_error", true)
            }
        },
    },

}