import plglib from 'plglib'
import { message } from 'ant-design-vue'
// 打印服务
/* 
data格式:
[
    {
        "head": {
            "集货区": "LH2018053107093",
            "件数": "000",
            "总件数": 28,
            "是否重打": null,
            "单据类型": 87,
            "承运商名称": "阿里健康大药房旗舰店",
            "条码":"121sdrsd12"
        },
        "table":[
            {"number":"111","name":"张三","age":12},
            {"number":"112","name":"张三","age":12},
            {"number":"113","name":"张三","age":12},
            {"number":"114","name":"张三","age":12},
            {"number":"115","name":"张三","age":12}
        ]
    }
]
*/
import request from '@/utils/request'
import qs from 'qs'
import { upcloud_base_common } from "@/config/serviceSettings";

export const tempType = {
  lhjhzd: 'lhjhzd', //零货拣货纸单
  buhuo: 'buhuo', //补货单
  YS_TZ: 'YS_TZ', //验收通知
  SJ_BQ: 'SJ_BQ', //上架标签
  ZJ_HZ: 'ZJ_HZ', //	质检汇总
  pxmx: 'pxmx', //拼箱明细
  ZJ_MX: 'ZJ_MX', //质检明细
  kcpc: 'kcpc', //库存批次
  wfhd: 'wfhd', //外复核单
  gnkfjhzd: 'gnkfjhzd', //功能库房拣货纸单
  wfhcd: 'wfhcd', //外复核草单
  pick_tag: 'pick_tag', //整件拣货标签
  zjbhbq: 'zjbhbq', //整件补货标签
  assimble_tag: 'assimble_tag', //拼箱标签
  pick_list: 'pick_list', //拣货纸单
  GSP_YH: 'GSP_YH', //GSP养护打印
  zjbhbq: 'zjbhbq', //GSP养护打印
  GSP_BS: 'GSP_BS', //报损
  GSP_XH: 'GSP_XH', //销毁
  YK_KP:'YK_KP',//移库开票
  YW_KP:'YW_KP',//移位开票
  RQC_PRINT_TEMP:'RQC_PRINT_TEMP',//验收
  RCV_PRINT_TEMP:'RCV_PRINT_TEMP',//收货
  INSTOCK_PRINT_TEMP:'INSTOCK_PRINT_TEMP',//上架

}
/* 
打印功能:
data: 打印数据(格式见上注释)
warehouseId: 仓库ID
tempType: 打印模板(字典report_template)
printParms: 打印请求参数{consignorId：货主id,shipvendorid：物流商id},
hookFun: 钩子函数，修改打印数据
*/
export function toPrintServer(data, warehouseId, tempType, printParms, hookFun) {
  let result = new Promise(async (resolve, reject) => {
    // console.log(printParms)
    printParms = printParms ? printParms : {}
    let tempRes = await handleQueryTemp(warehouseId, tempType, printParms)
    if (!tempRes.success) {
      plglib.ExceptionModal(tempRes)
    }
    if (!tempRes.data) {
      message.warn('获取打印模板配置错误！')
      return
    }
    let params = {
      data: data,
      url: tempRes.data.temppath,
      printSolution: tempRes.data.tempname, //打印预览预设打印模板，后面可能修改需问熊强强
      preview: false, // 是否预览
      count: 1
    }
    /* 处理钩子函数 */
    if (typeof hookFun === 'function') {
      params = hookFun(params)
    }
    /* 异步处理方案---promise */
    if (params instanceof Promise) {
      params.then(hRes => {
        handlePrint(hRes, tempRes.data.printServiceUrl).then(res => {
          resolve(res)
        })
      })
    } else if (params) {
      /* 同步处理方案 */
      handlePrint(params, tempRes.data.printServiceUrl).then(res => {
        resolve(res)
      })
    } else {
      reject('ERROR: Print Server quest-params error!')
    }
  })
  return result
}

// 查询打印模板
function handleQueryTemp(warehouseId, tempType, printParms) {
  return request({
    method: 'get',
    url: upcloud_base_common+'/printconf/print-templet',
    params: {
      warehouseId: warehouseId,
      tempType: tempType,
      consignorId: printParms.consignorId,
      shipvendorid: printParms.shipvendorid
    }
  })
}

// 调用打印机服务
export function handlePrint(parameter, url) {
  // parameter.printSolution = "batch_order.grf"
  // parameter.url = "http://minio.cloud.prolog.org/print/pingxiang.grf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=nyu8dWdGLqiUkvgF4ue5%2F20210531%2F%2Fs3%2Faws4_request&X-Amz-Date=20210531T122120Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=05902424cdc778adcf47d5afe4bae01a39138fc8dfa7f7a65e653064df2f66c2"

  return request({
    method: 'post',
    url: url || 'http://127.0.0.1:6060/print',
    data: parameter
  })
}

/*
打印服务
{
  "printSolution": "batch_order.grf",
  "data": [
        {
            "head": {
                "集货区": "LH2018053107093",
                "件数": "000",
                "总件数": 28,
                "是否重打": null,
                "单据类型": 87,
                "承运商名称": "阿里健康大药房旗舰店",
                "条码":"121sdrsd12"
            },
            "table":[
                {"number":"111","name":"张三","age":12},
                {"number":"112","name":"张三","age":12},
                {"number":"113","name":"张三","age":12},
                {"number":"114","name":"张三","age":12},
                {"number":"115","name":"张三","age":12}
            ]
        }
    ],
  "url": "http://192.168.10.13:3000/download/gridReport/整货拣货标签.grf",

  "preview":true,
  "count":2
}
*/
