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

export default {
    namespaced: true,
    state: {
        lookup_error_message:'',
        lookup_unit:0,
        divisions:[],
        total_divisions:0,
        total_filter_divisions:0,
        current_page:1,
        x_search: '',
        autocomplete_status: 0,
        pagination:{
            descending:'asc',
            sortBy:'DivisionCode'
        },
        search_status: false,

        dialog_form_division: false,
        alert_success: false,
        msg_success: "",
        show_all: 'N',
        errors: [],
        act: 'new',

        selected_division: {
            name: ""
        },
        save_status: 0,
        save_error_message: '',
        dialogdeletealert: false,
        action_delete: false
    },
    mutations: {
        update_dialogdeletealert(state, val) {
            state.dialogdeletealert = val
            if(val == false){
                state.action_delete = false
            }
        },
        update_action_delete(state, val) {
            state.action_delete = val
        },
        update_total_divisions(state, val) {
            state.total_divisions = val
        },
        update_total_filter_divisions(state, val) {
            state.total_filter_divisions = val
        },
        update_lookup_error_message(state, status) {
            state.lookup_error_message = status
        },
        update_lookup_unit(state, status) {
            state.lookup_unit = status
        },
        update_divisions(state, data) {
            state.divisions = data
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_autocomplete_status(state, val) {
            state.autocomplete_status = val
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_pagination(state, val) {
            state.pagination = val
        },

        update_dialog_form_division(state, val) {
            console.log(state, val);
            state.dialog_form_division = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_selected_division(state, val) {
            state.selected_division = val
        },
        update_x_search(state, val) {
            state.x_search = val
            state.current_page = 1;
        },
    },
    actions: {
        // load data dan search
        async lookupbyname(context, prm) {
            context.commit("update_lookup_unit", 1)
            context.commit("update_search_status", true)
            try {
                // prm.token = one_token()
                var prm = {
                    // query:context.state.xsearch,
                    current_page:context.state.current_page,
                    // last_id:context.state.last_id,
                    search:context.state.x_search,
                    sortBy:context.state.pagination.sortBy,
                    sortStatus:context.state.pagination.descending,
                    token:one_token()
                }
                let resp = await api.lookupbyname(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status", false)
                    context.commit("update_lookup_unit", 3)
                    context.commit("update_lookup_error_message", resp.message)
                } else {
                    context.commit("update_search_status", false)
                    context.commit("update_lookup_unit", 2)
                    context.commit("update_lookup_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total,
                        total_filter: resp.data.total_filter
                    }
                    context.commit("update_divisions", data.records)
                    context.commit("update_total_divisions", data.total)
                    context.commit("update_total_filter_divisions", data.total_filter)
                    if(context.state.last_id != -1){
                        var alldatas = resp.data.records
                        // var selected = _.find(alldatas, function(o) { return o.id === context.state.last_id })
                        var selected = _.find(alldatas, function (o) {
                            return o.id === context.state.last_id;
                          });
                        context.commit("update_selected_division", selected)
                    }
                    
                }
            } catch (e) {
                context.commit("update_search_status", false)
                context.commit("update_lookup_unit", 3)
                context.commit("update_lookup_error_message", e.message)
            }
        },

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

                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form_division", false)
                        var msg =" Divisi " + prm.name + " sudah tersimpan dong ..."
                        context.commit("update_msg_success", msg)         
                        context.dispatch("lookupbyname")               
                    } else {
                        context.commit("update_errors", resp.data.errors)

                    }

                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                console.log(e)
            }
        },

        // hapus
        async delete(context, prm) {
            context.commit("update_save_status", 1)
            try {
                let resp = await api.xdelete(one_token(), prm.id)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_dialogdeletealert", true)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_alert_success", true)

                    var msg = " Divisi " + prm.name + " sudah dihapus dong"
                    context.commit("update_msg_success", msg)
                    context.commit("update_alert_success", true)
                    context.commit("update_action_delete", false)
                    // context.commit("update_selected_unit", {})
                    context.dispatch("lookupbyname")  
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                console.log(e)
            }
        }, 

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

                    if (data.total !== -1) {
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form_division", false)
                        var msg = " Divisi " + prm.name + " sudah terupdate dong ..."
                        context.commit("update_msg_success", msg)
                        context.dispatch("lookupbyname")   
                    }else {
                        context.commit("update_errors", resp.data.errors)

                    }

                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                console.log(e)
            }
        },
    }
}