<template>
  <div class="dr-panel">

    <!-- 计划概览 -->
    <div class="panel-block">
      <div class="pb-header">
        <span class="pb-title">双机架计划</span>
        <el-tag v-if="plan" size="mini" type="success" style="margin-left:6px">已找到</el-tag>
        <el-tag v-else-if="loading" size="mini" type="info" style="margin-left:6px">查询中</el-tag>
        <el-tag v-else size="mini" type="warning" style="margin-left:6px">未找到</el-tag>
      </div>

      <template v-if="plan">
        <div class="plan-grid">
          <div class="pg-item">
            <span class="pg-label">计划号</span>
            <span class="pg-value plan-no">{{ plan.planNo }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">合金牌号</span>
            <span class="pg-value">{{ plan.alloyNo || '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">采料厚度</span>
            <span class="pg-value">{{ plan.inMatThick != null ? plan.inMatThick + ' mm' : '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">成品厚度</span>
            <span class="pg-value accent">{{ plan.outThick != null ? plan.outThick + ' mm' : '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">采料宽度</span>
            <span class="pg-value">{{ plan.inMatWidth != null ? plan.inMatWidth + ' mm' : '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">采料重量</span>
            <span class="pg-value">{{ plan.inMatWeight != null ? plan.inMatWeight + ' t' : '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">采料长度</span>
            <span class="pg-value">{{ plan.inMatLength != null ? plan.inMatLength + ' m' : '—' }}</span>
          </div>
          <div class="pg-item">
            <span class="pg-label">绑定方案</span>
            <span class="pg-value">{{ plan.recipeNo || '未绑定' }}</span>
          </div>
        </div>

        <!-- 写入整条计划数据 -->
        <el-button type="primary" size="small" style="width:100%;margin-top:10px"
          icon="el-icon-download" @click="fillFromPlan">
          写入计划数据（重量/宽度/长度/厚度）
        </el-button>
      </template>

      <div v-else-if="loading" class="pb-empty"><i class="el-icon-loading" /></div>
      <div v-else class="pb-empty">未找到关联的双机架计划</div>
    </div>

    <!-- 道次列表 -->
    <div class="panel-block" style="margin-top:12px" v-if="plan && passList.length > 0">
      <div class="pb-header">
        <span class="pb-title">道次明细</span>
        <span class="pb-sub">共 {{ passList.length }} 道次 · 点击行快捷写入成品厚度</span>
      </div>

      <el-table :data="passList" size="mini" border style="width:100%" max-height="300"
                highlight-current-row @row-click="fillFromPass">
        <el-table-column label="道次" prop="passNo" width="44" align="center" />
        <el-table-column label="入口厚(mm)" prop="inThick" width="78" align="right">
          <template slot-scope="{ row }">{{ row.inThick != null ? row.inThick : '—' }}</template>
        </el-table-column>
        <el-table-column label="出口厚(mm)" width="78" align="right">
          <template slot-scope="{ row }">
            <span class="pass-out-thick">{{ row.outThick != null ? row.outThick : '—' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="轧制力(kN)" prop="rollForce" min-width="72" align="right">
          <template slot-scope="{ row }">{{ row.rollForce != null ? row.rollForce : '—' }}</template>
        </el-table-column>
        <el-table-column label="" width="46" fixed="right" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="mini" @click.stop="fillFromPass(row)">写入</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pass-hint">↑ 通常选最后一道次的出口厚度作为成品实测厚度</div>
    </div>

    <!-- 无道次提示 -->
    <div class="panel-block" style="margin-top:12px"
         v-else-if="plan && plan.recipeId && passList.length === 0">
      <div class="pb-empty">该方案暂无道次数据</div>
    </div>

  </div>
</template>

<script>
import { getDrPlanByActionId } from '@/api/wms/drMill'

export default {
  name: 'DrMatchPanel',
  props: {
    actionId: { type: [String, Number], default: null }
  },
  data() {
    return {
      loading: false,
      plan: null,
    }
  },
  computed: {
    passList() {
      return (this.plan && this.plan.passList) || []
    }
  },
  watch: {
    actionId(val) {
      if (val) this.loadPlan(val)
    }
  },
  created() {
    if (this.actionId) this.loadPlan(this.actionId)
  },
  methods: {
    loadPlan(actionId) {
      this.plan = null
      this.loading = true
      getDrPlanByActionId(actionId)
        .then(res => { this.plan = res.data || null })
        .catch(() => {})
        .finally(() => { this.loading = false })
    },

    /** 写入计划整体数据：重量/宽度/长度/成品厚度 */
    fillFromPlan() {
      const p = this.plan
      this.$emit('fill', {
        outThick:    p.outThick,
        inMatWidth:  p.inMatWidth,
        inMatWeight: p.inMatWeight,
        inMatLength: p.inMatLength,
      })
      this.$message.success('已写入计划数据')
    },

    /** 写入某道次的出口厚度作为成品厚度 */
    fillFromPass(pass) {
      this.$emit('fill', {
        outThick:    pass.outThick,
        inMatWidth:  this.plan && this.plan.inMatWidth,
        inMatWeight: this.plan && this.plan.inMatWeight,
        inMatLength: this.plan && this.plan.inMatLength,
      })
      this.$message.success(`已写入第 ${pass.passNo} 道次厚度`)
    },
  }
}
</script>

<style scoped>
.dr-panel { font-size: 13px; }

.panel-block {
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 12px;
}

.pb-header {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f2f5;
}
.pb-title { font-weight: 600; color: #303133; font-size: 13px; }
.pb-sub   { font-size: 11px; color: #909399; margin-left: 8px; }

.plan-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
}
.pg-item { display: flex; flex-direction: column; }
.pg-label { font-size: 11px; color: #909399; }
.pg-value { font-size: 13px; color: #303133; font-weight: 500; }
.pg-value.accent { color: #409eff; }
.plan-no { font-family: 'Courier New', monospace; font-size: 12px; color: #606266; }

.pb-empty {
  text-align: center;
  color: #c0c4cc;
  padding: 20px 0;
  font-size: 12px;
}

.pass-out-thick {
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: #409eff;
}

.pass-hint {
  font-size: 11px;
  color: #909399;
  margin-top: 6px;
  text-align: center;
}
</style>
