import {
  paperDimensionList,
  paperStructList,
  paperDefineList,
  moduleEvaluationList
} from "../../api";

const state = {
  baseInfo: {},
  dimensionList: [],
  structList: [],
  defineList: null,
  evaluationList: []
};

const mutations = {
  setBaseInfo(state, baseInfo) {
    state.baseInfo = baseInfo;
  },
  setDimensionList(state, dimensionList) {
    state.dimensionList = dimensionList;
  },
  setStructList(state, structList) {
    state.structList = structList;
  },
  setDefineList(state, defineList) {
    state.defineList = defineList;
  },
  setEvaluationList(state, evaluationList) {
    state.evaluationList = evaluationList;
  },
  initConfigStore(state) {
    state.baseInfo = {};
    state.dimensionList = [];
    state.structList = [];
    state.defineList = null;
    state.evaluationList = [];
  }
};

const actions = {
  async fetchDimensionList({ state, commit }) {
    const datas = {
      paperNumber: state.baseInfo.paperNumber,
      paperType: state.baseInfo.paperType
    };
    const data = await paperDimensionList(datas).catch(() => {});
    if (!data) return Promise.reject();
    commit("setDimensionList", data);
  },
  async fetchStructList({ state, commit }) {
    const datas = {
      paperNumber: state.baseInfo.paperNumber,
      paperType: state.baseInfo.paperType
    };
    const data = await paperStructList(datas).catch(() => {});
    if (!data) return Promise.reject();
    commit("setStructList", data);
  },
  async fetchDefineList({ state, commit }) {
    const datas = {
      paperNumber: state.baseInfo.paperNumber,
      paperType: state.baseInfo.paperType
    };
    let err = false;
    const data = await paperDefineList(datas).catch(() => {
      err = true;
    });
    if (err) return Promise.reject();
    commit("setDefineList", data);
  },
  async fetchEvaluationList({ state, commit }) {
    const datas = {
      paperNumber: state.baseInfo.paperNumber,
      paperType: state.baseInfo.paperType
    };
    const data = await moduleEvaluationList(datas).catch(() => {});
    if (!data) return Promise.reject();
    commit("setEvaluationList", data);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
