import getLodop from "./LodopFuncs.js";
import { formatDecimal } from "@/utils/index.js";

/**
 * 打印订单小票
 */
export default (data) => {
  console.log("需要打印的订单数据===", data);
  // console.log("data.deviceName===", data.deviceName);
  let LODOP = getLodop();
  LODOP.PRINT_INIT("打印小票");
  // 设置打印纸大小D
  LODOP.SET_PRINT_PAGESIZE(3, "58mm", 20, "");
  //设置默认打印机(这里用的是打印机名称)
  LODOP.SET_PRINTER_INDEX(data.deviceName);
  // 文字内容
  let t1 = 40;
  let t2 = (100 - t1) / 3;
  let html = `
        <div style="font-size: 24px;display:flex;justify-content:center;">
          ${data.shop_name}
        </div>
        <div style="font-size: 16px;display: flex; justify-content:center;margin-top:6px;">
          ${data.isBefore ? "预" : ""}结算单  #${data.orderInfo.orderNum || ''}
        </div>
        <div style="font-size: 16px;display: flex; justify-content:center;margin-top:6px;">
          桌号：${data.orderInfo && data.orderInfo.tableName || '无'}
        </div>
        <div style="margin-top: 30px;font-size: 12px;">
          订单号：${data.orderInfo && data.orderInfo.orderNo}
        </div>
        <div style="margin-top: 4px;font-size: 12px;">
          交易时间：${data.createdAt}
        </div>
        <div style="margin-top: 4px;font-size: 12px;">
          收银员：${data.loginAccount}
        </div>
        <div style="margin-top: 6px;margin-bottom: 6px;width: 100%">
          <hr/>
        </div>
        <table class="table" style="width: 100%;">
          <tr>
            <td style="font-size: 12px;width:${t1}%;">品名</td>
            <td style="font-size: 12px;width:${t2}%;">单价</td>
            <td style="font-size: 12px;width:${t2}%;">数量</td>
            <td style="font-size: 12px;width:${t2}%;">小计</td>
          </tr>
  `;

  let table = "";
  for (let item of data.carts) {
    if (item.proGroupInfo) {
      table += `
        <tr>
          <td style="font-size: 12px;width:${t1}%" colspan="3">
            <div>${item.name}</div>
          </td>
          <td style="font-size: 12px;width:${t2}%;">${item.totalAmount}</td>
        </tr>
      `;

      let proGroupInfo = item.proGroupInfo;
      for (let item of proGroupInfo) {
        table += `
          <tr>
            <td style="font-size: 12px;width:${t1}%;">
              <div>>${item.proName}</div>
              ${
                item.skuName
                  ? `<div class="sku">规格：${item.skuName}</div>`
                  : ""
              }
            </td>
            <td style="font-size: 12px;width:${t2}%;">0</td>
            <td style="font-size: 12px;width:${t2}%;">${item.number}</td>
            <td style="font-size: 12px;width:${t2}%;">0</td>
          </tr>
        `;
      }
    } else {
      table += `
      <tr>
        <td style="font-size: 12px;width:${t1}%;">
          <div>${item.name}</div>
          ${item.skuName ? `<div class="sku">规格：${item.skuName}</div>` : ""}
        </td>
        <td style="font-size: 12px;width:${t2}%;">${item.salePrice}</td>
        <td style="font-size: 12px;width:${t2}%;">${item.number}</td>
        <td style="font-size: 12px;width:${t2}%;">
          ${item.totalAmount}
        </td>
      </tr>
`;
    }
  }

  let str = `
        </table>
        <div style="margin-top: 6px;margin-bottom: 6px;width: 100%">
          <hr/>
        </div>
        <div style="margin-top: 6px; font-size: 12px;display:flex;justify-content: space-between;">
          <span>原价</span>
          <span>${data.originAmount}</span>
        </div>
        <div style="margin-top: 6px; font-size: 12px;display:flex;justify-content: space-between;">
          <span>折扣</span>
          <span>-${data.discountAllAmount}</span>
        </div>
        <div style="margin-top: 6px;margin-bottom: 6px;width: 100%">
          <hr/>
        </div>
        <div style="margin-top: 6px; font-size: 22px;display:flex;justify-content: space-between;">
          <span>实付</span>
          <span>￥${data.orderAmount}</span>
        </div>
        <div style="margin-top: 6px;margin-bottom: 6px;width: 100%">
          <hr/>
        </div>
        <div style="margin-top: 4px; font-size: 16px;font-weight: bold;">备注：${
          data.remark
        }</div>
        <div style="margin-top: 4px; font-size: 12px;">
          打印时间：${data.printTime}
        </div>
        <div>.</div>
        <div>.</div>
        <div>.</div>
        <div>.</div>
  `;

  let lastHtml = `${html}${table}${str}`;

  setTimeout(() => {
    LODOP.ADD_PRINT_HTM("9mm", "0mm", "RightMargin:0mm", 20, lastHtml);
    LODOP.SET_LICENSES("", "DCFF409304DFCEB3E2C644BF96CD0720", "", "");
    LODOP.PRINT();
  }, 800);
};
