import { login, applogin, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '', // 姓名
    deptId: '', // 部门编号
    deptName: '', // 部门名称
    roleList: [], // 角色编号集合
    roleNames: [], // 角色名称集合
    roleTips: [], // 角色标签集合
    wellTypes: [], // 支持闸井类型
    deviceTypes: [], // 支持设备类型
    communications: [], // 支持通讯方式
    area: '' // 默认地区
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_ACCOUNT: (state, account) => {
      state.account = account
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_ROLELIST: (state, roleList) => {
      state.roleList = roleList
    },
    SET_ROLENAMES: (state, roleNames) => {
      state.roleNames = roleNames
    },
    SET_ROLETIPS: (state, roleTips) => {
      state.roleTips = roleTips
    },
    SET_DEPTID: (state, deptId) => {
      state.deptId = deptId
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName
    },
    SET_WELLTYPES: (state, wellTypes) => {
      state.wellTypes = wellTypes
    },
    SET_DEVICETYPES: (state, deviceTypes) => {
      state.deviceTypes = deviceTypes
    },
    SET_COMMUNICATIONS: (state, communications) => {
      state.communications = communications
    },
    SET_AREA: (state, area) => {
      state.area = area
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code === 200) {
            const data = response.data
            setToken(data.token)
            commit('SET_TOKEN', data.token)
            resolve()
          } else {
            reject(response.message)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    AppLogin({ commit }, token) {
      return new Promise((resolve, reject) => {
        if (token) {
          debugger
          setToken(token)
          commit('SET_TOKEN', token)
          resolve()
        } else {
          window.location.href = 'http://10.18.0.50:8086/platform-sso-server/login?service=' + process.env.BASE_API + '/app/caslogin'
          // applogin().then(response => {
          //   if (response.code === 200) {
          //     const data = response.data
          //     setToken(data.token)
          //     commit('SET_TOKEN', data.token)
          //     resolve()
          //   } else {
          //     reject(response.message)
          //   }
          // }).catch(error => {
          //   reject(error)
          // })
        }
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          if (data.roleList && data.roleList.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLELIST', data.roleList)
            commit('SET_ROLENAMES', data.roleNames)
            commit('SET_ROLETIPS', data.roleTips)
          } else {
            reject('该用户无角色')
          }
          if (data.deptId) {
            commit('SET_DEPTID', data.deptId)
            commit('SET_DEPTNAME', data.deptName)
          } else {
            reject('该用户无组织机构')
          }
          commit('SET_ID', data.id)
          commit('SET_ACCOUNT', data.account)
          commit('SET_NAME', data.name)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

export default user
