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

export default {
    namespaced: true,
    state: {
        Fa_ClassID: 0,
        a_coas: [],
        a_msg_info: '',
        a_open_dialog_info: false,
        a_save_status: 0,
        // search table
        a_x_search: '',
        a_current_page: 1,
        a_last_id: -1,
        a_total_coa: 0,
        // search table
        a_selected_a_coa: {},
        a_search_status: 0,
        a_alert_success: false,
        a_msg_success: '',
        a_alert_error: false,
        a_save_error_message: '',
        dialog_add_coa: false,
        dialog_edit_coa: false,
        e_loading_nat_group: false,
        // text classname
        a_classname: '',
        e_classname: '',
        // ac coa
        a_loading_coa: false,
        e_loading_coa: false,
        a_coa_list: [],
        a_coa: {},
        a_coa_search: '',

        e_coa_list: [],
        e_coa: {},
        e_coa_search: '',
        // text deprecorp
        a_deprecorp: '',
        a_age: 0,
        e_age: 0,
        e_deprecorp: '',
        // ac coa accum depre
        a_loading_coa_accum_depre: false,
        e_loading_coa_accum_depre: false,
        a_coa_accum_depre_list: [],
        a_coa_accum_depre: {},
        a_coa_search_accum_depre: '',

        e_coa_accum_depre_list: [],
        e_coa_accum_depre: {},
        e_coa_accum_depre_search: '',

        a_loading_coa_depre_cost: false,
        e_loading_coa_depre_cost: false,
        a_coa_depre_cost_list: [],
        a_coa_depre_cost: {},
        a_coa_search_depre_cost: '',

        e_coa_depre_cost_list: [],
        e_coa_depre_cost: {},
        e_coa_depre_cost_search: '',
        // text note
        a_note: '',
        e_note: '',
        a_errors_save: [],
        Fa_ClassID: 0,
        a_errors_save_coa: [],
    },
    mutations: {
        update_a_errors_save_coa(state, val) {
            state.a_errors_save_coa = val
        },
        update_a_selected_a_coa(state, val) {
            state.a_selected_a_coa = val
        },
        update_e_loading_nat_group(state, val) {
            state.e_loading_nat_group = val
        },
        update_a_errors_save(state, val) {
            state.a_errors_save = val
        },
        update_a_save_error_message(state, val) {
            state.a_save_error_message = val
        },
        update_a_alert_error(state, val) {
            state.a_alert_error = val
        },
        update_a_msg_success(state, val) {
            state.a_msg_success = val
        },
        update_a_msg_info(state, val) {
            state.a_msg_info = val
        },
        update_a_open_dialog_info(state, val) {
            state.a_open_dialog_info = val
        },
        update_a_alert_success(state, val) {
            state.a_alert_success = 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
        },
        // text classname
        update_a_classname(state, val) {
            state.a_classname = val
        },
        update_e_classname(state, val) {
            state.e_classname = val
        },
        // ac coa
        update_a_loading_coa(state, val) {
            state.a_loading_coa = val
        },
        update_e_loading_coa(state, val) {
            state.e_loading_coa = val
        },
        update_a_coa_list(state, val) {
            state.a_coa_list = val
        },
        update_e_coa_list(state, val) {
            state.e_coa_list = val
        },
        update_a_coa(state, val) {
            state.a_coa = val
        },
        update_e_coa(state, val) {
            state.e_coa = val
        },
        update_a_coa_search(state, val) {
            state.a_coa_search = val
        },
        update_e_coa_search(state, val) {
            state.e_coa_search = val
        },
        // text depre corp
        update_a_deprecorp(state, val) {
            state.a_deprecorp = val
        },
        update_a_age(state, val) {
            state.a_age = val
        },
        update_e_age(state, val) {
            state.e_age = val
        },
        update_e_deprecorp(state, val) {
            state.e_deprecorp = val
        },
        // ac coa accum depre
        update_a_loading_coa_accum_depre(state, val) {
            state.a_loading_coa_accum_depre = val
        },
        update_e_loading_coa_accum_depre(state, val) {
            state.e_loading_coa_accum_depre = val
        },
        update_a_coa_accum_depre_list(state, val) {
            state.a_coa_accum_depre_list = val
        },
        update_e_coa_accum_depre_list(state, val) {
            state.e_coa_accum_depre_list = val
        },
        update_a_coa_accum_depre(state, val) {
            state.a_coa_accum_depre = val
        },
        update_e_coa_accum_depre(state, val) {
            state.e_coa_accum_depre = val
        },
        update_a_coa_search_accum_depre(state, val) {
            state.a_coa_search_accum_depre = val
        },
        update_e_coa_accum_depre_search(state, val) {
            state.e_coa_accum_depre_search = val
        },
        update_a_loading_coa_depre_cost(state, val) {
            state.a_loading_coa_depre_cost = val
        },
        update_e_loading_coa_depre_cost(state, val) {
            state.e_loading_coa_depre_cost = val
        },
        update_a_coa_depre_cost_list(state, val) {
            state.a_coa_depre_cost_list = val
        },
        update_e_coa_depre_cost_list(state, val) {
            state.e_coa_depre_cost_list = val
        },
        update_a_coa_depre_cost(state, val) {
            state.a_coa_depre_cost = val
        },
        update_e_coa_depre_cost(state, val) {
            state.e_coa_depre_cost = val
        },
        update_a_coa_search_depre_cost(state, val) {
            state.a_coa_search_depre_cost = val
        },
        update_e_coa_depre_cost_search(state, val) {
            state.e_coa_depre_cost_search = val
        },
        // text note
        update_a_note(state, val) {
            state.a_note = val
        },
        update_e_note(state, val) {
            state.e_note = val
        },
        update_dialog_add_coa(state, val) {
            state.dialog_add_coa = val
        },
        update_dialog_edit_coa(state, val) {
            state.dialog_edit_coa = val
        },
        update_Fa_ClassID(state, val) {
            state.Fa_ClassID = val
        },
        update_a_save_status(state, val) {
            state.a_save_status = val
        },
    },
    actions: {
        // 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 ac coa from api
        async open_add_coa(context, payload) {
            context.commit("update_a_loading_coa", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcCoa(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa", false)
                    context.commit("update_a_coa_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa", false)
            }
        },

        // get ac coa accum depre from api
        async open_add_coa_accum_depre(context, payload) {
            context.commit("update_a_loading_coa_accum_depre", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcAccumDepre(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_accum_depre", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_accum_depre", false)
                    context.commit("update_a_coa_accum_depre_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_accum_depre", false)
            }
        },

        async open_add_coa_depre_cost(context, payload) {
            context.commit("update_a_loading_coa_depre_cost", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcDepreCost(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_coa_depre_cost", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_coa_depre_cost", false)
                    context.commit("update_a_coa_depre_cost_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_coa_depre_cost", false)
            }
        },
        // add data
        async add_data(context, payload) {
            context.commit("update_a_save_status", 1)
            try {
                let prm = {};
                prm.token = one_token()
                prm.a_classname = payload.a_classname_param
                prm.a_coa = payload.a_coa_param
                prm.a_deprecorp = payload.a_deprecorp_param
                prm.a_age = payload.a_age_param
                prm.a_coa_accum_depre = payload.a_coa_accum_depre_param
                prm.a_coa_depre_cost = payload.a_coa_depre_cost_param
                prm.a_note = payload.a_note_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 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("faclass/update_dialog_add_coa", false);
                        context.commit("faclass/update_a_classname", "");
                        context.commit("faclass/update_a_coa", {});
                        context.commit("faclass/update_a_deprecorp", "");
                        context.commit("faclass/update_a_age", 0);
                        context.commit("faclass/update_a_coa_accum_depre", {});
                        context.commit("faclass/update_a_coa_depre_cost", {});
                        context.commit("faclass/update_a_note", "");
                    } else {
                        context.commit("update_a_errors_save", [])
                    }

                }
            } 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)
            }
        },

        // get_by_id
        async get_by_id(context, payload) {
            context.commit("update_e_loading_nat_group", true)
            try {
                let prm = {};
                prm.token = one_token()
                // fa_class
                prm.Fa_ClassID = payload.Fa_ClassID
                // coa
                prm.Fa_ClassCoaID = payload.Fa_ClassCoaID
                // accum depre
                prm.Fa_ClassAccumDepreCoaID = payload.Fa_ClassAccumDepreCoaID

                prm.Fa_ClassAccumDepreCostCoaID = payload.DepreCostCoaID

                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_Fa_ClassID", resp.data.record_fa_class[0].Fa_ClassID)

                    // coa
                    context.commit("update_e_coa_list", resp.data.records_coa)
                    context.commit("update_e_coa", {
                        coaID: resp.data.records_coa[0].coaID,
                        coaAccountNo: resp.data.records_coa[0].coaAccountNo,
                        coaDescription: resp.data.records_coa[0].coaDescription,
                        coaSubDescription: resp.data.records_coa[0].coaSubDescription,
                        coaAccountType: resp.data.records_coa[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_coa[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_coa[0].coaIsInput,
                        coaReportSchedule: resp.data.records_coa[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_coa[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_coa[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_coa[0].coaCreated,
                        coaLastUpdated: resp.data.records_coa[0].coaLastUpdated,
                        coaIsActive: resp.data.records_coa[0].coaIsActive,
                        coaLevel: resp.data.records_coa[0].coaLevel
                    })

                    // accum depre
                    context.commit("update_e_coa_accum_depre_list", resp.data.records_accum_depre)
                    context.commit("update_e_coa_accum_depre", {
                        coaID: resp.data.records_accum_depre[0].coaID,
                        coaAccountNo: resp.data.records_accum_depre[0].coaAccountNo,
                        coaDescription: resp.data.records_accum_depre[0].coaDescription,
                        coaSubDescription: resp.data.records_accum_depre[0].coaSubDescription,
                        coaAccountType: resp.data.records_accum_depre[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_accum_depre[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_accum_depre[0].coaIsInput,
                        coaReportSchedule: resp.data.records_accum_depre[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_accum_depre[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_accum_depre[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_accum_depre[0].coaCreated,
                        coaLastUpdated: resp.data.records_accum_depre[0].coaLastUpdated,
                        coaIsActive: resp.data.records_accum_depre[0].coaIsActive,
                        coaLevel: resp.data.records_accum_depre[0].coaLevel
                    })

                    context.commit("update_e_coa_depre_cost_list", resp.data.records_depre_cost)
                    context.commit("update_e_coa_depre_cost", {
                        coaID: resp.data.records_depre_cost[0].coaID,
                        coaAccountNo: resp.data.records_depre_cost[0].coaAccountNo,
                        coaDescription: resp.data.records_depre_cost[0].coaDescription,
                        coaSubDescription: resp.data.records_depre_cost[0].coaSubDescription,
                        coaAccountType: resp.data.records_depre_cost[0].coaAccountType,
                        coaSpecialAccountType: resp.data.records_depre_cost[0].coaSpecialAccountType,
                        coaIsInput: resp.data.records_depre_cost[0].coaIsInput,
                        coaReportSchedule: resp.data.records_depre_cost[0].coaReportSchedule,
                        coaCurrencyCode: resp.data.records_depre_cost[0].coaCurrencyCode,
                        coaCashFlowCategory: resp.data.records_depre_cost[0].coaCashFlowCategory,
                        coaCreated: resp.data.records_depre_cost[0].coaCreated,
                        coaLastUpdated: resp.data.records_depre_cost[0].coaLastUpdated,
                        coaIsActive: resp.data.records_depre_cost[0].coaIsActive,
                        coaLevel: resp.data.records_depre_cost[0].coaLevel
                    })
                    // fa classname
                    context.commit("update_e_classname", resp.data.record_fa_class[0].Fa_ClassName)

                    // note
                    context.commit("update_e_note", resp.data.record_fa_class[0].Fa_ClassAccumNote)

                    // depre corp
                    context.commit("update_e_deprecorp", resp.data.record_fa_class[0].Fa_ClassDepreCorp)

                    context.commit("update_e_age", resp.data.record_fa_class[0].Fa_ClassAgeAsset)

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

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

        // get ac coa accum depre from api
        async open_edit_coa_accum_depre(context, payload) {
            context.commit("update_e_loading_coa_accum_depre", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcAccumDepre(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_e_loading_coa_accum_depre", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_e_loading_coa_accum_depre", false)
                    context.commit("update_e_coa_accum_depre_list", resp.data)
                }
            } catch (e) {
                context.commit("update_e_loading_coa_accum_depre", false)
            }
        },
        async open_edit_coa_depre_cost(context, payload) {
            context.commit("update_e_loading_coa_depre_cost", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getAcAccumDepre(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_e_loading_coa_depre_cost", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_e_loading_coa_depre_cost", false)
                    context.commit("update_e_coa_depre_cost_list", resp.data)
                }
            } catch (e) {
                context.commit("update_e_loading_coa_depre_cost", false)
            }
        },
        
        // edit data
        async edit_data(context, payload) {
            context.commit("update_a_save_status", 1)
            try {
                let prm = {};
                prm.token = one_token()
                prm.Fa_ClassID = payload.Fa_ClassID
                prm.e_classname = payload.e_classname_param
                prm.e_coa = payload.e_coa_param
                prm.e_deprecorp = payload.e_deprecorp_param
                prm.e_coa_accum_depre = payload.e_coa_accum_depre_param
                prm.e_coa_depre_cost = payload.e_coa_depre_cost_param
                prm.e_note = payload.e_note_param
                prm.e_age = payload.e_age_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 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.Fa_ClassName + " 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)
            }
        },
    },
}