/* eslint-disable */
import { hiprint } from "vue-plugin-hiprint";
import { HiprintTemplateType } from "./interface/index";

// 自定义设计元素1
export const panelsProvider1 = function () {
  var addElementTypes = function (context: any) {
    context.removePrintElementTypes("panelsProvider1");
    context.addPrintElementTypes("panelsProvider1", [
      new hiprint.PrintElementTypeGroup("平台", [
        {
          tid: "panelsProvider1.header",
          title: "单据表头",
          data: "单据表头",
          type: "text",
          options: {
            testData: "单据表头",
            height: 17,
            fontSize: 16.5,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        {
          tid: "panelsProvider1.type",
          title: "单据类型",
          data: "单据类型",
          type: "text",
          options: {
            testData: "单据类型",
            height: 16,
            fontSize: 15,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        {
          tid: "panelsProvider1.order",
          title: "订单编号",
          data: "XS888888888",
          type: "text",
          options: {
            field: "orderId",
            testData: "XS888888888",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider1.date",
          title: "业务日期",
          data: "2020-01-01",
          type: "text",
          options: {
            field: "date",
            testData: "2020-01-01",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider1.barcode",
          title: "条形码",
          data: "XS888888888",
          type: "text",
          options: {
            field: "barcode",
            testData: "XS888888888",
            height: 32,
            fontSize: 12,
            lineHeight: 18,
            textType: "barcode"
          }
        },
        {
          tid: "panelsProvider1.qrcode",
          title: "二维码",
          data: "XS888888888",
          type: "text",
          options: {
            field: "qrcode",
            testData: "XS888888888",
            height: 32,
            fontSize: 12,
            lineHeight: 18,
            textType: "qrcode"
          }
        },
        {
          tid: "panelsProvider1.platform",
          title: "平台名称",
          data: "平台名称",
          type: "text",
          options: {
            testData: "平台名称",
            height: 17,
            fontSize: 16.5,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        { tid: "panelsProvider1.logo", title: "Logo", data: "", type: "image" }
      ]),
      new hiprint.PrintElementTypeGroup("库管", [
        {
          tid: "panelsProvider1.creater",
          title: "制单人",
          data: "李四",
          type: "text",
          options: {
            field: "creater",
            testData: "李四",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider1.printDate",
          title: "打印时间",
          data: "2022-01-01 09:00",
          type: "text",
          options: {
            field: "printDate",
            testData: "2022-01-01 09:00",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider1.signer",
          title: "库管签字",
          data: "",
          type: "text",
          options: {
            title: "库管签字：",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        }
      ]),
      new hiprint.PrintElementTypeGroup("表格/其他", [
        {
          tid: "panelsProvider1.table",
          title: "订单数据",
          type: "table",
          options: {
            field: "table",
            tableHeaderRepeat: "first",
            tableFooterRepeat: "last",
            fields: [
              { text: "序", field: "printid" },
              { text: "名称", field: "name" },
              { text: "数量", field: "count" },
              { text: "性别", field: "gender" },
              { text: "金额", field: "amount" }
            ]
          },
          editable: true,
          columnDisplayEditable: true, //列显示是否能编辑
          columnDisplayIndexEditable: true, //列顺序显示是否能编辑
          columnTitleEditable: true, //列标题是否能编辑
          columnResizable: true, //列宽是否能调整
          columnAlignEditable: true, //列对齐是否调整
          isEnableEditField: true, //编辑字段
          isEnableContextMenu: true, //开启右键菜单 默认true
          isEnableInsertRow: true, //插入行
          isEnableDeleteRow: true, //删除行
          isEnableInsertColumn: true, //插入列
          isEnableDeleteColumn: true, //删除列
          isEnableMergeCell: true, //合并单元格
          columns: [
            [
              { title: "序", align: "center", field: "printid", width: 50, tableTextType: "sequence" },
              { title: "名称", align: "center", field: "name", width: 150 },
              { title: "数量", align: "center", field: "count", width: 80, tableSummary: "count" },
              { title: "性别", align: "center", field: "gender", width: 80 },
              { title: "金额", align: "center", field: "amount", width: 100, tableSummary: "sum" }
            ]
          ],
          // rowsColumnsMerge: function (data: any, col: any, index: number) {
          //   // 返回一个数组,参数一为行（rowspan）合并数,参数二为列（colspan）合并数, 被合并的行或者列值设为0
          //   if (index == 0) {
          //     return [1, data.INDEX % 2 == 1 ? 2 : 1];
          //   } else if (index > 0 && index < 2) {
          //     return [data.INDEX % 2 == 1 ? 0 : 1, 1];
          //   } else {
          //     return [data.INDEX % 2 == 1 ? 2 : 0, 1];
          //   }
          // },
          footerFormatter: function (options: any, rows: any, data: any, currentPageGridRowsData: any) {
            if (data && data["totalCap"]) {
              console.log("footerFormatter data :>> ", data);
              return `<td style="padding:0 10px" colspan="100">${"应收金额大写: " + data["totalCap"]}</td>`;
            }
            return '<td style="padding:0 10px" colspan="100">应收金额大写: </td>';
          }
        },
        { tid: "panelsProvider1.customText", title: "文本", customText: "自定义文本", custom: true, type: "text" },
        {
          tid: "panelsProvider1.longText",
          title: "长文本",
          type: "longText",
          options: {
            field: "test.longText",
            width: 200,
            testData: "长文本分页/不分页测试"
          }
        }
      ]),
      new hiprint.PrintElementTypeGroup("辅助", [
        {
          tid: "panelsProvider1.hline",
          title: "横线",
          type: "hline"
        },
        {
          tid: "panelsProvider1.vline",
          title: "竖线",
          type: "vline"
        },
        {
          tid: "panelsProvider1.rect",
          title: "矩形",
          type: "rect"
        },
        {
          tid: "panelsProvider1.oval",
          title: "椭圆",
          type: "oval"
        }
      ])
    ]);
  };
  return {
    addElementTypes: addElementTypes
  };
};

// 自定义设计元素2
export const panelsProvider2 = function () {
  var addElementTypes = function (context: any) {
    context.removePrintElementTypes("panelsProvider2");
    context.addPrintElementTypes("panelsProvider2", [
      new hiprint.PrintElementTypeGroup("常规", [
        {
          tid: "panelsProvider2.header",
          title: "单据表头",
          data: "单据表头",
          type: "text",
          options: {
            testData: "单据表头",
            height: 17,
            fontSize: 16.5,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        {
          tid: "panelsProvider2.type",
          title: "单据类型",
          data: "单据类型",
          type: "text",
          options: {
            testData: "单据类型",
            height: 16,
            fontSize: 15,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        {
          tid: "panelsProvider2.order",
          title: "订单编号",
          data: "XS888888888",
          type: "text",
          options: {
            field: "orderId",
            testData: "XS888888888",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider2.date",
          title: "业务日期",
          data: "2020-01-01",
          type: "text",
          options: {
            field: "date",
            testData: "2020-01-01",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider2.barcode",
          title: "条形码",
          data: "XS888888888",
          type: "text",
          options: {
            field: "barcode",
            testData: "XS888888888",
            height: 32,
            fontSize: 12,
            lineHeight: 18,
            textType: "barcode"
          }
        },
        {
          tid: "panelsProvider2.qrcode",
          title: "二维码",
          data: "XS888888888",
          type: "text",
          options: {
            field: "qrcode",
            testData: "XS888888888",
            height: 32,
            fontSize: 12,
            lineHeight: 18,
            textType: "qrcode"
          }
        },
        {
          tid: "panelsProvider2.platform",
          title: "平台名称",
          data: "平台名称",
          type: "text",
          options: {
            testData: "平台名称",
            height: 17,
            fontSize: 16.5,
            fontWeight: "700",
            textAlign: "center",
            hideTitle: true
          }
        },
        { tid: "panelsProvider2.image", title: "Logo", data: "", type: "image" }
      ]),
      new hiprint.PrintElementTypeGroup("客户", [
        {
          tid: "panelsProvider2.khname",
          title: "客户名称",
          data: "高级客户",
          type: "text",
          options: {
            field: "name",
            testData: "高级客户",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        },
        {
          tid: "panelsProvider2.tel",
          title: "客户电话",
          data: "18888888888",
          type: "text",
          options: {
            field: "tel",
            testData: "18888888888",
            height: 16,
            fontSize: 6.75,
            fontWeight: "700",
            textAlign: "left",
            textContentVerticalAlign: "middle"
          }
        }
      ]),
      new hiprint.PrintElementTypeGroup("表格/其他", [
        {
          tid: "panelsProvider2.table",
          title: "订单数据",
          type: "table",
          options: {
            field: "table",
            fields: [
              { text: "名称", field: "NAME" },
              { text: "数量", field: "SL" },
              { text: "规格", field: "GG" },
              { text: "条码", field: "TM" },
              { text: "单价", field: "DJ" },
              { text: "金额", field: "JE" },
              { text: "备注", field: "DETAIL" }
            ]
          },
          editable: true,
          columnDisplayEditable: true, //列显示是否能编辑
          columnDisplayIndexEditable: true, //列顺序显示是否能编辑
          columnTitleEditable: true, //列标题是否能编辑
          columnResizable: true, //列宽是否能调整
          columnAlignEditable: true, //列对齐是否调整
          columns: [
            [
              {
                title: "序",
                align: "center",
                field: "printid",
                width: 50,
                tableTextType: "sequence"
              },
              {
                field: "mtrlcuscode",
                align: "center",
                title: "销售订单相关号"
              },
              {
                field: "order_mtrlname_str",
                align: "center",
                title: "成品产品名称",
                tableSummary: "count"
              },
              {
                field: "saletask_mxid",
                align: "center",
                title: "销售订单明细序号"
              },
              {
                field: "mtrlname",
                align: "center",
                title: "物料名称"
              },
              {
                field: "mtrlmode",
                align: "center",
                title: "物料规格"
              },
              {
                field: "qty",
                align: "center",
                title: "数量",
                tableSummary: "sum"
              },
              {
                field: "unit",
                align: "center",
                title: "单位"
              },
              {
                field: "requiredate",
                align: "center",
                title: "要求交期"
              },
              {
                field: "mxdscrp",
                align: "center",
                title: "明细备注"
              }
            ]
          ],
          footerFormatter: function (options: any, rows: any, data: any, currentPageGridRowsData: any) {
            if (data && data["totalCap"]) {
              return `<td style="padding:0 10px" colspan="100">${"应收金额大写: " + data["totalCap"]}</td>`;
            }
            return '<td style="padding:0 10px" colspan="100">应收金额大写: </td>';
          }
        },
        { tid: "panelsProvider2.customText", title: "文本", customText: "自定义文本", custom: true, type: "text" },
        {
          tid: "panelsProvider2.longText",
          title: "长文本",
          type: "longText",
          options: {
            field: "test.longText",
            width: 200,
            testData: "长文本分页/不分页测试"
          }
        }
      ]),
      new hiprint.PrintElementTypeGroup("辅助", [
        {
          tid: "panelsProvider2.hline",
          title: "横线",
          type: "hline"
        },
        {
          tid: "panelsProvider2.vline",
          title: "竖线",
          type: "vline"
        },
        {
          tid: "panelsProvider2.rect",
          title: "矩形",
          type: "rect"
        },
        {
          tid: "panelsProvider2.oval",
          title: "椭圆",
          type: "oval"
        }
      ])
    ]);
  };
  return {
    addElementTypes: addElementTypes
  };
};

export const providers = [panelsProvider1(), panelsProvider2()];
export const providerList = [
  {
    name: "设计1",
    type: 1,
    value: "panelsProvider1"
  },
  {
    name: "设计2",
    type: 2,
    value: "panelsProvider2"
  },
  {
    name: "设计2",
    type: 2,
    value: "panelsProvider2"
  }
];

export const createProvider = (data: HiprintTemplateType.Providers, t: any) => {
  const addElementTypes = function (context: any) {
    const { key, options } = data;
    // 先清空, 避免重复添加. 如果有特殊需求, 可以不清空
    context.removePrintElementTypes(key);
    // 第 3 步: 创建元素数组
    let printElementGroup: any = [];
    options?.map((group: any) => {
      let printElements = group.printElements.map((item: any) => {
        // 提出来, 方便处理 二维码 条形码;
        // 如果和后端约定好,那么就更简单了,直接把后端返回的数据,直接赋值给 options
        let options = {
          title: item.title, // 在 options 中添加 title, 是可以清空的
          field: item.field,
          testData: item.testData,
          ...item.options // 可选参数之类的, 或者参数都在这里面
        };
        // 有些元素可以不设置宽高的,比如 table
        item.width && (options.width = item.width);
        item.height && (options.height = item.height);
        // 特殊处理 二维码 条形码
        if (["qrcode", "barcode"].includes(item.type)) {
          options.textType = item.type;
        }
        // 基础打印元素选项
        let printElement: any = {
          tid: `${key}.${item.tid}`, // 确保不重复就行
          title: item.title, // 这个 title 清空了,还是会有这个默认值
          type: item.type,
          options: options
        };
        // 特殊处理 表格 (表格参比较多咯~~~)
        if (["table", "tableCustom"].includes(item.type)) {
          // 根据实际情况 进行处理
          if (item.columns) {
            item.columns.forEach((col: any) => {
              if (col.table) {
                col.title = t(`table.${col.table}.${col.field}`);
              }
            });
            printElement.columns = item.columns;
          } else {
            printElement.columns = [
              [
                { align: "center", width: 100 },
                { align: "center", width: 100 }
              ]
            ];
          }
          return printElement;
        }
        return printElement;
      });
      // 第 2 步: 创建分组对象
      printElementGroup.push(new hiprint.PrintElementTypeGroup(group.groupName, printElements));
    });
    // 第 1 步: 添加到 key 数组中
    context.addPrintElementTypes(key, printElementGroup);
  };
  return {
    addElementTypes: addElementTypes
  };
};
