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

export default {
    namespaced: true,
    state: {
        user: one_user(),
        user_level: {},
        filter_sdate: new Date().toISOString().substr(0, 10),
        filter_edate: new Date().toISOString().substr(0, 10),
        filter_cabang: {
            M_BranchID: "0",
            M_BranchCode: "All",
            M_BranchName: "Semua"
        },
        filter_status: "All",
        filter_search: "",
        listing_cabang: [],
        listing_status: [
            "All",
            "Draft",
            "Pending",
            "Verified",
            "Approved",
            "Process",
            "Completed"
        ],

        listing_tf_req: {
            total: 0,
            records: []
        },
        listing_tf_page: 1,
        selected_tf: {},

        snackbar: {
            isOpen: false,
            color: "error",
            msg: ""
        }
    },
    mutations: {
        update_filter_sdate(state, data) {
            state.filter_sdate = data
        },
        update_filter_edate(state, data) {
            state.filter_edate = data
        },
        update_filter_cabang(state, data) {
            state.filter_cabang = data
        },
        update_filter_status(state, data) {
            state.filter_status = data
        },
        update_filter_search(state, data) {
            state.filter_search = data
        },
        update_listing_cabang(state, data) {
            state.listing_cabang = data
        },
        update_listing_status(state, data) {
            state.listing_status = data
        },
        update_listing_tf_req(state, data) {
            state.listing_tf_req = data
        },
        update_listing_tf_page(state, data) {
            state.listing_tf_page = data
        },
        update_selected_tf(state, data) {
            state.selected_tf = data
        },
        update_snackbar(state, data) {
            state.snackbar = data
        },
        update_user_level(state, data) {
            state.user_level = data
        }
    },
    actions: {
        async getBranches(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID
                }
                let resp = await api.getBranches(params)
                if (resp.status != "OK") {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", snac)
                } else {
                    let all = [
                        {
                            M_BranchID: "0",
                            M_BranchCode: "All",
                            M_BranchName: "Semua"
                        }
                    ]

                    all = all.concat(resp.data.records)
                    context.commit("update_listing_cabang", all)
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", snac)
            }
        },
        async cariTF(context) {
            try {
                let params = {
                    token: one_token(),
                    startDate: context.state.filter_sdate,
                    endDate: context.state.filter_edate,
                    M_BranchCode: context.state.filter_cabang.M_BranchCode,
                    StatusName: context.state.filter_status,
                    currentPage: context.state.listing_tf_page,
                    search: context.state.filter_search
                }

                let resp = await api.search(params)
                if (resp.status != "OK") {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", snac)
                } else {
                    let data = {
                        records: resp.data.records,
                        total: parseInt(resp.data.total)
                    }
                    context.commit("update_listing_tf_req", data)
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", snac)
            }
        },
        async deleteTF(context, params) {
            try {
                params.token = one_token()
                let resp = await api.deleteTransfer(params)
                if (resp.status != "OK") {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", snac)
                } else {
                    context.dispatch("p_form/newForm", { },{ root: true })
                    context.dispatch("cariTF")

                    let snac = { isOpen: true, color: "success", msg: "Sukses"}
                    context.commit("update_snackbar", snac)
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", snac)
            }
        },
        async userlevel(context) {
            try {
                let resp = await api.getUserLevel({token: one_token()})
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", snac)
                } else {
                    context.commit("update_user_level", resp.data);
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", snac)
            }
        }
    },
}