import * as api from "../api/api.js";

export default {
    namespaced: true,
    state: {
        user: one_user(),
        snackbar: {
            color: "success",
            msg: "",
            state: false
        },

        // left side
        filter_listreg: [],
        filter_listcab: [],
        filter_listing: {
            regional: "",
            cabang: 0,
            keyword: ""
        },
        list_ruangan: {
            total: 0,
            records: []
        },
        list_pagination: 1,

        // right side
        form_status: "BARU",
        form_listreg: [],
        form_listcab: [],
        form_detail: {
            id: 0,
            nama: "",
            kode: "",
            cabang: "",
            regional: ""
        }
    },
    mutations: {
        update_snackbar(state, val) {
            state.snackbar = val;
        },

        // left side
        update_filter_listreg(state, val) {
            state.filter_listreg = val;
        },
        update_filter_listcab(state, val) {
            state.filter_listcab = val;
        },
        update_filter_listing(state, val) {
            state.filter_listing = val;
        },
        update_list_ruangan(state, val) {
            state.list_ruangan = val;
        },
        update_list_pagination(state, val) {
            state.list_pagination = val;
        },

        // right side
        update_form_status(state, val) {
            state.form_status = val
        },
        update_form_listreg(state, val) {
            state.form_listreg = val;
        },
        update_form_listcab(state, val) {
            state.form_listcab = val;
        },
        update_form_detail(state, val) {
            state.form_detail = val
        }
    },
    actions: {
        async listingRegional(context, side) {
            try {
                const param = { token: one_token() }

                const resp = await api.listingRegional(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                const datas = resp.data.records;
                const users = context.state.user;

                if (side == 'left') {
                    const sel_reg = datas.find((reg) => reg.S_RegionalID == users.S_RegionalID);
                    let filter = context.state.filter_listing;
                    filter.regional = sel_reg.S_RegionalID;

                    context.commit("update_filter_listing", filter);
                    context.commit("update_filter_listreg", datas);
                } else {
                    let reg = users.loginlevel == 'pusat'
                        ? datas
                        : datas.filter((r) => r.S_RegionalID == users.S_RegionalID);
                    context.commit("update_form_listreg", reg);
                }
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },

        async listingCabang(context, side) {
            try {
                let param = {
                    token: one_token(),
                    regionalID: side == "left"
                        ? context.state.filter_listing.regional
                        : context.state.form_detail.regional
                }

                const resp = await api.listingCabang(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }
                const users = context.state.user;

                if (side == "left") {
                    let datas = [{ M_BranchID: 0, M_BranchName: "SEMUA" }, ...resp.data.records]
                    const usr_cab = datas.find((cab) => cab.M_BranchID == users.M_BranchID);
                    let filter = context.state.filter_listing;
                    filter.cabang = usr_cab.M_BranchID;

                    context.commit("update_filter_listing", filter);
                    context.commit("update_filter_listcab", datas);
                } else {
                    context.commit("update_form_listcab", resp.data.records);
                }
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },

        async searchRuangan(context) {
            try {
                const param = {
                    token: one_token(),
                    latestPage: context.state.list_pagination,
                    regionalID: context.state.filter_listing.regional,
                    branchID: context.state.filter_listing.cabang ?? 0
                }

                const resp = await api.searchRuangan(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                context.commit("update_list_ruangan", resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },

        async insertRuangan(context) {
            try {
                const param = {
                    token: one_token(),
                    regionalID: context.state.form_detail.regional,
                    branchID: context.state.form_detail.cabang,
                    ruanganName: context.state.form_detail.nama,
                    ruanganCode: context.state.form_detail.kode
                }

                const resp = await api.insertRuangan(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                const snac = { color: 'success', msg: "sukses insert data ruangan", state: true };
                context.commit("update_snackbar", snac);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },

        async updateRuangan(context, id) {
            try {
                const param = {
                    token: one_token(),
                    ruanganID: id,
                    regionalID: context.state.form_detail.regional,
                    branchID: context.state.form_detail.cabang,
                    ruanganName: context.state.form_detail.nama,
                    ruanganCode: context.state.form_detail.kode
                }
                console.log(param);

                const resp = await api.updateRuangan(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                const snac = { color: 'success', msg: "sukses update data ruangan", state: true };
                context.commit("update_snackbar", snac);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },

        async deleteRuangan(context, id) {
            try {
                const param = {
                    token: one_token(),
                    ruanganID: id
                }

                const resp = await api.deleteRuangan(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                const snac = { color: 'success', msg: "sukses delete data ruangan", state: true };
                context.commit("update_snackbar", snac);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
    }
}
