import { getMobileUserInfo, getDictOption, loginLog, getReportInfo } from '@/api/index';

const user = {
  state: {
    userInfo: null,
    activaTypeStore: null, //未拜访门店
    activaCreateTypeStore: null, //建店未完工、未结案门店
    storeType: [],
    reportInfo: null,
  },

  mutations: {
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_ACTIVA_TYPE_STORE: (state, value) => {
      state.activaTypeStore = value;
    },
    SET_STORE_TYPE: (state, value) => {
      state.storeType = value;
    },
    SET_ACTIVA_CREATE_TYPE_STORE: (state, value) => {
      state.activaCreateTypeStore = value;
    },
    SET_REPORT_INFO: (state, reportInfo) => {
      state.reportInfo = reportInfo;
    },
  },

  actions: {
    // 获取用户信息
    getUserInfo({ dispatch, commit, state }) {
      return new Promise((resolve, reject) => {
        getMobileUserInfo()
          .then((res) => {
            commit('SET_USER_INFO', res.data);
            localStorage.setItem('nickName', res.data.nickName);
            localStorage.setItem('postName', res.data.postName);
            localStorage.setItem('zipPhoto', res.data.zipPhoto);
            //deptLevel 0：本部；1：公司；2：大区；3：销售部
            localStorage.setItem('deptLevel', res.data.depts[0].deptLevel);
            localStorage.setItem('userId', res.data.userId);
            localStorage.setItem('deptIds', JSON.stringify(res.data.deptIds));
            localStorage.setItem('chainUser', res.data.chainUser); //是否经销商用户 true
            localStorage.setItem('getRequestFlage', 'true');
            // userDeptLevel用户部门等级字段，0-总部，1-公司，2-大区，3-销售部，4-业务员
            localStorage.setItem('userDeptLevel', res.data.userDeptLevel);
            getReportInfo({ isContent: false }).then((res) => {
              if (res.code == 200) {
                commit('SET_REPORT_INFO', res.data);
                localStorage.setItem('powerGrade', res.data.positionId);
                localStorage.setItem('isDiy', res.data.diy);
                localStorage.setItem('uType', res.data.userType);
                localStorage.setItem('jzType', res.data.jzType);
                localStorage.setItem('customerVisits', res.data.customerManagerVisits);
                localStorage.setItem('postType', res.data.postType);
                localStorage.setItem('reportType', res.data.reportType);
                // 门店类型
                getDictOption({}, 'sfa_store_type').then((res) => {
                  commit('SET_STORE_TYPE', res.data);
                  resolve();
                });
              }
            });
            // 登录日志
            var appVersion = navigator.appVersion;
            var Logapp = appVersion.split('(')[1].split(';');
            var arrLog = '';
            if (Logapp[0] == 'Linux') {
              arrLog = {
                browser: navigator.appCodeName,
                os: Logapp[1],
                phoneBrand: Logapp[2].split('/')[1],
                phoneModel: Logapp[2].split('/')[0],
              };
            } else {
              arrLog = {
                browser: navigator.appCodeName,
                os: Logapp[0],
                phoneBrand: '',
                phoneModel: '',
              };
            }
            loginLog(arrLog);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 储存提示类-未拜访-从那个店铺类型进入未拜访列表
    setActivaTypeStore({ commit }, value) {
      commit('SET_ACTIVA_TYPE_STORE', value);
    },
    // 储存提示类-建店-从那个店铺类型进入未完工、未结案列表
    activaCreateTypeStore({ commit }, value) {
      commit('SET_ACTIVA_CREATE_TYPE_STORE', value);
    },
    // 获取用户ReportInfo
    getReportInfoDeta({ dispatch, commit, state }) {
      return new Promise((resolve, reject) => {
        getReportInfo({ isContent: false })
          .then((res) => {
            if (res.code == 200) {
              commit('SET_REPORT_INFO', res.data);
              localStorage.setItem('powerGrade', res.data.positionId);
              localStorage.setItem('isDiy', res.data.diy);
              localStorage.setItem('uType', res.data.userType);
              localStorage.setItem('jzType', res.data.jzType);
              localStorage.setItem('customerVisits', res.data.customerManagerVisits);
              localStorage.setItem('postType', res.data.postType);
              localStorage.setItem('reportType', res.data.reportType);
              resolve();
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};

export default user;
