import { login, getAuthorizationInfo, logout } from '@/api/login'
import { getAccountList } from '@/api/systemSetting/account'
import { setToken, removeToken } from '@/utils/auth'

const log = require('electron-log')
const Store = require('electron-store')
const localStore = new Store()
const user = {
  state: {
    // token: getToken(),
    id: '',
    name: '',
    avatar: '',
    menuList: [],
    salesList: [],
    permissionList: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_SCENIC_NAME: (state, name) => {
      state.scenicName = name
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_MENU_LIST: (state, menuList) => {
      state.menuList = menuList
    },
    SET_PERMISSION_LIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_SALES_LIST: (state, data) => {
      state.salesList = data
    }
  },

  actions: {
    getSalesList ({ commit }) {
      getAccountList({ pageNum: 0, pageSize: -1 }).then(res => {
        console.log('销售账号列表', res.data.records)
        // let list = res.list.filter(i => i.roles.find(j => j.nickName.indexOf('售票') > -1))
        // if (list.length === 0) {
        //   list = res.list
        // }
        commit('SET_SALES_LIST', res.data.records || [])
      })
    },
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(data => {
          if (data.code === '200') {
            console.log('登录成功', data)
            const { token, admin, resourceList, roleList } = data.data
            const { id, loginName } = admin || {}
            commit('SET_TOKEN', token)
            // 设置ID
            commit('SET_ID', id)
            // 设置用户名
            commit('SET_NAME', loginName)
            // 设置菜单
            commit('SET_MENU_LIST', resourceList || [])
            // 设置按钮权限
            commit('SET_PERMISSION_LIST', roleList || [])
            setToken(token)
            localStore.set('token', token)
            resolve(data.data)
          } else {
            reject(data.msg || '登录失败')
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取授权信息，所有接口必须调用此接口后执行
    GetInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        getAuthorizationInfo().then(res => {
          const { admin, roleList, resourceList } = res.data || {}
          const { id, loginName } = admin || {}
          // 设置ID
          commit('SET_ID', id || '')
          // 设置用户名
          commit('SET_NAME', loginName || '')
          // 设置菜单
          commit('SET_MENU_LIST', resourceList || [])
          // 设置按钮权限
          commit('SET_PERMISSION_LIST', roleList || [])

          log.info(`【${loginName}】 登录成功`)
          // let permissions = _(info.permissionList)
          //   .groupBy(i => {
          //     return i.split(':')[0]
          //   })
          //   .mapValues(i => {
          //     return i.map(j => {
          //       return j.split(':')[1]
          //     })
          //   })
          //   .value()

          // console.log(permissions)

          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

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

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

export default user
