<template>
  <div class="mod-config">
    <!-- 条件查询 -->
    <el-card :class="['search-card', { 'collapsed': !searchExpanded }]" shadow="hover">
      <div slot="header" class="search-header">
        <div class="header-left">
          <i class="el-icon-search"></i>
          <span class="header-title">工单材料上机记录</span>
        </div>
        <div class="header-right">
          <el-button
            type="text"
            size="small"
            @click="toggleSearchExpand"
            class="collapse-btn">
            <i :class="searchExpanded ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
            {{ searchExpanded ? '收起' : '展开' }}
          </el-button>
        </div>
      </div>

      <el-form
        :inline="true"
        label-position="top"
        :model="searchData"
        class="search-form"
        @keyup.enter.native="getData">

        <!-- 所有查询条件 -->
        <template v-if="searchExpanded">
          <!-- 第一行 -->
          <el-row :gutter="16">
            <el-col :span="4">
              <el-form-item label="工厂编码">
                <el-select v-model="searchData.site" placeholder="请选择工厂" clearable filterable style="width: 100%">
                  <el-option v-for="item in siteList" :key="item" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="工单号">
                <el-input v-model="searchData.orderNo" placeholder="请输入工单号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="派工单号">
                <el-input v-model="searchData.seqNo" placeholder="请输入派工单号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="材料卷号">
                <el-input v-model="searchData.rmRollNo" placeholder="请输入材料卷号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="材料编码">
                <el-input v-model="searchData.partNo" placeholder="请输入材料编码" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="材料名称">
                <el-input v-model="searchData.partDesc" placeholder="请输入材料名称" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第二行 -->
          <el-row :gutter="16">
            <el-col :span="4">
              <el-form-item label="产品编码">
                <el-input v-model="searchData.productPartNo" placeholder="请输入产品编码" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="加工中心">
                <el-input v-model="searchData.workCenterNo" placeholder="请输入加工中心" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="机台">
                <el-input v-model="searchData.resourceId" placeholder="请输入机台" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="操作类型">
                <el-select v-model="searchData.histType" placeholder="请选择" clearable style="width: 100%">
                  <el-option label="全部" value=""></el-option>
                  <el-option label="发料" value="发料"></el-option>
                  <el-option label="换料" value="换料"></el-option>
                  <el-option label="退料" value="退料"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="是否结束">
                <el-select v-model="searchData.finishedFlag" placeholder="请选择" clearable style="width: 100%">
                  <el-option label="全部" value=""></el-option>
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="是否退料">
                <el-select v-model="searchData.unissueFlag" placeholder="请选择" clearable style="width: 100%">
                  <el-option label="全部" value=""></el-option>
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 第三行 -->
          <el-row :gutter="16">
            <el-col :span="4">
              <el-form-item label="是否关键料">
                <el-select v-model="searchData.keyRMFlag" placeholder="请选择" clearable style="width: 100%">
                  <el-option label="全部" value=""></el-option>
                  <el-option label="是" value="Y"></el-option>
                  <el-option label="否" value="N"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="上机时间">
                <el-date-picker
                  v-model="searchData.startDate1"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="开始日期"
                  style="width: 45%">
                </el-date-picker>
                <span style="margin: 0 6px; color: #DCDFE6;">~</span>
                <el-date-picker
                  v-model="searchData.startDate2"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="结束日期"
                  style="width: 45%">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </template>

        <!-- 操作按钮区域 -->
        <el-row :gutter="16">
          <el-col :span="24">
            <div class="search-actions">
              <div class="action-left">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  :loading="searchLoading"
                  @click="getData">
                  查询
                </el-button>
                <el-button
                  icon="el-icon-refresh-left"
                  @click="resetSearch">
                  重置
                </el-button>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 主信息列表 -->
    <div class="section-title-bar">
      <i class="el-icon-document"></i>
      <span>材料上机记录</span>
      <span class="total-count">（共 {{ totalCount }} 条）</span>
    </div>
    <el-table
      ref="mainTable"
      :height="tableHeight"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="60"
        label="序号">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="120"
        fixed="left"
        label="操作">
        <template slot-scope="scope">
          <el-link style="cursor: pointer; margin-right: 8px" @click="printMaterialLabel(scope.row)">打印</el-link>
          <el-link style="cursor: pointer" v-if="scope.row.finishedFlag == 'N' && scope.row.histType == '发料'" @click="showReturnMaterialDialog(scope.row)">异常退料</el-link>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in columnList"
        :key="index"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="true"
        :align="item.align"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span
            :class="{
              'status-yes': (item.columnProp === 'finishedFlagDesc' || item.columnProp === 'unissueFlagDesc' || item.columnProp === 'keyRMFlagDesc') && scope.row[item.columnProp] === '是',
              'status-no': (item.columnProp === 'finishedFlagDesc' || item.columnProp === 'unissueFlagDesc' || item.columnProp === 'keyRMFlagDesc') && scope.row[item.columnProp] === '否'
            }">
            {{ scope.row[item.columnProp] }}
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 0px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 异常退料对话框 -->
    <el-dialog
      title="异常退料"
      :visible.sync="returnMaterialVisible"
      width="600px"
      class="material-dialog"
      v-drag>
      <div class="material-content">
        <el-form :model="returnData" label-position="top" label-width="100px">
          <!-- 材料信息卡片 -->
          <div class="material-info-card">
            <div class="info-header">
              <i class="el-icon-document"></i>
              <span>材料信息</span>
            </div>
            <div class="info-content">
              <el-row :gutter="15">
                <el-col :span="12">
                  <div class="info-row">
                    <label class="info-label">工单号：</label>
                    <span class="info-value">{{ returnData.orderNo || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-row">
                    <label class="info-label">派工单号：</label>
                    <span class="info-value">{{ returnData.seqNo || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="15">
                <el-col :span="12">
                  <div class="info-row">
                    <label class="info-label">材料卷号：</label>
                    <span class="info-value">{{ returnData.rmRollNo || '-' }}</span>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-row">
                    <label class="info-label">材料编码：</label>
                    <span class="info-value">{{ returnData.partNo || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="15">
                <el-col :span="24">
                  <div class="info-row">
                    <label class="info-label">材料名称：</label>
                    <span class="info-value">{{ returnData.partDesc || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="15">
                <el-col :span="12">
                  <div class="info-row">
                    <label class="info-label">规格型号：</label>
                    <span class="info-value">{{ returnData.spec || '-' }}</span>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>

          <!-- 退料设置区域 -->
          <div class="return-settings">
            <el-row :gutter="15">
              <el-col :span="8">
                <el-form-item label="上机数量" class="form-item-enhanced">
                  <el-input
                    v-model="displayOnMachineQty"
                    disabled
                    size="large"
                    style="width: 100%">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="退料数量" class="form-item-enhanced">
                  <el-input class="inlineNumber numInput"
                            v-model="returnData.returnQty"
                            type="number"
                            placeholder="请输入退料数量"
                            size="large"
                            @input="calculateRemainQty"
                            style="width: 100%">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="剩余数量" class="form-item-enhanced">
                  <el-input
                    v-model="displayRemainQty"
                    disabled
                    size="large"
                    :class="{'remain-negative': returnData.remainQty < 0}"
                    style="width: 100%">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 备注 -->
            <el-row style="margin-bottom: 30px">
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input
                    v-model="returnData.remark"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入备注"
                    show-word-limit>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="returnLoading" @click="submitReturnMaterial">确定</el-button>
        <el-button @click="returnMaterialVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterialOnMachineRecord,
  materialReturnDuringProduction
} from '@/api/schedule/materialOnMachineRecord.js';
import {getSiteList } from "@/api/report/partTemplateStatus.js";
import { callUspPartLabelTemplate } from '@/api/wms/wms';
import getLodop from '@/utils/LodopFuncs.js';
import labelPrintTemplates from '@/mixins/labelPrintTemplates.js';

export default {
  mixins: [labelPrintTemplates],
  data() {
    return {
      searchExpanded: true,
      searchLoading: false,
      dataListLoading: false,
      searchData: {
        site: '',
        orderNo: '',
        seqNo: '',
        partNo: '',
        partDesc: '',
        rmRollNo: '',
        histType: '',
        finishedFlag: '',
        unissueFlag: '',
        keyRMFlag: '',
        productPartNo: '',
        workCenterNo: '',
        resourceId: '',
        startDate1: '',
        startDate2: ''
      },
      siteList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 50,
      totalCount: 0,
      tableHeight: 400,
      columnList: [
        { columnProp: 'orderNo', columnLabel: '工单号', columnWidth: '120', headerAlign: 'center', align: 'left' },
        { columnProp: 'seqNo', columnLabel: '派工单号', columnWidth: '150', headerAlign: 'center', align: 'left' },
        { columnProp: 'itemNo', columnLabel: '工序号', columnWidth: '70', headerAlign: 'center', align: 'center' },
        { columnProp: 'operationDesc', columnLabel: '工序描述', columnWidth: '100', headerAlign: 'center', align: 'left' },
        { columnProp: 'workCenterNo', columnLabel: '加工中心', columnWidth: '100', headerAlign: 'center', align: 'left' },
        { columnProp: 'resourceId', columnLabel: '机台', columnWidth: '100', headerAlign: 'center', align: 'left' },
        { columnProp: 'rmRollNo', columnLabel: '材料卷号', columnWidth: '150', headerAlign: 'center', align: 'left' },
        { columnProp: 'partNo', columnLabel: '材料编码', columnWidth: '120', headerAlign: 'center', align: 'left' },
        { columnProp: 'partDesc', columnLabel: '材料名称', columnWidth: '150', headerAlign: 'center', align: 'left' },
        { columnProp: 'spec', columnLabel: '规格型号', columnWidth: '120', headerAlign: 'center', align: 'left' },
        { columnProp: 'transQty', columnLabel: '上机数量', columnWidth: '100', headerAlign: 'center', align: 'right' },
        { columnProp: 'netIssueQty', columnLabel: '净发料数量', columnWidth: '100', headerAlign: 'center', align: 'right' },
        { columnProp: 'histType', columnLabel: '操作类型', columnWidth: '80', headerAlign: 'center', align: 'center' },
        { columnProp: 'startDate', columnLabel: '上机时间', columnWidth: '150', headerAlign: 'center', align: 'center' },
        { columnProp: 'finishedFlagDesc', columnLabel: '是否结束', columnWidth: '80', headerAlign: 'center', align: 'center' },
        { columnProp: 'finishedDate', columnLabel: '结束时间', columnWidth: '150', headerAlign: 'center', align: 'center' },
        { columnProp: 'unissueFlagDesc', columnLabel: '是否退料', columnWidth: '80', headerAlign: 'center', align: 'center' },
        { columnProp: 'keyRMFlagDesc', columnLabel: '是否关键料', columnWidth: '90', headerAlign: 'center', align: 'center' },
        { columnProp: 'reportedBy', columnLabel: '操作人', columnWidth: '100', headerAlign: 'center', align: 'left' },
        { columnProp: 'productPartNo', columnLabel: '产品编码', columnWidth: '120', headerAlign: 'center', align: 'left' },
        { columnProp: 'productPartDesc', columnLabel: '产品名称', columnWidth: '150', headerAlign: 'center', align: 'left' },
        { columnProp: 'orderStatus', columnLabel: '工单状态', columnWidth: '80', headerAlign: 'center', align: 'center' },
        { columnProp: 'customerId', columnLabel: '客户编码', columnWidth: '100', headerAlign: 'center', align: 'left' },
        { columnProp: 'customerName', columnLabel: '客户名称', columnWidth: '120', headerAlign: 'center', align: 'left' },
        { columnProp: 'remark', columnLabel: '备注', columnWidth: '150', headerAlign: 'center', align: 'left' }
      ],
      // 异常退料对话框
      returnMaterialVisible: false,
      returnLoading: false,
      returnData: {
        site: '',
        orderNo: '',
        itemNo: '',
        seqNo: '',
        rollNo: '',
        rmRollNo: '',
        partNo: '',
        partDesc: '',
        spec: '',
        histSeqNo: '',
        operatorId: '',
        onMachineQty: 0,
        returnQty: 0,
        remainQty: 0,
        remark: ''
      }
    }
  },
  computed: {
    displayOnMachineQty() {
      return this.returnData.onMachineQty || this.returnData.onMachineQty === 0 ? String(this.returnData.onMachineQty) : '';
    },
    displayRemainQty() {
      return this.returnData.remainQty || this.returnData.remainQty === 0 ? String(this.returnData.remainQty) : '';
    }
  },
  created() {
    this.setDefaultDates();
    this.getSiteList();
    this.calculateTableHeight();
    window.addEventListener('resize', this.calculateTableHeight);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateTableHeight);
  },
  methods: {
    // 设置默认日期（本月）
    setDefaultDates() {
      const now = new Date();
      const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
      const lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0);
      this.searchData.startDate1 = this.formatDate(firstDay);
      this.searchData.startDate2 = this.formatDate(lastDay);
    },
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    // 获取Site列表
    getSiteList() {
      getSiteList().then(({ data }) => {
        if (data && data.code === 0) {
          this.siteList = data.siteList || [];
          // 如果有权限的site，默认选择第一个
          if (this.siteList.length > 0) {
            this.searchData.site = this.siteList[0];
          }
        }
      }).catch(err => {
        console.error('获取Site列表失败:', err);
      });
    },
    // 计算表格高度
    calculateTableHeight() {
      this.$nextTick(() => {
        const windowHeight = window.innerHeight;
        const headerHeight = this.searchExpanded ? 360 : 186;
        this.tableHeight = windowHeight - headerHeight - 105;
      });
    },
    // 切换查询条件展开/收起
    toggleSearchExpand() {
      this.searchExpanded = !this.searchExpanded;
      this.calculateTableHeight();
    },
    // 查询数据
    getData() {
      this.searchLoading = true;
      this.dataListLoading = true;

      const params = {
        ...this.searchData,
        username: this.$store.state.user.name,
        page: this.pageIndex,
        limit: this.pageSize
      };

      listMaterialOnMachineRecord(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list || [];
          this.totalCount = data.page.totalCount || 0;
        } else {
          this.dataList = [];
          this.totalCount = 0;
          this.$message.error(data.msg || '查询失败');
        }
      }).catch(error => {
        this.dataList = [];
        this.totalCount = 0;
        this.$message.error('查询失败: ' + (error.message || '未知错误'));
      }).finally(() => {
        this.searchLoading = false;
        this.dataListLoading = false;
      });
    },
    // 重置查询条件
    resetSearch() {
      this.searchData = {
        site: this.siteList.length > 0 ? (this.$store.state.user.site || this.siteList[0]) : '',
        orderNo: '',
        seqNo: '',
        partNo: '',
        partDesc: '',
        rmRollNo: '',
        histType: '',
        finishedFlag: '',
        unissueFlag: '',
        keyRMFlag: '',
        productPartNo: '',
        workCenterNo: '',
        resourceId: '',
        startDate1: '',
        startDate2: ''
      };
      this.setDefaultDates();
      this.pageIndex = 1;
    },
    // 每页数变化
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getData();
    },
    // 当前页变化
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getData();
    },
    // 显示异常退料对话框
    showReturnMaterialDialog(row) {
      this.returnData = {
        site: row.site,
        orderNo: row.orderNo,
        itemNo: row.itemNo,
        seqNo: row.seqNo,
        rollNo: row.rmRollNo,
        rmRollNo: row.rmRollNo,
        partNo: row.partNo,
        partDesc: row.partDesc,
        spec: row.spec,
        histSeqNo: row.histSeqNo,
        operatorId: this.$store.state.user.name,
        onMachineQty: parseFloat(row.transQty) || 0,
        returnQty: 0,
        remainQty: parseFloat(row.transQty) || 0,
        remark: ''
      };
      this.returnMaterialVisible = true;
    },
    // 计算剩余数量
    calculateRemainQty() {
      const onMachineQty = parseFloat(this.returnData.onMachineQty) || 0;
      const returnQty = parseFloat(this.returnData.returnQty) || 0;
      this.returnData.remainQty = onMachineQty - returnQty;
    },
    // 提交异常退料
    submitReturnMaterial() {
      if (!this.returnData.returnQty || this.returnData.returnQty === '' || this.returnData.returnQty < 0) {
        this.returnData.returnQty = 0;
      }

      this.calculateRemainQty();

      if (this.returnData.remainQty < 0) {
        this.$message.warning('退料数量不能大于上机数量！');
        return;
      }

      this.returnLoading = true;

      const submitData = {
        site: this.returnData.site,
        orderNo: this.returnData.orderNo,
        itemNo: this.returnData.itemNo,
        seqNo: this.returnData.seqNo,
        rollNo: this.returnData.rmRollNo,
        returnQty: this.returnData.returnQty,
        histSeqNo: this.returnData.histSeqNo,
        operatorId: this.returnData.operatorId,
        remark: this.returnData.remark || ''
      };

      materialReturnDuringProduction(submitData).then(({ data }) => {
        if (data.code == 500 || data.code == 400) {
          this.$message.error(data.msg || data.message);
        } else if (data.code == 201) {
          this.$message.success(data.msg || '操作成功');
          // 处理打印
          if (data.printData) {
            this.executePrint([data.printData]);
          }
          this.returnMaterialVisible = false;
          this.getData();
        } else {
          this.$message.success(data.msg || data.message || '操作成功');
          this.returnMaterialVisible = false;
          this.getData();
        }
      }).catch(error => {
        this.$message.error('操作失败：' + (error.message || '未知错误'));
      }).finally(() => {
        this.returnLoading = false;
      });
    },
    // 打印材料清单中的卷标签
    async printMaterialLabel(row) {
      try {
        // 1. 获取 LODOP 打印控件
        const LODOP = getLodop();
        if (!LODOP) {
          this.$message.error('无法连接到打印控件，请确保已安装并启动 CLodop！');
          return;
        }

        // 2. 获取默认打印机
        const printerCount = LODOP.GET_PRINTER_COUNT();
        if (printerCount <= 0) {
          this.$message.error('未检测到打印机，请确保已安装并连接打印机！');
          return;
        }

        // 3. 调用存储过程获取打印参数
        const params = {
          site: row.site,
          buNo: '',
          menuID: '104003006',
          relatedOrderNo: row.orderNo,
          relatedOrderLineNo: row.seqNo,
          documentNo: '',
          partNo: row.partNo,
          labelNo: '',
          rollNo: row.rmRollNo
        };

        const {data: printData} = await callUspPartLabelTemplate(params);
        if (printData && printData.code === 0 && printData.row) {
          const labelData = printData.row;

          // 初始化打印任务
          LODOP.PRINT_INIT('材料卷标签打印_' + row.rmRollNo);
          LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", true);

          // 根据 labelNo 调用不同的打印方法
          if (labelData.labelNo === 'A001') {
            await this.printLabelA001(LODOP, labelData, false);
          } else if (labelData.labelNo === 'A002') {
            this.printLabelA002(LODOP, labelData, false);
          } else if (labelData.labelNo === 'A003') {
            this.printLabelA003(LODOP, labelData, false);
          } else if (labelData.labelNo === 'A004') {
            this.printLabelA004(LODOP, labelData, false);
          } else {
            this.$message.warning(`未知的标签模板：${labelData.labelNo}`);
            return;
          }

          // 执行打印
          LODOP.PRINT();
          this.$message.success('打印任务已发送！');
        } else {
          this.$message.error(printData.msg || '获取打印参数失败');
        }
      } catch (error) {
        console.error('打印材料标签失败:', error);
        this.$message.error('打印失败: ' + (error.message || '请重试'));
      }
    },
    // 执行打印
    executePrint(printDataList) {
      try {
        const LODOP = getLodop();
        if (!LODOP) {
          this.$message.warning('无法连接到打印控件，跳过打印');
          return;
        }

        const printerCount = LODOP.GET_PRINTER_COUNT();
        if (printerCount === 0) {
          this.$message.warning('未检测到打印机，跳过打印');
          return;
        }

        if (!printDataList || printDataList.length === 0) {
          return;
        }

        const labelNo = printDataList[0].labelNo;
        if (!labelNo) {
          return;
        }

        for (let i = 0; i < printDataList.length; i++) {
          const printData = printDataList[i];
          LODOP.PRINT_INIT('异常退料标签打印_' + (i + 1));
          LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", true);

          if (labelNo === 'A001') {
            this.printLabelA001(LODOP, printData, false);
          } else if (labelNo === 'A002') {
            this.printLabelA002(LODOP, printData, false);
          } else if (labelNo === 'A003') {
            this.printLabelA003(LODOP, printData, false);
          } else if (labelNo === 'A004') {
            this.printLabelA004(LODOP, printData, false);
          }

          LODOP.PRINT();
        }

        this.$message.success('标签打印任务已发送！');
      } catch (error) {
        console.error('打印失败:', error);
      }
    }
  }
}
</script>

<style scoped>
/* 搜索卡片样式 */
.search-card {
  margin-bottom: 16px;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.search-card:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.search-card /deep/ .el-card__header {
  padding: 5px 20px;
  background: linear-gradient(135deg, #9ac3d0 20%, #b6c7dd 80%);
  border-bottom: none;
}

.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  color: #fff;
}

.header-left i {
  font-size: 16px;
  margin-right: 8px;
}

.header-title {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-right {
  color: #fff;
}

.collapse-btn {
  color: #fff;
  font-weight: 500;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  color: #f0f0f0;
  transform: translateY(-1px);
}

.collapse-btn i {
  transition: transform 0.3s ease;
}

/* 搜索表单样式 */
.search-form {
  padding: 6px 0;
  min-height: 0;
}

/* 卡片主体样式 */
.search-card /deep/ .el-card__body {
  padding: 10px;
  transition: all 0.3s ease;
}

/* 收起时的样式 */
.search-card.collapsed /deep/ .el-card__body {
  padding: 10px 20px;
}

.search-form /deep/ .el-form-item {
  margin-bottom: 12px;
}

.search-form /deep/ .el-form-item__label {
  font-weight: 500;
  color: #606266;
  padding-bottom: 4px;
}

.search-form /deep/ .el-input__inner,
.search-form /deep/ .el-textarea__inner {
  border-radius: 6px;
  border: 1px solid #DCDFE6;
  transition: all 0.3s ease;
}

.search-form /deep/ .el-input__inner:focus,
.search-form /deep/ .el-textarea__inner:focus {
  border-color: #9ac3d0;
  box-shadow: 0 0 0 2px rgba(154, 195, 208, 0.1);
}

.search-form /deep/ .el-select {
  width: 100%;
}

/* 操作按钮区域 */
.search-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0 2px 0;
}

/* 展开时显示上边框 */
.search-card:not(.collapsed) .search-actions {
  border-top: 1px solid #f0f0f0;
  margin-top: 6px;
}

/* 收起时不显示上边框和上边距 */
.search-card.collapsed .search-actions {
  border-top: none;
  margin-top: 0;
  padding-top: 0;
}

.action-left {
  display: flex;
  gap: 8px;
  align-items: center;
}

.search-actions .el-button {
  border-radius: 4px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.search-actions .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.search-actions .el-button--primary {
  background: #60aeff;
  border-color: #60aeff;
}

.search-actions .el-button--primary:hover {
  background: #7dbdff;
  border-color: #7dbdff;
}

/* 区域标题栏样式 */
.section-title-bar {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
  border-radius: 4px;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #303133;
}

.section-title-bar i {
  font-size: 16px;
  margin-right: 8px;
  color: #409eff;
}

.section-title-bar .total-count {
  margin-left: 10px;
  font-size: 12px;
  font-weight: normal;
  color: #909399;
}

/* 状态样式 */
.status-yes {
  color: #67C23A;
  font-weight: 600;
}

.status-no {
  color: #F56C6C;
  font-weight: 600;
}

/* 表格样式 */
/deep/ .el-table th {
  background-color: #f5f7fa;
  color: #303133;
  font-weight: 600;
}

/deep/ .el-table tr:hover > td {
  background-color: #f5f7fa !important;
}

/* 异常退料对话框样式 */
.material-dialog /deep/ .el-dialog {
  border-radius: 8px;
  overflow: hidden;
}

.material-dialog /deep/ .el-dialog__header {
  background: linear-gradient(135deg, #9ac3d0 20%, #b6c7dd 80%);
  padding: 15px 20px;
}

.material-dialog /deep/ .el-dialog__title {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.material-dialog /deep/ .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
  font-size: 18px;
}

.material-dialog /deep/ .el-dialog__headerbtn .el-dialog__close:hover {
  color: #f0f0f0;
}

.material-dialog /deep/ .el-dialog__body {
  padding: 20px;
  background-color: #f8f9fa;
}

.material-dialog /deep/ .el-dialog__footer {
  padding: 12px 20px;
  background-color: #fff;
  border-top: 1px solid #e9ecef;
}

.material-info-card {
  background: linear-gradient(135deg, rgba(154, 195, 208, 0.15) 0%, rgba(182, 199, 221, 0.15) 100%);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  border: 1px solid #e3e8f0;
  box-shadow: 0 2px 8px rgba(154, 195, 208, 0.08);
}

.material-info-card .info-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid #dee2e6;
}

.material-info-card .info-header i {
  font-size: 18px;
  color: #9ac3d0;
  margin-right: 8px;
}

.material-info-card .info-header span {
  font-size: 14px;
  font-weight: 600;
  color: #495057;
}

.material-info-card .info-content {
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 4px;
  padding: 12px;
}

.material-info-card .info-row {
  display: flex;
  align-items: center;
  padding: 4px 0;
}

.material-info-card .info-label {
  font-size: 13px;
  color: #6c757d;
  min-width: 90px;
  font-weight: 500;
}

.material-info-card .info-value {
  font-size: 13px;
  color: #212529;
  font-weight: 600;
  flex: 1;
}

.return-settings {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #e3e8f0;
  box-shadow: 0 2px 8px rgba(154, 195, 208, 0.05);
}

.form-item-enhanced {
  margin-bottom: 10px;
}

.form-item-enhanced /deep/ .el-form-item__label {
  color: #495057;
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 8px;
}

.form-item-enhanced /deep/ .el-input .el-input__inner {
  border-radius: 6px;
  border: 1px solid #dee2e6;
  height: 42px;
  line-height: 42px;
  font-size: 15px;
}

.form-item-enhanced /deep/ .el-input .el-input__inner:focus {
  border-color: #9ac3d0;
  box-shadow: 0 0 0 3px rgba(154, 195, 208, 0.1);
}

.form-item-enhanced /deep/ .remain-negative .el-input__inner {
  color: #f56c6c;
  font-weight: 600;
  border-color: #f56c6c;
  background-color: #fef0f0;
}

.numInput /deep/ .el-input__inner {
  text-align: right;
}

/deep/ .inlineNumber input::-webkit-outer-spin-button,
/deep/ .inlineNumber input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

/deep/ .inlineNumber input[type="number"] {
  -moz-appearance: textfield;
  padding-right: 5px !important;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .search-actions {
    flex-direction: column;
    gap: 10px;
  }

  .action-left {
    width: 100%;
    justify-content: center;
  }
}
</style>
