<template>
  <el-card class="box-card">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      :table-events="{
        select: selectRow,
        'select-all': selectRow,
      }"
      :action-list="[
        {
          text: '打印生产面单',
          action: () => clickSelectPrint(),
        },
        {
          text: '合并工单',
          disabled: selectData.length < 2,
          action: () => clickMerge(),
        },
      ]"
      @get-list="getList"
      @clickReset="clickReset">
      <template #code="{ item }">
        <div>
          <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="clickCode(item)">{{ item.code }}</a>
        </div>
      </template>
      <template #address="{ item }">
        <div>{{ item.province }}, {{ item.city }}, {{ item.county }}, {{ item.detailedAddress }}</div>
      </template>
    </byTable>
    <div style="display: none">
      <div style="width: 250px" id="printMe">
        <div v-for="(item, index) in QRcodeList" :key="index">
          <div style="height: 442px; padding-top: 2px; overflow: hidden; position: relative">
            <div style="border-bottom: 1px solid #000; display: flex; align-items: center; justify-content: center">
              <CycleBarcode :value="item.orderWlnCode || item.orderCode" :index="index" style="max-width: 100%"></CycleBarcode>
            </div>
            <div style="display: flex; align-items: center; justify-content: center">
              <div style="width: 150px; height: 150px; margin-top: 8px" :id="'print' + index" :ref="'print' + index">
                <img src="" alt="" style="vertical-align: middle; height: 100%; width: 100%" />
              </div>
            </div>
            <div style="text-align: center; font-size: 18px; font-weight: 700; padding: 4px 0; border-bottom: 1px solid #000">{{ item.completeTime }}</div>
            <div style="word-break: break-all; padding: 2px">SKU品号：{{ item.skuSpecCode }}</div>
            <div style="word-break: break-all; padding: 2px">SKU品名：{{ item.skuSpecName }}</div>
            <div style="word-break: break-all; padding: 2px">BOM品号：{{ item.bomSpecCode }}</div>
            <div style="word-break: break-all; padding: 2px">BOM品名：{{ item.bomSpecName }}</div>
            <div v-if="item.orderWlnCode" class="one">
              <div class="two">
                <div class="three">万里牛</div>
              </div>
            </div>
          </div>
          <div style="page-break-after: always"></div>
        </div>
      </div>
      <el-button type="primary" style="display: none" v-print="printObj" id="printBtnMini"></el-button>
    </div>
  </el-card>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import QRCode from "qrcodejs2-fix";
import { ElMessage, ElMessageBox } from "element-plus";
import CycleBarcode from "/src/components/CycleBarcode";

const { proxy } = getCurrentInstance();
const statusList = ref([
  {
    dictKey: 0,
    dictValue: "待投产",
  },
  {
    dictKey: 1,
    dictValue: "已扫描",
  },
  {
    dictKey: 2,
    dictValue: "生产中",
  },
  {
    dictKey: 3,
    dictValue: "生产完成",
  },
  {
    dictKey: 4,
    dictValue: "生产异常",
  },
  {
    dictKey: 5,
    dictValue: "重新排单",
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    orderCode: "",
    orderWlnCode: "",
    skuSpecCode: "",
    skuSpecName: "",
    bomSpecCode: "",
    bomSpecName: "",
    productionWorkOrderCode: "",
    status: [0, 4],
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "orderCode",
      label: "订单号",
    },
    {
      type: "input",
      prop: "orderWlnCode",
      label: "E10单号",
    },
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "BOM品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "BOM品名",
    },
    {
      type: "input",
      prop: "productionWorkOrderCode",
      label: "工单号",
    },
    {
      type: "select",
      prop: "status",
      label: "生产状态",
      data: statusList.value,
      multiple: true,
    },
  ];
});
const config = computed(() => {
  return [
    {
      type: "selection",
      attrs: {
        checkAtt: "isCheck",
      },
    },
    {
      attrs: {
        label: "订单号",
        prop: "orderCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "工单号",
        prop: "code",
        width: 190,
      },
    },
    {
      attrs: {
        label: "E10单号",
        prop: "orderWlnCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "SKU品名",
        prop: "skuSpecName",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "BOM品号",
        prop: "bomSpecCode",
        width: 130,
      },
    },
    {
      attrs: {
        label: "BOM品名",
        prop: "bomSpecName",
        "min-width": 280,
      },
    },
    {
      attrs: {
        label: "生产状态",
        prop: "status",
        width: 80,
      },
      render(val) {
        return proxy.dictKeyValue(val, statusList.value);
      },
    },
    {
      attrs: {
        label: "完成时间",
        prop: "completeTime",
        width: 160,
        align: "center",
        fixed: "right",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 120,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "打印生产面单",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickPrint([row]);
            },
          },
        ];
      },
    },
  ];
});
const selectData = ref([]);
const selectRow = (data) => {
  selectData.value = data;
};
const getList = async (req, status) => {
  selectData.value = [];
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
      status: [0, 4],
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/productionWorkOrder/page", sourceList.value.pagination).then((res) => {
    if (res.rows && res.rows.length > 0) {
      sourceList.value.data = res.rows.map((item) => {
        if (item.status == 0 && !item.type) {
          return {
            ...item,
            isCheck: true,
          };
        } else {
          return {
            ...item,
            isCheck: false,
          };
        }
      });
    } else {
      sourceList.value.data = [];
    }
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const clickCode = (row) => {
  proxy.$router.replace({
    path: "/addOrder",
    query: {
      detailId: row.id,
      text: "订单详情",
      random: proxy.random(),
      orderInquiry: true,
    },
  });
};
const printObj = ref({
  id: "printMe",
  popTitle: "",
  extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
});
const QRcodeList = ref([]);
const clickPrint = (list) => {
  QRcodeList.value = list;
  nextTick(() => {
    for (let i = 0; i < QRcodeList.value.length; i++) {
      proxy.$refs["print" + i][0].innerHTML = ""; //清除二维码方法一
      let text = QRcodeList.value[i].id;
      new QRCode(proxy.$refs["print" + i][0], {
        text: text, //页面地址 ,如果页面需要参数传递请注意哈希模式#
        width: 150,
        height: 150,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
    }
    nextTick(() => {
      const btn = document.getElementById("printBtnMini");
      btn.click();
    });
  });
};
const clickSelectPrint = () => {
  if (selectData.value && selectData.value.length > 0) {
    clickPrint(selectData.value);
  } else {
    return ElMessage("请选择需要打印的工单");
  }
};
const clickMerge = () => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy
        .post(
          "/productionWorkOrder/combined",
          selectData.value.map((item) => item.id)
        )
        .then(() => {
          ElMessage({ message: "操作成功", type: "success" });
          getList();
        });
    })
    .catch(() => {});
};
watch(selectData, (newVal) => {
  if (newVal && newVal.length > 0) {
    sourceList.value.data.forEach((x) => {
      if (x.status == 0 && !x.type) {
        if (x.bomSpecCode === newVal[0].bomSpecCode && x.skuSpecCode === newVal[0].skuSpecCode) {
          x.isCheck = true;
        } else {
          x.isCheck = false;
        }
      } else {
        x.isCheck = false;
      }
    });
  } else {
    sourceList.value.data.forEach((x) => {
      if (x.status == 0 && !x.type) {
        x.isCheck = true;
      } else {
        x.isCheck = false;
      }
    });
  }
});
</script>

<style lang="scss" scoped>
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
.one {
  position: absolute;
  right: 10px;
  top: 320px;
  width: 70px;
  height: 70px;
  z-index: 99;
  line-height: 80px;
  text-align: center;
  border-radius: 50%;
  border: 1px solid #000;
  .two {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 60px;
    height: 60px;
    line-height: 60px;
    text-align: center;
    border-radius: 50%;
    border: 1px dashed #000;
    .three {
      rotate: -45deg;
      font-size: 14px;
    }
  }
}
:deep(.el-table__header-wrapper .el-checkbox) {
  display: none;
}
</style>
