import axios from '~plugins/axios'

// 获取品牌详情产品分类信息
function loadBrandCategories({ commit }, params = {}) {
  let id = params.id
  commit('brandCategories/REQUEST_CATEGORIES', params)
  return axios.get(`/api/product/brand/${id}/kinds`)
    .then(response => {
      commit('brandCategories/GET_CATEGORIES_SUCCESS', response.data)
    }, err => {
      commit('brandCategories/GET_CATEGORIES_FAILURE', err)
    })
}

// 获取品牌详情型号列表数据
function loadBrandComponent({ commit }, params = {}) {
  commit('brandComponent/REQUEST_COMPONENT', params)
  return axios.get('/api/product/component/list', { params })
    .then(response => {
      commit('brandComponent/GET_COMPONENT_SUCCESS', response.data)
    }, err => {
      commit('brandComponent/GET_COMPONENT_FAILURE', err)
    })
}

export const actions = {
  // 全局服务初始化
  nuxtServerInit(store, { params, route, isDev, req }) {
    // 检查设备类型
    const userAgent = process.server ? req.headers['user-agent'] : navigator.userAgent
    const isMobile = /(iPhone|iPad|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent)
    const cookie = process.server ? req.headers['cookie'] : null
    store.commit('option/SET_MOBILE_LAYOUT', isMobile)
    store.commit('option/SET_USER_AGENT', userAgent)
    store.commit('option/SET_COOKIES', cookie)
    // console.log(req.headers.referer)
    if (route.query) {
      let messageType = route.query.messageType || ''
      store.commit('option/GET_MESSAGETYPE', messageType)
    }
    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', 'https://mall.usoftchina.com')
    }
    return Promise.all([
      // 全局数据
      store.dispatch('loadUserInfo')
    ])
  },
  // 获取用户信息
  loadUserInfo({ commit }) {
    commit('option/REQUEST_USER_INFO')
    return axios.get('/user/authentication')
      .then(response => {
        if (response.data.userName) {
          let ens = response.data.enterprises
          if (ens && ens.length) {
            response.data.enterprise = ens.find(item => item.current) || { enName: '个人账户' }
          } else {
            response.data.enterprise = { enName: '个人账户' }
          }
        }
        commit('option/REQUEST_USER_INFO_SUCCESS', response.data)
      }, err => {
        commit('option/REQUEST_USER_INFO_FAILURE', err)
      })
  },
  loadUserWithAdmin ({commit}, params = {}) {
    commit('option/REQUEST_ADMIN_INFO')
    return axios.get('/basic/user/getUserByUU', {params: params})
      .then(response => {
        commit('option/REQUEST_ADMIN_INFO_SUCCESS', response.data)
      }, err => {
        commit('option/REQUEST_ADMIN_INFO_FAILURE', err)
      })
  },
  // 用户退出
  logout({ commit }) {
    return axios.get('/logout')
      .then(response => {
        commit('option/REQUEST_LOGOUT_SUCCESS', response.data)
      })
  },
  // 获取商城统计数据
  loadCounterData ({commit}) {
    commit('option/REQUEST_COUNTER')
    return axios.get('/api/product/commoncount')
      .then(res => {
        commit('option/REQUEST_COUNTER_SUCCESS', res.data)
      }, err => {
        commit('option/REQUEST_COUNTER_FAILURE')
      })
  },
  // 获取币别信息
  loadCurrencyData ({commit}) {
    return axios.get('/basic/enterprise/currency')
      .then(res => {
        commit('option/SET_CURRENCY', res.data)
      })
  },
  // 获取楼层配置
  loadFloors({ commit }) {
    commit('floor/REQUEST_LIST')
    return axios.get('/api/floors/home')
      .then(response => {
        commit('floor/GET_LIST_SUCCESS', response.data)
      }, err => {
        commit('floor/GET_LIST_FAILURE', err)
      })
  },
  // 获取轮播配置
  loadBanners({ commit }, params = {}) {
    commit('carousel/REQUEST_BANNER')
    return axios.get(`/cmsApi?method=queryContentPage&module=${params.type}&orderBy=order_number%20ASC`)
      .then(response => {
        commit('carousel/GET_BANNER_SUCCESS', response.data)
      }, err => {
        commit('carousel/GET_BANNER_FAILURE', err)
      })
  },

  // 获取首页加载热门搜索列表
  loadHotSearch({ commit }) {
    commit('hotSearch/REQUEST_HOT')
    return axios.get('/cmsApi?method=queryContentPage&module=index_hotModels&orderBy=order_number%20ASC')
      .then(response => {
        commit('hotSearch/GET_HOT_SUCCESS', response.data)
      }, err => {
        commit('hotSearch/GET_HOT_FAILURE', err)
      })
  },

  // 获取楼层配置和特价信息
  loadNewFloors({ commit }, params = {}) {
    commit('floor/REQUEST_NEWLIST')
    return axios.get(`/cmsApi?method=queryContentPage&module=${params.type}&orderBy=order_number%20ASC&status=normal`)
      .then(response => {
        commit('floor/GET_NEWLIST_SUCCESS', response.data)
      }, err => {
        commit('floor/GET_NEWLIST_FAILURE', err)
      })
  },
  // 获取子器件类目
  loadProductKinds({ commit }, params = {}) {
    let id = params.id
    commit('product/kind/REQUEST_KIND', params)
    return axios.get(`/api/product/kind/${id}/children`)
      .then(response => {
        commit('product/kind/GET_KIND_SUCCESS', { id, result: response.data })
      }, err => {
        commit('product/kind/GET_KIND_FAILURE', { id, err })
      })
  },
  // 获取全部子器件类目
  loadAllProductKinds({ commit }, params = {}) {
    let id = params.id
    commit('product/kind/REQUEST_KIND', params)
    return axios.get(`/api/product/kind/${id}/children_all`)
      .then(response => {
        commit('product/kind/GET_KIND_SUCCESS', { id, result: response.data })
      }, err => {
        commit('product/kind/GET_KIND_FAILURE', { id, err })
      })
  },
  // 获取首页新闻
  loadNewsSnapshot({ commit }, params = {}) {
    commit('news/REQUEST_SNAPSHOT')
    return axios.get('/api/news/created', { params })
      .then(response => {
        commit('news/GET_SNAPSHOT_SUCCESS', response.data)
      }, err => {
        commit('news/GET_SNAPSHOT_FAILURE', err)
      })
  },
  // 获取器件统计信息
  loadProductCounts({ commit }, params = {}) {
    commit('product/common/REQUEST_COUNTS')
    return axios.get('/api/product/commoncount', { params })
      .then(response => {
        commit('product/common/GET_COUNTS_SUCCESS', response.data)
      }, err => {
        commit('product/common/GET_COUNTS_FAILURE', err)
      })
  },
  // 搜索关键字
  searchKeywords({ commit }, params = {}) {
    commit('search/REQUEST_KEYWORDS')
    return axios.get('/search/similarKeywords', { params })
      .then(response => {
        commit('search/GET_KEYWORDS_SUCCESS', response.data)
      }, err => {
        commit('search/GET_KEYWORDS_FAILURE', err)
      })
  },
  // 搜索关键字列表
  searchKeywordsList({ commit }, params = {}) {
    commit('search/REQUEST_KEYWORDSLIST')
    return axios.get('/search/201819', { params })
      .then(response => {
        commit('search/GET_KEYWORDSLIST_SUCCESS', response.data)
      }, err => {
        commit('search/GET_KEYWORDSLIST_FAILURE', err)
      })
  },
  resetSearchKeywords({ commit }) {
    commit('search/RESET_KEYWORDS')
  },
  // 热卖推荐页面
  loadProductHot({ commit }, params = {}) {
    commit('original/REQUEST_HOT')
    return axios.get('/api/commodity/latest', { params })
      .then(response => {
        commit('original/GET_HOT_SUCCESS', response.data)
      }, err => {
        commit('original/GET_HOT_FAILURE', err)
      })
  },
  // 器件详情页面
  // 获得器件详情信息
  loadComponentDetail({ commit }, params = {}) {
    let id = params.id
    commit('componentDetail/REQUEST_DETAIL', params)
    return axios.get(`/api/product/component/${id}`)
      .then(response => {
        commit('componentDetail/GET_DETAIL_SUCCESS', response.data)
        if (response.data) {
          commit('componentMenu/REQUEST_MENU', params)
          return axios.get(`/api/product/kind/structing/${response.data.kindid}`)
            .then(response => {
              commit('componentMenu/GET_MENU_SUCCESS', response.data)
            }, err => {
              commit('componentMenu/GET_MENU_FAILURE', err)
            })
        }
      }, err => {
        commit('componentDetail/GET_DETAIL_FAILURE', err)
      })
  },
  // 获取器件详情页面包屑导航
  loadComponentMenu({ commit }, params = {}) {
    let pid = params.id
    commit('componentMenu/REQUEST_MENU', params)
    return axios.get(`/api/product/kind/structing/${pid}`)
      .then(response => {
        commit('componentMenu/GET_MENU_SUCCESS', response.data)
      }, err => {
        commit('componentMenu/GET_MENU_FAILURE', err)
      })
  },
  // 器件详情页选择商家
  loadComponentStore({ commit }, params = {}) {
    let id = params.uuid
    commit('componentStore/REQUEST_STORE', params)
    return axios.get(`/api/store-service/stores/uuid/${id}`)
      .then(response => {
        commit('componentStore/GET_STORE_SUCCESS', response.data)
      }, err => {
        commit('componentStore/GET_STORE_FAILURE', err)
      })
  },
  // 器件详情页商家列表
  loadComponentInformation({ commit }, params = {}) {
    // let params = {}
    // let filter = {uuid: uuid.uuid, ignoreUMall: false, ignoreStore: false}
    // // let sorting = {minPriceRMB: 'ASC'}
    // params.filter = filter
    // // params.sorting = sorting
    // params.page = pageParams.page
    // params.count = pageParams.count
    commit('componentInformation/REQUEST_INFORMATION')
    return axios.get('/api/commodity/goods/page', { params })
      .then(response => {
        commit('componentInformation/GET_INFORMATION_SUCCESS', response.data)
      }, err => {
        commit('componentInformation/GET_INFORMATION_FAILURE', err)
      })
  },
  // 获取库存寄售店铺storeid
  getUmallStoreId({ commit }) {
    commit('componentUmallStoreId/REQUEST_STOREID')
    return axios.get('/api/store-service/stores/UmallStore')
      .then(response => {
        commit('componentUmallStoreId/GET_STOREID_SUCCESS', response.data)
      }, err => {
        commit('componentUmallStoreId/GET_STOREID_FAILURE', err)
      })
  },
  // 品牌详情页面
  // 获得品牌详情信息
  loadBrandDetail({ commit, dispatch }, params = {}) {
    let id = params.id
    commit('brandDetail/REQUEST_DETAIL', params)
    return axios.get(`/api/product/brand/${id}`)
      .then(response => {
        let brand = response.data || {}
        commit('brandDetail/GET_DETAIL_SUCCESS', response.data)
        return Promise.all([
          loadBrandCategories({ commit }, { id: brand.id }),
          loadBrandComponent({ commit }, { count: 10, filter: { brandid: brand.id }, page: 1 })
        ])
      }, err => {
        commit('brandDetail/GET_DETAIL_FAILURE', err)
      })
  },
  // 获取品牌详情产品分类信息
  loadBrandCategories({ commit }, params = {}) {
    let id = params.id
    commit('brandCategories/REQUEST_CATEGORIES', params)
    return axios.get(`/api/product/brand/${id}/kinds`)
      .then(response => {
        commit('brandCategories/GET_CATEGORIES_SUCCESS', response.data)
      }, err => {
        commit('brandCategories/GET_CATEGORIES_FAILURE', err)
      })
  },
  // 获取品牌详情型号列表数据
  loadBrandComponent({ commit }, params = {}) {
    commit('brandComponent/REQUEST_COMPONENT', params)
    return axios.get('/api/product/component/list', { params })
      .then(response => {
        commit('brandComponent/GET_COMPONENT_SUCCESS', response.data)
      }, err => {
        commit('brandComponent/GET_COMPONENT_FAILURE', err)
      })
  },
  // 获取品牌详情分页信息
  loadBrandPages({ commit }, params = {}) {
    commit('brandPages/REQUEST_PAGES', params)
    return axios.get('/api/product/PAGES/list', { params })
      .then(response => {
        commit('brandPages/GET__SUCCESS', response.data)
      }, err => {
        commit('brandPages/GET_COMPONENT_FAILURE', err)
      })
  },
  // 留言板
  // 提交了留言板信息
  uploadMessageBoardInformation({ commit }, params) {
    commit('messageBoard/REQUEST_INFORMATION')
    return axios.post('/api/operation/messageBoard', params)
      .then(response => {
        commit('messageBoard/GET_INFORMATION_SUCCESS', response.data)
      }, err => {
        commit('messageBoard/GET_INFORMATION_FAILURE', err)
      })
  },
  // 验证用户是否登录
  userIsLogin({ commit }) {
    commit('messageBoardIsLogin/REQUEST_LOGIN')
    return axios.get('/user/authentication')
      .then(response => {
        commit('messageBoardIsLogin/GET_LOGIN_SUCCESS', response.data)
      }, err => {
        commit('messageBoardIsLogin/GET_LOGIN_FAILURE', err)
      })
  },
  // 获取留言记录信息
  getMessageBoardInformation({ commit }, params) {
    let sorting = { createDate: 'DESC' }
    params.sorting = sorting
    commit('messageBoardInformation/REQUEST_INFORMATION', params)
    return axios.get('/operation/messageBoard/pageInfo/user', { params })
      .then(response => {
        commit('messageBoardInformation/GET_INFORMATION_SUCCESS', response.data)
      }, err => {
        commit('messageBoardInformation/GET_INFORMATION_FAILURE', err)
      })
  },
  // 获取帮助中心信息
  loadHelpSnapsho({ commit }, params = {}) {
    commit('help/REQUEST_SNAPSHO')
    return axios.get('/api/help-service/helps', { params })
      .then(response => {
        commit('help/GET_SNAPSHO_SUCCESS', response.data)
      }, err => {
        commit('help/GET_SNAPSHO_FAILURE', err)
      })
  },
  // 获取帮助中心二级菜单
  loadHelpList({ commit }, params = {}) {
    commit('help/REQUEST_HELPLIST')
    return axios.get('/api/help-service/issues', { params })
      .then(response => {
        commit('help/GET_HELPLIST_SUCCESS', response.data)
      }, err => {
        commit('help/GET_HELPLIST_FAILURE', err)
      })
  },
  // 获取帮助中心名称
  loadHelpTitle({ commit }, params = {}) {
    let id = params.id
    commit('help/REQUEST_TITLE')
    return axios.get(`/api/help-service/${id}`, { params })
      .then(response => {
        commit('help/GET_TITLE_SUCCESS', response.data)
      }, err => {
        commit('help/GET_TITLE_FAILURE', err)
      })
  },
  // 获取详情
  loadHelpDetail({ commit }, params = {}) {
    let id = params.id
    commit('help/REQUEST_DETAIL')
    return axios.get(`/api/help-service/issues/${id}`, { params })
      .then(response => {
        commit('help/GET_DETAIL_SUCCESS', response.data)
        let id = response.data.navId
        commit('help/REQUEST_TITLE')
        return axios.get(`/api/help-service/${id}`)
          .then(response => {
            commit('help/GET_TITLE_SUCCESS', response.data)
          }, err => {
            commit('help/GET_TITLE_FAILURE', err)
          })
      }, err => {
        commit('help/GET_DETAIL_FAILURE', err)
      })
  },
  // 获取用户开店信息
  loadStoreStatus({ commit }, params = {}) {
    commit('option/REQUEST_STORE_STATUS')
    return axios.get('/store-service/stores', { params: params })
      .then(response => {
        commit('option/REQUEST_STORE_STATUS_SUCCESS', response.data)
      }, err => {
        commit('option/REQUEST_STORE_STATUS_FAILURE', err)
      })
  },
  // 获取首页悬浮计数器交易金额
  loadAllCount ({commit}, params) {
    commit('count/REQUEST_ALLCOUNT')
    return axios.get('/api/product/commoncount', {params})
      .then(res => {
        commit('count/GET_ALLCOUNT_SUCCESS', res.data)
      }, (err) => {
        commit('count/GET_ALLCOUNT_FAILURE', err)
      })
  },
  // 获取首页悬浮计数器询价单
  loadInquirySheet ({commit}, params) {
    commit('count/REQUEST_INQUIRYSHEET')
    return axios.get('/inquiry/public/getCountOfLastAndThisMonth', {params})
      .then(res => {
        commit('count/GET_INQUIRYSHEET_SUCCESS', res.data.current || 0)
        commit('count/GET_INQUIRYSHEETLAST_SUCCESS', res.data.last || 0)
      }, (err) => {
        commit('count/GET_INQUIRYSHEET_FAILURE', err)
        commit('count/GET_INQUIRYSHEETLAST_FAILURE', err)
      })
  },
  // 保存微信信息
  GerWechatInfo({ commit }, params = {}) {
    let id = params.openId
    commit('option/REQUEST_WECHATINFO_STATUS')
    return axios.get('/wx/getWxUserInfo', { params: params })
      .then(response => {
        let Userinfo = localStorage.getItem('USOFTMALLWECHATINFO')
        Userinfo = Userinfo && JSON.parse(Userinfo) || {}
        localStorage.removeItem('USOFTMALLWECHATINFO')
        if (response.data.status >= 0) { // 请求到数据
          if (id !== '' && id && id !== undefined) { // 传了openid
            Userinfo.openid = id
          } else { // 没有openid
            Userinfo.headimgurl = response.data.headimgurl
            Userinfo.nickname = response.data.nickname
            Userinfo.openid = response.data.openid
          }
          if (response.data.status === 1) { // 已绑定
            Userinfo.userAccount = response.data.userAccount
          }
          Userinfo.status = response.data.status
          Userinfo.enterprises = response.data.enterprises
          localStorage.setItem('USOFTMALLWECHATINFO', JSON.stringify(Userinfo))
          commit('option/REQUEST_WECHATINFO_STATUS_SUCCESS', Userinfo)
        } else {
          // localStorage.removeItem('USOFTMALLWECHATINFO')
        }
      }, err => {
        localStorage.removeItem('USOFTMALLWECHATINFO')
        commit('option/REQUEST_WECHATINFO_STATUS_FAILURE', err)
      })
  },
  // 获取品牌中心轮播图
  loadBrandCarousel ({ commit }) {
    commit('carousel/REQUEST_BRANDCAROUSEL')
    return axios.get('/cmsApi?method=queryContentPage&module=brandCenter_littleSlideshow')
      .then(response => {
        commit('carousel/GET_BRANDCAROUSEL_SUCCESS', response.data)
      }, err => {
        commit('carousel/GET_BRANDCAROUSEL_FAILURE', err)
      })
  },
  // 获取品牌中心首屏背景图
  loadBrandBanner ({ commit }) {
    commit('product/banner/REQUEST_BRANDBANNER')
    return axios.get('/cmsApi?method=queryContentPage&module=brandCenter_firstScreen')
      .then(response => {
        commit('product/banner/GET_BRANDBANNER_SUCCESS', response.data)
      }, err => {
        commit('product/banner/GET_BRANDBANNER_FAILURE', err)
      })
  }
}