import {getToken}from './token-util.js'
import router from '../bc-router'
import config from '@/config'

const systemPrefix = config.deploymentStandAlone?'':'cas/'

/**
 * 跳转到预览打印签章, 通过指定路径的方式
 * @param fileName 文件名称
 * @param filePath 文件路径,全路径
 * @param operationType 操作类型
 */
export const jumpPrintPageAssignation = (fileName,filePath,operationType) => {
  let getFileWay = "assignation"
  operationType = operationType || "previewAndPrint"
  let routeData = router.resolve({
    name: "bochu-print-jump",
    params: {
    }
  });
  window.open(routeData.resolved.meta.href+"?token="+getToken()+
    "&getFileWay="+getFileWay+"&operationType="+operationType+"&fileName="+fileName+"&filePath="+filePath, '_blank');

}

/**
 * 跳转到预览打印签章, 通过指定分布式储存的文件id的方式
 * @param fdfsId 文件id
 * @param operationType 操作类型
 */
export const jumpPrintPageFdfs = (fdfsId,operationType,pageOrientationType,paperSizeType) => {
  let getFileWay = "assignation"
  operationType = operationType || "previewAndPrint"
  let params = "?token="+getToken()+"&getFileWay="+getFileWay+"&operationType="+operationType+"&fdfsId="+fdfsId
  if (pageOrientationType){
    params += "&pageOrientationType="+pageOrientationType
  }
  if (paperSizeType){
    params += "&paperSizeType="+paperSizeType
  }
  let routeData = router.resolve({
    name: "bochu-print-jump",
    params: {
    }
  });
  window.open(routeData.resolved.meta.href+params, '_blank');

}

/**
 * 普通导出下载方法
 * @param params
 */
export const downOrExportCommon = (params,url) => {
  let urlParams = ''
  for( let i in params){
    if (params[i]){
      urlParams+="&"+i+"="+params[i]
    }
  }
  window.location.href = systemPrefix+url+'/?access_token='+getToken()+urlParams;

}

/**
 * 分布式文件存储,下载方法
 * @param fdfsId 文件id
 */
export const downFileFdfs = (fdfsId) => {
  window.location.href = systemPrefix+'api/v1/attach/download/'+fdfsId+'/?access_token='+getToken();

}

/**
 * 分布式文件存储,打包下载方法
 * @param params 参数
 * {
 *   ids 文件id数组串
 *   fid 文件外键
 *   batchKey 文件批次key
 *   newFileName 打包后的新名字
 * }
 */
export const downFileFdfsZip = (params) => {
  let paramsStr = '?'
  if (params.ids){
    paramsStr += '&ids=' + params.ids
  }
  if (params.newFileName){
    paramsStr += '&newFileName=' + encodeURIComponent(params.newFileName)
  }
  if (params.fid){
    paramsStr += '&fid=' + params.fid
  }
  if (params.batchKey){
    paramsStr += '&batchKey=' + params.batchKey
  }
  window.location.href = systemPrefix+'api/v1/attach/download/zip'
    +paramsStr
    +'&access_token='+getToken();

}
