/**
 * 用户令牌信息
 */
import { loginByWxLoginCode, loginByPhoneAndSmsCode, logout } from '@/api/login';
import request from '@/utils/request';

export default {
  state: () => ({
    // login info
    id: null,
    accessToken: '',
    refreshToken: '',
    mobile: '',
    anonymous: false,
    duration: 0,
    // scope: 'CUESTOMER',
  }),

  mutations: {
    SET_ID: (state, id) => {
      state.id = id;
    },
    SET_ACCESS_TOKEN: (state, accessToken) => {
      state.accessToken = accessToken;
    },
    SET_REFRESH_TOKEN: (state, refreshToken) => {
      state.refreshToken = refreshToken;
    },
    SET_MOBILE: (state, mobile) => {
      state.mobile = mobile;
    },
    SET_ANONYMOUS: (state, anonymous) => {
      state.anonymous = anonymous;
    },
    SET_DURATION: (state, duration) => {
      state.duration = duration;
    },
    // SET_SCOPE: (state, scope) => {
    //   state.scope = scope;
    // },
  },
  actions: {
    LoginByWxCode({ commit }, payload) {
      const data = {
        scope: this.scope,
        id: 'chelvc_client',
        secret: 'chelvc@secret#2023!',
        code: payload.code,
        mobile: payload.mobile,
      }
      return new Promise((resolve, reject) => {
        loginByWxLoginCode(data)
          .then(res => {
            if (res.code === 'OK') {
              commit('SET_ID', res.data.id);
              commit('SET_ACCESS_TOKEN', res.data.accessToken);
              commit('SET_REFRESH_TOKEN', res.data.refreshToken);
              commit('SET_MOBILE', res.data.mobile);
              commit('SET_ANONYMOUS', res.data.anonymous);
              commit('SET_DURATION', res.data.duration);
              // commit('SET_SCOPE', res.data.scope);
              resolve();
            } else {
              reject(res)
            }
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    LoginBySmsCode({ commit }, payload) {
      const data = {
        scope: this.scope,
        id: 'chelvc_client',
        secret: 'chelvc@secret#2023!',
        code: payload.code,
        mobile: payload.mobile,
        captcha: payload.captcha
      }
      return new Promise((resolve, reject) => {
        loginByPhoneAndSmsCode(data)
          .then(res => {
            console.log(res);
            if (res.code === 'OK') {
              commit('SET_ID', res.data.id);
              commit('SET_ACCESS_TOKEN', res.data.accessToken);
              commit('SET_REFRESH_TOKEN', res.data.refreshToken);
              commit('SET_MOBILE', res.data.mobile);
              commit('SET_ANONYMOUS', res.data.anonymous);
              commit('SET_DURATION', res.data.duration);
              // commit('SET_SCOPE', res.data.scope);
              resolve();
            } else {
              uni.showToast({
              	title:res.message,
				icon:'none'
              })
			  return
            }
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    // 切换身份
    // SwitchIdentity({ commit }, payload) {
    //   const data = {
    //     scope: payload,
    //     token: this.getters.refreshToken,
    //     id: 'chelvc_client',
    //     secret: 'chelvc@secret#2023!'
    //   }
    //   request({
    //     url: '/maintain/token/refresh',
    //     method: 'post',
    //     data: data
    //   }).then(res=>{
    //     commit('SET_SCOPE', payload);
    //   })
    // },
    Logout({ commit }) {
      const data = {
        id: 'chelvc_client',
        secret: 'chelvc@secret#2023!',
      }
      return new Promise((resolve, reject) => {
        logout(data)
          .then(res => {
            console.log(res);
            if (res.code === 'OK') {
              commit('SET_ID', '');
              commit('SET_ACCESS_TOKEN', '');
              commit('SET_REFRESH_TOKEN', '');
              commit('SET_MOBILE', '');
              commit('SET_ANONYMOUS', false);
              commit('SET_DURATION', 0);
              resolve();
            } else {
              reject()
            }
          })
          .catch(err => {
            reject(err);
          });
      });
    }
  }
};
