import { getEntPolicy, saveEntPolicy } from "@/api/sys-ent-policy.js";

export const POLICY_ID = "P2022040101";
export const SET_LOADING_POLICY = "SET_LOADING_POLICY";
export const SET_POLICY_RESULT = "SET_POLICY_RESULT";
export const SET_DISPLAYED_TIP = "SET_DISPLAYED_TIP";

export default {
  namespaced: true,
  state: {
    // 获取政策；
    loadingPolicy: false,
    policyResult: null,

    // 是否已经显示提示弹窗，防止多次弹窗；
    displayedTip: false,
  },
  getters: {
    /**
     * 存在错误；
     */
    existError(state) {
      let result = false;
      if (!state.loadingPolicy) {
        result = state.policyResult && state.policyResult.code !== 0;
      }
      return result;
    },
    /**
     * 政策是否有效；
     */
    policyValid(state) {
      // 默认 false 无效；
      let result = false;
      if (
        !state.loadingPolicy &&
        state.policyResult &&
        state.policyResult.code === 0
      ) {
        result = state.policyResult.data.policyValid;
      }
      return result;
    },
    /**
     * 是否使用政策标识；
     */
    useFlag(state) {
      // 默认 false 不使用；
      let result = false;
      if (
        !state.loadingPolicy &&
        state.policyResult &&
        state.policyResult.code === 0
      ) {
        result = state.policyResult.data.useFlag;
      }
      return result;
    },
    /**
     * 是否提示标识;
     */
    tipFlag(state) {
      // 默认 false 不提示；
      let result = false;
      if (
        !state.loadingGetPolicy &&
        state.policyResult &&
        state.policyResult.code === 0
      ) {
        result = state.policyResult.data.tipFlag;
      }
      return result;
    },
    /**
     * 是否显示提示弹窗;
     */
    visibleTip(state, getters) {
      return getters.policyValid && getters.tipFlag && !state.displayedTip;
    },
  },
  mutations: {
    [SET_LOADING_POLICY](state, val) {
      state.loadingPolicy = val;
    },
    [SET_POLICY_RESULT](state, val) {
      state.policyResult = val;
    },
    [SET_DISPLAYED_TIP](state, val) {
      state.displayedTip = val;
    },
  },
  actions: {
    /**
     * 获取政策。
     */
    getPolicyAsync({ state, getters, dispatch }) {
      if (!state.policyResult || getters.existError) {
        dispatch("refreshPolicyAsync");
      }
    },

    /**
     * 刷新政策；
     * 多次调用忽略。
     */
    refreshPolicyAsync({ state, commit }) {
      if (!state.loadingPolicy) {
        commit(SET_LOADING_POLICY, true);
        getEntPolicy(POLICY_ID)
          .then((res) => {
            console.log("refreshPolicyAsync", res);
            commit(SET_POLICY_RESULT, res);
          })
          .catch((err) => {
            console.log("refreshPolicyAsync err", err);
            commit(SET_POLICY_RESULT, err);
          })
          .finally(() => {
            commit(SET_LOADING_POLICY, false);
          });
      }
    },

    /**
     * 保存政策。
     */
    savePolicyAsync(
      { state, getters, commit },
      { useFlag = null, tipFlag = null }
    ) {
      return new Promise((resolve, reject) => {
        if (state.policyResult && !getters.existError) {
          const policyResultCopy = JSON.parse(
            JSON.stringify(state.policyResult)
          );
          if (useFlag !== null) {
            policyResultCopy.data.useFlag = useFlag;
          }
          if (tipFlag != null) {
            policyResultCopy.data.tipFlag = tipFlag;
          }

          saveEntPolicy(policyResultCopy.data)
            .then((res) => {
              console.log("savePolicyAsync", res);

              // 更新政策；
              commit(SET_POLICY_RESULT, policyResultCopy);
              resolve(res);
            })
            .catch((err) => {
              console.log("savePolicyAsync err", err);
              reject(err);
            });
        } else {
          reject({ msg: `无政策“${POLICY_ID}”信息，无法保存！` });
        }
      });
    },
  },
};
