import Vue from "vue";
import Vuex from "vuex";
import api from "@/common/api.js";
import method from "@/common/methodTool";
import ipconfig from "../common/config";
Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    hideBuyState: false, //是否隐藏购买流程和订单列表
    login: true,
    token: "",
    avatarUrl: "",
    userName: "",
    playNextId: "", //正在播放的节id
    userInfo: null,
    dictObj: null,
    chapterOpen: true,
    allowLoading: true,
    goodsAuditionConfigIdList: [], //当前访问页面的试听节ID
    shoppingCartList: [], //购物车支付商品
    applyData: {}, //预约考试数据存放
    backPageApplyData: {}, //预约考试返回页面的数据存放
    playSectionId: 0, //正在播放的录播节ID
    copyData: null, //存放审核资料数据
    playChannelId: 0, //正在播放的直播频道号
    playVID: 0, //正在播放的保利威视频ID
    liveLast: null,
    tabNums: 0, // '我的'右上角的数字
    sysTime: 0, //系统时间
    scene: 0, //进入小程序的场景值
    sac: "",
    config: {},
  },
  getters: {
    sysTime: (state) => {
      return state.sysTime;
    },
    userInfo: (state) => {
      // if (state.userInfo == null) {
      // 	if (uni.getStorageSync('user_account')) {
      // 		getUserInfo(state)
      // 	}
      // }
      return state.userInfo;
    },
    dictObj: (state) => {
      if (state.dictObj == null) {
        api.dictList().then((res) => {
          if (res.data.code === 200) {
            let newList = {};
            let list = res.data.data;
            for (let i = 0; i < list.length; i++) {
              let item = list[i];
              if (newList.hasOwnProperty(item.dictType)) {
                newList[item.dictType].push(item.dictLabel);
              } else {
                newList[item.dictType] = [item.dictLabel];
              }
            }
            state.dictObj = newList;
          }
        });
      }
      return state.dictObj;
    },
    allowLoading: (state) => {
      return state.allowLoading;
    },
    playNextId: (state) => {
      return state.playNextId;
    },
    chapterOpen: (state) => {
      return state.chapterOpen;
    },
    goodsAuditionConfigIdList: (state) => {
      return state.goodsAuditionConfigIdList;
    },
    hideBuyState: (state) => {
      return state.hideBuyState;
    },
    shoppingCartList: (state) => {
      return state.shoppingCartList;
    },
    getApplyData: (state) => {
      return state.applyData;
    },
    getBackPageApplyData: (state) => {
      return state.backPageApplyData;
    },
    playSectionId: (state) => {
      return state.playSectionId;
    },
    getCopyData: (state) => state.copyData,
    playChannelId: (state) => state.playChannelId,
    playVID: (state) => state.playVID,
    liveLast: (state) => state.liveLast,
    sac: (state) => state.sac,
    config: (state) => state.config,
  },
  mutations: {
    commonSystemTime(state, time) {
      state.sysTime = time;
    },
    tabNum(state, nums) {
      // state.tabLists[4].count = nums
      // if (nums) {
      // 	uni.setTabBarBadge({
      // 		index: 4,
      // 		text: nums + ''
      // 	})
      // } else {
      // 	uni.removeTabBarBadge({
      // 		index: 4
      // 	})
      // }
    },
    updateLiveLast(state, liveLast) {
      state.liveLast = liveLast;
    },
    updateAllowLoading(state, isShowloading) {
      state.allowLoading = isShowloading;
    },
    updatePlayNextId(state, str) {
      console.log(str, "str");
      state.playNextId = str;
    },
    updateChapterOpen(state, boolean) {
      state.chapterOpen = boolean;
    },
    updataCopyData(state, objs) {
      state.copyData = objs;
    },
    updateApplyData(state, arrays) {
      state.applyData = arrays;
    },
    updateBackApplyData(state, arrays) {
      state.backPageApplyData = arrays;
    },
    updateUserInfo(state, provider) {
      state.userInfo = provider.userInfo;
    },
    setGoodsAuditionConfigIdList(state, provider) {
      state.goodsAuditionConfigIdList = provider.goodsAuditionConfigIdList;
    },
    setShoppingCartList(state, provider) {
      state.shoppingCartList = provider.shoppingCartList;
    },
    setPlaySectionId(state, provider) {
      state.playSectionId = provider.playSectionId;
    },
    setPlayChannelId(state, provider) {
      state.playChannelId = provider.playChannelId;
    },
    setPlayVID(state, provider) {
      state.playVID = provider.playVID;
    },
    setScene(state, scene) {
      state.scene = scene;
    },
    setSac(state, sac) {
      state.sac = sac;
    },
    setConfig(state, config) {
      state.config = config;
    },
  
  },
  actions: {
    /**
     * 设置系统时间
     */
    setSystemTime({ commit }) {
      return new Promise((resolve) => {
        api.commonSystemTime().then((res) => {
          if (res.data.code == 200) {
            commit("commonSystemTime", res.data.data);
            resolve();
          }
        });
      });
    },
    changeTabsNum({ commit }) {
      api.getinfoAttached().then((res) => {
        if (res.data.code === 200) {
          const nums =
            res.data.data.informSum +
            res.data.data.orderSum +
            res.data.data.periodSum +
            res.data.data.planSum +
            res.data.data.subscribeSum;
          commit("tabNum", nums);
        }
      });
    },
    getUserInfo(context) {
      return new Promise(async (resolve) => {
        const resdata = await api.getInfo();
        if (resdata.data.code == 200) {
          context.state.userInfo = resdata.data.data;
          method.setUuid(new Date().valueOf() + "");
          resolve();
        }
      });
    },
    async appCommonConfig(context, data) {
      const resdata = await api.appCommonConfig(data);
      if (resdata.data.code == 200) {
        context.state.hideBuyState = resdata.data.data.hide;
      }
    },
    getConfig({ commit }) {
      return new Promise(async (resolve) => {
        let config = {};
        api.mobileConfig().then((res) => {
          let data = res.data.rows.find((e) => e.configKey == "home.mobile");
          if (data) {
            config = { ...config, ...JSON.parse(data.configValue) };
          }
          let header = res.data.rows.find((e) => e.configKey == "home.header");
          if (header) {
            config = {
              ...config,
              companyName: JSON.parse(header.configValue).companyName,
            };
          }
          let foot = res.data.rows.find(
            (e) => e.configKey == "home.footer.record"
          );
          if (foot) {
            config = { ...config, ...JSON.parse(foot.configValue) };
          }
          commit("setConfig", config);
          resolve(config);
        });
      });
    },
  },
});
async function getUserInfo(state) {
  const resdata = await api.getInfo();
  if (resdata.data.code == 200) {
    state.userInfo = resdata.data.data;
  }
}
export default store;
