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.baozheng.baseService
const baozhengApi = apiUrl.baozheng
const baozheng = {
  namespaced: true,
  state: {
    userInfo:auth.getUserInfo(),
    isLogin:false,
    witchPanel:'',
    img: 'static/images/UserAvatar.jpg',
    customerInfo:null,
    sendContent:'',
    selectFriendId: 0
  },

  mutations: {
    USER_LOGIN(state, value){
      auth.setTokenChenzhen(value.token)
    },
    CHANGE_LOGINSTATUS(state, value){
      state.isLogin = value
    },
    CHAGNGE_PANEL(state, value){
      state.witchPanel = value
    },
    SET_CUSTOMERINFO(state, value){
      state.customerInfo = value
    },
    CHANGE_CONTENT(state, value){
      state.sendContent = value
    },
  },
  actions: {
    init({ dispatch, commit, state }){
      let token = auth.getTokenChenzhen()
      if(token){
        dispatch('changeLogin', true)
      }
    },
    changeContent({ dispatch, commit, state }, value){
      commit('CHANGE_CONTENT', value)
    },
    changePanel({ dispatch, commit, state }, value){
      commit('CHAGNGE_PANEL', value)
    },
    setCustomerInfo({ commit, rootGetters , state }, value){
      let customerId = rootGetters.selectedChat.user.id
      return new Promise((resolve, reject) => {
        service.baozheng.getCustomerInfo({ chatUserId: customerId }).then(response=>{
          if(response.code==200){
              commit('SET_CUSTOMERINFO', response.data)
              resolve(response.data)
          }else{
            commit('SET_CUSTOMERINFO', null)
              reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    changeCustomerInfo({ dispatch, commit, state }, value){
      commit('SET_CUSTOMERINFO', value)
    },
    changeLogin({ commit, state }, value){
      commit('CHANGE_LOGINSTATUS', value)
      if(!value){
        auth.removeTokenChenzhen()
      }
    },
    login:({ dispatch, commit }, value)=>{
        return new Promise((resolve, reject) => {
            service.baozheng.login(value).then(response=>{
                if(response.code==200){
                    dispatch('changeLogin', true)
                    commit('baozheng/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.baozheng.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.baozheng.personTodo(value).then(response=>{
                if(response.code==200){
                    resolve(response.data)
                }else{
                    reject(response)
                }
            }).catch(error => {
              reject(error)
            })
        })
    },
    teamTodo:({ dispatch, commit }, param)=>{
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.teamTodo)({resolve, reject,param,isComplete:true})
        })
    },
    getScriptCatgary:({ dispatch, commit }, param)=>{
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.getScriptCatgary)({resolve, reject,param,isComplete:true})
        })
    },
    getScriptTags:({ dispatch, commit }, param)=>{
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.getScriptTags)({resolve, reject,param,isComplete:true})
        })
    },
    searchBiaoqingbao:({ dispatch, commit }, param)=>{
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.searchBiaoqingbao)({resolve, reject,param,isComplete:true})
        })
    },
    findByMobile:({ dispatch, commit }, param)=>{
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.findByMobile)({resolve, reject,param,isComplete:true})
        })
    },
    bindChatUser:({ dispatch, rootGetters, commit }, param)=>{
        param.chatUserId = rootGetters.selectedChat.user.id 
        return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.bindChatUser)({resolve, reject,param,isComplete:true})
        })
    },
    speechSort:({ dispatch, commit }, param)=>{
      return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.speechSort)({resolve, reject,param,isComplete:true})
        })
    },
    unDeliveryOut:({ dispatch, commit }, param)=>{
      return new Promise((resolve, reject) => {
            chenzhenService.get(baozhengApi.unDeliveryOut)({resolve, reject,param,isComplete:true})
        })
    }
  }
}
export default baozheng