import { getLodop } from './LodopFuncs';
import { encode, decode } from 'js-base64';
import { DetailTemplate, FormTemplate, LabelTemplate } from './type';
import { SettingValues } from '../../renderers/Lion/LabelPrint/types';

export default function () { }

// 由于getLodop()获取的LODOP是同一个对象，因此LODOP.On_Return重复赋值会被覆盖掉
// 所以将每次打印赋给On_Return的函数依次放到printCallbackArr里，（LODOP的打印任务是依次执行），每次取第一个数组的第一个函数执行
// 当当前任务的打印数达到目标后，删除对应的函数（数组第一个），此时第二个元素变成第一个，再赋值给On_Return
// var printCallbackArr: any[] = []

function new_eval(str: string) {
  var fn = Function
  return new fn(str)()
}

// 获取系统打印机设备信息
export function getPrinters(): string[] {
  const LODOP = getLodop();
  // 获取系统打印设备数量
  const count = LODOP.GET_PRINTER_COUNT();
  const printers = [];
  for (let i = 0; i < count; i++) {
    printers.push(LODOP.GET_PRINTER_NAME(i));
  }
  return printers;
}

export function getPrinterPageSizes(printer: string) {
  const LODOP = getLodop()
  return LODOP.GET_PAGESIZES_LIST(printer, ',').split(',')
}

export function templateDesign(rowData: any, onCompleted: (value: string) => void) {
  const LODOP = getLodop()
  const { TEMP_CONTENT: tempContent, WIDTH: width, HEIGHT: height } = rowData

  LODOP.PRINT_INITA(0, 0, `${width}mm`, `${height}mm`, 'design')
  if (tempContent) {
    const temp = decode(tempContent); // 解码base64
    // 保存模板包括了初始化
    // 将初始化语句分离出来
    const initalSentenceIndex = temp.indexOf(';');
    // 将初始化语句分离后的剩余的模板语句
    const restSentence = temp.substring(initalSentenceIndex + 1);
    new_eval(restSentence);
  }
  LODOP.PRINT_DESIGN()
  LODOP.On_Return = (_taskId: string, value: string) => {
    onCompleted(encode(value))
  }
}

export const detailPrint = (preview: boolean, settings: SettingValues, template: DetailTemplate, onCompleted?: (taskId: string, value: any) => void) => {
  const iframe = document.getElementById('dom-print-container') as HTMLIFrameElement | null
  const { printer: printerName, width, height, direction, copies, scale = 100 } = settings
  const { logo, code, codeLabel, extraTitle, title, subTitle1, subTitle2, subTitle3, html, footer, date, printer, sum, num } = template

  const LODOP = getLodop()
  LODOP.PRINT_INIT('report');
  LODOP.SET_PRINT_PAGESIZE(direction == 'vertical' ? 1 : 2, width * 10, height * 10, 'CreateCustomPage')
  LODOP.NEWPAGEA()
  // header LODOP.ADD_PRINT_RECT(`${header.top}mm`, `${header.left}mm`, `${header.width}mm`, `${header.height}mm`, 0, 1)
  LODOP.ADD_PRINT_IMAGE(`${logo.top * (scale / 100)}mm`, `${logo.left}mm`, `${logo.width * (scale / 100)}mm`, `${logo.height * (scale / 100)}mm`, logo.content!)
  LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

  if (code.content) {
    // LODOP.ADD_PRINT_RECT(`${code.top}mm`, `${code.left}mm`, `${code.width}mm`, `${code.height}mm`, 0, 1)
    LODOP.ADD_PRINT_BARCODE(`${code.top}mm`, `${code.left}mm`, `${code.width}mm`, `${code.height}mm`, code.codeType, code.content!)
    if (code.codeType == 'QRCode') {
      LODOP.SET_PRINT_STYLEA(0, "ScalX", 1.15)
      LODOP.SET_PRINT_STYLEA(0, "ScalY", 1.15)
    }
    LODOP.SET_PRINT_STYLEA(0, "ShowBarText", 0)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    // LODOP.ADD_PRINT_RECT(`${codeLabel.top}mm`, `${codeLabel.left}mm`, `${codeLabel.width}mm`, `${codeLabel.height}mm`, 0, 1)
    LODOP.ADD_PRINT_TEXT(`${codeLabel.top}mm`, `${codeLabel.left}mm`, `${codeLabel.width}mm`, `${codeLabel.height}mm`, codeLabel.content!)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  }

  // rightTilte LODOP.ADD_PRINT_RECT(`${extraTitle.top}mm`, `${extraTitle.left}mm`, `${extraTitle.width}mm`, `${extraTitle.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${extraTitle.top * (scale / 100)}mm`, `${extraTitle.left}mm`, `${extraTitle.width}mm`, `${extraTitle.height * (scale / 100)}mm`, extraTitle.content!)
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  // title LODOP.ADD_PRINT_RECT(`${title.top}mm`, `${title.left}mm`, `${title.width}mm`, `${title.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${title.top * (scale / 100)}mm`, `${title.left}mm`, `${title.width}mm`, `${title.height * (scale / 100)}mm`, title.content!)
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 12 * (scale / 100));
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  // subtitle LODOP.ADD_PRINT_RECT(`${subTitle.top}mm`, `${subTitle.left}mm`, `${subTitle.width}mm`, `${subTitle.height}mm`, 0, 1)
  //  LODOP.ADD_PRINT_RECT(`${subTitle1.top}mm`, `${subTitle1.left}mm`, `${subTitle1.width}mm`, `${subTitle1.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${subTitle1.top}mm`, `${subTitle1.left}mm`, `${subTitle1.width}mm`, `${subTitle1.height}mm`, subTitle1.content ?? '')
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
  LODOP.SET_PRINT_STYLEA(0, "Alignment", subTitle2.width == 0 && subTitle3.width == 0 ? 3 : 1)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  //  LODOP.ADD_PRINT_RECT(`${subTitle2.top}mm`, `${subTitle2.left}mm`, `${subTitle2.width}mm`, `${subTitle2.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${subTitle2.top}mm`, `${subTitle2.left}mm`, `${subTitle2.width}mm`, `${subTitle2.height}mm`, subTitle2.content ?? '')
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
  LODOP.SET_PRINT_STYLEA(0, "Alignment", subTitle3.width == 0 ? 3 : 2)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  // LODOP.ADD_PRINT_RECT(`${subTitle3.top}mm`, `${subTitle3.left}mm`, `${subTitle3.width}mm`, `${subTitle3.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${subTitle3.top}mm`, `${subTitle3.left}mm`, `${subTitle3.width}mm`, `${subTitle3.height}mm`, subTitle3.content ?? '')
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 3)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
  // LODOP.ADD_PRINT_RECT(`${html.top}mm`, `${html.left}mm`, `${html.width}mm`, `${html.height}mm`, 0, 1)
  LODOP.ADD_PRINT_HTM(`${html.top}mm`, `${html.left}mm`, `${html.width}mm`, `${html.height}mm`, iframe?.contentWindow?.document.documentElement.innerHTML ?? 'iframe error')

  LODOP.ADD_PRINT_RECT(`${footer.top - 2}mm`, `${footer.left}mm`, `${footer.width}mm`, `${0.3}`, 0, 1)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

  // LODOP.ADD_PRINT_RECT(`${date.top}mm`, `${date.left}mm`, `${date.width}mm`, `${date.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${date.top}mm`, `${date.left}mm`, `${date.width}mm`, `${date.height * (scale / 100)}mm`, date.content ?? '')
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 10 * (scale / 100));
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 2)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

  // LODOP.ADD_PRINT_RECT(`${printer.top}mm`, `${printer.left}mm`, `${printer.width}mm`, `${printer.height}mm`, 0, 1)
  LODOP.ADD_PRINT_TEXT(`${printer.top}mm`, `${printer.left}mm`, `${printer.width}mm`, `${printer.height * (scale / 100)}mm`, printer.content ?? '')
  LODOP.SET_PRINT_STYLEA(0, "FontSize", 10 * (scale / 100));
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 1)
  LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

  LODOP.ADD_PRINT_TEXT(`${num.top}mm`, `${num.left}mm`, `${num.width}mm`, `${num.height}mm`, num.content!)
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
  LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 1);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", num.content!.length > 0 ? 2 : 0)

  LODOP.ADD_PRINT_TEXT(`${sum.top}mm`, `${sum.left}mm`, `${sum.width}mm`, `${sum.height}mm`, sum.content!)
  LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
  LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 1);
  LODOP.SET_PRINT_STYLEA(0, "ItemType", sum.content!.length > 0 ? 3 : 0)

  LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
  LODOP.SET_PRINTER_INDEX(printerName)
  if (preview) {
    LODOP.PREVIEW('detail-preview')
  } else {
    LODOP.SET_PRINT_COPIES(copies)
    LODOP.PRINT()
  }
  if (onCompleted) {
    LODOP.On_Return = onCompleted
  }
}

export const formPrint = (preview: boolean, settings: SettingValues, template: FormTemplate, htmls: string[], onCompleted?: () => void) => {
  const { printer: printerName, width, height, direction, copies } = settings
  const { logo, headerTitle, headerLine, title, subTitle1, subTitle2, subTitle3, table, footerLine, footerTitle, printer, date, num, sum } = template

  const LODOP = getLodop()
  const createTask = (tableContent: string, index: number) => {
    const isFirstTask = index == 0
    LODOP.PRINT_INIT('report');
    LODOP.SET_PRINT_PAGESIZE(direction == 'vertical' ? 1 : 2, width * 10, height * 10, 'CreateCustomPage')
    LODOP.NEWPAGEA()

    LODOP.ADD_PRINT_IMAGE(`${logo.top}mm`, `${logo.left}mm`, `${logo.width}mm`, `${logo.height}mm`, logo.content!)
    LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_TEXT(`${headerTitle.top}mm`, `${headerTitle.left}mm`, `${headerTitle.width}mm`, `${headerTitle.height}mm`, headerTitle.content!)
    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_RECT(`${headerLine.top}mm`, `${headerLine.left}mm`, `${headerLine.width}mm`, `${headerLine.height}`, 0, 1)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)
    // title
    if (title.itemType === 1 || isFirstTask) {
      LODOP.ADD_PRINT_TEXT(`${title.top}mm`, `${title.left}mm`, `${title.width}mm`, `${title.height}mm`, title.content ?? '')
      LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
      LODOP.SET_PRINT_STYLEA(0, "Alignment", 2)
      LODOP.SET_PRINT_STYLEA(0, "ItemType", title.itemType ?? 1)
    }
    // subtitle
    if (subTitle1.itemType === 1 || isFirstTask) {
      LODOP.ADD_PRINT_TEXT(`${subTitle1.top}mm`, `${subTitle1.left}mm`, `${subTitle1.width}mm`, `${subTitle1.height}mm`, subTitle1.content ?? '')
      LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
      LODOP.SET_PRINT_STYLEA(0, "Alignment", subTitle2.width == 0 && subTitle3.width == 0 ? 3 : 1)
      LODOP.SET_PRINT_STYLEA(0, "ItemType", subTitle1.itemType ?? 1)
    }
    if (subTitle2.itemType === 1 || isFirstTask) {
      LODOP.ADD_PRINT_TEXT(`${subTitle2.top}mm`, `${subTitle2.left}mm`, `${subTitle2.width}mm`, `${subTitle2.height}mm`, subTitle2.content ?? '')
      LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
      LODOP.SET_PRINT_STYLEA(0, "Alignment", subTitle3.width == 0 ? 3 : 2)
      LODOP.SET_PRINT_STYLEA(0, "ItemType", subTitle2.itemType ?? 1)
    }
    if (subTitle3.itemType === 1 || isFirstTask) {
      LODOP.ADD_PRINT_TEXT(`${subTitle3.top}mm`, `${subTitle3.left}mm`, `${subTitle3.width}mm`, `${subTitle3.height}mm`, subTitle3.content ?? '')
      LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
      LODOP.SET_PRINT_STYLEA(0, "Alignment", 3)
      LODOP.SET_PRINT_STYLEA(0, "ItemType", subTitle3.itemType ?? 1)
    }
    // table
    LODOP.ADD_PRINT_TABLE(`${table.top}mm`, `${table.left}mm`, `${table.width}mm`, `${table.height}mm`, tableContent)
    LODOP.SET_PRINT_STYLEA(0, 'TableHeightScope', 1)

    // foot
    LODOP.ADD_PRINT_TEXT(`${footerTitle.top}mm`, `${footerTitle.left}mm`, `${footerTitle.width}mm`, `${footerTitle.height}mm`, footerTitle.content!)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_RECT(`${footerLine.top}mm`, `${footerLine.left}mm`, `${footerLine.width}mm`, `${footerLine.height}`, 0, 1)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_TEXT(`${printer.top}mm`, `${printer.left}mm`, `${printer.width}mm`, `${printer.height}mm`, printer.content!)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_TEXT(`${date.top}mm`, `${date.left}mm`, `${date.width}mm`, `${date.height}mm`, date.content!)
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 2)
    LODOP.SET_PRINT_STYLEA(0, "ItemType", 1)

    LODOP.ADD_PRINT_TEXT(`${num.top}mm`, `${num.left}mm`, `${num.width}mm`, `${num.height}mm`, num.content!)
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
    LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 1);
    LODOP.SET_PRINT_STYLEA(0, "ItemType", num.content!.length > 0 ? 2 : 0)

    LODOP.ADD_PRINT_TEXT(`${sum.top}mm`, `${sum.left}mm`, `${sum.width}mm`, `${sum.height}mm`, sum.content!)
    LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
    LODOP.SET_PRINT_STYLEA(0, "AlignJustify", 1);
    LODOP.SET_PRINT_STYLEA(0, "ItemType", sum.content!.length > 0 ? 3 : 0)

    LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
    LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
    LODOP.SET_PRINT_MODE("CUSTOM_TASK_NAME", `report${index}`)
    LODOP.SET_PRINTER_INDEX(printerName)
    LODOP.SET_PRINT_COPIES(copies)
  }

  if (preview) {
    createTask(htmls[0], 0)
    LODOP.PREVIEW('report-preview')
  } else {
    for (let i = 0; i < htmls.length; i++) {
      const tableContent = htmls[i]
      createTask(tableContent, i)
      LODOP.PRINT()
    }
  }

  if (onCompleted) {
    LODOP.On_Return = () => onCompleted()
  }
}

export function billPrint(params: {
  templateDatas: LabelTemplate[][],
  pageWidth: number, // 单位mm
  pageHeight: number, // 单位mm
  direction: boolean,
  preview: boolean,
  printer: string,
  count: number
}, onCompleted?: () => void) {
  const LODOP = getLodop()
  const { templateDatas, direction, pageWidth, pageHeight, printer, preview, count } = params

  LODOP.PRINT_INITA(0, 0, `${pageWidth}mm`, `${pageHeight}mm`, 'billprint');
  LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, '')
  for (let i = 0; i < templateDatas.length; i++) {
    LODOP.NEWPAGEA()
    const templateData = templateDatas[i]
    for (let j = 0; j < templateData.length; j++) {
      const labelData = templateData[j]
      const { labelTexts, labelHtmls, labelImages, labelTables, labelBarCodes, labelRects, labelEllipses, labelLines, labelShapes } = labelData
      labelTables.forEach(table => {
        LODOP.ADD_PRINT_TABLE(table.top.strValue, table.left.strValue, table.width.strValue, table.height.strValue, table.content)
        const styles = table.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelTexts.forEach(text => {
        const fieldName = text.fieldName
        if (fieldName) {
          LODOP.ADD_PRINT_TEXTA(fieldName, text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content)
        } else {
          LODOP.ADD_PRINT_TEXT(text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content)
        }
        const styles = text.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelHtmls.forEach(html => {
        LODOP.ADD_PRINT_HTM(html.top.strValue, html.left.strValue, html.width.strValue, html.height.strValue, html.content)
        const styles = html.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelImages.forEach(image => {
        LODOP.ADD_PRINT_IMAGE(image.top.strValue, image.left.strValue, image.width.strValue, image.height.strValue, image.url)
        const styles = image.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelBarCodes.forEach(barCode => {
        const fieldName = barCode.fieldName
        if (fieldName) {
          LODOP.ADD_PRINT_BARCODEA(fieldName, barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue)
        } else {
          LODOP.ADD_PRINT_BARCODE(barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue)
        }
        const styles = barCode.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelRects.forEach(rect => LODOP.ADD_PRINT_RECT(rect.top.strValue, rect.left.strValue, rect.width.strValue, rect.height.strValue, rect.lineStyle, rect.lineWidth))
      labelEllipses.forEach(ellipse => LODOP.ADD_PRINT_ELLIPSE(ellipse.top.strValue, ellipse.left.strValue, ellipse.width.strValue, ellipse.height.strValue, ellipse.lineStyle, ellipse.lineWidth))
      labelLines.forEach(line => LODOP.ADD_PRINT_LINE(line.top.strValue, line.left.strValue, line.width.strValue, line.height.strValue, line.lineStyle, line.lineWidth))
      labelShapes.forEach(shape => LODOP.ADD_PRINT_SHAPE(shape.shapeType, shape.top.strValue, shape.left.strValue, shape.width.strValue, shape.height.strValue, shape.lineStyle, shape.lineWidth, shape.fillColor))
    }
  }
  if (preview) {
    LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PREVIEW('bill-preview')
  } else {
    LODOP.SET_PRINT_COPIES(count)
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PRINT()
  }
  if (onCompleted) {
    LODOP.On_Return = () => onCompleted()
  }
}

export function labelPrint(params: {
  templateDatas: LabelTemplate[][],
  marginTop: number,
  marginLeft: number,
  pageWidth: number, // 单位mm
  pageHeight: number, // 单位mm
  direction: boolean,
  preview: boolean,
  printer: string,
  count: number
}, onCompleted?: () => void
) {
  const LODOP = getLodop()
  const { templateDatas, marginTop, marginLeft, direction, pageWidth, pageHeight, preview, printer } = params
  LODOP.PRINT_INITA(`${marginTop}mm`, `${marginLeft}mm`, `${pageWidth}mm`, `${pageHeight}mm`, `labelprint_${Date.now()}`)
  LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, '')
  for (let i = 0; i < templateDatas.length; i++) {
    LODOP.NEWPAGEA()
    const templateData = templateDatas[i]
    for (let j = 0; j < templateData.length; j++) {
      const labelData = templateData[j]
      const { tempTop, tempLeft, tempWidth, tempHeight, labelTexts, labelHtmls, labelTables, labelImages, labelBarCodes, labelRects, labelEllipses, labelLines, labelShapes } = labelData
      labelTables.forEach(table => {
        LODOP.ADD_PRINT_TABLE(table.top.strValue, table.left.strValue, table.width.strValue, table.height.strValue, table.content)
        const styles = table.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelTexts.forEach(text => {
        const fieldName = text.fieldName
        if (fieldName) {
          LODOP.ADD_PRINT_TEXTA(fieldName, text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content)
        } else {
          LODOP.ADD_PRINT_TEXT(text.top.strValue, text.left.strValue, text.width.strValue, text.height.strValue, text.content)
        }
        const styles = text.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelHtmls.forEach(html => {
        LODOP.ADD_PRINT_HTM(html.top.strValue, html.left.strValue, html.width.strValue, html.height.strValue, html.content)
        const styles = html.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelImages.forEach(image => {
        LODOP.ADD_PRINT_IMAGE(image.top.strValue, image.left.strValue, image.width.strValue, image.height.strValue, image.url)
        const styles = image.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelBarCodes.forEach(barCode => {
        const fieldName = barCode.fieldName
        if (fieldName) {
          LODOP.ADD_PRINT_BARCODEA(fieldName, barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue)
        } else {
          LODOP.ADD_PRINT_BARCODE(barCode.top.strValue, barCode.left.strValue, barCode.width.strValue, barCode.height.strValue, barCode.codeType, barCode.codeValue)
        }
        const styles = barCode.styles
        if (styles) {
          styles.forEach(([id, name, value]) => {
            LODOP.SET_PRINT_STYLEA(id, name, value)
          })
        }
      })
      labelRects.forEach(rect => LODOP.ADD_PRINT_RECT(rect.top.strValue, rect.left.strValue, rect.width.strValue, rect.height.strValue, rect.lineStyle, rect.lineWidth))
      labelEllipses.forEach(ellipse => LODOP.ADD_PRINT_ELLIPSE(ellipse.top.strValue, ellipse.left.strValue, ellipse.width.strValue, ellipse.height.strValue, ellipse.lineStyle, ellipse.lineWidth))
      labelLines.forEach(line => LODOP.ADD_PRINT_LINE(line.top.strValue, line.left.strValue, line.width.strValue, line.height.strValue, line.lineStyle, line.lineWidth))
      labelShapes.forEach(shape => LODOP.ADD_PRINT_SHAPE(shape.shapeType, shape.top.strValue, shape.left.strValue, shape.width.strValue, shape.height.strValue, shape.lineStyle, shape.lineWidth, shape.fillColor))
      if (preview) {
        LODOP.ADD_PRINT_RECT(tempTop.strValue, tempLeft.strValue, tempWidth.strValue, tempHeight.strValue, 0, 1);
      }
    }
  }
  LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true)
  if (preview) {
    LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PREVIEW('label-preview')
  } else {
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PRINT()
  }
  if (onCompleted) {
    LODOP.On_Return = () => onCompleted()
  }
}

// 文件仅预览
export function filePrint(
  params: {
    fileDatas: { type: string, content: string }[],
    printer: string,
    pageWidth: number,
    pageHeight: number,
    marginLeft: number,
    marginTop: number,
    marginRight: number,
    marginBottom: number,
    direction: boolean,
    preview: boolean
  }, onCompleted?: () => void
) {

  const LODOP = getLodop()
  const { fileDatas, printer, direction, pageWidth, pageHeight, preview, marginLeft, marginTop, marginRight, marginBottom } = params
  LODOP.PRINT_INIT();
  for (let i = 0; i < fileDatas.length; i++) {
    LODOP.NEWPAGEA();
    const content = fileDatas[i].content

    if (fileDatas[i].type === 'pdf') {
      LODOP.ADD_PRINT_PDF(`${marginTop}mm`, `${marginLeft}mm`, `RightMargin:${marginRight}mm`, `BottomMargin:${marginBottom}mm`, content); // 这里设置的宽高是无效的
      LODOP.SET_PRINT_STYLEA(0, 'PDFScalMode', 1);
    } else {
      // 测试地址图片 'https://img.zcool.cn/community/0147e86243ff520002c3290f77f99d.jpg@1280w_1l_2o_100sh.jpg'
      LODOP.ADD_PRINT_HTML(`${marginTop}mm`, `${marginLeft}mm`, `RightMargin:${marginRight}mm`, `BottomMargin:${marginBottom}mm`, `<img style="width:100%" src=${content} />`);
    }
    // 整页按比例缩放
    // 上面的语句会将内容铺满整张纸，由于打印机的可打印区域不是整张纸，因此打印可能会被裁掉部分内容，所以将内容缩小一些
    // LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', '95%');
    // 对于单页输出，如希望内容超出纸宽或纸高时对应缩小，用如下俩语句：
    // LODOP.SET_PRINT_MODE('FULL_WIDTH_FOR_OVERFLOW', true);
    // LODOP.SET_PRINT_MODE('FULL_HEIGHT_FOR_OVERFLOW', true);
    // LODOP.SET_PRINT_STYLEA(0, 'ScalX', 1.5); // 这个可以设置缩放高度、宽度比例
  }
  LODOP.SET_PRINT_PAGESIZE(direction ? 1 : 2, pageWidth * 10, pageHeight * 10, ''); // 设置纸张大小
  LODOP.SET_PRINTER_INDEX(printer); // 设置打印机
  LODOP.SET_SHOW_MODE('HIDE_SBUTTIN_PREVIEW', true); // 预览窗口隐藏设置按钮
  LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true); // 预览窗口隐藏打印按钮
  LODOP.SET_SHOW_MODE('LANDSCAPE_DEFROTATED', true); // 横向打印时正向显示
  // LODOP.SET_PRINT_MODE('AUTO_CLOSE_PREWINDOW', true); // 打印完毕自动关闭预览窗口
  // LODOP.SET_PREVIEW_WINDOW(1, 0, 1, 900, 600); // 设置预览窗口
  if (preview) {
    LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PREVIEW('file-preview')
  } else {
    LODOP.SET_PRINTER_INDEX(printer)
    LODOP.PRINT()
  }

  if (onCompleted) {
    LODOP.On_Return = () => onCompleted()
  }
}
