import { message } from 'antd';
import lodop from './LodopFuncs';

const createOneFormPage = (content, template) => {
  return template(content);
};

/**
 * 按模版打印，支持批量打印
 * ```
 * @param {array} tasks 打印任务 包含打印内容和hbs模版函数
 * @param {boolean} isBatchPrint 是否批量打印flag
 */
const print = (tasks, isBatchPrint) => {
  // console.log(tasks, isBatchPrint);
  const LODOP = lodop.getLodop();

  const printOnePage = (printerId, content, template) => {
    if (LODOP === undefined) { return; }
    LODOP.PRINT_INIT(content.buzName);
    LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4');

    LODOP.ADD_PRINT_IMAGE('10mm', '23mm', '45mm', '20mm', '<img src=\'/static/assets/img/contract-logo.png\'/>');
    LODOP.SET_PRINT_STYLEA(0, 'Stretch', 2);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 0);
    LODOP.SET_PRINT_STYLEA(0, 'Horient', 0);
    // LODOP.ADD_PRINT_TEXT('14mm', '169mm', '100mm', '10mm', '手工做好面');
    LODOP.SET_PRINT_STYLEA(0, 'FontSize', 15);
    LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);
    LODOP.SET_PRINT_STYLEA(0, 'FontColor', '#7F7F7F');
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 0);
    LODOP.SET_PRINT_STYLEA(0, 'Horient', 1);
    LODOP.ADD_PRINT_LINE('20mm', '12.7mm', '20mm', '196mm', 0, 1);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
    LODOP.SET_PRINT_STYLEA(0, 'Horient', 3);

    const html = createOneFormPage(content, template);
    LODOP.ADD_PRINT_HTM('25mm', '12.7mm', '184.6mm', '115mm', html);
    LODOP.SET_PRINT_STYLEA(0, 'FontSize', 15);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 4);
    LODOP.SET_PRINT_STYLEA(0, 'Horient', 3);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 3);

    LODOP.ADD_PRINT_TEXT('145mm', '15mm', '184.6mm', '10mm', '佛山市麦点食品有限公司          Tel: 0757-86701089          Email:gzicd@jiumaojiu.com          Web: http://www.jiumaojiu.com');
    LODOP.SET_PRINT_STYLEA(0, 'FontSize', 7);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 1);

    LODOP.ADD_PRINT_TEXT('149mm', '70mm', '184.6mm', '10mm', '地址:广东省佛山市南海区三山大道泰山北路2号');
    LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 1);

    LODOP.ADD_PRINT_TEXT('149mm', '165mm', '184.6mm', '10mm', '第#页 共&页');
    LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 2);
    LODOP.SET_PRINT_STYLEA(0, 'Vorient', 1);

    if (isBatchPrint) {
      LODOP.SET_PRINTER_INDEXA(printerId); // 设置默认打印机
      LODOP.PRINT();
    } else {
      LODOP.PREVIEW();
    }
  };

  if (isBatchPrint) {
    if (LODOP.CVERSION) {
      LODOP.On_Return = function(TaskID, value) {
        if (value >= 0) {
          console.log('选择成功!', TaskID, value);
          tasks.forEach((task) => {
            // console.log(task);
            printOnePage(value, task.content, task.tmpl);
          });
          message.success('打印指令发送完成！');
        } else {
          console.log('选择失败！');
        }
      };
      LODOP.SELECT_PRINTER();
      return 0;
    }
  } else {
    tasks.forEach((task) => {
      printOnePage(-1, task.content, task.tmpl);
    });
  }

  // LODOP.SELECT_PRINTER() // 选择打印机
  // LODOP.SET_PRINTER_INDEXA(-1) // 设置默认打印机
  // LODOP.PRINT_INIT(templateName)
  // LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4')
  // LODOP.ADD_PRINT_HTML(0, 0, '100%', '100%', html)
  // LODOP.PREVIEW()
  // LODOP.PRINT()
  return 1;
};
/**
 * @deprecated
 * 按模版打印,tasks中的内容拼接为一个输出
 * ```
 * @param {array} tasks 打印任务 包含打印内容和hbs模版函数
 * @param 打印任务名
 */
const printJoin = (tasks, strPrintTaskName) => {
  const LODOP = lodop.getLodop();
  LODOP.PRINT_INIT(strPrintTaskName);
  LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4');
  let html = '';
  tasks.forEach((task) => {
    // console.log("task==>",task.content);
    if (task.tmpl) {
      html = html += createOneFormPage(task.content, task.tmpl);
    } else {
      html = html += task.content;
    }
  });
  LODOP.ADD_PRINT_HTM(30, 10, 'RightMargin:10', 'BottomMargin:30', html);
  LODOP.PREVIEW();
  return 1;
};
export default {
  print,
  printJoin,
};
