import * as service from '@/service'
import router from '@/router'
import apiUrl from '@/service/api.js'
import * as auth from '@/utils/auth'
import { MessageBox, Alert } from 'element-ui'
import Vue from 'vue'
const chenzhenService = service.chenzhen.baseService()

const user = {
  namespaced: true,
  state: {
    userInfo:auth.getUserInfo(),
    isLogin:false,
    img: 'static/images/UserAvatar.jpg',
    customerInfo:null,
    selectFriendId: 0
  },

  mutations: {
    USER_LOGIN(state, value){
      auth.setTokenChenzhen(value.token)
    },
    CHANGE_LOGINSTATUS(state, value){
      state.isLogin = value
    },
    SET_CUSTOMERINFO(state, value){
      state.customerInfo = value
    }
  },
  actions: {
    init({ dispatch, commit, state }){
      let token = auth.getTokenChenzhen()
      console.log(token)
      if(token){
        dispatch('changeLogin', true)
      }
    },
    setCustomerInfo({ commit, rootGetters , state }, value){
      console.log(6666666666666)
      console.log(rootGetters.selectedChat )
      let customerId = rootGetters.selectedChat.user.id
      return new Promise((resolve, reject) => {
        service.chenzhen.getCustomerInfo({ chatUserId: customerId }).then(response=>{
          if(response.code==200){
              commit('SET_CUSTOMERINFO', response.data)
              resolve(response.data)
          }else{
              reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    changeLogin({ commit, state }, value){
      commit('CHANGE_LOGINSTATUS', value)
    },
    login:({ dispatch, commit }, value)=>{
        return new Promise((resolve, reject) => {
            service.chenzhen.login(value).then(response=>{
                if(response.code==200){
                    dispatch('changeLogin', true)
                    commit('chenzhen/USER_LOGIN', response.data, { root: true })
                    resolve(response.data)
                }else{
                    reject(response)
                }
            }).catch(error => {
              reject(error)
            })
        })
    },
    getPersonStatistics:({ dispatch, commit }, value)=>{
        return new Promise((resolve, reject) => {
            service.chenzhen.getPersonStatistics(value).then(response=>{
                if(response.code==200){
                    resolve(response.data)
                }else{
                    reject(response)
                }
            }).catch(error => {
              reject(error)
            })
        })
    },
    personTodo:({ dispatch, commit }, value)=>{
        return new Promise((resolve, reject) => {
            service.chenzhen.personTodo(value).then(response=>{
                if(response.code==200){
                    resolve(response.data)
                }else{
                    reject(response)
                }
            }).catch(error => {
              reject(error)
            })
        })
    },
    speechSort:({ dispatch, commit }, value)=>{
      console.log(chenzhenService)
      console.log(apiUrl)
        return new Promise((resolve, reject) => {
          console.log(apiUrl)
            chenzhenService.get(apiUrl.chenzhen.speechSort)(value).then(response=>{
                if(response.code==200){
                    resolve(response.data)
                }else{
                    reject(response)
                }
            }).catch(error => {
              reject(error)
            })
        })
    }
  }
}
export default user