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

export default {
    namespaced: true,
    state: {
        a_errors_save_coa: [],
        selected_item: {},
        items: [],
        a_save_status: 0,
        a_save_error_message: '',
        a_msg_success: '',
        a_msg_info: '',
        a_open_dialog_info: false,
        a_alert_error: false,
        a_alert_success: false,
        // search table
        a_x_search: '',
        a_current_page: 1,
        a_last_id: -1,
        total_item: 0,
        // search table

        // dropdown category
        selected_category: {},
        categorys: [],

        // array item division
        item_division_arr: [],

        // item division selected
        selected_itemdivision: {},
        // list item division
        itemdivisions: [],

        // state dialog
        msg_gagal: "",
        dialog_gagal: false,
        msg_success: "",
        dialog_success: false,
    },
    mutations: {
        update_msg_success(state, val) {
            state.msg_success = val;
        },
        update_dialog_success(state, val) {
            state.dialog_success = val;
        },
        update_msg_gagal(state, val) {
            state.msg_gagal = val;
        },
        update_dialog_gagal(state, val) {
            state.dialog_gagal = val;
        },
        update_a_errors_save_coa(state, val) {
            state.a_errors_save_coa = val
        },
        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_search_statusr(state, val) {
            state.a_search_statusr = val
        },
        update_selected_item(state, val) {
            state.selected_item = val
        },
        update_items(state, val) {
            state.items = 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_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_total_item(state, val) {
            state.total_item = val
        },
        update_a_msg_success(state, val) {
            state.a_msg_success = 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_alert_success(state, val) {
            state.a_alert_success = val
        },
        update_a_alert_error(state, val) {
            state.a_alert_error = val
        },
        update_dialog_add_coa(state, val) {
            state.dialog_add_coa = val
        },
        update_dialog_edit_coa(state, val) {
            state.dialog_edit_coa = val
        },
        update_a_loading_category(state, val) {
            state.a_loading_nat_group = val
        },
        update_e_loading_nat_group(state, val) {
            state.e_loading_nat_group = val
        },
        update_a_nat_group(state, val) {
            state.a_nat_group = val
        },
        update_e_nat_group(state, val) {
            state.e_nat_group = val
        },
        update_a_nat_group_list(state, val) {
            state.a_nat_group_list = val
        },
        update_e_nat_group_list(state, val) {
            state.e_nat_group_list = val
        },

        // dropdown category
        update_selected_category(state, val) {
            state.selected_category = val
        },

        update_categorys(state, val) {
            state.categorys = val
        },

        // array item division
        update_item_division_arr(state, val) {
            state.item_division_arr = val
        },

        // item division selected
        update_selected_itemdivision(state, val) {
            state.selected_itemdivision = val
        },

        update_itemdivisions(state, val) {
            state.itemdivisions = val
        },

        // update item division arr
        update_itemdivision_arr(state, val) {
            const idx = state.itemdivisions.findIndex(
                item => item.M_ItemDivisionID === val.M_ItemDivisionID
            );

            if (idx !== -1) {
                state.itemdivisions[idx].IsChecked = val.IsChecked;
            }

            const arrIndex = state.item_division_arr.findIndex(
                item => item.M_ItemDivisionID === val.M_ItemDivisionID
            );

            if (arrIndex !== -1) {
                state.item_division_arr.splice(arrIndex, 1);
                state.item_division_arr.push(val);
            } else {
                if (val.IsChecked === "1") {
                    state.item_division_arr.push(val);
                } else {
                    state.item_division_arr.push(val);
                }
            }
        },
    },
    actions: {
        // get category from api
        async getDropdownCategoryA(context) {
            context.commit("update_a_loading_category", true)
            try {
                let prm = {};
                prm.token = one_token()
                let resp = await api.getDropdownCategory(prm)
                // console.log('data nat_group ',resp)
                if (resp.status != "OK") {
                    context.commit("update_a_loading_category", false)
                } else {
                    // console.log('data nat_group ',resp.data)
                    context.commit("update_a_loading_category", false)
                    context.commit("update_categorys", resp.data.records)
                    // context.commit("update_selected_category", resp.data.records[0])
                }
            } catch (e) {
                context.commit("update_a_loading_category", false)
            }
        },

        // searchItem
        async searchItemA(context, prm) {
            context.commit("update_a_search_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.searchItem(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_items", data.records)
                    context.commit("update_total_item", 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)
            }
        },

        // item division list by item selected
        async itemDivisionListA(context, prm) {
            context.commit("update_a_search_status", 1)
            try {
                // prm.token = one_token()
                var prm = {
                    current_page: context.state.current_pager,
                    M_ItemID: context.state.selected_item.M_ItemID,
                    last_id: -1,
                    current_page: -1,
                    token: one_token()
                };

                let resp = await api.itemDivisionList(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_itemdivisions", data.records)
                    // context.commit("update_total_item", 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)
            }
        },

        // update item division
        async updateItemDivisionA(context, prm) {
            context.commit("update_a_search_status", 1)
            try {
                var prm = {
                    M_ItemID: context.state.selected_item.M_ItemID,
                    M_ItemDivisionID: context.state.item_division_arr,
                    last_id: -1,
                    current_page: -1,
                    token: one_token()
                };

                let resp = await api.updateItemDivision(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)

                    context.commit("update_dialog_success", true);
                    var msg = "Update Sukses";
                    context.commit("update_msg_success", msg);

                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }

                    context.commit("update_itemdivisions", data.records)
                }
            } 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)
            }
        },
    },
}