import { getToken, setToken, removeToken } from '@/utils/auth'
import { $api } from '@/api/api-config' // 接口配置
import request from '@/utils/request'
import Cookies from 'js-cookie'
const getDefaultState = () => {
  return {
    token: getToken(),
    merchantsId: Cookies.get('merchantsId') ? Cookies.get('merchantsId') : '',
    getstate: Cookies.get('state') ? Cookies.get('state') : '',
    merchantsName: Cookies.get('merchantsName') ? Cookies.get('merchantsName') : '',
    merchantsAdminId: Cookies.get('merchantsAdminId') ? Cookies.get('merchantsAdminId') : ''
  }
}
const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    setToken(token)
  },
  SET_MERCHANTSNAME: (state, merchantsName) => {
    state.merchantsName = merchantsName
  },
  SET_MERCHANTSADMINID: (state, id) => {
    state.merchantsAdminId = id
  },
  SET_STATE_CODE: (state, getstate) => {
    state.getstate = getstate
  },
  SET_MERCHANTSID: (state, merchantsid) => {
    state.merchantsId = merchantsid
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      request.post($api.login, userInfo).then(response => {
        if (response.state === '1') {
          commit('SET_STATE_CODE', response.state)
          commit('SET_MERCHANTSADMINID', response.merchantsAdminId)
          commit('SET_MERCHANTSID', response.merchantsId)
          commit('SET_MERCHANTSNAME', response.merchantsName)
          Cookies.set('merchantsAdminId', response.merchantsAdminId)
          Cookies.set('merchantsId', response.merchantsId)
          Cookies.set('state', response.state)
          Cookies.set('merchantsName', response.merchantsName)
          resolve(response)
        } else {
          resolve(response)
          Cookies.remove('merchantsName')
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 退出
  logout({ commit, state }) {
    return new Promise(resolve => {
      removeToken()
      Cookies.remove('merchantsName')
      sessionStorage.removeItem("userName")  //退出，删除登录标识
      commit('RESET_STATE')
      resolve()
    })
  },
  // 修改密码
  changepaw({ commit }, payload) {
    return new Promise((resolve, reject) => {
      request.post($api.modiPassword, payload).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 注册
  register({ commit }, payload) {
    return new Promise((resolve, reject) => {
      request.post($api.addMerchants, payload).then(response => {
        commit('SET_STATE_CODE', response.state)
        Cookies.set('state', response.state)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 编辑供应商/店铺
  editMerchants({ commit }, payload) {
    return new Promise((resolve, reject) => {
      request.post($api.editMerchants, payload).then(response => {
        commit('SET_STATE_CODE', response.state)
        Cookies.set('state', response.state)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

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

