// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/patient.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_patient: 0,
        search_status: 0,
        search_error_message: '',
        norm: '',
        patient_prefix: '',
        patient_name: '',
        patient_suffix: '',
        titles: [],
        selected_title: {},
        dob: '',
        patients: [],
        total_patients: 0,
        patient_address: [],
        sexes: [],
        selected_sex: {},
        religions: [],
        selected_religion: {},
        phone: '',
        hp: '',
        email: '',
        pob: '',
        kartuidentitass: [],
        selected_kartuidentitas: {},
        noidentitas: '',
        note: '',
        nik: '',
        jabatan: '',
        kedudukan: '',
        pj: '',
        location: '',
        job: '',
        total_patient: 0,
        selected_patient: {},
        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,
        label_address: '',
        addresses: [],
        cities: [],
        city_address: {},
        companies:[],
        company:{},
        mous:[],
        mou:{},
        doctors:[],
        doctor:{},
        districts: [],
        district_address: {},
        kelurahans: [],
        kelurahan_address: {},
        description_address: '',
        errors: [],
        current_page:1,
        autocomplete_status:0,
        status:{id:"N",name:"Belum di download"},
        start_date:moment(new Date()).format('YYYY-MM-DD'),
        end_date:moment(new Date()).format('YYYY-MM-DD'),
        selected_mous:[],
        selected_doctors:[],
        xid:-1,
        csv_json:[],
        data_setup:{},
        default_mu :{},
        default_doctor:{},
        setupcode:'',
        search:''
    },
    mutations: {
        update_search(state,val){
            state.search = val
        },
        update_setupcode(state,val){
            state.setupcode = val
        },
        update_default_mu(state,val){
            state.default_mu = val
        },
        update_default_doctor(state,val){
            state.default_doctor = val
        },
        update_data_setup(state,val){
            state.data_setup = val
        },
        update_csv_json(state,val){
            state.csv_json = val
        },
        update_xid(state,val){
            state.xid = val
        },
        update_selected_doctors(state,val){
            state.selected_doctors = val
        },
        update_selected_mous(state,val){
            state.selected_mous = val
        },
        update_companies(state,val){
            state.companies = val
        },
        update_company(state,val){
            state.company = val
        },
        update_mous(state,val){
            state.mous = val
        },
        update_mou(state,val){
            state.mou = val
        },
        update_doctors(state,val){
            state.doctors = val
        },
        update_doctor(state,val){
            state.doctor = val
        },
        update_autocomplete_status(state,val){
            state.autocomplete_status = val
        },
        update_start_date(state, val) {
            state.start_date = val
        },
        update_end_date(state, val) {
            state.end_date = val
        },
        update_status(state, val) {
            state.status = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        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_patient(state, patient) {
            state.search_patient = patient
        },
        update_dob(state, val) {
            state.dob = val
        },
        update_patient_prefix(state, val) {
            state.patient_prefix= val
        },
        update_patient_name(state, val) {
            state.patient_name = val
        },
        update_patient_suffix(state, val) {
            state.patient_suffix = val
        },
        update_norm(state, val) {
            state.norm = val
        },
        update_titles(state, val) {
            state.titles = val
        },
        update_selected_title(state, val) {
            state.selected_title = val
        },
        update_patients(state, data) {
            state.patients = data
        },
        update_selected_patient(state, val) {
            state.selected_patient = val
        },
        update_sexes(state, val) {
            state.sexes = val
        },
        update_selected_sex(state, val) {
            state.selected_sex = val
        },
        update_religions(state, val) {
            state.religions = val
        },
        update_selected_religion(state, val) {
            state.selected_religion = val
        },
        update_phone(state, val) {
            state.phone = val
        },
        update_email(state, val) {
            state.email = val
        },
        update_pob(state, val) {
            state.pob = val
        },
        update_hp(state, val) {
            state.hp = val
        },
        update_kartuidentitass(state, val) {
            state.kartuidentitass = val
        },
        update_selected_kartuidentitas(state, val) {
            state.selected_kartuidentitas = val
        },
        update_noidentitas(state, val) {
            state.noidentitas = val
        },
        update_note(state, val) {
            state.note = val
        },
        update_nik(state, val) {
            state.nik = val
        },
        update_jabatan(state, val) {
            state.jabatan = val
        },
        update_kedudukan(state, val) {
            state.kedudukan = val
        },
        update_pj(state, val) {
            state.pj = val
        },
        update_location(state, val) {
            state.location = val
        },
        update_job(state, val) {
            state.job = val
        },
        update_save_status(state, val) {
            state.save_status = 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_cities(state, val) {
            state.cities = val
        },
        update_city_address(state, val) {
            state.city_address = val
        },
        update_districts(state, val) {
            state.districts = val
        },
        update_district_address(state, val) {
            state.district_address = val
        },
        update_kelurahans(state, val) {
            state.kelurahans = val
        },
        update_kelurahan_address(state, val) {
            state.kelurahan_address = val
        },
        update_description_address(state, val) {
            state.description_address = val
        },
        update_search_status(state, val) {
            state.search_status = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_total_patients(state, val) {
            state.total_patients = val
        },
        update_total_patient(state, val) {
            state.total_patient = val
        }
    },
    actions: {
        async generatesetup(context,prm) {
            context.commit("update_get_data_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.generatesetup(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
                    }
                    console.log(resp.data.records)
                    context.commit("update_data_setup", resp.data.records)
                    context.commit("update_setupcode", '')
                    context.commit("update_current_page", 1)
                    //context.commit("update_patients", data.records['patients'])
                    context.dispatch("search", {
                        search:'',
                        xid:context.state.data_setup.McuOfflinePrepareID,
                        current_page: 1,
                        lastid: -1
                    })
                }
            } catch (e) {
                context.commit("update_get_data_status", 3)
            }
        },
        async search(context, prm) {
            context.commit("update_search_patient", 1)
            try {
                prm.token = one_token()
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_patient", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_patient", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_patients", data.records)
                    context.commit("update_total_patient", data.total)
                    context.commit("update_no_save", 0)

                    //context.commit("update_data_setup",  resp.data.setup)

                }
            } catch (e) {
                context.commit("update_search_patient", 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_titles", data.records.titles)
                    context.commit("update_kartuidentitass", data.records.kartuidentitass)
                }
            } catch (e) {
                context.commit("update_get_data_status", 3)
            }
        },
        async savecsv(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)
                    //location.reload()
                    context.dispatch("search", {
                        search:'',
                        xid:context.state.data_setup.McuOfflinePrepareID,
                        current_page: 1,
                        lastid: -1
                    })
                }
            } catch (e) {
                context.commit("update_save_status", 3)
            }
        },
        async gotoreg(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.gotoreg(prm)
                if (resp.status != "OK") {
                    context.commit("update_save_status", 3)
                } else {
                    context.commit("update_save_status", 2)
                    var pre_id = prm.Mcu_PreregisterDetailsID
                    location.replace("/one-ui/test/vuex/one-fo-registration-v8/" + "?pre_id=" +pre_id)
                }
            } catch (e) {
                context.commit("update_save_status", 3)
            }
        },
        async newpatient(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.newpatient(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 pasien " + prm.M_PatientName + " 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 setup " + prm.code + " sudah dihapus dong ..."
                    context.commit("update_msg_success", msg)
                    context.commit("update_company", {})
                    context.commit("update_selected_mous", [])
                    context.commit("update_mou", {})
                    context.commit("update_selected_doctors", [])
                    context.commit("update_start_date", moment(new Date()).format('YYYY-MM-DD'))
                    context.commit("update_end_date", moment(new Date()).format('YYYY-MM-DD'))
                    context.commit("update_default_mou", {})
                    context.commit("update_default_doctor", {})
                    }
            } catch (e) {
                context.commit("update_save_status", 3)
            }
        },
        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 savesetup(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.savesetup(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_company", {})
                    context.commit("update_selected_mous", [])
                    context.commit("update_mou", {})
                    context.commit("update_selected_doctors", [])
                    context.commit("update_start_date", moment(new Date()).format('YYYY-MM-DD'))
                    context.commit("update_end_date", moment(new Date()).format('YYYY-MM-DD'))
                    context.commit("update_default_mou", {})
                    context.commit("update_default_doctor", {})
                    context.commit("update_dialog_success", true)
                    if(context.state.act === 'new')
                        var msg = "Pembuatan setup nomor " + resp.data.records.xnumber + " sudah berhasil dong ..."
                    else
                        var msg = "Perubahan setup nomor " + resp.data.records.xnumber + " sudah berhasil dong ..."

                    context.commit("update_msg_success", msg)
                }
            } 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 searchdoctor(context,prm) {
            context.commit("update_autocomplete_status",1)
            try {
                var xpar = {}
                xpar.search = prm
                xpar.token = one_token()
                let resp= await api.searchdoctor(xpar)
                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
                    }
                    var arr = resp.data.records
                    var xsel = context.state.selected_doctors
                    var filtered = arr.filter(o1 => xsel.filter(o2 => o2.id === o1.id).length === 0)
                    context.commit("update_doctors",filtered)
                }
            } catch(e) {
            context.commit("update_autocomplete_status",3)
            }
        },
        async searchcompany(context,prm) {
            context.commit("update_autocomplete_status",1)
            try {
                prm.token = one_token()
                
                let resp= await api.searchcompany(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_companies",resp.data.records)
                }
            } catch(e) {
            context.commit("update_autocomplete_status",3)
            }
        },
        async getdistrict(context, prm) {
            context.commit("update_get_data_status", 1)
            try {
                let resp = await api.getdistrict(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_districts", resp.data.records)
                }
            } catch (e) {
                context.commit("update_get_data_status", 3)
            }
        },
        async getkelurahan(context, prm) {
            context.commit("update_get_data_status", 1)
            try {
                let resp = await api.getkelurahan(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_kelurahans", 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_PatientAddressM_PatientID)
                    context.commit("update_dialog_success", true)
                    var msg = "Penambahan data alamat pasien " + prm.M_PatientName + " 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_PatientAddressM_PatientID)
                    context.commit("update_dialog_success", true)
                    var msg = "Perubahan data alamat pasien " + prm.M_PatientName + " 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_PatientAddressM_PatientID)
                    context.commit("update_dialog_success", true)
                    var msg = "Penghapusan data alamat " + prm.M_PatientAddressNote + " dari pasien " + prm.M_PatientName + " sudah berhasil dong ..."
                    context.commit("update_msg_success", msg)

                }
            } catch (e) {
                context.commit("update_save_status", 3)
            }
        },
        async getmou(context,prm) {
            context.commit("update_lookup_order",1)
            try {
                prm.token = one_token()
                let resp= await api.getmou(prm)
                if (resp.status != "OK") {
                    context.commit("update_lookup_order",3)
                } else {
                    context.commit("update_lookup_order",2)
                    let data = {
                        records : resp.data.records,
                        total: resp.data.total
                    }
                    context.commit("update_mous",resp.data.records)
                    context.commit("update_mou",{})
                    context.commit("update_selected_mous",[])
                }
            } catch(e) {
            context.commit("update_lookup_order",3)
            }
        },
    }
}