<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="入场钢卷号" prop="enterCoilNo">
        <el-input v-model="queryParams.enterCoilNo" placeholder="请输入入场钢卷号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="当前钢卷号" prop="currentCoilNo">
        <el-input v-model="queryParams.currentCoilNo" placeholder="请输入当前钢卷号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="数据状态">
        <el-select v-model="queryParams.dataType" placeholder="请选择数据状态" clearable>
          <el-option :value="0" label="历史数据">历史数据</el-option>
          <el-option :value="1" label="当前数据">当前数据</el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="逻辑库位" prop="warehouseId" v-if="!hideWarehouseQuery">
        <warehouse-select v-model="queryParams.warehouseId" placeholder="请选择仓库/库区/库位"
          style="width: 100%; display: inline-block;" clearable />
      </el-form-item>

      <el-form-item label="实际库区" prop="actualWarehouseId" v-if="!hideWarehouseQuery">
        <actual-warehouse-select v-model="queryParams.actualWarehouseId" placeholder="请选择仓库/库区/库位"
          style="width: 100%; display: inline-block;" clearable :canSelectDisabled="true" :canSelectLevel2="true"
          :clearInput="false" :showEmpty="true" />
      </el-form-item>

      <el-form-item label="产品名称" prop="itemName">
        <muti-select v-model="queryParams.itemName" :options="dict.type.coil_itemname" placeholder="请选择物料" clearable />
      </el-form-item>

      <el-form-item label="规格" prop="itemSpecification">
        <memo-input v-model="queryParams.itemSpecification" storageKey="coilSpec" placeholder="请选择规格" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="材质" prop="itemMaterial">
        <muti-select v-model="queryParams.itemMaterial" :options="dict.type.coil_material" placeholder="请选择材质" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="厂家" prop="itemManufacturer">
        <muti-select v-model="queryParams.itemManufacturer" :options="dict.type.coil_manufacturer" placeholder="请选择厂家"
          clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <MaterialSelect :hideType="hideType" :itemId.sync="queryParams.itemIds" :itemType.sync="queryParams.itemType"
        :multiple="true" />

      <el-form-item v-if="editNext" label="显示流程图" prop="showProcessFlow">
        <el-checkbox v-model="showProcessFlow" @change="handleShowProcessFlowChange"></el-checkbox>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-download" size="mini" @click="handleNewExport" v-if="showNewExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="showControl">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleCheck">修正</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExportAll">导出</el-button>
      </el-col>
      <el-col :span="2" v-if="canExportAll">
        <el-button type="info" plain icon="el-icon-printer" size="mini" :disabled="multiple"
          @click="handleBatchPrintLabel">批量打印标签</el-button>
      </el-col>
      <el-col :span="1.5" v-if="showOrderBy">
        <el-checkbox v-model="queryParams.orderBy" v-loading="loading" @change="getList"
          label="orderBy">按实际库区排序</el-checkbox>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <KLPTable v-loading="loading" :data="materialCoilList" @selection-change="handleSelectionChange" :floatLayer="true"
      :floatLayerConfig="floatLayerConfig" @row-click="handleRowClick"
      :height="showAbnormal ? 'calc(100vh - 400px)' : ''">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入场钢卷号" align="center" prop="enterCoilNo">
        <template slot-scope="scope">
          <coil-no :coil-no="scope.row.enterCoilNo"></coil-no>
        </template>
      </el-table-column>
      <el-table-column label="当前钢卷号" align="center" prop="currentCoilNo">
        <template slot-scope="scope">
          <current-coil-no :current-coil-no="scope.row.currentCoilNo"></current-coil-no>
        </template>
      </el-table-column>
      <!-- <el-table-column label="厂家卷号" align="center" prop="supplierCoilNo" /> -->
      <el-table-column label="逻辑库位" align="center" prop="warehouseName" v-if="!hideWarehouseQuery" />
      <el-table-column label="实际库区" align="center" prop="actualWarehouseName"
        v-if="!hideWarehouseQuery && !showExportTime" />
      <!-- <el-table-column label="物料类型" align="center" prop="materialType" /> -->
      <el-table-column label="产品类型" align="center" width="180">
        <template slot-scope="scope">
          <ProductInfo v-if="scope.row.itemType == 'product'" :product="scope.row" />
          <RawMaterialInfo v-else-if="scope.row.itemType === 'raw_material'" :material="scope.row" />
        </template>
      </el-table-column>
      <el-table-column v-if="showAbnormal" label="异常数量" align="center" prop="abnormalCount"></el-table-column>
      <el-table-column label="长度 (米)" align="center" prop="length" v-if="showLength" />
      <el-table-column label="更新时间" v-if="!showExportTime" align="center" prop="updateTime" />
      <el-table-column label="发货时间" v-if="showExportTime" align="center" prop="exportTime" width="205">
        <template slot-scope="scope">
          <el-date-picker @change="handleExportTimeChange(scope.row)" v-if="canEditExportTime" style="width: 100%"
            v-model="scope.row.exportTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择发货时间" />
          <div v-else>{{ scope.row.exportTime }}</div>
        </template>
      </el-table-column>
      <el-table-column label="发货人" v-if="showExportTime" align="center" prop="exportByName" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.exportBy" placeholder="请选择发货人" filterable
            @change="handleExportByNameChange(scope.row)">
            <el-option v-for="item in userList" :key="item.userName" :value="item.userName" :label="item.nickName" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="更新人" v-if="!showExportTime" align="center" prop="updateByName" />
      <!-- <el-table-column label="二维码">
        <template slot-scope="scope">
          <QRCode :content="scope.row.qrcodeRecordId" :size="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="关联信息" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentCoilNos && scope.row.hasMergeSplit === 1 && scope.row.dataType === 1">
            <el-tag type="warning" size="mini">来自母卷：{{ scope.row.parentCoilNos }}</el-tag>
          </span>
          <span v-else-if="scope.row.parentCoilNos && scope.row.dataType === 0">
            <el-tag type="info" size="mini">分为子卷：{{ scope.row.parentCoilNos }}</el-tag>
          </span>
          <span v-else-if="scope.row.parentCoilNos && scope.row.hasMergeSplit === 2">
            <el-tag type="success" size="mini">合并自：{{ scope.row.parentCoilNos }}</el-tag>
          </span>
          <span v-else>—</span>
        </template>
      </el-table-column>

      <el-table-column v-if="showGrade" label="质量状态" align="center" prop="qualityStatus">
        <template slot-scope="scope">
          <el-select v-model="scope.row.qualityStatus" placeholder="请选择质量状态" @change="handleGradeChange(scope.row)">
            <el-option v-for="item in dict.type.coil_quality_status" :key="item.value" :value="item.value"
              :label="item.label" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="逻辑库位" align="center" prop="warehouseId" v-if="editWarehouse">
        <template slot-scope="scope">
          <warehouse-select @change="handleWarehouseChange(scope.row)" v-model="scope.row.warehouseId"
            placeholder="请选择仓库/库区/库位" style="width: 100%;" clearable />
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="钢卷去向" align="center" prop="nextWarehouseId" v-if="editNext" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.nextWarehouseId" placeholder="钢卷去向" filterable
            @change="handleNextWarehouseChange(scope.row)">
            <el-option v-for="item in dict.type.wms_next_warehouse" :key="item.value" :value="item.value"
              :label="item.label" />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="业务目的" align="center" prop="businessPurpose" v-if="showBusinessPurpose" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.businessPurpose" placeholder="业务目的" filterable
            @change="handleRowChange(scope.row)">
            <el-option v-for="item in dict.type.coil_business_purpose" :key="item.value" :value="item.value"
              :label="item.label" />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="关联订单" align="center" prop="relatedToOrder" v-if="showRelatedToOrder" width="150">
        <template slot-scope="scope">
          <el-switch @change="handleRowChange(scope.row)" v-model="scope.row.isRelatedToOrder" :active-value="1"
            :inactive-value="0" />
        </template>
      </el-table-column>

      <el-table-column label="发货计划" align="center" prop="nextWarehouseId" v-if="showWaybill" width="150">
        <template slot-scope="scope">
          {{ scope.row.bindPlanName || '-' }}
        </template>
      </el-table-column>

      <el-table-column label="发货单据" align="center" prop="nextWarehouseId" v-if="showWaybill" width="150">
        <template slot-scope="scope">
          <el-popover placement="top" width="400" trigger="hover">
            <div>
              <el-descriptions :column="2" :border="false">
                <el-descriptions-item label="单据名称">
                  {{ scope.row.bindWaybillName || '-' }}
                </el-descriptions-item>
                <el-descriptions-item label="车牌号">
                  {{ scope.row.bindLicensePlate || '-' }}
                </el-descriptions-item>
                <el-descriptions-item label="收货单位">
                  {{ scope.row.bindConsigneeUnit || '-' }}
                </el-descriptions-item>
                <el-descriptions-item label="负责人">
                  {{ scope.row.bindPrincipal || '-' }}
                </el-descriptions-item>
                <el-descriptions-item label="单据状态">
                  <el-tag v-if="scope.row.bindWaybillStatus === 0" type="info" size="mini">未发货</el-tag>
                  <el-tag v-else-if="scope.row.bindWaybillStatus === 1" type="info" size="mini">已发货</el-tag>
                  <el-tag v-else-if="scope.row.bindWaybillStatus === 2" type="info" size="mini">未打印</el-tag>
                  <el-tag v-else-if="scope.row.bindWaybillStatus === 3" type="info" size="mini">已打印</el-tag>
                  <el-tag v-else type="danger" size="mini">未知状态</el-tag>
                </el-descriptions-item>
              </el-descriptions>
            </div>
            <div slot="reference" class="text-ellipsis" v-text>{{ scope.row.bindLicensePlate || '-' }}</div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="发货状态" align="center" prop="status" v-if="showWaybill" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success" size="mini">已发货</el-tag>
          <el-tag v-else type="info" size="mini">未发货</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="实测宽度" align="center" prop="width" v-if="showWidthEdit" width="150">
        <template slot-scope="scope">
          <el-input v-model="scope.row.actualWidth" placeholder="请输入实测宽度"
            @change="handleRowChange(scope.row)"></el-input>
        </template>
      </el-table-column>

      <el-table-column label="实测厚度(m)" align="center" prop="actualThickness" v-if="showWidthEdit" width="150">
        <template slot-scope="scope">
          <el-input v-model="scope.row.actualThickness" placeholder="请输入实测厚度"
            @change="handleRowChange(scope.row)"></el-input>
        </template>
      </el-table-column>

      <el-table-column label="预留宽度" align="center" prop="width" v-if="showWidthEdit" width="150">
        <template slot-scope="scope">
          <el-input v-model="scope.row.reservedWidth" placeholder="请输入预留宽度"
            @change="handleRowChange(scope.row)"></el-input>
        </template>
      </el-table-column>

      <el-table-column label="生产开始" align="center" prop="productionStartTime" v-if="showProductionTimeEdit" width="150">
      </el-table-column>

      <el-table-column label="生产结束" align="center" prop="productionEndTime" v-if="showProductionTimeEdit" width="150">
      </el-table-column>

      <el-table-column label="生产耗时" align="center" prop="productionDuration" v-if="showProductionTimeEdit" width="150">
        <template slot-scope="scope">
          {{ formatDuration(scope.row.productionDuration * 60 * 1000) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handlePreviewLabel(scope.row)">
            预览标签
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-printer" @click="handlePrintLabel(scope.row)">
            打印标签
          </el-button>
          <el-button size="mini" v-if="showStatus" type="text" icon="el-icon-upload"
            @click="handleExportCoil(scope.row)">
            发货
          </el-button>
          <el-button size="mini" v-if="showExportTime" type="text" icon="el-icon-close"
            @click="handleCancelExport(scope.row)">
            撤回发货
          </el-button>
          <el-button size="mini" v-if="showProductionTimeEdit" type="text" icon="el-icon-close"
            @click="handleProductionTimeEdit(scope.row)">
            加工修正
          </el-button>
          <el-button size="mini" v-if="showExportTime" type="text" icon="el-icon-sold-out"
            @click="handleReturnCoil(scope.row)">
            退货钢卷
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCheck(scope.row)"
            v-if="showControl">修正</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleTrace(scope.row)">追溯</el-button>
        </template>
      </el-table-column>
    </KLPTable>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改钢卷物料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="入场钢卷号" prop="enterCoilNo">
          <el-input v-model="form.enterCoilNo" placeholder="请输入入场钢卷号" :disabled="form.coilId" />
        </el-form-item>
        <el-form-item label="当前钢卷号" prop="currentCoilNo">
          <el-input v-model="form.currentCoilNo" placeholder="请输入当前钢卷号" />
        </el-form-item>
        <el-form-item label="厂家原料卷号" prop="supplierCoilNo">
          <el-input v-model="form.supplierCoilNo" placeholder="请输入厂家原料卷号" />
        </el-form-item>
        <el-form-item label="所在库位" prop="warehouseId">
          <warehouse-select v-model="form.warehouseId" placeholder="请选择仓库/库区/库位" style="width: 100%;" clearable />
        </el-form-item>
        <el-form-item label="实际库区" prop="actualWarehouseId">
          <actual-warehouse-select v-model="form.actualWarehouseId" :clearInput="form.coilId != null"
            placeholder="请选择实际库区" style="width: 100%;" clearable />
        </el-form-item>
        <el-form-item label="班组" prop="team">
          <el-select v-model="form.team" placeholder="请选择班组" style="width: 100%">
            <el-option key="甲" label="甲" value="甲" />
            <el-option key="乙" label="乙" value="乙" />
          </el-select>
        </el-form-item>
        <el-form-item label="材料类型" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请选择材料类型" @change="handleMaterialTypeChange">
            <el-option label="成品" value="成品" />
            <el-option label="原料" value="原料" />
          </el-select>
        </el-form-item>
        <el-form-item :label="getItemLabel" prop="itemId">
          <product-select v-if="form.itemType == 'product'" v-model="form.itemId" placeholder="请选择成品"
            style="width: 100%;" clearable />
          <raw-material-select v-else-if="form.itemType == 'raw_material'" v-model="form.itemId" placeholder="请选择原料"
            style="width: 100%;" clearable />
          <div v-else>请先选择材料类型</div>
        </el-form-item>
        <!-- <el-form-item label="质量状态" prop="qualityStatus">
          <el-select v-model="form.qualityStatus" placeholder="请选择质量状态" style="width: 100%">
            <el-option v-for="item in dict.type.coil_quality_status" :key="item.value" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item> -->

        <el-form-item label="切边要求" prop="trimmingRequirement">
          <el-select v-model="form.trimmingRequirement" placeholder="请选择切边要求" style="width: 100%">
            <el-option label="净边料" value="净边料" />
            <el-option label="毛边料" value="毛边料" />
          </el-select>
        </el-form-item>

        <el-form-item label="原料材质" prop="packingStatus">
          <el-input v-model="form.packingStatus" placeholder="请输入原料材质">
          </el-input>
        </el-form-item>

        <el-form-item label="包装要求" prop="packagingRequirement">
          <el-select v-model="form.packagingRequirement" placeholder="请选择包装要求" style="width: 100%">
            <el-option label="裸包" value="裸包" />
            <el-option label="普包" value="普包" />
            <el-option label="简包" value="简包" />
          </el-select>
        </el-form-item>
        <el-form-item label="毛重" prop="grossWeight">
          <el-input v-model="form.grossWeight" placeholder="请输入毛重" />
        </el-form-item>
        <el-form-item label="净重" prop="netWeight">
          <el-input v-model="form.netWeight" placeholder="请输入净重" />
        </el-form-item>
        <el-form-item label="长度" prop="length" v-if="showLength">
          <el-input v-model="form.length" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="实测长度(m)" prop="actualLength">
          <el-input-number :controls="false" v-model="form.actualLength" placeholder="请输入实测长度" type="number"
            :step="0.01" />
        </el-form-item>
        <el-form-item label="实测厚度(m)" prop="actualThickness" class="form-item-half">
          <el-input-number :controls="false" v-model="form.actualThickness" placeholder="请输入实测厚度" type="number"
            :step="0.01" />
        </el-form-item>
        <el-form-item label="实测宽度(m)" prop="actualWidth">
          <el-input-number :controls="false" v-model="form.actualWidth" placeholder="请输入实测宽度" type="number"
            :step="0.01" />
        </el-form-item>
        <el-form-item label="调制度" prop="temperGrade">
          <el-input v-model="form.temperGrade" placeholder="请输入调制度" />
        </el-form-item>
        <el-form-item label="镀层种类" prop="coatingType">
          <MemoInput storageKey="coatingType" v-model="form.coatingType" placeholder="请输入镀层种类" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 钢卷追溯对话框（使用封装的组件） -->
    <el-dialog title="钢卷追溯" :visible.sync="traceOpen" width="90%" append-to-body>
      <coil-trace-result v-loading="traceLoading" :trace-result="traceResult"></coil-trace-result>
    </el-dialog>

    <!-- 标签预览弹窗 -->
    <el-dialog title="标签预览" :visible.sync="labelRender.visible" append-to-body>
      <label-render :forceSpecialTag="forceSpecialTag" :content="labelRender.data" :labelType="labelRender.type" />
    </el-dialog>

    <label-render :forceSpecialTag="forceSpecialTag" ref="labelRender" v-show="false" :content="labelRender.data"
      :labelType="labelRender.type" />

    <!-- 批量导出标签PDF弹窗 -->
    <el-dialog title="批量导出标签PDF" :visible.sync="batchPrint.visible" width="520px" append-to-body>
      <div style="line-height: 22px; font-size: 12px; color: #909399; margin-bottom: 10px;">
        已选择 <b>{{ batchPrint.list.length }}</b> 个钢卷。点击“生成PDF并打开”将每个标签作为一页（180mm × 100mm）。
      </div>
      <div style="text-align:right;">
        <el-button size="mini" @click="batchPrint.visible = false">取消</el-button>
        <el-button type="primary" size="mini" :loading="batchPrint.loading"
          @click="handleBatchExportLabelPdf">生成PDF并打开</el-button>
      </div>

      <!-- 渲染容器：屏幕隐藏，仅用于截图生成PDF -->
      <div ref="batchPdfContainer" class="batch-pdf-root" aria-hidden="true">
        <div v-for="(item, idx) in batchPrint.list" :key="item.coilId || idx" class="batch-pdf-page">
          <label-render :content="item" :hideActions="true" :forceSpecialTag="forceSpecialTag" />
        </div>
      </div>
    </el-dialog>

    <el-dialog title="生产时间修正" :visible.sync="productionTimeFormVisible" width="500px" append-to-body>
      <el-form ref="productionTimeForm" :model="productionTimeForm" :rules="productionTimeFormRules"
        label-width="100px">
        <el-form-item label="生产开始时间" prop="productionStartTime">
          <el-date-picker v-model="productionTimeForm.productionStartTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择生产时间"
            @change="(value) => { productionTimeForm.productionStartTime = value; calculateProductionDuration(); }" />
        </el-form-item>
        <el-form-item label="生产结束时间" prop="productionEndTime">
          <el-date-picker v-model="productionTimeForm.productionEndTime" type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择生产时间"
            @change="(value) => { productionTimeForm.productionEndTime = value; calculateProductionDuration(); }" />
        </el-form-item>
        <el-form-item label="生产耗时" prop="productionDuration">
          <!-- <div>{{ productionTimeForm.formattedDuration }}</div> -->
          <el-input v-model="productionTimeForm.formattedDuration" placeholder="自动计算" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitProductionTimeForm">确 定</el-button>
        <el-button @click="productionTimeFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <abnormal-list v-if="showAbnormal && currentCoilId" :coil-id="currentCoilId"></abnormal-list>

    <log-table v-if="showWareLog && currentCoilId" :coil-id="currentCoilId"></log-table>

    <DragResizeBox v-if="editNext && showProcessFlow" storageKey="coil-process-flow" @size-change="resizeChart">
      <div style="height: 100%; width: 100%; overflow-y: scroll; display: flex; background-color: #fff;">
        <process-flow v-if="showProcessFlow" ref="processFlow"></process-flow>
      </div>
    </DragResizeBox>
  </div>
</template>

<script>
import {
  listMaterialCoil,
  getMaterialCoil,
  delMaterialCoil,
  addMaterialCoil,
  updateMaterialCoilSimple,
  getMaterialCoilTrace,
  exportCoil,
  cancelExportCoil,
  checkCoilNo,
  returnCoil,
} from "@/api/wms/coil";
import { listBoundCoil } from "@/api/wms/deliveryWaybillDetail";
import { addPendingAction } from "@/api/wms/pendingAction";
import WarehouseSelect from "@/components/KLPService/WarehouseSelect";
import QRCode from "../../print/components/QRCode.vue";
import * as XLSX from 'xlsx'
import { saveAsImage } from '@/utils/klp';
import ProductSelect from "@/components/KLPService/ProductSelect";
import RawMaterialSelect from "@/components/KLPService/RawMaterialSelect";
// import MaterialSelect from "@/components/KLPService/ProductSelect";
import ProductInfo from "@/components/KLPService/Renderer/ProductInfo";
import RawMaterialInfo from "@/components/KLPService/Renderer/RawMaterialInfo";
// 引入封装的追溯结果组件
import CoilTraceResult from "./CoilTraceResult.vue";
import LabelRender from './LabelRender/index.vue'
import OuterTagPreview from './LabelRender/OuterTagPreview.vue'
import MaterialSelect from "@/components/KLPService/MaterialSelect";
import ActualWarehouseSelect from "@/components/KLPService/ActualWarehouseSelect";
import { findItemWithBom } from "@/store/modules/category";
import CoilNo from "@/components/KLPService/Renderer/CoilNo.vue";
import MemoInput from "@/components/MemoInput";
import MutiSelect from "@/components/MutiSelect";
import html2canvas from 'html2canvas';
import { PDFDocument } from 'pdf-lib';
import { listUser } from "@/api/system/user";
import AbnormalList from "./abnormal.vue";
import LogTable from "@/views/wms/warehouse/components/LogTable.vue";
import { getCoilTagPrintType } from '@/views/wms/coil/js/coilPrint';
import DragResizeBox from '@/components/DragResizeBox/index.vue';
import ProcessFlow from '../components/ProcessFlow.vue';



export default {
  name: "MaterialCoil",
  components: {
    WarehouseSelect,
    QRCode,
    MaterialSelect,
    ProductSelect,
    RawMaterialSelect,
    ProductInfo,
    RawMaterialInfo,
    CoilTraceResult,
    LabelRender,
    ActualWarehouseSelect,
    CoilNo,
    MemoInput,
    MutiSelect,
    OuterTagPreview,
    AbnormalList,
    LogTable,
    ProcessFlow,
    DragResizeBox,
  },
  dicts: ['product_coil_status', 'coil_material', 'coil_itemname', 'coil_manufacturer', 'coil_quality_status', 'wms_next_warehouse', 'coil_business_purpose'],
  props: {
    qrcode: {
      type: Boolean,
      default: false,
    },
    querys: {
      type: Object,
      default: () => { },
    },
    labelType: {
      type: String,
      default: '2'
    },
    hideWarehouseQuery: {
      type: Boolean,
      default: false,
    },
    showStatus: {
      type: Boolean,
      default: false,
    },
    hideType: {
      type: Boolean,
      default: false,
    },
    showControl: {
      type: Boolean,
      default: true,
    },
    showExportTime: {
      type: Boolean,
      default: false,
    },
    showGrade: {
      type: Boolean,
      default: false,
    },
    showAbnormal: {
      type: Boolean,
      default: false,
    },
    showLength: {
      type: Boolean,
      default: false,
    },
    canExportAll: {
      type: Boolean,
      default: false,
    },
    editNext: {
      type: Boolean,
      default: false,
    },
    forceSpecialTag: {
      type: String,
      required: false,
    },
    editWarehouse: {
      type: Boolean,
      default: false,
    },
    showWaybill: {
      type: Boolean,
      default: false,
    },
    showWareLog: {
      type: Boolean,
      default: false,
    },
    showBusinessPurpose: {
      type: Boolean,
      default: false,
    },
    showRelatedToOrder: {
      type: Boolean,
      default: false,
    },
    showOrderBy: {
      type: Boolean,
      default: false,
    },
    showNewExport: {
      type: Boolean,
      default: false,
    },
    // 展示宽度快捷编辑
    showWidthEdit: {
      type: Boolean,
      default: false,
    },
    // 展示生产时间快捷编辑
    showProductionTimeEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      showProcessFlow: false,
      // 遮罩层
      loading: true,
      // 追溯加载中
      traceLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钢卷物料表格数据
      materialCoilList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 追溯对话框显示
      traceOpen: false,
      // 追溯结果数据（传递给组件）
      traceResult: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterCoilNo: undefined,
        currentCoilNo: undefined,
        supplierCoilNo: undefined,
        warehouseId: undefined,
        nextWarehouseId: undefined,
        actualWarehouseId: undefined,
        qrcodeRecordId: undefined,
        team: undefined,
        hasMergeSplit: undefined,
        parentCoilNos: undefined,
        itemId: undefined,
        itemIds: undefined,
        status: undefined,
        updateTime: undefined,
        orderBy: false,
        ...this.querys,
      },
      // 表单参数
      form: {},
      transferCoilForm: {},
      // 表单校验
      rules: {
        enterCoilNo: [
          { required: true, message: "入场钢卷号不能为空", trigger: "blur" },
          // 自定义校验，必须是8位的阿拉伯数字
          {
            validator: (rule, value, callback) => {
              if (this.form.coilId) {
                // 新增时触发校验
                callback();
              } else {
                if (!/^\d{8}$/.test(value)) {
                  callback(new Error('入场钢卷号必须是8位的阿拉伯数字'));
                } else {
                  callback();
                }
              }
            }, trigger: 'blur'
          },
          // 远程校验，检查钢卷号是否存在
          {
            validator: (rule, value, callback) => {
              if (this.form.coilId) {
                // 新增时触发校验 
                console.log('新增时触发校验');
                callback();
              } else {
                checkCoilNo({ enterCoilNo: value }).then(res => {
                  const { duplicateType } = res.data;
                  if (duplicateType === 'enter' || duplicateType === 'both') {
                    // alert('入场钢卷号重复，请重新输入');
                    callback(new Error('入场钢卷号重复，请重新输入'));
                  } else {
                    callback();
                  }
                })
              }
            }, trigger: 'blur'
          },
        ],
        currentCoilNo: [
          { required: true, message: "当前钢卷号不能为空", trigger: "blur" },
          // 仅在新增的时候校验
          // {
          //   validator: (rule, value, callback) => {
          //     // if (this.form.coilId) {
          //     //   // 修改时会有coilId，不触发校验
          //     //   console.log('修改时会有coilId，不触发校验');
          //     //   callback();
          //     // } else {
          //     // 没有coilId则为新增 触发校验
          //     checkCoilNo({ currentCoilNo: value, coilId: this.form.coilId }).then(res => {
          //       const { duplicateType } = res.data;
          //       if (duplicateType === 'current' || duplicateType === 'both') {
          //         // alert('当前钢卷号重复，请重新输入');
          //         callback(new Error('当前钢卷号重复，请重新输入'));
          //       } else {
          //         callback();
          //       }
          //     })
          //     // }
          //   }, trigger: 'blur'
          // }
        ],
        itemId: [
          { required: true, message: "物品ID不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "物品类型不能为空", trigger: "change" }
        ],
        // 净重和毛重
        netWeight: [
          { required: true, message: "净重不能为空", trigger: "blur" }
        ],
        grossWeight: [
          { required: true, message: "毛重不能为空", trigger: "blur" }
        ],
      },
      labelRender: {
        visible: false,
        data: {},
        type: '2'
      },
      batchPrint: {
        visible: false,
        loading: false,
        list: [],
      },
      __printOldTitle: document.title,
      floatLayerConfig: {
        columns: [
          { label: '入场钢卷号', prop: 'enterCoilNo' },
          { label: '当前钢卷号', prop: 'currentCoilNo' },
          { label: '厂家卷号', prop: 'supplierCoilNo' },
          { label: '逻辑库位', prop: 'warehouseName' },
          { label: '实际库位', prop: 'actualWarehouseName' },
          { label: '物料类型', prop: 'materialType' },
          { label: '班组', prop: 'team' },
          { label: '净重', prop: 'netWeight' },
          { label: '毛重', prop: 'grossWeight' },
          { label: '备注', prop: 'remark' },
          { label: '质量状态', prop: 'qualityStatus' },
          { label: '原料材质', prop: 'packingStatus' },
          { label: '切边要求', prop: 'edgeRequirement' },
          { label: '包装要求', prop: 'packagingRequirement' },
          { label: '厂家', prop: 'itemManufacturer' },
          { label: '调制度', prop: 'temperGrade' },
          { label: '镀层种类', prop: 'coatingType' },
          { label: '实测长度(m)', prop: 'actualLength' },
          { label: '实测宽度(m)', prop: 'actualWidth' },
        ],
        title: '详细信息'
      },
      abnormalOpen: false,
      currentCoilId: '',
      userList: [],
      logOpen: false,
      productionTimeForm: {
        productionStartTime: '',
        productionEndTime: '',
        formattedDuration: '',
        productionDuration: 0,
      },
      productionTimeFormRules: {
        productionTime: [
          { required: true, message: "生产时间不能为空", trigger: "blur" }
        ],
      },
      productionTimeFormVisible: false,
    };
  },
  computed: {
    // 动态显示标签
    getItemLabel() {
      if (this.form.materialType === '成品') {
        return '产品类型';
      } else if (this.form.materialType === '原料') {
        return '原料类型';
      }
      return '物品类型';
    },
    canEditExportTime() {
      // 徐梦琪和若依管理员
      const canEdit = ['1988841895986642945', 1];
      const currentUserId = this.$store.getters.id;
      return canEdit.includes(currentUserId);
    },
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    // 处理行点击事件
    handleRowClick(row) {
      this.currentCoilId = row.coilId;
      this.logOpen = true;
    },
    // 处理大小变化事件
    resizeChart() {
      this.$refs.processFlow.resizeChart();
    },
    // 获取用户列表
    getUserList() {
      listUser({ pageNum: 1, pageSize: 1000 }).then(res => {
        this.userList = res.rows || [];
      });
    },
    handleProductionTimeEdit(row) {
      // 创建一个新对象，避免直接引用row
      this.productionTimeForm = { ...row };
      this.productionTimeFormVisible = true;
      // 初始化时计算一次
      this.calculateProductionDuration();
    },
    // 格式化毫秒值为xx天xx小时xx分钟
    formatDuration(milliseconds) {
      if (!milliseconds || milliseconds < 0) return '';

      const seconds = Math.floor(milliseconds / 1000);
      const minutes = Math.floor(seconds / 60);
      const hours = Math.floor(minutes / 60);
      const days = Math.floor(hours / 24);

      const remainingHours = hours % 24;
      const remainingMinutes = minutes % 60;

      let result = '';
      if (days > 0) result += `${days}天`;
      if (remainingHours > 0) result += `${remainingHours}小时`;
      if (remainingMinutes > 0) result += `${remainingMinutes}分钟`;

      return result || '0分钟';
    },
    // 计算生产耗时
    calculateProductionDuration() {
      const { productionStartTime, productionEndTime } = this.productionTimeForm;
      if (productionStartTime && productionEndTime) {
        const start = new Date(productionStartTime).getTime();
        const end = new Date(productionEndTime).getTime();
        if (end < start) {
          this.$message({
            message: '结束时间不能早于开始时间',
            type: 'error',
          });
          this.$set(this.productionTimeForm, 'productionDuration', '');
          this.$set(this.productionTimeForm, 'formattedDuration', '');
        } else {
          const durationMs = end - start;
          const durationMinutes = Math.round(durationMs / (1000 * 60));
          this.$set(this.productionTimeForm, 'productionDuration', durationMinutes);
          // 同时保存格式化后的显示值
          this.$set(this.productionTimeForm, 'formattedDuration', this.formatDuration(durationMinutes * 60 * 1000));
        }
      } else {
        this.$set(this.productionTimeForm, 'productionDuration', '');
        this.$set(this.productionTimeForm, 'formattedDuration', '');
      }
    },
    // 处理生产时间提交
    submitProductionTimeForm() {
      this.$refs.productionTimeForm.validate((valid) => {
        if (valid) {
          // 再次验证时间逻辑
          const { productionStartTime, productionEndTime } = this.productionTimeForm;
          if (productionStartTime && productionEndTime) {
            const start = new Date(productionStartTime).getTime();
            const end = new Date(productionEndTime).getTime();
            if (end < start) {
              this.$message({
                message: '结束时间不能早于开始时间',
                type: 'error',
              });
              return false;
            }
          }
          this.buttonLoading = true;
          updateMaterialCoilSimple(this.productionTimeForm).then(res => {
            this.buttonLoading = false;
            this.$message({
              message: '更新成功',
              type: 'success',
            });
            this.productionTimeFormVisible = false;
          })
        } else {
          return false;
        }
      })
    },
    handleNextWarehouseChange(row) {
      if (!this.editNext) {
        return;
      }
      updateMaterialCoilSimple(row).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '更新成功',
            type: 'success',
          });
        } else {
          this.$message({
            message: res.msg || '更新失败',
            type: 'error',
          });
        }
      })
    },
    // 处理行数据变化
    handleRowChange(row) {
      updateMaterialCoilSimple(row).then(res => {
        this.$message({
          message: '更新成功',
          type: 'success',
        });
      })
    },
    // 打印标签
    handlePrintLabel(row) {
      const type = getCoilTagPrintType(row);
      this.labelRender.type = type;

      this.labelRender.data = {
        ...row,
        updateTime: row.updateTime?.split(' ')[0] || '',
      };
      this.$nextTick(() => {
        this.$refs.labelRender.printLabel();
      })
    },
    // 处理材料类型变化
    handleMaterialTypeChange(value) {
      // 清空物品选择
      this.form.itemId = null;

      // 根据材料类型设置物品类型
      if (value === '成品') {
        this.form.itemType = 'product';
      } else if (value === '原料') {
        this.form.itemType = 'raw_material';
      }
    },
    /** 查询钢卷物料列表 */
    async getList() {
      this.loading = true;
      const { updateTime, ...query } = {
        ...this.queryParams,
        startTime: this.queryParams.updateTime?.[0],
        endTime: this.queryParams.updateTime?.[1],
      }
      // 如果没有设置itemType，则设置为raw_material
      query.selectType = this.querys.materialType === '原料' ? 'raw_material' : 'product';
      if (this.showWaybill) {
        listBoundCoil(query).then(res => {
          this.materialCoilList = res.rows || [];
          this.total = res.total;
          this.loading = false;
        })
        return;
      }
      listMaterialCoil(query).then(response => {
        this.materialCoilList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 追溯按钮操作 */
    handleTrace(row) {
      this.traceOpen = true;
      this.traceLoading = true;
      this.traceResult = null; // 清空历史数据
      getMaterialCoilTrace({
        coilId: row.coilId,
        currentCoilNo: row.currentCoilNo,
      }).then(res => {
        this.traceResult = res.data; // 将结果传递给组件
      }).catch(err => {
        console.error('溯源查询失败：', err);
        this.$message.error('溯源查询失败，请重试');
      }).finally(() => {
        this.traceLoading = false;
      });
    },
    handleGradeChange(row) {
      updateMaterialCoilSimple(row).then(res => {
        this.$message.success('质量状态更新成功');
        this.getList(); // 刷新列表
      })
    },
    handleWarehouseChange(row) {
      if (!this.editWarehouse) {
        return;
      }
      updateMaterialCoilSimple(row).then(res => {
        this.$message.success('库位更新成功');
        this.getList(); // 刷新列表
      })
    },
    /** 预览标签 */
    handlePreviewLabel(row) {
      this.labelRender.visible = true;
      const itemName = row.itemName || '';
      
      this.labelRender.type = row.itemType === 'product' ? '3' : '2';
      this.labelRender.data = {
        ...row,
        itemName: itemName,
        updateTime: row.updateTime?.split(' ')[0] || '',
      };
    },
    /** 下载二维码 */
    handleDownloadQRCode(row) {
      try {
        saveAsImage(
          row.qrcodeRecordId,
          '',
          1,
          {
            barcodeWidth: 200,
            barcodeHeight: 200
          }
        );
        this.$message.success('图片保存成功');
      } catch (error) {
        console.error('保存图片失败', error);
        this.$message.error('保存图片失败，请稍后重试');
      }
    },
    handleAbnormal(row) {
      this.currentCoilId = row.coilId;
      this.abnormalOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        coilId: undefined,
        enterCoilNo: undefined,
        currentCoilNo: undefined,
        supplierCoilNo: undefined,
        dataType: 1,
        warehouseId: undefined,
        nextWarehouseId: undefined,
        qrcodeRecordId: undefined,
        actualWarehouseId: undefined,
        team: undefined,
        hasMergeSplit: undefined,
        parentCoilNos: undefined,
        itemId: undefined,
        itemType: undefined,
        status: undefined,
        remark: undefined,
        delFlag: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        materialType: '原料',
        temperGrade: undefined,
        coatingType: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.coilId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isCheck = false;
      this.reset();

      // 如果父组件传入了 materialType，使用它作为默认值
      if (this.querys.materialType) {
        this.form.materialType = this.querys.materialType;
        // 同时设置对应的 itemType
        if (this.querys.materialType === '成品') {
          this.form.itemType = 'product';
        } else if (this.querys.materialType === '原料') {
          this.form.itemType = 'raw_material';
        }
      }

      this.open = true;
      this.title = "添加钢卷物料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isCheck = false;
      this.loading = true;
      this.reset();
      const coilId = row.coilId || this.ids
      getMaterialCoil(coilId).then(response => {
        this.loading = false;
        this.form = response.data;

        // 设置 materialType（优先级：后端返回 > itemType推断 > 父组件传入）
        if (!this.form.materialType) {
          if (this.form.itemType) {
            // 根据 itemType 推断
            if (this.form.itemType === 'product') {
              this.form.materialType = '成品';
            } else if (this.form.itemType === 'raw_material') {
              this.form.materialType = '原料';
            }
          } else if (this.querys.materialType) {
            // 使用父组件传入的默认值
            this.form.materialType = this.querys.materialType;
            // 同时设置对应的 itemType
            if (this.querys.materialType === '成品') {
              this.form.itemType = 'product';
            } else if (this.querys.materialType === '原料') {
              this.form.itemType = 'raw_material';
            }
          }
        }

        this.open = true;
        this.title = "修改钢卷物料";
      });
    },
    handleExportCoil(row) {
      exportCoil(row.coilId).then(response => {
        this.$modal.msgSuccess("发货成功");
        // 2. 插入一条已完成的待操作记录
        addPendingAction({
          coilId: row.coilId,
          currentCoilNo: row.currentCoilNo,
          actionType: 402, // 402=发货
          actionStatus: 2, // 直接标记为完成状态
          scanTime: new Date(),
          // scanDevice: ,
          priority: 0, // 0=普通
          sourceType: 'scan',
          warehouseId: row.warehouseId,
          processTime: new Date(),
          completeTime: new Date()
        });
        this.getList();
      }).catch(error => {
        this.$modal.msgError("发货失败");
      });
    },
    async handleNewExport(row) {
      this.loading = true
      let coilIds = ''
      const query = {
        ...this.queryParams,
        selectType: 'product',
        pageSize: 9999,
        pageNum: 1,
      }
      if (this.showWaybill) {
        const res = await listBoundCoil(query)
        coilIds = res.rows.map(item => item.coilId).join(',')
        this.loading = false
        this.download('/wms/materialCoil/exportDelivery', {
          coilIds,
        }, 'coil.xlsx')
      } else {
        const { rows: coils } = await listMaterialCoil(query)
        coilIds = coils.map(item => item.coilId).join(',')
        this.loading = false
        this.download('wms/materialCoil/exportAll', {
          coilIds,
        }, 'coil.xlsx')
      }
    },
    handleCheck(row) {
      this.isCheck = true;
      this.loading = true;
      this.reset();
      const coilId = row.coilId || this.ids
      getMaterialCoil(coilId).then(response => {
        this.loading = false;
        this.form = response.data;

        // 设置 materialType（优先级：后端返回 > itemType推断 > 父组件传入）
        if (!this.form.materialType) {
          if (this.form.itemType) {
            // 根据 itemType 推断
            if (this.form.itemType === 'product') {
              this.form.materialType = '成品';
            } else if (this.form.itemType === 'raw_material') {
              this.form.materialType = '原料';
            }
          } else if (this.querys.materialType) {
            // 使用父组件传入的默认值
            this.form.materialType = this.querys.materialType;
            // 同时设置对应的 itemType
            if (this.querys.materialType === '成品') {
              this.form.itemType = 'product';
            } else if (this.querys.materialType === '原料') {
              this.form.itemType = 'raw_material';
            }
          }
        }

        this.open = true;
        this.title = "修改钢卷物料";
      });
    },
    handleLog(row) {
      this.logOpen = true;
      this.currentCoilId = row.coilId;
    },
    transferCoil() { },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.coilId != null) {
            updateMaterialCoilSimple(this.form).then(_ => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterialCoil(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    handleCancelExport(row) {
      // 将相关的action改为已取消，然后将钢卷的已发货状态撤回
      this.$modal.confirm('是否确认撤回钢卷编号为"' + row.currentCoilNo + '"的发货操作？').then(() => {
        this.buttonLoading = true;
        cancelExportCoil(row.coilId).then(response => {
          this.$modal.msgSuccess("撤回发货成功");
          this.getList();
        }).catch(error => {
          this.$modal.msgError("撤回发货失败");
        }).finally(() => {
          this.buttonLoading = false;
        });
      }).catch(() => {
      });
    },
    handleReturnCoil(row) {
      this.$modal.confirm('是否确认退货钢卷编号为"' + row.currentCoilNo + '"的退货操作？').then(() => {
        this.buttonLoading = true;
        returnCoil(row.coilId).then(response => {
          this.$modal.msgSuccess("退货成功");
          this.getList();
        }).catch(error => {
          this.$modal.msgError("退货失败");
        }).finally(() => {
          this.buttonLoading = false;
        });
      })
    },
    handleExportTimeChange(row) {
      if (row.exportTime) {
        row.exportTime = row.exportTime.replace('T', ' ');
      }
      this.buttonLoading = true;
      updateMaterialCoilSimple(row).then(_ => {
        this.$modal.msgSuccess("发货时间修改成功");
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    handleExportByNameChange(row) {
      this.buttonLoading = true;
      updateMaterialCoilSimple(row).then(_ => {
        this.$modal.msgSuccess("发货人修改成功");
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const coilIds = row.coilId || this.ids;
      this.$modal.confirm('是否确认删除钢卷物料编号为"' + coilIds + '"的数据项，会同时清理刚钢卷相关的待操作记录且无法恢复！！！是否继续删除？').then(() => {
        this.loading = true;
        return delMaterialCoil(coilIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleExportAll() {
      this.download('wms/materialCoil/export', {
        ...this.queryParams
      }, `materialCoil_${new Date().getTime()}.xlsx`)
    },
    /** 批量打印标签按钮 */
    handleBatchPrintLabel() {
      if (!this.ids || this.ids.length === 0) {
        this.$message.warning('请先勾选要打印标签的钢卷');
        return;
      }

      // 取出选中行数据，并补齐标签渲染需要的字段
      const selectedData = this.materialCoilList
        .filter(item => this.ids.includes(item.coilId))
        .map(row => {
          const itemName = row.itemName || '';

          return {
            ...row,
            itemName,
            // OuterTagPreview.vue 里字段名使用了 specification/material（而列表里是 itemSpecification/itemMaterial）
            specification: row.itemSpecification || row.specification || '',
            material: row.itemMaterial || row.material || '',
            updateTime: row.updateTime?.split(' ')[0] || row.updateTime || '',
          }
        });

      this.batchPrint.list = selectedData;
      this.batchPrint.visible = true;
    },

    /** 批量导出标签PDF：每个标签一页（180mm × 100mm） */
    async handleBatchExportLabelPdf() {
      if (!this.batchPrint.list || this.batchPrint.list.length === 0) {
        this.$message.warning('没有可导出的数据');
        return;
      }

      const container = this.$refs.batchPdfContainer;
      if (!container) {
        this.$message.error('PDF渲染容器未初始化');
        return;
      }

      try {
        this.batchPrint.loading = true;

        await this.$nextTick();
        await new Promise(resolve => setTimeout(resolve, 100));

        const mmToPt = 72 / 25.4;
        // 纸张尺寸
        const paperWidthMm = 180;
        const paperHeightMm = 100;
        // 边距：左右 4mm，上下对称 2mm，确保垂直居中
        const marginXmm = 4;
        const marginYmm = 0.5; // 上下对称边距（不裁切前提下尽量贴边）

        const pageWidthPt = paperWidthMm * mmToPt;
        const pageHeightPt = paperHeightMm * mmToPt;
        const marginXPt = marginXmm * mmToPt;
        const marginYPt = marginYmm * mmToPt;

        const contentWidthPt = pageWidthPt - marginXPt * 2;
        const contentHeightPt = pageHeightPt - marginYPt * 2;

        const pdfDoc = await PDFDocument.create();

        // 关键：只截取标签本身（.label-container），不要把外层容器/按钮高度算进去
        const pageEls = container.querySelectorAll('.batch-pdf-page');

        for (let i = 0; i < pageEls.length; i++) {
          const el = pageEls[i];

          // 在每一页内部优先查找标签根节点
          const labelEl =
            el.querySelector('.label-container') ||
            el.querySelector('.material-label-container') ||
            el; // 兜底：找不到时退回整页

          // 强制用标签的实际尺寸作为截图基准，避免被外层布局影响
          const canvas = await html2canvas(labelEl, {
            backgroundColor: '#ffffff',
            scale: 3,
            useCORS: true,
            // 让 html2canvas 为频繁读回优化 Canvas（浏览器会提示 willReadFrequently）
            willReadFrequently: true,
            // 确保按元素尺寸截图
            width: labelEl.offsetWidth,
            height: labelEl.offsetHeight,
            windowWidth: labelEl.scrollWidth,
            windowHeight: labelEl.scrollHeight,
          });

          const imgDataUrl = canvas.toDataURL('image/png');
          const pngImage = await pdfDoc.embedPng(imgDataUrl);

          const page = pdfDoc.addPage([pageWidthPt, pageHeightPt]);

          // 图片铺满页面（保持比例，居中）
          const imgW = pngImage.width;
          const imgH = pngImage.height;
          // 标准适配：确保完整显示且不变形（在内容区域内等比缩放）
          const scale = Math.min(contentWidthPt / imgW, contentHeightPt / imgH);
          const drawW = imgW * scale;
          const drawH = imgH * scale;

          // 内容区域内居中 + 外层边距（上下对称）
          const x = marginXPt + (contentWidthPt - drawW) / 2;
          const y = marginYPt + (contentHeightPt - drawH) / 2;

          page.drawImage(pngImage, { x, y, width: drawW, height: drawH });
        }

        const pdfBytes = await pdfDoc.save();
        const blob = new Blob([pdfBytes], { type: 'application/pdf' });
        const url = URL.createObjectURL(blob);

        // 尽量避免被浏览器拦截弹窗：优先在当前tab打开；如仍被策略限制，可改为下载
        const win = window.open(url, '_blank');
        if (!win) {
          // fallback：触发下载
          const a = document.createElement('a');
          a.href = url;
          a.download = `钢卷标签_${new Date().getTime()}.pdf`;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        }
      } catch (e) {
        console.error(e);
        this.$message.error('生成PDF失败，请重试');
      } finally {
        this.batchPrint.loading = false;
      }
    },
    /** 导出选中数据操作 */
    handleExport() {
      // 1. 判断是否有选中数据
      if (this.ids.length === 0) {
        this.$message.warning('请先选中要导出的数据');
        return;
      }

      // 2. 筛选选中的数据（通过ids匹配表格数据）
      const selectedData = this.materialCoilList.filter(item =>
        this.ids.includes(item.coilId) // 用选中的coilId匹配表格数据
      );

      // 3. 处理导出数据格式（和之前一致，转换枚举值）
      const exportData = selectedData.map(item => {
        return {
          '入场钢卷号': item.enterCoilNo || '',
          '当前钢卷号': item.currentCoilNo || '',
          '厂家原料卷号': item.supplierCoilNo || '',
          '物料类型': item.itemType === 'product' ? '成品' : '原料',
          '仓区': item.warehouseName || '',
          '实际库区': item.actualWarehouseName || '',
          '物品': findItemWithBom(item.itemType, item.itemId)?.itemName || '',
          '数据类型': item.dataType === 0 ? '历史数据' : '当前数据',
          '班组': item.team || '',
          '毛重': item.grossWeight || '',
          '净重': item.netWeight || '',
          '备注': item.remark || ''
        };
      });

      // 4. 生成Excel并下载（复用之前的逻辑）
      const worksheet = XLSX.utils.json_to_sheet(exportData);
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, '选中钢卷物料');

      const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
      this.saveExcelFile(excelBuffer, '选中钢卷物料数据');
    },

    /** 保存Excel文件到本地 */
    saveExcelFile(buffer, fileName) {
      const blob = new Blob([buffer], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
      });
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = `${fileName}_${new Date().getTime()}.xlsx`; // 文件名带时间戳
      document.body.appendChild(a);
      a.click(); // 触发下载
      document.body.removeChild(a); // 清理DOM
      URL.revokeObjectURL(url); // 释放URL对象
    }
  }
};
</script>

<style scoped>
/* 批量导出PDF：渲染容器屏幕隐藏，但保留真实布局尺寸给 html2canvas 截图 */
.batch-pdf-root {
  position: fixed;
  left: -100000px;
  top: 0;
  width: 180mm;
  background: #fff;
}

.batch-pdf-page {
  width: 180mm;
  height: 100mm;
  box-sizing: border-box;
  overflow: hidden;
}
</style>
