<template>
  <div class="app-container">
    <el-card style="height: 850px;padding: 0">
      <!-- 修改人:Ld 修改时间:2026-07-06 08:36:02 -->
      <!-- 参考制定工艺组件的订单、产品、零件查询入口 -->
      <div class="toolbar">
        <el-select v-model="queryForm.订单流水号" clearable filterable size="small" style="width: 170px" placeholder="订单号" @change="handleOrderChange">
          <el-option v-for="item in orderNumberType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="queryForm.组件流水号" clearable filterable size="small" style="width: 190px" placeholder="产品名称" @change="handleProductChange">
          <el-option v-for="item in productName" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="queryForm.工艺计划流水号" clearable filterable size="small" style="width: 220px" placeholder="零件名称" @change="pageCurrent = 1;searchTable()">
          <el-option v-for="item in partName" :key="item.value" :label="item.label" :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
          </el-option>
        </el-select>
        <el-select v-model="queryForm.是否启用" clearable filterable size="small" style="width: 120px" placeholder="启用状态" @change="pageCurrent = 1;searchTable()">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-input v-model="queryForm.工序名称" clearable size="small" style="width: 150px" placeholder="工序名称" @keyup.enter.native="pageCurrent = 1;searchTable()" />
        <el-input v-model="queryForm.零件名称" clearable size="small" style="width: 150px" placeholder="零件名称" @keyup.enter.native="pageCurrent = 1;searchTable()" />
        <el-input v-model="queryForm.图号" clearable size="small" style="width: 150px" placeholder="图号" @keyup.enter.native="pageCurrent = 1;searchTable()" />
        <el-button icon="el-icon-search" type="primary" plain size="small" style="margin-left: 10px" @click="pageCurrent = 1;searchTable()">查询</el-button>
        <!-- 修改人:Ld 修改时间:2026-08-03 14:24:48; 工序工价按当前查询条件导出测试库新增的导出过程数据 -->
        <el-button type="success" plain icon="el-icon-download" size="small" @click="exportExcel()">导出</el-button>
        <el-button :disabled="!queryForm.工艺计划流水号 || tableData.length === 0" icon="el-icon-collection" type="success" plain size="small" @click="handleSaveSource()">保存</el-button>
        <el-button :disabled="!queryForm.工艺计划流水号" icon="el-icon-document-copy" type="warning" plain size="small" @click="openCopyDialog()">拷贝</el-button>
        <el-tag v-if="savedSourceInfo" size="small" type="success" style="margin-left: 6px" closable @close="savedSourceInfo = null">已保存: {{ savedSourceInfo.零件名称 }}</el-tag>
      </div>

      <!-- 修改人:Ld 修改时间:2026-07-02 15:33:23 -->
      <!-- 工序工价列表 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        highlight-current-row
        stripe
        size="mini"
        height="720"
        style="width: 100%;">
        <el-table-column type="index" label="序号" align="center" width="60" />
        <el-table-column label="订单号" align="center" show-overflow-tooltip width="140">
          <template slot-scope="scope">
            {{ scope.row.订单号 }}
          </template>
        </el-table-column>
        <el-table-column label="产品名称" align="center" show-overflow-tooltip width="140">
          <template slot-scope="scope">
            {{ scope.row.产品名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工序名称" align="center" show-overflow-tooltip width="150">
          <template slot-scope="scope">
            {{ scope.row.工序名称 }}
          </template>
        </el-table-column>
        <el-table-column label="零件名称" align="center" show-overflow-tooltip width="150">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="图号" align="center" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            {{ scope.row.图号 }}
          </template>
        </el-table-column>
        <el-table-column label="工序顺序" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.工序顺序 }}
          </template>
        </el-table-column>
        <el-table-column label="定额工时" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.定额工时 }}
          </template>
        </el-table-column>
        <el-table-column label="准结工时" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.准结工时 }}
          </template>
        </el-table-column>
        <el-table-column label="机动时间s" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.机动时间s }}
          </template>
        </el-table-column>
        <el-table-column label="辅助时间s" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.辅助时间s }}
          </template>
        </el-table-column>
        <el-table-column label="总时间s" align="center" width="90">
          <template slot-scope="scope">
            {{ getTotalTime(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column label="单装数量(件)" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row['单装数量(件)'] }}
          </template>
        </el-table-column>
        <el-table-column label="操作台数" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.操作台数 }}
          </template>
        </el-table-column>
        <el-table-column label="日产能(件)" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row['日产能(件)'] }}
          </template>
        </el-table-column>
        <el-table-column label="最终单价(元/件)" align="center" width="130">
          <template slot-scope="scope">
            {{ scope.row['最终单价(元/件)'] }}
          </template>
        </el-table-column>
        <el-table-column label="生效日期" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.生效日期 }}
          </template>
        </el-table-column>
        <el-table-column label="变更次数" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.变更次数 }}
          </template>
        </el-table-column>
        <el-table-column label="启用状态" align="center" width="90">
          <template slot-scope="scope">
            <el-tag :type="Number(scope.row.是否启用) === 1 ? 'success' : 'info'" size="mini">
              {{ Number(scope.row.是否启用) === 1 ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" show-overflow-tooltip min-width="140">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="170" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="mini" icon="el-icon-edit-outline" @click="openEditDialog(scope.row)">设置</el-button>
            <el-button :disabled="!scope.row.工价流水号" type="text" size="mini" icon="el-icon-document" @click="openHistoryDialog(scope.row)">历史</el-button>
            <el-button :disabled="!scope.row.工价流水号" type="text" size="mini" icon="el-icon-turn-off" style="color:#f56c6c" @click="handleDisable(scope.row)">停用</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 修改人:Ld 修改时间:2026-07-02 15:33:23 -->
      <!-- 分页 -->
      <div class="block" style="margin-top: 10px;">
        <!-- 修改人:Ld 修改时间:2026-07-06 08:52:37 -->
        <!-- 工序工价数据量超过 100 条，放开分页条数选择 -->
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[20, 50, 100, 200, 500, 1000]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>

    <!-- 修改人:Ld 修改时间:2026-07-02 15:33:23 -->
    <!-- 工价设置弹窗 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" center width="960px">
      <!-- 修改人:Ld 修改时间:2026-08-04 09:39:18; 工价公式字段名称较长，放大标签宽度避免遮挡输入框 -->
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="130px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="工艺名称" prop="工艺名称">
              <el-input v-model="form.工艺名称" disabled size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="工序名称">
              <el-input v-model="form.工序名称" disabled size="small" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="零件名称" prop="零件名称">
              <el-input v-model="form.零件名称" disabled size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图号" prop="图号">
              <el-input v-model="form.图号" disabled size="small" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 修改人:Ld 修改时间:2026-08-04 09:39:18; 设置弹窗补齐截图公式字段，计算字段随输入自动联动 -->
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="机动时间(秒)" prop="机动时间s">
              <el-input-number v-model="form.机动时间s" :min="0" :precision="2" :step="1" controls-position="right" style="width: 100%;" @change="refreshTotalTime()" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="辅助时间(秒)" prop="辅助时间s">
              <el-input-number v-model="form.辅助时间s" :min="0" :precision="2" :step="1" controls-position="right" style="width: 100%;" @change="refreshTotalTime()" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总时间(秒)" prop="总时间s">
              <!-- 修改人:Ld 修改时间:2026-08-03 14:40:10; 总时间由机动时间和辅助时间自动相加，不允许手工改写 -->
              <el-input-number v-model="form.总时间s" :min="0" :precision="2" :step="1" controls-position="right" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="单装数量(件)" prop="单装数量(件)">
              <el-input-number v-model="form['单装数量(件)']" :min="0" :precision="2" :step="1" controls-position="right" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作台数(台/人)" prop="操作台数">
              <el-input-number v-model="form.操作台数" :min="0" :precision="2" :step="1" controls-position="right" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作时间(时)" prop="工作时间(时)">
              <el-input-number v-model="form['工作时间(时)']" :min="0" :precision="2" :step="0.5" controls-position="right" style="width: 100%;" @change="refreshPriceFormula()" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="宽放时间(时)" prop="宽放时间(时)">
              <el-input-number v-model="form['宽放时间(时)']" :min="0" :precision="2" :step="0.5" controls-position="right" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日工资(元)" prop="日工资(元)">
              <el-input-number v-model="form['日工资(元)']" :min="0" :precision="2" :step="1" controls-position="right" style="width: 100%;" @change="refreshPriceFormula()" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标准产能(件/天)" prop="标准产能(件/天)">
              <el-input-number v-model="form['标准产能(件/天)']" :min="0" :precision="2" :step="1" controls-position="right" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="工价定额" prop="工价定额">
              <el-input-number v-model="form.工价定额" :min="0" :precision="2" :step="0.1" controls-position="right" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生效日期" prop="生效日期">
              <el-date-picker v-model="form.生效日期" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" clearable size="small" placeholder="选择生效日期" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="变更次数" prop="变更次数">
              <el-input-number v-model="form.变更次数" :min="0" :precision="0" :step="1" controls-position="right" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="启用状态" prop="是否启用">
              <el-select v-model="form.是否启用" clearable filterable size="small" style="width: 100%;" placeholder="请选择启用状态">
                <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="备注" prop="备注">
              <el-input v-model="form.备注" :rows="3" type="textarea" clearable maxlength="200" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改人:Ld 修改时间:2026-07-06 08:45:45 -->
    <!-- 工序工价拷贝弹窗，仅保留零件名称、工序号和工艺名称预览 -->
    <el-dialog v-el-drag-dialog :visible.sync="copyVisible" title="工序工价拷贝" center width="760px">
      <el-row style="margin-bottom: 10px">
        <el-input v-model="copyForm.零件筛选" clearable placeholder="零件图号或零件名称筛选" size="small" style="width: 260px">
          <el-button slot="append" icon="el-icon-right" @click="searchCopyParts()" />
        </el-input>
        <el-select v-model="copyForm.源零件图号" clearable filterable size="small" style="width: 260px;margin-left: 10px" placeholder="源零件" @change="searchCopyProcess()">
          <el-option v-for="item in copyPartList" :key="item.value" :label="item.label" :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-row>
      <el-table :data="copyProcessList" border highlight-current-row stripe size="mini" height="420" style="width: 100%;">
        <el-table-column type="index" label="序号" align="center" width="60" />
        <el-table-column label="零件名称" align="center" show-overflow-tooltip min-width="180">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工序号" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.工序顺序 }}
          </template>
        </el-table-column>
        <el-table-column label="工艺名称" align="center" show-overflow-tooltip min-width="180">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="copyVisible = false">取消</el-button>
        <el-button type="primary" size="small" @click="submitCopy()">确定</el-button>
      </div>
    </el-dialog>

    <!-- 修改人:Ld 修改时间:2026-07-02 15:33:23 -->
    <!-- 历史定价弹窗 -->
    <el-dialog v-el-drag-dialog :visible.sync="historyVisible" title="历史定价" center width="980px">
      <el-table :data="historyData" border stripe size="mini" height="420" style="width: 100%;">
        <el-table-column type="index" label="序号" align="center" width="60" />
        <el-table-column label="工价流水号" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.工价流水号 }}
          </template>
        </el-table-column>
        <el-table-column label="操作类型" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.操作类型 }}
          </template>
        </el-table-column>
        <el-table-column label="工艺名称" align="center" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            {{ scope.row.工艺名称 }}
          </template>
        </el-table-column>
        <el-table-column label="工序名称" align="center" show-overflow-tooltip width="150">
          <template slot-scope="scope">
            {{ scope.row.工序名称 }}
          </template>
        </el-table-column>
        <el-table-column label="变更前" align="center" show-overflow-tooltip min-width="150">
          <template slot-scope="scope">
            {{ scope.row.变更前 }}
          </template>
        </el-table-column>
        <el-table-column label="变更后" align="center" show-overflow-tooltip min-width="150">
          <template slot-scope="scope">
            {{ scope.row.变更后 }}
          </template>
        </el-table-column>
        <el-table-column label="操作人" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.操作人 }}
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" width="140">
          <template slot-scope="scope">
            {{ scope.row.操作时间 }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="historyVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 修改人:Ld 修改时间:2026-07-06 08:36:02
      // 查询条件参考制定工艺组件，表格显示字段保持不变
      queryForm: {
        订单流水号: '',
        组件流水号: '',
        工艺计划流水号: '',
        工序名称: '',
        零件名称: '',
        图号: '',
        是否启用: ''
      },
      // 修改人:Ld 修改时间:2026-07-06 08:36:02
      // 制定工艺查询入口下拉数据
      orderNumberType: [],
      productName: [],
      partName: [],
      // 修改人:Ld 修改时间:2026-07-02 15:33:23
      // 下拉选项
      statusOptions: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 }
      ],
      // 修改人:Ld 修改时间:2026-07-02 15:33:23
      // 表格与分页
      loading: false,
      tableData: [],
      historyData: [],
      total: 0,
      pageCurrent: 1,
      pageSize: 100,
      // 修改人:Ld 修改时间:2026-07-02 15:33:23
      // 弹窗状态
      dialogFormVisible: false,
      dialogTitle: '设置工序工价',
      dialogMode: 'edit',
      historyVisible: false,
      // 修改人:Ld 修改时间:2026-07-06 08:45:45
      // 工序工价拷贝数据，参考制定工艺拷贝但去掉工艺编辑功能
      copyVisible: false,
      copyForm: {
        零件筛选: '',
        源零件图号: ''
      },
      copyPartList: [],
      copyProcessList: [],
      // 保存的源零件信息（供拷贝时直接使用）
      savedSourceInfo: null,
      // 修改人:Ld 修改时间:2026-07-17 08:44:08; 从工时统计跳转时携带的查询条件，赋值后直接调用查询接口
      jumpQuery订单号: '',
      jumpDirectQuery: false,
      // 跳转时是否需要自动级联选择
      jumpAutoCascade: false,
      // 修改人:Ld 修改时间:2026-07-02 15:33:23
      // 表单主键
      form: {
        工价流水号: '',
        工艺名称: '',
        工序名称: '',
        零件名称: '',
        图号: '',
        机动时间s: 0,
        辅助时间s: 0,
        总时间s: 0,
        '单装数量(件)': 0,
        操作台数: 0,
        '工作时间(时)': 0,
        '宽放时间(时)': 0,
        '日工资(元)': 0,
        '标准产能(件/天)': 0,
        工价定额: 0,
        '日产能(件)': 0,
        '最终单价(元/件)': 0,
        生效日期: '',
        变更次数: 0,
        是否启用: 1,
        备注: '',
        工序流水号: ''
      },
      rules: {
        工艺名称: [{ required: true, message: '请输入工艺名称', trigger: 'blur' }],
        工序名称: [{ required: true, message: '请输入工序名称', trigger: 'blur' }],
        零件名称: [{ required: true, message: '请输入零件名称', trigger: 'blur' }],
        图号: [{ required: true, message: '请输入图号', trigger: 'blur' }],
        是否启用: [{ required: true, message: '请选择启用状态', trigger: 'change' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  watch: {
    // 修改人:Ld 修改时间:2026-07-17 16:30:55; 同一路由再次跳转时组件会复用，监听 query 变化后重新赋值并查询
    '$route.query': function(query) {
      this.applyJumpQuery(query)
    }
  },
  created() {
    this.applyJumpQuery(this.$route.query, true)
    this.getOrderNumberType()
    this.searchTable()
  },
  methods: {
    // 修改人:Ld 修改时间:2026-08-03 14:40:10; 总时间统一按机动时间加辅助时间计算
    getTotalTime(row) {
      var total = Number(row.机动时间s || 0) + Number(row.辅助时间s || 0)
      return Number(total.toFixed(2))
    },
    // 修改人:Ld 修改时间:2026-08-03 14:40:10; 编辑弹窗中保持总时间自动等于机动时间加辅助时间
    refreshTotalTime() {
      if (!this.form) {
        return
      }
      this.form.总时间s = this.getTotalTime(this.form)
      this.refreshPriceFormula()
    },
    // 修改人:Ld 修改时间:2026-08-04 09:39:18; 按截图公式计算标准产能和工价定额，并同步旧字段兼容后端编辑过程
    refreshPriceFormula() {
      if (!this.form) {
        return
      }
      var totalTime = Number(this.form.总时间s || 0)
      var workTime = Number(this.form['工作时间(时)'] || 0)
      var dayWage = Number(this.form['日工资(元)'] || 0)
      var capacity = totalTime === 0 ? 0 : workTime * 3600 / totalTime
      var priceQuota = capacity === 0 ? 0 : dayWage / capacity
      this.form['标准产能(件/天)'] = Number(capacity.toFixed(4))
      this.form.工价定额 = Number(priceQuota.toFixed(4))
      this.form['日产能(件)'] = this.form['标准产能(件/天)']
      this.form['最终单价(元/件)'] = this.form.工价定额
    },
    // 修改人:Ld 修改时间:2026-07-17 16:30:55; 统一处理工时统计跳转参数，避免再次跳转时保留上一次查询条件
    applyJumpQuery(query) {
      if (!query || (!query.订单号 && !query.零件名称 && !query.图号 && !query.工序名称)) {
        return
      }
      this.jumpQuery订单号 = query.订单号 || ''
      this.jumpDirectQuery = !!(query.零件名称 || query.图号 || query.工序名称)
      this.jumpAutoCascade = false
      if (this.jumpDirectQuery) {
        this.queryForm.组件流水号 = ''
        this.queryForm.工艺计划流水号 = ''
        this.queryForm.零件名称 = query.零件名称 || ''
        this.queryForm.图号 = query.图号 || ''
        this.queryForm.工序名称 = query.工序名称 || ''
        this.productName = []
        this.partName = []
      }
      this.setJumpOrderValue()
      this.pageCurrent = 1
      this.searchTable()
    },
    // 修改人:Ld 修改时间:2026-07-17 16:30:55; 已加载订单下拉时，按跳转订单号补充订单流水号
    setJumpOrderValue() {
      if (!this.jumpQuery订单号 || !this.orderNumberType || this.orderNumberType.length === 0) {
        return false
      }
      for (var i = 0; i < this.orderNumberType.length; i++) {
        if (this.orderNumberType[i].label === this.jumpQuery订单号) {
          this.queryForm.订单流水号 = this.orderNumberType[i].value
          this.jumpQuery订单号 = ''
          return true
        }
      }
      return false
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 获取制定工艺组件同源订单下拉
    getOrderNumberType() {
      this.orderNumberType = []
      var param = []
      var Data = this.CreateData('11', '精工车间_制定工艺_订单查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.orderNumberType.push({
            label: response.data[i].订单号,
            value: response.data[i].订单流水号
          })
        }
        // 修改人:Ld 修改时间:2026-07-17 16:30:55; 订单下拉异步加载完成后补充订单流水号，再按跳转条件查询
        if (this.setJumpOrderValue()) {
          if (this.jumpDirectQuery) {
            this.pageCurrent = 1
            this.searchTable()
          } else {
            this.jumpAutoCascade = true
            this.getproductName()
          }
        }
      })
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 订单切换后按制定工艺组件口径加载产品
    handleOrderChange() {
      this.queryForm.组件流水号 = ''
      this.queryForm.工艺计划流水号 = ''
      this.productName = []
      this.partName = []
      this.getproductName()
      this.pageCurrent = 1
      this.searchTable()
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 获取制定工艺组件同源产品下拉
    getproductName() {
      if (!this.queryForm.订单流水号) {
        return
      }
      this.productName = []
      var param = []
      param[0] = ['订单流水号', this.queryForm.订单流水号]
      var Data = this.CreateData('11', '订单信息_产品信息_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.productName.push({
            label: response.data[i].产品名称,
            value: response.data[i].组件流水号
          })
        }
        // 修改人:Ld 修改时间:2026-07-09; 跳转时自动选中第一个产品
        if (this.jumpAutoCascade && this.productName.length > 0) {
          this.queryForm.组件流水号 = this.productName[0].value
          this.getpartName()
        }
      })
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 产品切换后按制定工艺组件口径加载零件
    handleProductChange() {
      this.queryForm.工艺计划流水号 = ''
      this.partName = []
      this.getpartName()
      this.pageCurrent = 1
      this.searchTable()
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 获取制定工艺组件同源零件下拉
    getpartName() {
      if (!this.queryForm.组件流水号) {
        return
      }
      this.partName = []
      var param = []
      param[0] = ['组件流水号', this.queryForm.组件流水号]
      var Data = this.CreateData('11', '精工车间_制定工艺_零件名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.partName.push({
            label: response.data[i].零件名称,
            name: response.data[i].零件图号,
            value: response.data[i].工艺计划流水号
          })
        }
        // 修改人:Ld 修改时间:2026-07-09; 跳转时自动选中第一个零件并查询
        if (this.jumpAutoCascade && this.partName.length > 0) {
          this.queryForm.工艺计划流水号 = this.partName[0].value
          this.jumpAutoCascade = false
          this.pageCurrent = 1
          this.searchTable()
        }
      })
    },
    // 修改人:Ld 修改时间:2026-08-03 14:24:48; 查询和导出共用参数，保持筛选口径一致
    buildQueryParam() {
      var param = []
      param[0] = ['订单流水号', this.queryForm.订单流水号]
      param[1] = ['订单流水号_check', this.queryForm.订单流水号 ? 1 : 0]
      param[2] = ['组件流水号', this.queryForm.组件流水号]
      param[3] = ['组件流水号_check', this.queryForm.组件流水号 ? 1 : 0]
      param[4] = ['工艺计划流水号', this.queryForm.工艺计划流水号]
      param[5] = ['工艺计划流水号_check', this.queryForm.工艺计划流水号 ? 1 : 0]
      param[6] = ['工序名称', this.queryForm.工序名称]
      param[7] = ['工序名称_check', this.queryForm.工序名称 ? 1 : 0]
      param[8] = ['工艺名称', this.queryForm.工序名称]
      param[9] = ['工艺名称_check', this.queryForm.工序名称 ? 1 : 0]
      param[10] = ['是否启用', this.queryForm.是否启用]
      param[11] = ['是否启用_check', this.queryForm.是否启用 === '' || this.queryForm.是否启用 === null || typeof this.queryForm.是否启用 === 'undefined' ? 0 : 1]
      param[12] = ['零件名称', this.queryForm.零件名称]
      param[13] = ['零件名称_check', this.queryForm.零件名称 ? 1 : 0]
      // 修改人:Ld 修改时间:2026-07-17 08:44:08; 工时统计跳转后按图号精确查询，避免同名零件匹配过宽
      param[14] = ['图号', this.queryForm.图号]
      param[15] = ['图号_check', this.queryForm.图号 ? 1 : 0]
      return param
    },
    // 修改人:Ld 修改时间:2026-07-06 08:36:02
    // 查询列表
    searchTable() {
      this.loading = true
      var param = this.buildQueryParam()
      var Data = this.CreateData('11', '车间生产管理工艺_工序工价_查询数据', param, this.pageSize, this.pageCurrent)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data && response.data.rows ? response.data.rows : response.data
        this.total = response.data && response.data.total ? response.data.total : (this.tableData ? this.tableData.length : 0)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 修改人:Ld 修改时间:2026-08-03 14:24:48; 调用测试库工序工价导出过程，导出当前查询条件下的全部数据
    exportExcel() {
      var Data = this.CreateData('2001', '车间生产管理工艺_工序工价_导出表格_查询', this.buildQueryParam())
      this.exportExcel_NPOI(Data)
    },
    // 修改人:Ld 修改时间:2026-07-08; 保存当前零件的工价信息，供拷贝时直接使用
    handleSaveSource() {
      if (!this.queryForm.工艺计划流水号 || !this.tableData || this.tableData.length === 0) {
        this.$message.warning('当前没有可保存的零件数据')
        return
      }
      var firstRow = this.tableData[0]
      this.savedSourceInfo = {
        图号: firstRow.图号 || '',
        零件名称: firstRow.零件名称 || '',
        工艺计划流水号: this.queryForm.工艺计划流水号
      }
      this.$message.success('已保存「' + this.savedSourceInfo.零件名称 + '」的工价数据')
    },
    // 修改人:Ld 修改时间:2026-07-08; 打开工序工价拷贝（优先使用已保存数据）
    openCopyDialog() {
      if (!this.queryForm.工艺计划流水号) {
        this.$message.warning('请先选择要复制到的零件')
        return
      }
      if (this.savedSourceInfo && this.savedSourceInfo.图号) {
        this.$confirm(
          '检测到已保存的工价数据（来源零件：' + this.savedSourceInfo.零件名称 + '），是否直接使用？',
          '拷贝方式',
          { confirmButtonText: '使用已保存数据', cancelButtonText: '选择其他零件', type: 'info' }
        ).then(function() {
          this.submitCopyFromSaved()
        }.bind(this)).catch(function() {
          this.openSourcePartDialog()
        }.bind(this))
      } else {
        this.openSourcePartDialog()
      }
    },
    // 修改人:Ld 修改时间:2026-07-08; 使用已保存的源零件信息直接调用后端复制
    submitCopyFromSaved() {
      if (!this.savedSourceInfo || !this.savedSourceInfo.图号) {
        this.$message.warning('没有已保存的源零件数据')
        return
      }
      var param = []
      param[0] = ['目标工艺计划流水号', this.queryForm.工艺计划流水号]
      param[1] = ['源零件图号', this.savedSourceInfo.图号]
      param[2] = ['修改人', this.id]
      var Data = this.CreateData('12', '车间生产管理工艺_工序工价_复制数据', param)
      this.ExecDatabase(Data).then(function(response) {
        if (response.data && response.data[0] && response.data[0].result === '1') {
          this.$message.success('拷贝成功')
          this.searchTable()
        } else {
          this.$message.error(response.data && response.data[0] && response.data[0].message ? response.data[0].message : '拷贝失败')
        }
      }.bind(this))
    },
    // 修改人:Ld 修改时间:2026-07-08; 打开源零件选择弹窗（原 openCopyDialog 逻辑）
    openSourcePartDialog() {
      this.copyVisible = true
      this.copyForm = {
        零件筛选: '',
        源零件图号: ''
      }
      this.copyPartList = []
      this.copyProcessList = []
    },
    // 修改人:Ld 修改时间:2026-07-06 08:45:45
    // 参考制定工艺拷贝的源零件查询
    searchCopyParts() {
      this.copyPartList = []
      this.copyForm.源零件图号 = ''
      this.copyProcessList = []
      var param = []
      param[0] = ['零件图号_check', this.copyForm.零件筛选 ? 1 : 0]
      param[1] = ['零件图号', this.copyForm.零件筛选]
      var Data = this.CreateData('11', '精工车间_制定工艺_零件名称_查询_工艺拷贝', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.copyPartList.push({
            label: response.data[i].零件名称,
            name: response.data[i].零件图号,
            value: response.data[i].零件图号
          })
        }
      })
    },
    // 修改人:Ld 修改时间:2026-07-06 08:45:45
    // 源零件工序预览，仅显示零件名称、工序号和工艺名称
    searchCopyProcess() {
      this.copyProcessList = []
      if (!this.copyForm.源零件图号) {
        return
      }
      var param = []
      param[0] = ['零件图号', this.copyForm.源零件图号]
      var Data = this.CreateData('11', '车间生产管理工艺_工序工价_复制源_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        this.copyProcessList = response.data
      })
    },
    // 修改人:Ld 修改时间:2026-07-06 08:45:45
    // 将源零件已维护的工价按工序号和工艺名称复制到当前零件
    submitCopy() {
      if (!this.queryForm.工艺计划流水号) {
        this.$message.warning('请先选择要复制到的零件')
        return
      }
      if (!this.copyForm.源零件图号 || this.copyProcessList.length === 0) {
        this.$message.warning('请先选择源零件')
        return
      }
      var param = []
      param[0] = ['目标工艺计划流水号', this.queryForm.工艺计划流水号]
      param[1] = ['源零件图号', this.copyForm.源零件图号]
      param[2] = ['修改人', this.id]
      var Data = this.CreateData('12', '车间生产管理工艺_工序工价_复制数据', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data && response.data[0] && response.data[0].result === '1') {
          this.$message.success('拷贝成功')
          this.copyVisible = false
          this.searchTable()
        } else {
          this.$message.error(response.data && response.data[0] && response.data[0].message ? response.data[0].message : '拷贝失败')
        }
      })
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 打开编辑
    openEditDialog(row) {
      this.dialogMode = 'edit'
      this.dialogTitle = '设置工序工价'
      this.dialogFormVisible = true
      this.form = {
        工价流水号: row.工价流水号,
        工艺名称: row.工艺名称,
        工序名称: row.工序名称,
        零件名称: row.零件名称,
        图号: row.图号,
        机动时间s: Number(row.机动时间s || 0),
        辅助时间s: Number(row.辅助时间s || 0),
        总时间s: this.getTotalTime(row),
        '单装数量(件)': Number(row['单装数量(件)'] || 0),
        操作台数: Number(row.操作台数 || 0),
        '工作时间(时)': Number(row['工作时间(时)'] || 0),
        '宽放时间(时)': Number(row['宽放时间(时)'] || 0),
        '日工资(元)': Number(row['日工资(元)'] || 0),
        '标准产能(件/天)': Number(row['标准产能(件/天)'] || row['日产能(件)'] || 0),
        工价定额: Number(row.工价定额 || row['最终单价(元/件)'] || 0),
        '日产能(件)': Number(row['标准产能(件/天)'] || row['日产能(件)'] || 0),
        '最终单价(元/件)': Number(row.工价定额 || row['最终单价(元/件)'] || 0),
        生效日期: row.生效日期,
        变更次数: Number(row.变更次数 || 0),
        是否启用: row.是否启用 === '' || row.是否启用 === null || typeof row.是否启用 === 'undefined' ? 1 : Number(row.是否启用),
        备注: row.备注,
        工序流水号: row.工序流水号
      }
      this.refreshPriceFormula()
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 提交新增或编辑
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.refreshTotalTime()
        this.refreshPriceFormula()
        var param = []
        param[0] = ['工价流水号', this.form.工价流水号]
        param[1] = ['工艺名称', this.form.工艺名称]
        param[2] = ['工序名称', this.form.工序名称]
        param[3] = ['零件名称', this.form.零件名称]
        param[4] = ['图号', this.form.图号]
        param[5] = ['机动时间s', this.form.机动时间s]
        param[6] = ['辅助时间s', this.form.辅助时间s]
        param[7] = ['总时间s', this.getTotalTime(this.form)]
        // 修改人:Ld 修改时间:2026-07-03 08:02:29
        // 带括号字段提交给存储过程时使用安全参数名，查询返回仍保持原字段名
        param[8] = ['单装数量件', this.form['单装数量(件)']]
        param[9] = ['操作台数', this.form.操作台数]
        param[10] = ['日产能件', this.form['日产能(件)']]
        param[11] = ['最终单价元件', this.form['最终单价(元/件)']]
        param[12] = ['生效日期', this.form.生效日期]
        param[13] = ['变更次数', this.form.变更次数]
        param[14] = ['是否启用', this.form.是否启用]
        param[15] = ['备注', this.form.备注]
        param[16] = ['工序流水号', this.form.工序流水号]
        param[17] = ['修改人', this.id]
        var Data = this.CreateData('12', '车间生产管理工艺_工序工价_编辑数据', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data && response.data[0] && response.data[0].result === '1') {
            this.$message.success('设置成功')
            this.dialogFormVisible = false
            this.searchTable()
          } else {
            this.$message.error('设置失败')
          }
        })
      })
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 重置表单
    resetForm() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form = {
        工价流水号: '',
        工艺名称: '',
        工序名称: '',
        零件名称: '',
        图号: '',
        机动时间s: 0,
        辅助时间s: 0,
        总时间s: 0,
        '单装数量(件)': 0,
        操作台数: 0,
        '工作时间(时)': 0,
        '宽放时间(时)': 0,
        '日工资(元)': 0,
        '标准产能(件/天)': 0,
        工价定额: 0,
        '日产能(件)': 0,
        '最终单价(元/件)': 0,
        生效日期: '',
        变更次数: 0,
        是否启用: 1,
        备注: '',
        工序流水号: ''
      }
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 关闭弹窗
    closeDialog() {
      this.dialogFormVisible = false
      this.resetForm()
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 停用工价
    handleDisable(row) {
      this.$confirm('确定停用该工序工价吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['工价流水号', row.工价流水号]
        param[1] = ['修改人', this.id]
        var Data = this.CreateData('12', '车间生产管理工艺_工序工价_删除数据', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data && response.data[0] && response.data[0].result === '1') {
            this.$message.success('停用成功')
            this.searchTable()
          } else {
            this.$message.error('停用失败')
          }
        })
      })
    },
    // 修改人:Ld 修改时间:2026-07-06 09:00:29
    // 打开该零件该工序名称的历史定价
    openHistoryDialog(row) {
      this.historyVisible = true
      var param = []
      param[0] = ['零件名称_check', row.零件名称 ? 1 : 0]
      param[1] = ['零件名称', row.零件名称]
      param[2] = ['图号_check', row.图号 ? 1 : 0]
      param[3] = ['图号', row.图号]
      param[4] = ['工序名称_check', row.工序名称 ? 1 : 0]
      param[5] = ['工序名称', row.工序名称]
      var Data = this.CreateData('11', '车间生产管理工艺_工序工价_历史_查询数据', param)
      this.ExecDatabase(Data).then(response => {
        this.historyData = response.data
      })
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 分页条数变化
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    // 修改人:Ld 修改时间:2026-07-02 15:33:23
    // 页码变化
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  padding: 10px 0 12px;
}

.el-card {
  padding: 0;
}
</style>
