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

export default {
    namespaced: true,
    state: {
        last_id:-1,
        last_saved_id:-1,
        x_addr_id:0,
        act:'edit',
        act_addr:'new',
        get_data_status:0,
        search_staff: 0,
        search_status: 0,
        statuses:[{label:'Bukan Kurir', value:'N'},{label:'Sebagai Kurir', value:'Y'}],
        selected_status:{label:'Bukan Kurir', value:'N'},
        search_error_message: '',
        dobs:[],
        selected_dob:{},
        dob: '',
        staff_name:'',
        staffs: [],
        total_staffs:0,
        sexes:[],
        selected_sex:{},
        address:'',
        cities: [],
        city_address: {},
        subareas: [],
        subarea_address: {},
        phone:'',
        hp:'',
        branchs:[],
        selected_branch:{},
        religions:[],
        selected_religion:{},
        startdate:'',
        enddate:'',
        positions:[],
        selected_position:{},
        nik:'',
        timework:'',
        timeworksaturday:'',
        blood:'',
        study:'', 
        iscourier:'N',
        total_staff: 0,
        selected_staff: {},
        save_status: 0,
        btn_save_seen: true,
        pgrs_save: false,
        save_error_message: '',
        no_save: 0,
        open_alert_confirmation:false,
        alert_success: false,
        msg_success: "",
        dialog_success: false,
        dialog_confirmation_delete: false,
        msg_confirmation_delete: "",
        dialog_confirmation_delete_addr: false,
        msg_confirmation_delete_addr: "",
        autocomplete_status:0,
        dialog_form_address: false,
        errors:[],
        dialog_form_position: false,
        ihsnumber: ''
    },
    mutations: {
        update_x_addr_id(state, val) {
            state.x_addr_id = val
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_last_saved_id(state, val) {
            state.last_saved_id = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_act_addr(state, val) {
            state.act_addr = val
        },
        update_get_data_status(state, val) {
            state.get_data_status = val
        },
        update_search_error_message(state, patient) {
            state.search_error_message = patient
        },
        update_search_staff(state, staff) {
            state.search_staff = staff
        },
        update_staff_name(state, val) {
            state.staff_name = val
        },
        update_staffs(state, data) {
            state.staffs = data
        },
        update_selected_staff(state, val) {
            state.selected_staff = val
        },
        update_dob(state, val) {
            state.dob = val
        },
        update_sexes(state, val) {
            state.sexes = val
        },
        update_selected_sex(state, val) {
            state.selected_sex = val
        },
        update_address(state, val) {
            state.address = val
        },
        update_cities(state, val) {
            state.cities = val
        },
        update_city_address(state, val) {
            state.city_address = val
        },
        update_subareas(state, val) {
            state.subareas = val
        },
        update_subarea_address(state, val) {
            state.subarea_address = val
        },
        update_phone(state, val) {
            state.phone = val
        },
        update_hp(state, val) {
            state.hp = val
        },
        update_religions(state, val) {
            state.religions = val
        },
        update_selected_religion(state, val) {
            state.selected_religion = val
        },
        update_branchs(state, val) {
            state.branchs = val
        },
        update_selected_branch(state, val) {
            state.selected_branch = val
        },
        update_startdate(state, val) {
            state.startdate = val
        },
        update_enddate(state, val) {
            state.enddate = val
        },
        update_positions(state, val) {
            state.positions = val
        },
        update_selected_position(state, val) {
            state.selected_position = val
        },
        update_nik(state, val) {
            state.nik = val
        },
        update_timework(state, val) {
            state.timework = val
        },
        update_timeworksaturday(state, val) {
            state.timeworksaturday = val
        },
        update_blood(state, val) {
            state.blood = val
        },
        update_study(state, val) {
            state.study = val
        },
        update_iscourier(state, val) {
            state.iscourier = val
        },
        update_btn_save_seen(state, val) {
            state.btn_save_seen = val
        },
        update_pgrs_save(state, val) {
            state.pgrs_save = val
        },
        update_save_error_message(state, msg) {
            state.save_error_message = ''
        },
        update_no_save(state, val) {
            state.no_save = val
        },
        update_open_alert_confirmation(state, val) {
            state.open_alert_confirmation = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },
        update_dialog_success(state, val) {
            state.dialog_success = val
        },
        update_dialog_confirmation_delete(state, val) {
            state.dialog_confirmation_delete = val
        },
        update_msg_confirmation_delete(state, val) {
            state.msg_confirmation_delete = val
        },
        update_dialog_confirmation_delete_addr(state, val) {
            state.dialog_confirmation_delete_addr = val
        },
        update_msg_confirmation_delete_addr(state, val) {
            state.msg_confirmation_delete_addr = val
        },
        update_addresses(state, val) {
            state.addresses = val
        },
        update_autocomplete_status(state,val){
            state.autocomplete_status = val
        },
        update_dialog_form_address(state, val) {
            state.dialog_form_address = val
        },
        update_label_address(state, val) {
            state.label_address = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_total_staffs(state, val) {
            state.total_staffs = val
        },
        update_selected_status(state, val) {
            state.selected_status = val
        },
        update_dialog_form_position(state, val) {
            state.dialog_form_position = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_ihsnumber(state, val) {
            state.ihsnumber = val
        }
    },
    actions: {
        async search(context, prm) {
            context.commit("update_search_staff", 1)
            try {
                let resp = await api.search(one_token(), prm.name, prm.snik, prm.status)
                if (resp.status != "OK") {
                    context.commit("update_search_staff", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_staff", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_staffs", data.records)
                    context.commit("update_total_staffs", data.total)
                    context.commit("update_no_save", 0)
                    if(prm.lastid === -1){
                        context.commit("update_selected_staff", data.records[0])
                        var sta = data.records[0]
                        context.commit("update_selected_branch",{M_BranchID:sta.M_StaffM_BranchID,M_BranchName:sta.M_BranchName}) 
                        context.commit("update_staff_name",sta.M_StaffName)   
                        context.commit("update_dob",sta.M_StaffDOBx)        
                        context.commit("update_selected_sex",{M_SexID:sta.M_StaffM_SexID,m_sexname:sta.m_sexname})
                        context.commit("update_address",sta.M_StaffAddress)
                        context.commit("update_cities",[{M_CityID:sta.M_StaffM_CityID,M_CityName:sta.M_CityName}])
                        context.commit("update_city_address",{M_CityID:sta.M_StaffM_CityID,M_CityName:sta.M_CityName})
                        context.commit("update_subareas",[{M_SubareaID:sta.M_StaffM_SubareaID,M_SubareaName:sta.M_SubareaName}])
                        context.commit("update_subarea_address",{M_SubareaID:sta.M_StaffM_SubareaID,M_SubareaName:sta.M_SubareaName})             
                        context.commit("update_phone",sta.M_StaffPhone)
                        context.commit("update_hp",sta.M_StaffHP)
                        context.commit("update_selected_religion",{M_ReligionID:sta.M_StaffM_ReligionID,M_ReligionName:sta.M_ReligionName})
                        context.commit("update_startdate",sta.M_StaffStartDate) 
                        context.commit("update_enddate",sta.M_StaffEndDate) 
                        context.commit("update_selected_position",{M_PositionID:sta.M_StaffM_PositionID,M_PositionName:sta.M_PositionName})
                        context.commit("update_nik",sta.M_StaffNIK) 
                        context.commit("update_timework",sta.M_StaffTimeWork) 
                        context.commit("update_timeworksaturday",sta.M_StaffTimeWorkSaturday) 
                        context.commit("update_blood",sta.M_StaffBlood) 
                        context.commit("update_study",sta.M_StaffStudy) 
                        context.commit("update_iscourier",sta.M_StaffIsCourier)
                        context.commit("update_ihsnumber",sta.OHStaffMapIhsNumber)
                    }
                    else{
                        context.commit("update_selected_staff", data.records[prm.lastid])
                        var sta = data.records[prm.lastid]
                        context.commit("update_selected_branch",{M_BranchID:sta.M_StaffM_BranchID,M_BranchName:sta.M_BranchName}) 
                        context.commit("update_staff_name",sta.M_StaffName)   
                        context.commit("update_dob",sta.M_StaffDOBx)        
                        context.commit("update_selected_sex",{M_SexID:sta.M_StaffM_SexID,m_sexname:sta.m_sexname})
                        context.commit("update_address",sta.M_StaffAddress)
                        context.commit("update_cities",[{M_CityID:sta.M_StaffM_CityID,M_CityName:sta.M_CityName}])
                        context.commit("update_city_address",{M_CityID:sta.M_StaffM_CityID,M_CityName:sta.M_CityName})
                        context.commit("update_subareas",[{M_SubareaID:sta.M_StaffM_SubareaID,M_SubareaName:sta.M_SubareaName}])
                        context.commit("update_subarea_address",{M_SubareaID:sta.M_StaffM_SubareaID,M_SubareaName:sta.M_SubareaName})            
                        context.commit("update_phone",sta.M_StaffPhone)
                        context.commit("update_hp",sta.M_StaffHP)
                        context.commit("update_selected_religion",{M_ReligionID:sta.M_StaffM_ReligionID,M_ReligionName:sta.M_ReligionName})
                        context.commit("update_startdate",sta.M_StaffStartDate) 
                        context.commit("update_enddate",sta.M_StaffEndDate) 
                        context.commit("update_selected_position",{M_PositionID:sta.M_StaffM_PositionID,M_PositionName:sta.M_PositionName})
                        context.commit("update_nik",sta.M_StaffNIK) 
                        context.commit("update_timework",sta.M_StaffTimeWork) 
                        context.commit("update_timeworksaturday",sta.M_StaffTimeWorkSaturday) 
                        context.commit("update_blood",sta.M_StaffBlood) 
                        context.commit("update_study",sta.M_StaffStudy) 
                        context.commit("update_iscourier",sta.M_StaffIsCourier)
                        context.commit("update_ihsnumber",sta.OHStaffMapIhsNumber)
                    }
                    
                }
            } catch (e) {
                context.commit("update_search_staff", 3)
                context.commit("update_search_error_message", e.message)
                console.log(e)
            }
        },
        async getsexreg(context) {
            context.commit("update_get_data_status",1)
            try {
                let resp= await api.getsexreg(one_token())
                if (resp.status != "OK") {
                    context.commit("update_get_data_status",3)
                } else {
                    context.commit("update_get_data_status",2)
                    let data = {
                        records : resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_sexes",data.records.sexes)
                    context.commit("update_religions",data.records.religions)
                    context.commit("update_branchs",data.records.branchs)
                    context.commit("update_positions",data.records.positions)
                }
            } catch(e) {
            context.commit("update_get_data_status",3)
            }
        },
        async save(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.save(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    context.commit("update_last_id", prm.M_StaffID)
                    context.commit("update_dialog_success", true)
                    var msg = "Data karyawan " + prm.M_StaffName + " sudah terupdate dong ..."
                    context.commit("update_msg_success", msg)
                    
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async newstaff(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.newstaff(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    // console.log(resp.data.id)
                    context.commit("update_last_id", resp.data.id)
                    context.commit("update_dialog_success", true)
                    var msg = "Data karyawan " + prm.M_StaffName + " sudah tersimpan dong ..."
                    context.commit("update_msg_success", msg)
                    
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async delete(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.xdelete(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    context.commit("update_last_id", 0)
                    context.commit("update_dialog_confirmation_delete",false)
                    context.commit("update_dialog_success", true)
                    var msg = "Data karyawan " + prm.M_StaffName + " sudah dihapus dong ..."
                    context.commit("update_msg_success", msg)
                    context.commit("update_selected_branch", {})
                    context.commit("update_staff_name", '')
                    context.commit("update_dob", '')
                    context.commit("update_selected_sex", {})
                    context.commit("update_address", '')
                    context.commit("update_city_address", {})
                    context.commit("update_subarea_address", {})
                    context.commit("update_phone", '')
                    context.commit("update_hp", '')
                    context.commit("update_selected_religion", {})
                    context.commit("update_startdate", '')
                    context.commit("update_enddate", '')
                    context.commit("update_selected_position", {})
                    context.commit("update_nik", '')
                    context.commit("update_timework", '')
                    context.commit("update_timeworksaturday", '')
                    context.commit("update_blood", '')
                    context.commit("update_study", '')
                    context.commit("update_iscourier",'N')
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async saveposition(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.saveposition(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                } 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_positions", resp.data.records.positions)
                    } 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)
            }
        },
        async updateposition(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.updateposition(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    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_positions", resp.data.records.positions)
                    }

                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                console.log(e)
            }
        },
        async deleteposition(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deleteposition(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                    context.commit("update_save_error_message", resp.message)
                } else {
                    context.commit("update_save_status", 2)
                    context.commit("update_save_error_message", resp.message)
                    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_positions", resp.data.records.positions)
                    }
                }
            } catch (e) {
                context.commit("update_save_status", 3)
                context.commit("update_save_error_message", e.message)
                console.log(e)
            }
        },
        async getaddress(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.getaddress(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    let data = {
                        records : resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_addresses",data.records)
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async searchcity(context,prm) {
            context.commit("update_autocomplete_status",1)
            try {
                let resp= await api.searchcity(one_token(),prm)
                if (resp.status != "OK") {
                    context.commit("update_autocomplete_status",3)
                } else {
                    context.commit("update_autocomplete_status",2)
                    let data = {
                        records : resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_cities",resp.data.records)
                }
            } catch(e) {
            context.commit("update_autocomplete_status",3)
            }
        },
        async getsubarea(context,prm) {
            context.commit("update_get_data_status",1)
            try {
                let resp= await api.getsubarea(one_token(),prm)
                if (resp.status != "OK") {
                    context.commit("update_get_data_status",3)
                } else {
                    context.commit("update_get_data_status",2)
                    let data = {
                        records : resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_subareas",resp.data.records)
                }
            } catch(e) {
            context.commit("update_get_data_status",3)
            }
        },
        async savenewaddress(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.savenewaddress(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    context.commit("update_dialog_form_address",false)
                    context.commit("update_last_id", prm.M_StaffAddressM_StaffID)
                    context.commit("update_dialog_success", true)
                    var msg = "Penambahan data alamat karyawan " + prm.M_StaffName + " sudah berhasil dong ..."
                    context.commit("update_msg_success", msg)
                    
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async saveeditaddress(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.saveeditaddress(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    context.commit("update_dialog_form_address",false)
                    context.commit("update_last_id", prm.M_StaffAddressM_StaffID)
                    context.commit("update_dialog_success", true)
                    var msg = "Perubahan data alamat karyawan " + prm.M_StaffName + " sudah berhasil dong ..."
                    context.commit("update_msg_success", msg)
                    
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        },
        async deleteaddress(context,prm) {
            context.commit("update_save_status",1)
            try {
                prm.token = one_token()
                let resp= await api.deleteaddress(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status",3)
                } else {
                    context.commit("update_save_status",2)
                    context.commit("update_dialog_confirmation_delete_addr",false)
                    context.commit("update_last_id", prm.M_StaffAddressM_StaffID)
                    context.commit("update_dialog_success", true)
                    var msg = "Penghapusan data alamat "+prm.M_StaffAddressNote+" dari karyawan " + prm.M_StaffName + " sudah berhasil dong ..."
                    context.commit("update_msg_success", msg)
                    
                }
            } catch(e) {
            context.commit("update_save_status",3)
            }
        }
    }
}