import axios from '~plugins/axios-nuxt'
import cityService from '~plugins/city-service'
// var axios = axiosHttp()
export const actions = {
  // 全局服务初始化
  nuxtServerInit (store, context) {
    // 检查设备类型
    // axios = axiosHttp(store, context.req)
    const route = context.route
    const isServer = context.isServer
    const isDev = context.isDev
    const userAgent = isServer ? context.req.headers['user-agent'] :context.req.headers['user-agent']
    const isMobile = /(iPhone|iPad|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent)
    const cookie = process.server ? context.req.headers['cookie'] : null
    store.commit('option/SET_MOBILE_LAYOUT', isMobile)
    store.commit('option/SET_USER_AGENT', userAgent)
    store.commit('option/SET_COOKIES', cookie)
    // 保存appId和returnUrl信息
    // 保存链接?之后的所有键值对以字符串形式
    let fullPath = route.fullPath.lastIndexOf('?') !== -1 ? route.fullPath.slice(route.fullPath.lastIndexOf('?')) : ''
    store.commit('option/SET_FULLPATH', fullPath)
    if (route.query) {
      let appId = route.query.appId || ''
      let returnUrl = route.query.returnURL || ''
      let baseUrl = route.query.baseUrl || ''
      let inviteSpaceUU = route.query.inviteSpaceUU || ''
      let inviteUserUU = route.query.inviteUserUU || ''
      let invitationTime = route.query.invitationTime || ''
      store.commit('option/SET_APPID', appId)
      store.commit('option/SET_RETURNURL', returnUrl)
      store.commit('option/SET_BASEURL', baseUrl)
      store.commit('option/SET_INVITESPACEUU', inviteSpaceUU)
      store.commit('option/SET_INVITEUSERUU', inviteUserUU)
      store.commit('option/SET_INVITATIONTIME', invitationTime)
    }
    if (cookie && cookie.length) {
      // let cookies = cookie.split(';')
      // for (let i = 0; i < cookies.length; i++) {
      //   let cookieArr = cookies[i].split('=')
      //   if (cookieArr.length === 2 && cookieArr[0] === 'JSESSIONID') {
      //     store.commit('option/SET_SESSION_ID', cookieArr[1])
      //     break
      //   }
      // }
      axios.defaults.headers['cookie'] = store.state.option.cookies
      axios.defaults.headers['User-Agent'] = store.state.option.userAgent
    } else {
      axios.defaults.headers['cookie'] = ''
      axios.defaults.headers['User-Agent'] = ''
    }
    // 设置跳转的URL
    if (!isDev) {
      store.commit('option/UPDATE_URL', 'http://192.168.253.66:8081/')
    }
    return Promise.all([
      // 全局数据
      // store.dispatch('loadCarouselInfo', { client_type: 'cc', cityId: '5' }),
      store.dispatch('loadIsLogin'),
      store.dispatch('loadUserInfo')
    ])
  },
  // 判断是否登录
  loadIsLogin ({ commit }) {
    commit('option/REQUEST_IS_LOGIN')
    return axios.get('/sso/login/isLogin')
    .then(response => {
      console.log(response.data)
      commit('option/REQUEST_IS_LOGIN_SUCCESS', response.data)
    }, err => {
      commit('option/REQUEST_IS_LOGIN_FAILURE', err)
    })
  },
  // 获取用户登录信息
  loadUserInfo ({ commit }) {
    commit('option/REQUEST_USER_INFO')
    return axios.get('/sso/center/user/info')
      .then(response => {
        // console.log(response.data)
        commit('option/REQUEST_USER_INFO_SUCCESS', response.data)
      }, err => {
        commit('option/REQUEST_USER_INFO_FAILURE', err)
      })
  },
  // 获取成员列表信息
  loadMemberList ({ commit }, params = {}) {
    commit('cloudCenter/REQUEST_MEMBER')
    // console.log(axios)
    return axios.get('/sso/center/member/info', {params})
      .then(res => {
        commit('cloudCenter/REQUEST_MEMBER_SUCCESS', res.data)
      }, err => {
        commit('cloudCenter/REQUEST_MEMBER_FAILURE', err)
      })
  },
  // 获取申请列表信息
  loadApplyList ({commit}, params = {}) {
    commit('cloudCenter/REQUEST_APPLY')
    return axios.get('/sso/center/apply/info', {params})
      .then(res => {
        commit('cloudCenter/REQUEST_APPLY_SUCCESS', res.data)
      }, err => {
        commit('cloudCenter/REQUEST_APPLY_FAILURE', err)
      })
  },
  // 关键字查询企业名称
  searchForName ({ commit }, params = {}) {
    commit('search/REQUEST_KEYWORDS')
    return axios.get('/api/userspace/search/name', {params})
      .then(res => {
        commit('search/GET_KEYWORDS_SUCCESS', res.data)
      }, err => {
        commit('search/GET_KEYWORDS_FAILURE', err)
      })
  },
  // 清空企业数据
  restForName ({commit}) {
    commit('search/REST_KEYWORDS')
  },
  // 通过企业名查询企业信息
  searchForInfo ({commit}, params = {}) {
    commit('search/REQUEST_ENTERPRISE')
    return axios.get('/api/userspace/info/name', {params})
      .then(res => {
        commit('search/GET_ENTERPRISE_SUCCESS', res.data)
      }, err => {
        commit('search/GET_ENTERPRISE_FAILURE', err)
      })
  },
  // 清空查询企业信息数据
  restForInfo ({commit}) {
    commit('search/REST_ENTERPRISE')
  },
  // 获取表单配置
  loadFormData ({ commit }, params = {}) {
    commit('formConfig/REQUEST_FORMDATA')
    return cityService.get('/api/serve/config.action', {params})
      .then(response => {
        commit('formConfig/GET_FORMDATA_SUCCESS', response.data)
      }, err => {
        commit('formConfig/GET_FORMDATA_FAILURE', err)
      })
  },
  // 登录页样式
  GetLoginStyle ({ commit }, params = {}) {
    return axios.get('/sso/login/page/style', { params })
      .then(response => {
        commit('login/REQUEST_LOGIN_STYLE_SUCCESS', response.data)
      }, err => {
        commit('login/REQUEST_LOGIN_STYLE_FAILURE', err)
      })
  },
  // 获取协议
  GetAgreementUrl ({ commit }, params = {}) {
    return axios.get('/api/app/getAgreementUrl', { params })
      .then(response => {
        commit('login/REQUEST_AGREEMENT_URL_SUCCESS', response.data)
      }, err => {
        commit('login/REQUEST_AGREEMENT_URL_FAILURE', err)
      })
  }
}
