const printerNameCacheKey = 'printerNameCache'
const printerIndexCacheKey = 'printerIndexCache'
const clodopBridgeIdCacheKey = 'clodopBridgeIdCache'
const clodopBridgeNameCacheKey = 'clodopBridgeName'

export function initClodop() {
  if (!window.WLODOP) {
    window.alert('未设置CLODOP')
    return
  }

  const printIndex = localStorage.getItem(printerIndexCacheKey)
  const bridgeId = localStorage.getItem(clodopBridgeIdCacheKey)
  const bridgeName = localStorage.getItem(clodopBridgeNameCacheKey)
  if (!printIndex || printIndex < 0) {
    window.alert('请先设置打印机')
    return
  }
  WLODOP.SET_BRIDGE_INDEX(bridgeId + ';' + bridgeName + ',' + printIndex)
}

export function printSKULabel(skuObj) {
  initClodop()
  const skuDesc = `${skuObj.CategoryName} ${skuObj.Color} ${skuObj.Size}`
  WLODOP.SET_PRINT_PAGESIZE(1, 1000, 500, '')

  WLODOP.SET_PRINT_STYLE('FontSize', 14)
  WLODOP.ADD_PRINT_TEXT('5mm', '5mm', '100mm', '10mm', skuDesc)

  WLODOP.SET_PRINT_STYLE('FontSize', 14)
  WLODOP.ADD_PRINT_TEXT('5mm', '45mm', '50mm', '10mm', skuObj.SkuNo)
  WLODOP.SET_PRINT_STYLEA(0, 'FontName', '黑体')
  WLODOP.SET_PRINT_STYLEA(0, 'Alignment', 3)

  WLODOP.ADD_PRINT_LINE('10mm', '5mm', '10mm', '95mm', 0, 1)

  WLODOP.SET_PRINT_STYLE('FontSize', 10)

  WLODOP.ADD_PRINT_BARCODE('15mm', '8mm', '92mm', '25mm', '128Auto', skuObj.BatchNo)
  WLODOP.SET_PRINT_STYLEA(0, 'AlignJustify', 2)

  WLODOP.ADD_PRINT_LINE('42mm', '5mm', '42mm', '95mm', 0, 1)

  WLODOP.ADD_PRINT_TEXT('45mm', '45mm', '50mm', '5mm', `签收时间:${skuObj.SignTimeText}`)
  WLODOP.SET_PRINT_STYLEA(0, 'Alignment', 3)

  // LODOP.PREVIEW("_blank");
  return WLODOP.PRINT()
}

export function printGridLabel(gridObj) {
  initClodop()
  const gridName = gridObj.name
  const id = gridObj.id
  const number = gridObj.number
  // WLODOP.PRINT_INITA(0, 0, '80mm', '40mm', '')
  WLODOP.SET_PRINT_PAGESIZE(0, '80mm', '40mm', '')
  WLODOP.SET_PRINT_STYLE('FontSize', 16)
  WLODOP.ADD_PRINT_TEXT('3mm', '10mm', '60mm', '10mm', gridName)
  WLODOP.SET_PRINT_STYLE('FontSize', 14)
  WLODOP.ADD_PRINT_BARCODE('10mm', '5mm', '70mm', '25mm', '128Auto', number)
  WLODOP.SET_PRINT_STYLEA(0, 'AlignJustify', 2)
  WLODOP.SET_PRINT_STYLEA(0, 'Alignment', 3)
  return WLODOP.PRINT()
}
