// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/item.js"

export default {
    namespaced: true,
    state: {
        autocomplete_status: 0,
        search_error_message: "",
        groups: [],
        selected_group: {"Nat_GroupID": "0", "Nat_GroupName": "All"},
        search_status: 0,
        items: [],
        total_page: 0,
        selected_item: {},
        current_page: 1,
        xsearch: "",
        dialog_form: false,
        act: "new",
        categories: [],
        selected_category: {},
        group_x: [],
        selected_group_x: {},
        subgroups: [],
        selected_subgroup: {},
        fa_class: [],
        selected_fa_class: {},
        fa_inventaris_gol: [],
        selected_fa_inventaris_gol: {},
        errors: [],
        item_units: [],
        selected_item_unit: {},
        class_type: [
            {"id": 1, "Fa_ClassType": "Kendaraan", "flagtype": "vehicle"},
            {"id": 2, "Fa_ClassType": "Operasional", "flagtype": "operational"},
            {"id": 3, "Fa_ClassType": "Gedung", "flagtype": "building"},
            {"id": 4, "Fa_ClassType": "Mechanical Elektrical & IT", "flagtype": "meit"},
            {"id": 5, "Fa_ClassType": "Software & Laboratorium System", "flagtype": "software"},
            {"id": 6, "Fa_ClassType": "Peralatan Kantor", "flagtype": "office"},
            {"id": 7, "Fa_ClassType": "Peralatan Umum", "flagtype": "general"},
            {"id": 8, "Fa_ClassType": "Tanah", "flagtype": "land"},
        ],
        selected_class_type: {},

        save_status: 0,
        item_errors: [],
        alert_success: false,
        alert_error: false,
        msg_success: "",
        save_error_message: "",
        opendialoginfo: false,
        msginfo: "",
        last_id: 0,
    },
    mutations: {
        update_autocomplete_status(state, val) {
            state.autocomplete_status = val
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_groups(state, val) {
            state.groups = val
        },
        update_selected_group(state, val) {
            state.selected_group = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_items(state, val) {
            state.items = val
        },
        update_total_page(state, val) {
            state.total_page = val
        },
        update_selected_item(state, val) {
            state.selected_item = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_xsearch(state, val) {
            state.xsearch = val
            state.current_page = 1
        },
        update_dialog_form(state, val) {
            state.dialog_form = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_categories(state, val) {
            state.categories = val
        },
        update_selected_category(state, val) {
            state.selected_category = val
        },
        update_group_x(state, val) {
            state.group_x = val
        },
        update_selected_group_x(state, val) {
            state.selected_group_x = val
        },
        update_subgroups(state, val) {
            state.subgroups = val
        },
        update_selected_subgroup(state, val) {
            state.selected_subgroup = val
        },
        update_fa_class(state, val) {
            state.fa_class = val
        },
        update_selected_fa_class(state, val) {
            state.selected_fa_class = val
        },
        update_fa_inventaris_gol(state, val) {
            state.fa_inventaris_gol = val
        },
        update_selected_fa_inventaris_gol(state, val) {
            state.selected_fa_inventaris_gol = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_item_units(state, val) {
            state.item_units = val
        },
        update_selected_item_unit(state, val) {
            state.selected_item_unit = val
        },
        update_class_type(state, val) {
            state.class_type = val
        },
        update_selected_class_type(state, val) {
            state.selected_class_type = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_item_errors(state, val) {
            state.item_errors = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_alert_error(state, val) {
            state.alert_error = val
        },
        update_opendialoginfo(state, val) {
            state.opendialoginfo = val
        },
        update_msginfo(state, val) {
            state.msginfo = val
        },
        update_last_id(state, val) {
            state.last_id = val
        },
    },
    actions: {
        async getGroup(context) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getGroup({ token: one_token() });
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_groups", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async search(context, prm) {
            context.commit("update_search_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.search(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_search_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_search_status", 2)
                    let data = {
                        records: resp.data.records,
                        total_page: resp.data.total_page,
                    };

                    context.commit("update_items", data.records);
                    context.commit("update_total_page", data.total_page);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_search_status", 3)
            }
        },
        async getCategory(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getCategory(one_token(), prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_categories", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async getGroupX(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.getGroupX(one_token(), prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_group_x", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async get_subgroup(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.get_subgroup(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_subgroups", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async get_fa_class(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.get_fa_class(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_fa_class", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async get_inventaris_gol(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.get_inventaris_gol(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_fa_inventaris_gol", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },
        async get_unit(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.get_unit(prm);
                if (resp.status != "OK") {
                    context.commit("update_search_error_message", resp.message)
                    context.commit("update_autocomplete_status", 3)
                } else {
                    context.commit("update_search_error_message", "")
                    context.commit("update_autocomplete_status", 2)
                    let data = {
                        records: resp.data.records,
                    };

                    context.commit("update_item_units", data.records);
                }
            } catch (e) {
                context.commit("update_search_error_message", e.message)
                context.commit("update_autocomplete_status", 3)
            }
        },

        // save item
        async save_item(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.save_item(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };

                    if (data.total !== -1) {
                        context.commit("update_item_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = "Item " + prm.itemname + " sudah tersimpan dong ..."
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            group_id: context.state.selected_group.Nat_GroupID
                        })
                    } else {
                        context.commit("update_item_errors", resp.data.errors)
                        context.commit("update_opendialoginfo", true)
                        context.commit("update_msginfo", resp.data.errors[0].msg)
                    }
                }
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },

        // edit item
        async update_item(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.update_item(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };

                    if (data.total !== -1) {
                        context.commit("update_item_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = "Item " + prm.itemname + " sudah terupdate dong ..."
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            group_id: context.state.selected_group.Nat_GroupID
                        })
                    } else {
                        context.commit("update_item_errors", [])
                        context.commit("update_opendialoginfo", true)
                        context.commit("update_msginfo", "error update")
                    }
                }
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },

        // delete item
        async delete_item(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.delete_item(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };

                    if (data.total !== -1) {
                        context.commit("update_item_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = "Item " + prm.itemname + " sudah terhapus dong ..."
                        context.commit("update_msg_success", msg)
                        context.dispatch("search", {
                            current_page: context.state.current_page,
                            search: context.state.xsearch,
                            group_id: context.state.selected_group.Nat_GroupID
                        })
                    } else {
                        context.commit("update_item_errors", [])
                        context.commit("update_opendialoginfo", true)
                        context.commit("update_msginfo", "error delete")
                    }
                }
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },
    }
};
