import { login, logout, getInfo } from '@/api/user';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { resetRouter } from '@/router';
import { isLogOut, notLogOut } from '@/utils/waitLogOut';

const getDefaultState = () => {
  return {
    token: getToken(),
    isOperate: '',
    isShowPurchasePrice: '',
    isShowSellPrice: '',
    name: '',
    role: '',
    username: '',
    phone: ''
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_USERNAME: (state, username) => {
    state.username = username;
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone;
  },
  SET_ISOPERATE: (state, isOperate) => {
    state.isOperate = isOperate;
  },
  SET_ROLE: (state, role) => {
    state.role = role;
  },
  SET_ISSHOWPURCHASEPRICE: (state, isShowPurchasePrice) => {
    state.isShowPurchasePrice = isShowPurchasePrice;
  },
  SET_ISSHOWSELLPRICE: (state, isShowSellPrice) => {
    state.isShowSellPrice = isShowSellPrice;
  }
};

const actions = {
  login({ commit }, userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        // console.log(response);
        // 标记登录,开始检测长时间未操作情况
        notLogOut();

        const { data } = response;
        commit('SET_ISOPERATE', data.isOperate);
        commit('SET_ISSHOWPURCHASEPRICE', data.isShowPurchasePrice);
        commit('SET_ISSHOWSELLPRICE', data.isShowSellPrice);
        commit('SET_NAME', data.name);
        commit('SET_PHONE', data.phone);
        commit('SET_USERNAME', data.username);
        commit('SET_ROLE', data.type);
        commit('SET_TOKEN', data.token);
        setToken(data.token);
        resolve();
      }).catch(error => {
        console.log(error);
        reject(error);
      });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response;
        // console.log(data);
        if (!data) {
          return reject('Verification failed, please Login again.');
        }

        commit('SET_ISOPERATE', data.isOperate);
        commit('SET_ISSHOWPURCHASEPRICE', data.isShowPurchasePrice);
        commit('SET_ISSHOWSELLPRICE', data.isShowSellPrice);
        commit('SET_NAME', data.name);
        commit('SET_PHONE', data.phone);
        commit('SET_USERNAME', data.username);
        commit('SET_ROLE', data.type);
        commit('SET_TOKEN', data.token);
        resolve(data);
      }).catch(error => {
        reject(error);
      });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 标记已经退出,不用再检测长时间未操作情况
      isLogOut();
      logout(state.token).then(() => {
        removeToken(); // must remove  token  first
        resetRouter();
        commit('RESET_STATE');
        resolve();
      }).catch(error => {
        reject(error);
      });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit('RESET_STATE');
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};

