import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

/**
 * 百望账户查询
 * @param {String} entTaxId 企业税号
 * @param {String} entName 企业名称
 * @param {Number} pageNum 页码
 * @param {Number} pageSize 每页数据条数
 */
function queryBWCloudInfo({ entTaxId, entName, pageNum = 1, pageSize = 10 }) {
  const url = '/sys/bw/cloudInfo/findPage'
  let token = cache.getToken()
  let mobile = JSON.parse(cache.getSession('user')).account
  let args = {
    entTaxId,
    entName,
    pageNum,
    pageSize,
    mobile
  }
  return new Promise(function (resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      if (val.code === 0) {
        resolve(val)
      } else {
        reject(val.msg)
      }
    })
  })
}

/**
 * 获取企业百望账户信息
 * @param {String} entTaxId 企业税号
 */
function getEntBWCloudInfo(entTaxId) {
  const url = '/sys/bw/cloudInfo/findByEntTaxId'
  let token = cache.getToken()
  let args = { entTaxId }
  return new Promise(function (resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      if (val.code === 0) {
        resolve(val)
      } else {
        reject(val.msg)
      }
    })
  })
}

/**
 * 客户信息同步到百望云。
 * @param {String} entTaxId 企业税号
 */
function syncCustomer(entTaxId) {
  const url = '/sys/customer/syncToBw'
  let token = cache.getToken()
  let args = { entTaxId }
  return new Promise(function (resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((val) => {
      if (val.code === 0) {
        resolve(val)
      } else {
        reject(val.msg)
      }
    })
  })
}

/**
 * 商品信息同步到百望云。
 * @param {String} entTaxId 企业税号
 */
function syncProduct(entTaxId) {
  const url = '/sys/product/syncToBw'
  let token = cache.getToken()
  let args = { entTaxId }
  return new Promise(function (resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((val) => {
      if (val.code === 0) {
        resolve(val)
      } else {
        reject(val.msg)
      }
    })
  })
}

export { queryBWCloudInfo, getEntBWCloudInfo, syncCustomer, syncProduct }
