import { axios } from '@/utils/request'

export function login (parameter) {
  return axios({
    url: '/auth/login',
    method: 'post',
    data: parameter,
    headers:{
      'module': encodeURIComponent('登录')
    }
  })
}

// 默认参数 
export const getDefaultCofig = (params) => {
  const url = `/sysParamsConfig/findByCode/${params.code}`
  return axios({
    url: url,
    data: params,
    method: 'post'
  })
}

export const listLookUp = params => {
  const url = `/lookup/queryLike`
  return axios({
    url: url,
    data: params,
    method: 'POST'
  })
}

export const getLookUpData = (params) => {
  const url = `/lookupItem/query/${params.pageNo}/${params.pageSize}`
  delete params.pageNo
  delete params.pageSize
  return axios({
    url: url,
    data: params,
    method: 'post',
    headers:{
        'module': encodeURIComponent('数据字典('+params.lookupCode+')项查询')
    }
  })
}

export const getAreaAll = () => {
  return axios({
    url: '/area/findAll',
    method: 'get'
  })
}

export const uploadFile = (params) => {
  return axios({
    url: '/upload',
    method: 'post',
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 获取ali OSS 上传签名
 * @returns {Q.Promise<any> | * | Q.Promise<T | never> | PromiseLike<T | never> | Promise<T | never>}
 */
export const getOSSToken = (params) => {
  return axios({
    url: `ossUpload?bizType=${params.bizType}`,
    method: 'get'
  })
}

/**
 * 上传文件到阿里OSS
 * @param params
 * @returns {Q.Promise<any> | * | Q.Promise<T | never> | PromiseLike<T | never> | Promise<T | never>}
 */
export const uploadOSS = (params) => {
  return axios({
    // url: params.url,
    url: 'ossUploadFile',
    method: 'post',
    data: params.data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 上传文件到阿里OSS
 * @param params
 * @returns {Q.Promise<any> | * | Q.Promise<T | never> | PromiseLike<T | never> | Promise<T | never>}
 */
export const uploadImgOnce = (bizType, params) => {
  return getOSSToken({
    bizType
  }).then(config => {
    const fileName = createFileName(params.name)
    const _data = new FormData()
    _data.append('key', config.dir + '/' + fileName)
    _data.append('name', fileName)
    _data.append('policy', config.policy)
    _data.append('OSSAccessKeyId', config['accessid'])
    _data.append('success_action_status', '200')
    _data.append('callback', config.callback)
    _data.append('signature', config.signature)
    _data.append('file', params)
    return uploadOSS({
      data: _data
    })
  })
}

function createFileName (filename) {
  const pos = filename.lastIndexOf('.')
  const suffix = filename.substring(pos)
  return randomStr(20) + suffix
}
function randomStr (len) {
  len = len || 32
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  const maxPos = chars.length
  let pwd = ''
  for (let i = 0; i < len; i++) {
    pwd += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return pwd
}

// 查询省
export const getProvince = () => {
  return axios({
    url: `/area/province`,
    method: 'post'
  })
}
// 查询市
export const getCityByPro = params => {
  return axios({
    url: `/area/city/${params.id}`,
    data: params,
    method: 'post'
  })
}
// 查询区
export const getDistrictByCity = params => {
  return axios({
    url: `/area/district/${params.id}`,
    data: params,
    method: 'post'
  })
}
// 查询省/市/区
export const getArea = (params) => {
  return axios({
    url: `/area/queryList`,
    data: params,
    method: 'post'
  })
}

// 打印记录-分页列表
export const printLogQueryPage = (params) => {
  const url = `/printLog/queryPage/${params.pageNo}/${params.pageSize}`
  delete params.pageNo
  delete params.pageSize
  return axios({
    url: url,
    data: params,
    method: 'post',
    headers:{
      'module': encodeURIComponent('打印记录列表查询')
    }
  })
}

// 打印记录-新增
export const printLogSave = (params) => {
  return axios({
    url: `/printLog/save`,
    data: params,
    method: 'post',
    headers:{
      'module': encodeURIComponent('新增打印记录')
    }
  })
}
// 打印记录-批量新增
export const printLogSaveBatch = (params) => {
  return axios({
    url: `/printLog/saveBatch`,
    data: params,
    method: 'post',
    headers:{
      'module': encodeURIComponent('批量新增打印记录')
    }
  })
}
// 下载模板文件
export const downLoadTemp = (params) => {
  return axios({
    url: `downLoad/temp/${params.type}`,
    method: 'get',
    responseType: 'blob',
    headers:{
      'module': encodeURIComponent('下载模板文件')
    }
  })
}
