import * as constructionApi from "@/api/construction-personnel";

export default {
    namespaced: true,
    state: {
        company: []
    },
    mutations: {
        setVal(state, obj) {
            state[obj.key] = obj.value;
            console.log(obj);
        }
    },
    actions: {
        /*
            如果厂商列表变化记得调用一下 getCompany({ rest: true }) 重新刷新数据源
        */
        getCompany({ commit, state }, payload = {}) {
            // 减少请求次数
            const rest = payload.rest || false;
            if (state.company && state.company.length && !rest) {
                return;
            }
            constructionApi
                .manufacturer(payload)
                .then((res) => {
                    commit("setVal", { key: "company", value: res });
                })
                .catch((error) => {
                    alert("获取厂商错误");
                });
        }
    },
    getters: {
        company: (state) => state.company
    }
};
