import { login, logout, getInfo, getBizPerm } from '@/api/login'
import { getToken, setToken, removeToken, getAccount, setAccount, removeAccount, getPwd, setPwd,removePwd } from '@/utils/auth'
import { USER_ACCOUNT, USER_PWD } from '@/assets/js/storageConst'
import {
  getCid,
  getDeptId,
  getDeptName,
  getName,
  getPhone,
  setCid,
  setDeptId,
  setDeptName,
  setName, setPhone
} from "../../utils/auth";
const user = {
  state: {
    token: getToken(),//token
    username: getAccount(),//用户名
    password:getPwd(),//加密密码
    clientid: getCid(),//客户端id 推送功能用
    name: getName(), // 姓名
    deptId: getDeptId(), // 部门编号
    deptName: getDeptName(), // 部门名称
    phone: getPhone(), // 手机号
    roleList: [], // 角色编号集合
    roleNames: [], // 角色名称集合
    roleTips: [], // 角色标签集合
    wellTypes: [], // 支持闸井类型
    deviceTypes: [], // 支持设备类型
    communications: [], // 支持通讯方式
    area: '' // 默认地区
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
      setToken(token)
    },
    SET_NAME: (state, name) => {
      state.name = name
      setName(name)
    },
    SET_CLIENTID: (state, clientid) => {
      state.clientid = clientid
      setCid(clientid)
    },
    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
      setDeptId(deptId)
    },
    SET_DEPTNAME: (state, deptName) => {
      state.deptName = deptName
      setDeptName(deptName)
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
      setPhone(phone)
    },
    SET_USERNAME: (state, username) => {
      state.username = username
      setAccount(username)
    },
    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
            setPwd(userInfo.password)
            commit('SET_TOKEN', data.token)
            commit('SET_USERNAME', userInfo.username)
            resolve()
          } else {
            reject(response.message)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          console.log('get UserInfo')
          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_NAME', data.name)
          commit('SET_PHONE', data.phone)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

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

export default user
