import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, getSeat, setSeat, removeSeat, getExten, setExten, removeExten } from '@/utils/auth'
import { getOnline, setOnline, removeOnline } from '@/utils/auth'
import { getProject } from '@/utils/baseConfig'

const user = {
  state: {
    token: getToken(),
    id: '', // 用户id
    account: '', // 用户账户
    name: '', // 姓名
    deptId: '', // 部门编号
    deptName: '', // 部门名称
    roleList: [], // 角色编号集合
    roleNames: [], // 角色名称集合
    roleTips: [], // 角色标签集合
    exten: getExten(), // 分机号
    seat: getSeat(), // 坐席号
    websocket: '0', // websocket状态，1是已连接，0是未连接
    online: getOnline(), // 坐席签入状态
    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_EXTEN: (state, exten) => {
      if (exten) {
        state.exten = exten
      } else {
        state.exten = ''
      }
      setExten(state.exten)
    },
    SET_SEAT: (state, seat) => {
      if (seat) {
        state.seat = seat
      } else {
        state.seat = '000'
      }
      setSeat(state.seat)
    },
    SET_WEBSOCKET: (state, websocket) => {
      state.websocket = websocket
    },
    SET_ONLINE: (state, online) => {
      state.online = online
      setOnline(online)
    },
    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) => {
        // 如果token存在直接setToken
        if (token) {
          setToken(token)
          commit('SET_TOKEN', token)
          resolve()
        } else {
          // 判断是开发环境还是生产环境，
          // 生产环境不存在跳转到公共登陆平台
          if (process.env.NODE_ENV === 'development') {
            reject()
          } else {
            const systemName = getProject().type
            window.location.href = 'http://10.18.0.50:8086/platform-sso-server/login?service=' + process.env.BASE_API + '/app/caslogin?systemName=' + systemName
          }
        }
      })
    },
    // 获取用户信息
    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)
          commit('SET_SEAT', data.seat)
          commit('SET_EXTEN', data.exten)
          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()
          removeExten()
          removeSeat()
          removeOnline()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

export default user
