<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" class="query-form">
      <el-form-item label="任务单号">
        <el-input v-model="searchData.taskNo" clearable placeholder="请输入任务单号" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="任务类型">
        <el-select v-model="searchData.taskType" clearable placeholder="全部" style="width: 120px">
          <el-option label="线缆自制" value="线缆自制"></el-option>
          <el-option label="COP自制" value="COP自制"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源项目号">
        <el-input v-model="searchData.sourceProjectNo" clearable placeholder="请输入项目号" style="width: 140px"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchData.status" clearable placeholder="全部" style="width: 120px">
          <el-option label="已排产" value="已排产"></el-option>
          <el-option label="进行中" value="进行中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计划完成日期">
        <el-date-picker v-model="searchData.planStartDate" type="date" value-format="yyyy-MM-dd" placeholder="开始" style="width: 130px"></el-date-picker>
      </el-form-item>
      <el-form-item label="至">
        <el-date-picker v-model="searchData.planEndDate" type="date" value-format="yyyy-MM-dd" placeholder="结束" style="width: 130px"></el-date-picker>
      </el-form-item>
      <el-form-item label=" " style="margin-top: -11px">
        <el-button @click="searchTable('Y')" plain class="search-btn">查询</el-button>
        <el-button @click="resetQuery()" plain class="reset-btn">重置</el-button>
        <el-button @click="openEditDialog()" plain class="add-btn">新增任务单</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="orderTable"
      class="data-table"
      :data="dataList"
      :height="tableHeight"
      border
      highlight-current-row
      v-loading="dataListLoading"
      style="width: 100%"
      @current-change="onCurrentRowChange">
      <el-table-column type="index" label="#" width="50" align="center"></el-table-column>
      <el-table-column prop="taskNo" label="任务单号" width="140" align="center"></el-table-column>
      <el-table-column prop="taskType" label="任务类型" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.taskType === '线缆自制' ? 'primary' : 'success'" size="small">{{ scope.row.taskType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sourceProjectNo" label="来源项目号" width="120" align="center"></el-table-column>
      <el-table-column prop="modelNo" label="型号" width="110" align="center"></el-table-column>
      <el-table-column prop="taskQty" label="任务数量" width="90" align="center"></el-table-column>
      <el-table-column prop="reportQty" label="累计报工数量" width="110" align="center"></el-table-column>
      <el-table-column prop="currentNode" label="当前节点" width="110" align="center"></el-table-column>
      <el-table-column prop="assigneeSummary" label="节点负责人" min-width="160" show-overflow-tooltip></el-table-column>
      <el-table-column label="节点进度" width="100" align="center">
        <template slot-scope="scope">
          <el-tag size="small" type="info">{{ scope.row.nodeDoneCount }}/{{ scope.row.nodeTotalCount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="90" align="center">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)" size="small">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="planFinishDate" label="计划完成日期" width="110" align="center"></el-table-column>
      <el-table-column prop="finishDate" label="完工日期" width="110" align="center">
        <template slot-scope="scope">{{ scope.row.finishDate || '-' }}</template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <a type="text" @click="openEditDialog(scope.row)"  v-if="scope.row.status !== '已完成'">修改</a>
          <a type="text" @click="openAssignDialog(scope.row)"  v-if="scope.row.status !== '已完成'">分配人员</a>
          <a type="text" @click="finishTask(scope.row)"  v-if="scope.row.status !== '已完成'">完工</a>
          <a type="text" style="color:#F56C6C" @click="deleteTask(scope.row)"  v-if="scope.row.status !== '已完成'">删除</a>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: right">
    </el-pagination>

    <div class="detail-tabs-wrap">
      <el-tabs v-model="detailTabName" type="border-card">
        <el-tab-pane label="节点状态和日志" name="statusLogs">
          <div v-if="selectedOrder.orderNo" class="two-column-layout">
            <div class="stages-column">
              <div class="column-header">
                <i class="el-icon-s-order"></i>
                <span>节点流程</span>
                <span class="progress-badge">{{ selectedOrderProgressPercent }}%</span>
              </div>
              <div class="stages-list">
                <div
                  v-for="(stage, index) in selectedOrderNodeList"
                  :key="stage.nodeCode || index"
                  class="stage-item"
                  :class="'stage-' + getStageClass(stage.status)">
                  <div class="stage-icon">
                    <i :class="getStageIcon(stage.status)"></i>
                  </div>
                  <div class="stage-content">
                    <div class="stage-name">{{ stage.nodeName }}</div>
                    <div class="stage-meta">
                      <el-tag :type="getStageTagType(stage.status)" size="mini" effect="plain">{{ stage.status || '未开始' }}</el-tag>
                      <span class="stage-owner">负责人：{{ stage.assigneeUserName || '-' }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="logs-column">
              <div class="column-header">
                <i class="el-icon-tickets"></i>
                <span>操作日志</span>
                <span class="logs-count">{{ selectedOrderLogList.length }}条</span>
              </div>
              <div class="logs-table-wrapper">
                <el-table :data="selectedOrderLogList" border size="small" class="detail-table" v-loading="detailLogLoading" height="300px">
                  <el-table-column prop="logTime" label="时间" width="160" align="center"></el-table-column>
                  <el-table-column prop="action" label="操作" width="95" align="center"></el-table-column>
                  <el-table-column prop="nodeName" label="节点" width="130" align="center">
                    <template slot-scope="scope">{{ scope.row.nodeName || scope.row.nodeCode || '-' }}</template>
                  </el-table-column>
                  <el-table-column prop="operatorName" label="操作人" width="100" align="center"></el-table-column>
                  <el-table-column prop="comment" label="备注" min-width="220" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.comment || '-' }}</template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
          <el-empty v-else description="请先选择一条任务记录"></el-empty>
        </el-tab-pane>
      </el-tabs>
    </div>

    <el-dialog :title="saveHeaderData.orderNo ? '修改线缆/COP自制任务单' : '新增线缆/COP自制任务单'" :visible.sync="setUp.reviewFlag" width="550px" :close-on-click-modal="false" v-drag>
      <el-form
        ref="editForm"
        :model="saveHeaderData"
        label-position="top"
        class="edit-form">
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="任务单号" required><el-input v-model="saveHeaderData.taskNo"></el-input></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="任务类型" required><el-select v-model="saveHeaderData.taskType" style="width: 100%"><el-option label="线缆自制" value="线缆自制"></el-option><el-option label="COP自制" value="COP自制"></el-option></el-select></el-form-item></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="来源项目号"><el-input v-model="saveHeaderData.sourceProjectNo"></el-input></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="型号"><el-input v-model="saveHeaderData.modelNo"></el-input></el-form-item></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"><el-form-item label="任务数量"><el-input v-model="saveHeaderData.taskQty" :min="1" :max="9999" style="width: 100%"></el-input></el-form-item></el-col>
          <el-col :span="12"><el-form-item label="计划完成日期"><el-date-picker v-model="saveHeaderData.planFinishDate" type="date" value-format="yyyy-MM-dd" style="width: 100%"></el-date-picker></el-form-item></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item>
              <template slot="label">
                人员分配策略
                <el-tooltip effect="dark" placement="top">
                  <div slot="content">
                    默认分配：创建订单后，系统会按节点角色自动分配该角色下全部人员。<br>
                    手动分配：创建订单后不自动分配，需要在“分配人员”里手工选择负责人。
                  </div>
                  <i class="el-icon-question" style="margin-left:6px;color:#909399;cursor:pointer;"></i>
                </el-tooltip>
              </template>
              <el-radio-group v-model="saveHeaderData.autoAssignAllUsers">
                <el-radio :label="true">默认分配</el-radio>
                <el-radio :label="false">手动分配</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="节点报工模式">
              <el-radio-group v-model="saveHeaderData.nodeReportMode">
                <el-radio label="PARALLEL">并行</el-radio>
                <el-radio label="SEQUENTIAL">按顺序</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-footer style="height: 40px; margin-top: 50px; text-align: center">
        <el-button plain class="reset-btn" @click="setUp.reviewFlag = false">取消</el-button>
        <el-button plain class="add-btn" :loading="setUp.saveButton" @click="saveTask">保存</el-button>
      </el-footer>
    </el-dialog>

    <el-dialog title="车间节点报工" :visible.sync="setUp.reportFlag" width="460px" :close-on-click-modal="false" v-drag>
      <el-form :model="reportData" label-width="110px" label-position="top">
        <el-form-item label="任务单号"><el-input v-model="reportData.taskNo" disabled></el-input></el-form-item>
        <el-form-item label="报工节点">
          <el-select v-model="reportData.nodeCode" placeholder="请选择节点" style="width: 100%">
            <el-option v-for="item in reportNodeOptions" :key="item.nodeCode" :label="item.nodeName" :value="item.nodeCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="本次报工数量"><el-input v-model="reportData.reportQty" :min="1" :max="9999" style="width: 100%"></el-input></el-form-item>
        <el-form-item label="报工备注"><el-input v-model="reportData.remark" type="textarea" :rows="2"></el-input></el-form-item>
      </el-form>
      <el-footer style="height: 40px; margin-top: 50px; text-align: center">
        <el-button plain class="reset-btn" @click="setUp.reportFlag = false">取消</el-button>
        <el-button plain class="search-btn" :loading="setUp.reportButton" @click="submitNodeReport">提交报工</el-button>
      </el-footer>
    </el-dialog>
    <el-dialog title="节点负责人分配" :visible.sync="setUp.assignFlag" width="500px" :close-on-click-modal="false" v-drag>
      <el-table :data="assignNodeList" border class="data-table">
        <el-table-column prop="nodeName" label="节点" width="180"></el-table-column>
        <el-table-column label="负责人">
          <template slot-scope="scope">
            <el-select v-model="scope.row.assigneeUserIdList" filterable clearable multiple collapse-tags placeholder="请选择负责人" style="width: 100%">
              <el-option v-for="user in scope.row.userOptions" :key="user.userId" :label="user.displayName || user.username" :value="user.userId"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <el-footer style="height: 40px; margin-top: 20px; text-align: center">
        <el-button plain class="reset-btn" @click="setUp.assignFlag = false">取消</el-button>
        <el-button plain class="add-btn" :loading="setUp.assignButton" @click="saveNodeAssigneeAction">保存分配</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
import { deleteCableCopTask, finishCableCopTask, getCableCopTaskList, getNodeAssigneeList, getNodeAssigneeUsers, getReportLogList, reportCableCopTaskNode, saveCableCopTask, saveNodeAssignee } from '@/api/longchuang/productionPlan'

export default {
  name: 'ProductionPlanCableCopTask',
  data() {
    return {
      searchData: { taskNo: '', taskType: '', sourceProjectNo: '', status: '', planStartDate: '', planEndDate: '', page: 1, limit: 20 },
      saveHeaderData: {},
      reportData: { orderNo: '', taskNo: '', nodeCode: '', reportQty: 1, remark: '' },
      reportNodeOptions: [],
      setUp: { reviewFlag: false, reportFlag: false, assignFlag: false, saveButton: false, reportButton: false, assignButton: false },
      dataList: [],
      currentAssignOrder: { orderNo: '', orderType: 'CABLE_COP' },
      assignNodeList: [],
      selectedOrder: {},
      detailTabName: 'statusLogs',
      selectedOrderLogList: [],
      detailLogLoading: false,
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      dataListLoading: false,
      tableHeight: (window.innerHeight - 320) / 2
    }
  },
  activated() {
    this.searchTable()
  },
  methods: {
    searchTable(flag) {
      if (flag === 'Y') this.pageIndex = 1
      this.searchData.page = this.pageIndex
      this.searchData.limit = this.pageSize
      this.dataListLoading = true
      getCableCopTaskList(this.searchData).then(({data}) => {
        this.dataListLoading = false
        if (data && data.code === 0) {
          this.dataList = (data.page.list || []).map(this.normalizeRow)
          this.totalPage = data.page.totalCount || 0
          this.syncSelectedOrder()
        } else this.loadMockData()
      }).catch(() => {
        this.dataListLoading = false
        this.loadMockData()
      })
    },
    normalizeRow(row) {
      const list = row.nodeList || []
      const done = list.filter(item => item.status === '已完成').length
      const currentNode = (list.find(item => item.status !== '已完成') || {}).nodeName || '全部完成'
      const assigneeSummary = list.filter(item => item.assigneeUserName).map(item => `${item.nodeName}:${item.assigneeUserName}`).join('；')
      return { ...row, autoAssignAllUsers: !!row.autoAssignAllUsers, nodeReportMode: row.nodeReportMode || 'PARALLEL', nodeList: list, nodeDoneCount: done, nodeTotalCount: list.length, currentNode: row.currentNode || currentNode, reportQty: row.reportQty || 0, assigneeSummary: assigneeSummary || '-' }
    },
    loadMockData() {
      this.dataList = [
        {
          orderNo: 'MOCK-CABLE-001',
          taskNo: 'TASK-XL-202604-001',
          taskType: '线缆自制',
          sourceProjectNo: 'LC-202604-001',
          modelNo: 'LC-HOME-820',
          taskQty: 6,
          reportQty: 2,
          autoAssignAllUsers: true,
          nodeReportMode: 'PARALLEL',
          planFinishDate: '2026-04-20',
          finishDate: '',
          status: '进行中',
          nodeList: [
            { nodeCode: 'lineProduction', nodeName: '线缆生产', status: '已完成' },
            { nodeCode: 'copProduction', nodeName: 'COP生产', status: '进行中' }
          ]
        },
        {
          orderNo: 'MOCK-CABLE-002',
          taskNo: 'TASK-COP-202604-002',
          taskType: 'COP自制',
          sourceProjectNo: 'LC-202604-002',
          modelNo: 'LC-HOME-1050',
          taskQty: 8,
          reportQty: 0,
          autoAssignAllUsers: true,
          nodeReportMode: 'PARALLEL',
          planFinishDate: '2026-04-22',
          finishDate: '',
          status: '已排产',
          nodeList: [
            { nodeCode: 'lineProduction', nodeName: '线缆生产', status: '未开始' },
            { nodeCode: 'copProduction', nodeName: 'COP生产', status: '未开始' }
          ]
        }
      ].map(this.normalizeRow)
      this.totalPage = this.dataList.length
      this.syncSelectedOrder()
    },
    syncSelectedOrder() {
      if (!this.dataList.length) {
        this.selectedOrder = {}
        this.selectedOrderLogList = []
        return
      }
      const selectedOrderNo = this.selectedOrder && this.selectedOrder.orderNo
      const matched = selectedOrderNo ? this.dataList.find(item => item.orderNo === selectedOrderNo) : null
      const current = matched || this.dataList[0]
      this.selectedOrder = current
      this.$nextTick(() => {
        if (this.$refs.orderTable && current) {
          this.$refs.orderTable.setCurrentRow(current)
        }
      })
      this.loadSelectedOrderLogList(current)
    },
    onCurrentRowChange(row) {
      if (!row || !row.orderNo) {
        this.selectedOrder = {}
        this.selectedOrderLogList = []
        return
      }
      this.selectedOrder = row
      this.loadSelectedOrderLogList(row)
    },
    loadSelectedOrderLogList(row) {
      if (!row || !row.orderNo) {
        this.selectedOrderLogList = []
        return
      }
      this.detailLogLoading = true
      getReportLogList({ orderNo: row.orderNo, orderType: 'CABLE_COP' }).then(({ data }) => {
        this.detailLogLoading = false
        this.selectedOrderLogList = data && data.code === 0 ? (data.rows || []) : []
      }).catch(() => {
        this.detailLogLoading = false
        this.selectedOrderLogList = []
      })
    },
    resetQuery() {
      this.searchData = { taskNo: '', taskType: '', sourceProjectNo: '', status: '', planStartDate: '', planEndDate: '', page: 1, limit: 20 }
      this.searchTable('Y')
    },
    openEditDialog(row) {
      this.saveHeaderData = row ? { ...row, autoAssignAllUsers: !!row.autoAssignAllUsers, nodeReportMode: row.nodeReportMode || 'PARALLEL' } : { orderNo: '', taskNo: '', taskType: '线缆自制', sourceProjectNo: '', modelNo: '', taskQty: 1, reportQty: 0, planFinishDate: '', status: '已排产', autoAssignAllUsers: true, nodeReportMode: 'PARALLEL', nodeList: [] }
      this.setUp.reviewFlag = true
    },
    saveTask() {
      if (!this.saveHeaderData.taskNo || !this.saveHeaderData.taskType) return this.$message.warning('请先填写任务单号和任务类型')
      this.saveHeaderData.projectNo = this.saveHeaderData.taskNo
      this.setUp.saveButton = true
      saveCableCopTask(this.saveHeaderData).then(({data}) => {
        this.setUp.saveButton = false
        if (data && data.code === 0) {
          this.$message.success(data.msg || '保存成功')
          this.setUp.reviewFlag = false
          this.searchTable()
        } else this.$message.error(data.msg || '保存失败')
      }).catch(() => {
        this.setUp.saveButton = false
        const orderNo = this.saveHeaderData.orderNo || String(Date.now())
        const index = this.dataList.findIndex(item => item.orderNo === orderNo)
        const saveData = this.normalizeRow({
          ...this.saveHeaderData,
          orderNo: orderNo,
          nodeList: this.saveHeaderData.nodeList && this.saveHeaderData.nodeList.length ? this.saveHeaderData.nodeList : [
            { nodeCode: 'lineProduction', nodeName: '线缆生产', status: '未开始' },
            { nodeCode: 'copProduction', nodeName: 'COP生产', status: '未开始' }
          ]
        })
        if (index > -1) this.$set(this.dataList, index, saveData)
        else this.dataList.unshift(saveData)
        this.totalPage = this.dataList.length
        this.setUp.reviewFlag = false
        this.$message.success('后端未完成，已在前端演示保存')
      })
    },
    openReportDialog(row) {
      this.reportNodeOptions = (row.nodeList || []).filter(item => item.status !== '已完成')
      this.reportData = { orderNo: row.orderNo, taskNo: row.taskNo, nodeCode: '', reportQty: 1, remark: '' }
      this.setUp.reportFlag = true
    },
    openAssignDialog(row) {
      if (!row.orderNo) return this.$message.warning('请先保存任务单后再分配人员')
      this.currentAssignOrder = { orderNo: row.orderNo, orderType: 'CABLE_COP' }
      getNodeAssigneeList({ orderNo: row.orderNo, orderType: 'CABLE_COP' }).then(({ data }) => {
        const assignRows = data && data.code === 0 ? (data.rows || []) : []
        this.assignNodeList = assignRows.map(item => ({ ...item, assigneeUserIdList: item.assigneeUserIdList || [], userOptions: [] }))
        const requests = this.assignNodeList.map(item =>
          getNodeAssigneeUsers({ orderType: 'CABLE_COP', nodeCode: item.nodeCode }).then(({ data: userData }) => {
            item.userOptions = userData && userData.code === 0 ? (userData.rows || []) : []
          }).catch(() => { item.userOptions = [] })
        )
        Promise.all(requests).finally(() => { this.setUp.assignFlag = true })
      }).catch(() => {
        this.$message.error('加载节点分配信息失败')
      })
    },
    saveNodeAssigneeAction() {
      this.setUp.assignButton = true
      const assigneeList = this.assignNodeList.map(item => ({
        nodeCode: item.nodeCode,
        nodeName: item.nodeName,
        assigneeUserIdList: item.assigneeUserIdList || []
      }))
      saveNodeAssignee({
        orderNo: this.currentAssignOrder.orderNo,
        orderType: this.currentAssignOrder.orderType,
        assigneeList: assigneeList
      }).then(({ data }) => {
        this.setUp.assignButton = false
        if (data && data.code === 0) {
          this.$message.success(data.msg || '分配成功')
          this.setUp.assignFlag = false
          this.searchTable()
        } else this.$message.error(data.msg || '分配失败')
      }).catch(() => {
        this.setUp.assignButton = false
        this.$message.error('分配失败')
      })
    },
    submitNodeReport() {
      if (!this.reportData.nodeCode) return this.$message.warning('请选择报工节点')
      if (!this.reportData.reportQty || this.reportData.reportQty <= 0) return this.$message.warning('请输入有效报工数量')
      this.setUp.reportButton = true
      reportCableCopTaskNode(this.reportData).then(({data}) => {
        this.setUp.reportButton = false
        if (data && data.code === 0) {
          this.$message.success(data.msg || '报工成功')
          this.setUp.reportFlag = false
          this.searchTable()
        } else this.$message.error(data.msg || '报工失败')
      }).catch(() => {
        this.setUp.reportButton = false
        this.simulateNodeReport(this.reportData.orderNo, this.reportData.nodeCode, this.reportData.reportQty)
        this.setUp.reportFlag = false
        this.$message.success('后端未完成，已在前端演示节点报工')
      })
    },
    simulateNodeReport(orderNo, nodeCode, qty) {
      const row = this.dataList.find(item => item.orderNo === orderNo)
      if (!row) return
      const node = row.nodeList.find(item => item.nodeCode === nodeCode)
      if (!node) return
      node.status = '已完成'
      row.reportQty = Number(row.reportQty || 0) + Number(qty || 0)
      row.status = '进行中'
      const nextNode = row.nodeList.find(item => item.status !== '已完成')
      row.currentNode = nextNode ? nextNode.nodeName : '全部完成'
      row.nodeDoneCount = row.nodeList.filter(item => item.status === '已完成').length
      if (!nextNode) {
        row.status = '已完成'
        row.finishDate = this.dayjs().format('YYYY-MM-DD')
      }
    },
    finishTask(row) {
      finishCableCopTask({ orderNo: row.orderNo }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success(data.msg || '完工成功')
          this.searchTable()
        } else this.$message.error(data.msg || '完工失败')
      }).catch(() => {
        row.status = '已完成'
        row.finishDate = this.dayjs().format('YYYY-MM-DD')
        row.reportQty = row.taskQty
        row.nodeList = row.nodeList.map(item => ({ ...item, status: '已完成' }))
        row.currentNode = '全部完成'
        row.nodeDoneCount = row.nodeTotalCount
        this.$message.success('后端未完成，已在前端演示完工')
      })
    },
    deleteTask(row) {
      this.$confirm('确定删除该任务单吗？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        deleteCableCopTask({ orderNo: row.orderNo }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg || '删除成功')
            this.searchTable()
          } else this.$message.error(data.msg || '删除失败')
        }).catch(() => {
          this.dataList = this.dataList.filter(item => item.orderNo !== row.orderNo)
          this.totalPage = this.dataList.length
          this.$message.success('后端未完成，已在前端演示删除')
        })
      }).catch(() => {})
    },
    getStatusType(status) {
      const map = { 已排产: 'info', 进行中: 'warning', 已完成: 'success' }
      return map[status] || 'info'
    },
    getStageClass(status) {
      if (status === '已完成') return 'done'
      if (status === '进行中') return 'processing'
      return 'pending'
    },
    getStageIcon(status) {
      if (status === '已完成') return 'el-icon-check'
      if (status === '进行中') return 'el-icon-loading'
      return 'el-icon-time'
    },
    getStageTagType(status) {
      if (status === '已完成') return 'success'
      if (status === '进行中') return 'warning'
      return 'info'
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.searchTable()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.searchTable()
    }
  },
  computed: {
    selectedOrderNodeList() {
      return (this.selectedOrder && this.selectedOrder.nodeList) ? this.selectedOrder.nodeList : []
    },
    selectedOrderProgressPercent() {
      const total = this.selectedOrderNodeList.length
      if (!total) {
        return 0
      }
      const done = this.selectedOrderNodeList.filter(item => item.status === '已完成').length
      return Math.round((done * 100) / total)
    }
  }
}
</script>

<style scoped>
.data-table {
  background-color: #fff;
  border-radius: 4px;
}
.data-table >>> .cell {
  line-height: 20px;
  height: 20px;
}
.data-table >>> .el-table__header-wrapper th,
.data-table >>> .el-table__fixed-header-wrapper th {
  background-color: #f5f7fa !important;
  color: #333;
  font-weight: 600;
  border-color: #ebeef5;
  padding: 8px 0;
}

.data-table >>> .el-table__header-wrapper .cell,
.data-table >>> .el-table__fixed-header-wrapper .cell,
.data-table >>> .el-table__body-wrapper .cell,
.data-table >>> .el-table__fixed-body-wrapper .cell {
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px !important;
}

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

.data-table >>> .el-table__body tr.current-row > td {
  background-color: #ecf5ff !important;
}

.query-form {
  background-color: #fff;
  padding: 15px 15px 5px 15px;
  border-radius: 4px;
  margin-bottom: 12px;
}

.query-form >>> .el-form-item__label {
  color: #333;
  font-size: 13px;
  padding-bottom: 5px;
}

.query-form >>> .el-input__inner {
  height: 32px;
  line-height: 32px;
  border-radius: 4px;
  font-size: 13px;
}

.query-form >>> .el-button {
  height: 32px;
  padding: 0 15px;
  font-size: 13px;
  border-radius: 4px;
}

.search-btn {
  background-color: #ecf5ff;
  border-color: #b3d8ff;
  color: #409eff;
}

.search-btn:hover {
  background-color: #409eff;
  border-color: #409eff;
  color: #fff;
}

.reset-btn {
  background-color: #f5f7fa;
  border-color: #d3d4d6;
  color: #606266;
}

.reset-btn:hover {
  background-color: #909399;
  border-color: #909399;
  color: #fff;
}

.add-btn {
  background-color: #f0f9eb;
  border-color: #c2e7b0;
  color: #67c23a;
}

.add-btn:hover {
  background-color: #67c23a;
  border-color: #67c23a;
  color: #fff;
}

.dialog-footer {
  text-align: right;
}

.edit-form {
  margin-left: 5px;
  margin-top: -5px;
}

.detail-tabs-wrap {
  margin-top: 12px;
  background-color: #fff;
  border-radius: 4px;
}

.detail-table {
  width: 100%;
}

.detail-table >>> .el-table__header-wrapper th,
.detail-table >>> .el-table__fixed-header-wrapper th {
  background-color: #f5f7fa !important;
  color: #333;
  font-weight: 600;
  border-color: #ebeef5;
}

.two-column-layout {
  display: flex;
  gap: 12px;
}

.stages-column {
  width: 38%;
  min-width: 320px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

.logs-column {
  flex: 1;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fff;
}

.column-header {
  height: 40px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  border-bottom: 1px solid #ebeef5;
  font-weight: 600;
  color: #303133;
}

.progress-badge,
.logs-count {
  margin-left: auto;
  color: #409eff;
  font-size: 12px;
  font-weight: 500;
}

.stages-list {
  max-height: 300px;
  overflow-y: auto;
  padding: 10px;
}

.stage-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 4px 8px;
  border-radius: 4px;
}

.stage-item + .stage-item {
  margin-top: 2px;
}

.stage-item.stage-done {
  background: #f0f9eb;
}

.stage-item.stage-processing {
  background: #fdf6ec;
}

.stage-item.stage-pending {
  background: #f5f7fa;
}

.stage-icon {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
}

.stage-content {
  flex: 1;
}

.stage-name {
  font-size: 13px;
  color: #303133;
  line-height: 20px;
}

.stage-meta {
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stage-owner {
  color: #606266;
  font-size: 12px;
}

.logs-table-wrapper {
  padding: 8px;
}
</style>
