import { login, logout, changePwd } from '@/api/user/login';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { SET_TOKEN, UPDATE_USERINFO } from './mutation-types';
import route from './route';

const state = {
  token: getToken(),
  name: '',
  id: '',
  createDate: '',
  loginState: 0,
  trueName: '',
};

const getters = {
  token: state => state.token,
  userInfo: ({ token, route, ...other }) => other,
  adminId: state => state.id,
};

const actions = {
  // 登录
  Login({ commit, dispatch }, entity) {
    return new Promise((resolve, reject) => {
      login(entity)
        .then(response => {
          const { admin, menu, token } = response.data;
          // console.log(admin, menu);
          if (token) {
            setToken(token);
            commit(SET_TOKEN, token);
          }
          commit(UPDATE_USERINFO, admin);
          dispatch('initRoute', menu).then(() => {
            let path = '/';
            if (menu.length) {
              path += menu[0].menuUrl;
              if (menu[0].childList && menu[0].childList.length) {
                path =
                  path.lastIndexOf('/') === path.length - 1
                    ? path + menu[0].childList[0].menuUrl
                    : path + '/' + menu[0].childList[0].menuUrl;
              }
            }
            resolve(path);
          });
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // 登出
  LogOut({ commit, state }) {
    return logout(state.token)
      .then(() => {
        commit(SET_TOKEN, '');
        removeToken();
      })
      .catch(err => {
        console.log(err);
        commit(SET_TOKEN, '');
        removeToken();
      });
  },

  // 前端 登出
  FedLogOut({ commit }) {
    return new Promise(resolve => {
      commit(SET_TOKEN, '');
      removeToken();
      resolve();
    });
  },

  // 修改密码
  updatePassword({ state }, { oldPwd, newPwd }) {
    return changePwd({
      id: state.id,
      oldPwd,
      newPwd,
    });
  },
};

const mutations = {
  [SET_TOKEN](state, token) {
    state.token = token;
  },
  [UPDATE_USERINFO](state, userInfo) {
    state = Object.assign(state, {
      ...userInfo,
      loginState: 1,
    });
  },
};

export default {
  modules: {
    route,
  },
  state,
  getters,
  actions,
  mutations,
};
