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

export default {
    namespaced: true,
    state: {
        dialog_form: false,
        itemunitconvert: [],
        search_status: 0,
        total_itemunitconverts: 0,
        current_page: 1,
        selected_itemunitconvert: {},
        x_search: "",
        pagination:{
            descending:'asc',
            sortBy: 'UnitConvertID'
        },
        last_id: -1,
        itemx: [],
        item_convert: {},
        fromitemunits: [],
        fromitemunit_convert: {},
        toitemunits: [],
        toitemunit_convert: {},
        act: "add",
        save_status: 0,
        msg_success: "",
        errors: [],
        loading_save:false,
        error_message: '',
        save_error_message: "",
        last_id: -1,
        dialog_error:false,
        alert_success: false,
        get_data_status:false
    },
    mutations: {
        update_get_data_status(state, val) {
            state.get_data_status = val
        },
        update_dialog_form(state, val) {
            state.dialog_form = val
        },
        update_itemunitconvert(state, data){
            state.itemunitconvert = data
        },
        update_search_status(state, status){
            state.search_status = status
        },
        update_total_itemunitconverts(state,val) {
            state.total_itemunitconverts = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_selected_itemunitconvert(state, val) {
            state.selected_itemunitconvert = val
        },
        update_pagination(state, val) {
            state.pagination = val
        },
        update_x_search(state, val) {
            state.x_search = val
            state.current_page = 1
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_itemx(state, val) {
            state.itemx = val
        },
        update_item_convert(state, val) {
            state.item_convert = val
        },
        update_fromitemunits(state, val) {
            state.fromitemunits = val
        },
        update_fromitemunit_convert(state, val){
            state.fromitemunit_convert = val
        },
        update_toitemunits(state, val) {
            state.toitemunits = val
        },
        update_toitemunit_convert(state, val){
            state.toitemunit_convert = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_alert_success(state, val) {
            state.alert_success = val;
          },
        update_errors(state, val) {
            state.errors = val
        },
        update_loading_save(state, data) {
            state.loading_save = data
        },
        update_error_message(state, data) {
            state.error_message = data
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_dialog_error(state, data) {
            state.dialog_error = data
        }        
    },
    actions: {
        async search(context) {
            context.commit("update_search_status", 1)

            try {
                var prm = {
                    token: one_token(),
                    search: context.state.x_search,
                    sortBy: context.state.pagination.sortBy,
                    sortStatus: context.state.pagination.descending,
                    current_page: context.state.current_page
                }

                let resp = await api.searchitem(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status", false)
                } else {
                    context.commit("update_search_status", false)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total_page,
                        total_filter: resp.data.total_filter
                    }
                    context.commit("update_itemunitconvert", resp.data.records)
                    context.commit("update_total_itemunitconverts", resp.data.total_page)
                    if(context.state.last_id == -1){
                        if(resp.data && resp.data.records.length > 0){
                            context.commit("update_selected_itemunitconvert", resp.data.records[0])
                        }
                    }
                    else{
                        if(resp.data && resp.data.records.length > 0){
                            let idx = _.findIndex(resp.data.records, function(o) { return o.id == context.state.last_id})
                            context.commit("update_selected_itemunitconvert", resp.data.records[idx])
                        }
                    }
                }
            } catch (e) {
                context.commit("update_search_status", false)
                // console.log(e)
            }
        },

        async searchitemx(context) {
            try {
                let prm = {
                    token: one_token(),
                }
                let resp = await api.searchitemx(prm)
                if (resp.status != "OK") {
                    console.log(resp.message)
                } else {
                    let data = resp.data.records;
                    context.commit("update_itemx", resp.data.records)
                }
            } catch (e) {
                console.log(e)
            }
        },

        async fromitemunit(context,search) {
            context.commit("update_get_data_status",true)
            try {
                let prm = {}
                prm.token = one_token()
                prm.search = search

                let resp = await api.fromitemunit(prm)
                if (resp.status != "OK") {
                    console.log(resp.message);
                    context.commit("update_get_data_status",false)
                } else {
                    context.commit("update_get_data_status",false)
                    let data = {
                        records: resp.data.records
                    }
                    context.commit("update_fromitemunits", resp.data.records)
                }
            } catch (e) {
                context.commit("update_get_data_status",false)
                console.log(e);
            }
        },
        async toitemunit(context,search) {
            context.commit("update_get_data_status",true)
            try {
                let prm = {}
                prm.token = one_token()
                prm.search = search

                let resp = await api.fromitemunit(prm)
                if (resp.status != "OK") {
                    console.log(resp.message);
                    context.commit("update_get_data_status",false)
                } else {
                    context.commit("update_get_data_status",false)
                    let data = {
                        records: resp.data.records
                    }
                    context.commit("update_toitemunits", resp.data.records)
                }
            } catch (e) {
                context.commit("update_get_data_status",false)
                console.log(e);
            }
        },

        
        async save(context, prm) {
            context.commit("update_save_status", 1)            
            try {
                context.commit("update_loading_save", true)
                prm.token = one_token()
                let resp = await api.saveAdd(prm)

                if (resp.status != "OK") {
                    context.commit("update_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_loading_save", false)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_loading_save", false)
                    context.commit("update_save_error_message", "")
                    let data = {
                        total: resp.data.total,
                        records: resp.data.records
                    }

                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_dialog_form", false)
                        var msg = "Konversi satuan sudah tersimpan"
                        context.commit("update_msg_success", msg)
                        context.commit("update_alert_success", true)
                        context.dispatch("search")
                    } else {
                        context.commit("update_errors", resp.data.errors)
                    }
                }
            } catch (e) {
                context.commit("update_error_message", resp.message)
                context.commit("update_save_status", 3)
                context.commit("update_loading_save", false)
            }
        },

        async saveEdit(context, prm) {
            context.commit("update_save_status", 1)
            try {
                context.commit("update_loading_save", true)
                prm.token = one_token()
                let resp = await api.saveedit(prm)
                
                if (resp.status != "OK") {
                    context.commit("update_error_message", resp.message)
                    context.commit("update_dialog_error", true)
                    context.commit("update_loading_save", false)
                } else {
                    context.commit("update_loading_save", false)
                    context.commit("update_dialog_error", false)
                    context.commit("update_save_error_message", "")
                    context.commit("update_dialog_form", false)
                    var msg = "Konversi satuan sudah terupdate"
                    context.commit("update_msg_success", msg)
                    context.commit("update_alert_success", true)
                    context.dispatch("search")
                }
            } catch (e) {
                context.commit("update_error_message", e.message)
                context.commit("update_dialog_error", true)
                context.commit("update_loading_save", false)
                console.log(e)
            }
        },

        async delete(context, prm) {
            context.commit("update_save_status", 1)
            try {
                context.commit("update_loading_save", true)
                prm.token = one_token()
                let resp = await api.xdelete(prm)

                if (resp.status != "OK") {
                    context.commit("update_error_message", resp.message)
                    context.commit("update_dialog_error", true)
                    context.commit("update_loading_save", false)
                } else {
                    context.commit("update_loading_save", false)
                    context.commit("update_dialog_error", false)
                    context.commit("update_save_error_message", "")
                    context.commit("update_dialog_form", false)
                    var msg = "Konversi satuan sudah terhapus"
                    context.commit("update_msg_success", msg)
                    context.commit("update_alert_success", true)
                    context.dispatch("search")
                }
            } catch (e) {
                context.commit("update_error_message", e.message)
                context.commit("update_dialog_error", true)
                context.commit("update_loading_save", false)
            }
        }
    }
}