import { commonAuth } from '@/api/pubic.js'
import { loginMobile } from '@/api/user.js'
import { loginAccount } from '@/api/oaUser.js'
import Routine from '@/libs/routine.js'
import Cache from '@/utils/cache';
import encrypt from '@/utils/encrypt.js';
import oaHttp from '@/utils/oahttp.js';

const state = {
  userInfo: JSON.parse(Cache.get('USER_INFO') || '{}') || null,
  token: Cache.get("TOKEN") || null
};

const mutations = {
  setUserInfo(state, data) {
    state.userInfo = data
    Cache.set("USER_INFO", data)
  },
  LOGOUT(state) {
    Cache.clear('USER_INFO')
    Cache.clear('TOKEN')
    uni.showModal({
      content: '登录已过期,是否重新登录?',
      success(e) {
        if (e.confirm) uni.reLaunch({
          url: '/pages/oaLogin/oaLogin'
        })
      }
    })
  },
  CLEAR(state) {
    state.userInfo = null;
    state.token = null;
    Cache.clear('USER_INFO')
    Cache.clear('TOKEN')
  },
  UPDATE_USERINFO(state, data) {
    let time = res.data.result.expires_time - Cache.time();
    state.userInfo = data.result.user
    state.token = data.result.token
    Cache.set("USER_INFO", data.result.user, time)
    Cache.set("TOKEN", data.result.token, time)
  },
  SET_USERINFO(state, data) {
    let time = Cache.time();
    state.userInfo = data.user
    state.token = data.token
    Cache.set("USER_INFO", data.user, time)
    Cache.set("TOKEN", data.token, time)
  },
  SET_TOKEN(state, data) {
    let time = Cache.time();
    state.token = data.token;
    Cache.set("TOKEN", data.token, time);
  },
};

const actions = {
  RE_LOGIN({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      let fromData = encrypt.decode('ACT');
      if(fromData) {
        loginAccount({ ...fromData }, true).then((res) => {
          commit('SET_TOKEN', res.data);
          oaHttp[data.method](data.url, data.data, data.opt).then((e) => {
            resolve(e);
          }).catch((err) => {
            reject(err)
          })
        }).catch((err) => {
          commit('LOGOUT')
          reject(err)
        })
      }else {
        commit('LOGOUT')
        reject();
      }
    })
  },
  MobileLogin({ state, commit }, force) {
    let data = {
      auth_token: uni.getStorageSync('auth_token'),
      phone: force.account,
      sms_code: force.captcha,
      spread: that.$Cache.get("spread"),
      // #ifdef APP-PLUS
      user_type: 'app',
      // #endif
      // #ifdef H5
      user_type: 'h5',
      // #endif
    }
    loginMobile(data).then(res => {
      console.log('手机号登录', res);
    })
  },
  async getWxLogin({ state, commit }, force) {
    let newCode = null
    Routine.getCode().then(code => {
      newCode = code;
    })
    Routine.getUserProfile().then(res => {
      let userInfo = res.userInfo;
      userInfo.code = newCode;
      commonAuth({
        auth: {
          type: 'routine',
          auth: userInfo
        }
      }).then(res => {
        commit("UPDATE_USERINFO", res.data);
      })
    })
  }
};

export default {
  state,
  mutations,
  actions
};