import { login } from '@/apis/user'
import {
  getToken,
  setToken,
  removeToken,
  getAd,
  setAd,
  getGoodsNum,
  setGoodsNum,
  getGoodsUnit,
  setGoodsUnit

} from '@/utils/auth'

const state = {
  token: getToken(),
  name: '',
  introduction: '',
  roles: ['admin'],
  ad: getAd() ? JSON.parse(getAd()) : {},
  goodsNum: isNaN(getGoodsNum()) ? 0 : parseInt(getGoodsNum()),
  goodsUnit: isNaN(getGoodsUnit()) ? 0 : parseInt(getGoodsUnit()),
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_HAS: (state, has) => {
    state.has = has
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_AD: (state, roles) => {
    state.ad = roles
  },
  SET_GOODSNUM: (state, roles) => {
    state.goodsNum = roles
  },
  SET_GOODSUNIT: (state, roles) => {
    state.getGoodsUnit = roles
  },

}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { mobile, code } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: mobile.trim(), password: code.trim() })
        .then(response => {

          if (response.code === 0) {
            const { data } = response
            console.log(data);
            commit('SET_TOKEN', data.token)
            setToken(data.token)
          }
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },


  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      commit('SET_HAS', '')

      // reset visited views and cached views
      // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
      dispatch('tagsView/delAllViews', null, { root: true })
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },
  set_ad({ commit }, item) {
    // const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      let key = JSON.parse(JSON.stringify(item))
      setAd(key)
      commit('SET_AD', key)
      resolve({ code: 0, data: {}, message: 'ok' })
    })
  },
  set_goodsNum({ commit }, item) {
    console.log(item);
    return new Promise((resolve, reject) => {
      let key = item
      setGoodsNum(key)
      commit('SET_GOODSNUM', key)
      resolve({ code: 0, data: {}, message: 'ok' })
    })
  },
  set_goodsUnit({ commit }, item) {
    // console.log(item);
    return new Promise((resolve, reject) => {
      let key = item
      setGoodsUnit(key)
      commit('SET_GOODSUNIT', key)
      resolve({ code: 0, data: {}, message: 'ok' })
    })
  },
}





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