import * as api from "../api/itemregional.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_faname: '',
        e_classname: '',
        // ac coa
        a_loading_coa: false,
        a_coa_list: [],
        a_coa: {},
        a_coa_search: '',

        // ac item
        a_loading_item: false,
        a_item_list: [],
        a_item: {},
        a_item_search: '',

        // ac regional
        a_loading_regional: false,
        a_regional_list: [],
        a_regional: {},
        a_regional_search: '',

        // ac unit
        a_loading_unit: false,
        a_unit_list: [],
        a_unit: {},
        a_unit_search: '',

        // ac department
        a_loading_department: false,
        a_department_list: [],
        a_department: {},
        a_department_search: '',
        a_inventaris : '',
        a_note : '',
        a_fa_account_id: 0,
        a_fa_account_name: '',
        a_accum_depre_account_id: 0,
        a_accum_depre_account_name: '',
        a_depre_account_id: 0,
        a_depre_account_name: '',
        a_qty: 0,
        a_acquisition_price: 0,


        // text note
        a_note: '',
        e_note: '',
        a_errors_save: [],
        Fa_ClassID: 0,
        a_errors_save_coa: [],
        xid: 0,
        xnumber: '',
        errors: []
    },
    mutations: {
        update_errors(state, val) {
            state.errors = val;
          },
        update_xid(state, val) {
            state.xid = val
        },
        update_xnumber(state, val) {
            state.xnumber = val
        },
        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_faname(state, val) {
            state.a_faname = 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
        },
        // ac item
        update_a_loading_item(state, val) {
            state.a_loading_item = val
        },
        update_a_item_list(state, val) {
            state.a_item_list = val
        },
        update_a_item(state, val) {
            state.a_item = val
        },
        update_a_item_search(state, val) {
            state.a_item_search = val
        },
        // ac regional
        update_a_loading_regional(state, val) {
            state.a_regional_item = val
        },
        update_a_regional_list(state, val) {
            state.a_regional_list = val
        },
        update_a_regional(state, val) {
            state.a_regional = val
        },
        update_a_regional_search(state, val) {
            state.a_regional_search = val
        },
        // ac unit
        update_a_loading_unit(state, val) {
            state.a_loading_unit = val
        },
        update_a_unit_list(state, val) {
            state.a_unit_list = val
        },
        update_a_unit(state, val) {
            state.a_unit = val
        },
        update_a_unit_search(state, val) {
            state.a_unit_search = val
        },
        // ac department
        update_a_loading_department(state, val) {
            state.a_loading_department = val
        },
        update_a_department_list(state, val) {
            state.a_department_list = val
        },
        update_a_department(state, val) {
            state.a_department = val
        },
        update_a_department_search(state, val) {
            state.a_department_search = val
        },
        update_a_inventaris(state, val) {
            state.a_inventaris = val
        },
        update_a_note(state, val) {
            state.a_note = val
        },
        update_a_fa_account_id(state, val) {
            state.a_fa_account_id = val
        },
        update_a_fa_account_name(state, val) {
            state.a_fa_account_name = val
        },
        update_a_accum_depre_account_id(state, val) {
            state.a_accum_depre_account_id = val
        },
        update_a_accum_depre_account_name(state, val) {
            state.a_accum_depre_account_name = val
        },
        update_a_depre_account_id(state, val) {
            state.a_depre_account_id = val
        },
        update_a_depre_account_name(state, val) {
            state.a_depre_account_name = val
        },
        update_a_qty(state, val) {
            state.a_qty = val
        },
        update_a_acquisition_price(state, val) {
            state.a_acquisition_price = 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)
            }
        },


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

        async open_add_regional(context, payload) {
            context.commit("update_a_loading_regional", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getRegional(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_regional", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_regional", false)
                    context.commit("update_a_regional_list", resp.data)
                    console.log('regional')
                    console.log(resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_regional", false)
            }
        },
        async open_add_unit(context, payload) {
            context.commit("update_a_loading_unit", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getUnit(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_unit", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_unit", false)
                    context.commit("update_a_unit_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_unit", false)
            }
        },
        async open_add_department(context, payload) {
            context.commit("update_a_loading_department", true)
            try {
                let prm = {};
                prm.token = one_token()
                prm.search = payload.search
                let resp = await api.getDepartment(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_department", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_department", false)
                    context.commit("update_a_department_list", resp.data)
                }
            } catch (e) {
                context.commit("update_a_loading_department", false)
            }
        },
        // add data
        async add_data(context, prm) {
            context.commit("update_a_save_status", 1)
            try {
                prm.token = one_token()

                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("itemregional/update_dialog_add_coa", false);
                        context.commit("itemregional/update_a_faname", "");
                        context.commit("itemregional/update_a_coa", {});
                        context.commit("itemregional/update_a_deprecorp", "");
                        context.commit("itemregional/update_a_coa_accum_depre", {});
                        context.commit("itemregional/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

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

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

        

        // edit data
        async edit_data(context, prm) {
            context.commit("update_a_save_status", 1)
            try {
                prm.token = one_token()

                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_add_coa', false)
                        context.dispatch("search", {
                            current_page: context.state.a_current_page,
                            search: context.state.a_x_search,
                            last_id: -1
                        })

                        context.commit("itemregional/update_dialog_add_coa", false);
                        context.commit("itemregional/update_a_faname", "");
                        context.commit("itemregional/update_a_coa", {});
                        context.commit("itemregional/update_a_deprecorp", "");
                        context.commit("itemregional/update_a_coa_accum_depre", {});
                        context.commit("itemregional/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)
            }
        },
        // 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.delete_data + " 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)
            }
        },
    },
}