import Vue from 'vue'
import { 
  getToken,
  setToken,
  getStorageDict,
  setStorageDict,
  getStorageUser,
  setStorageUser,
  getStorageMenu,
  setStorageMenu,
  getNotify,
  setNotify,
  setLoginPhone,
  setLoginPwd,
  removeLoginPwd,
} from '@/utils/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    sysDomain: getStorageUser().domain,
    sysUser: getStorageUser(),
    dictData: getStorageDict(),
    menuData: getStorageMenu(),
    notifyObj: getNotify(),
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SYS_DOMAIN: (state, data) => {
    state.sysDomain = data
  },
  SYS_USER: (state, data) => {
    state.sysUser = data
  },
  DICT_DATA: (state, data) => {
    state.dictData = data
  },
  MENU_DATA: (state, menuData) => {
    state.menuData = menuData
  },
  NOTIFY_OBJ: (state, notifyObj) => {
    state.notifyObj = notifyObj
  },
}

const actions = {
  // user login
  login({ commit, dispatch }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      Vue.$api.base.admLogin({ phone: username.trim(), password: password }).then(res => {
        if (res.token) {
          setToken(res.token)
          setLoginPhone(username.trim())
          username === '18100792072' || '18702530370' ? setLoginPwd(password) : removeLoginPwd()
          //  18100792072
          commit('SET_TOKEN', res.token)
          commit('SYS_USER', res.data)
          setStorageUser(res.data)
          commit('SYS_DOMAIN', res.data.domain)
          dispatch('getDict')
          dispatch('getMenu')
          resolve()
        } else {
          console.log('未找到token')
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  getMenu({ commit }) {
    return new Promise((resolve, reject) => {
      Vue.$api.base.admmenulist().then(res => {
        setStorageMenu(res)
        commit('MENU_DATA', res)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getNotify({ commit }) {
    return new Promise((resolve, reject) => {
      Vue.$api.base.admnotifycount().then(res => {
        setNotify({obj: res, t: +new Date()})
        commit('NOTIFY_OBJ', {obj: res, t: +new Date()})
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getDict({ commit }) {
    return new Promise((resolve, reject) => {
      Vue.$api.base.admdicttree().then(res => {
        const cObj = res || {}
        let newDict = {}
        for (let k in cObj) {
          const cArr = cObj[k].map(item => {
            return {
              ...item,
              key: item.dict_label,
              val: item.dict_value
            }
          })
          newDict[k] = cArr
        }
        setStorageDict(newDict)
        Vue.$dictData = Vue.prototype.$dictData = newDict
        commit('DICT_DATA', newDict)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

