<template>
  <div class="app-container roll-overview">

    <!-- 状态统计看板 -->
    <div class="stat-panel mb16">
      <div class="stat-item">
        <div class="stat-icon-wrap total-icon"><i class="el-icon-data-line" /></div>
        <div class="stat-body">
          <div class="stat-num">{{ stats.total || 0 }}</div>
          <div class="stat-label">轧辊总数</div>
        </div>
        <div class="stat-divider" />
      </div>
      <div class="stat-item">
        <div class="stat-icon-wrap online-icon"><i class="el-icon-circle-check" /></div>
        <div class="stat-body">
          <div class="stat-num online-num">{{ stats.Online || 0 }}</div>
          <div class="stat-label">在 线</div>
        </div>
        <div class="stat-divider" />
      </div>
      <div class="stat-item">
        <div class="stat-icon-wrap standby-icon"><i class="el-icon-time" /></div>
        <div class="stat-body">
          <div class="stat-num standby-num">{{ stats.Standby || 0 }}</div>
          <div class="stat-label">备 用</div>
        </div>
        <div class="stat-divider" />
      </div>
      <div class="stat-item">
        <div class="stat-icon-wrap offline-icon"><i class="el-icon-remove-outline" /></div>
        <div class="stat-body">
          <div class="stat-num offline-num">{{ stats.Offline || 0 }}</div>
          <div class="stat-label">离 线</div>
        </div>
        <div class="stat-divider" />
      </div>
      <div class="stat-item">
        <div class="stat-icon-wrap scrapped-icon"><i class="el-icon-warning-outline" /></div>
        <div class="stat-body">
          <div class="stat-num scrapped-num">{{ stats.Scrapped || 0 }}</div>
          <div class="stat-label">报 废</div>
        </div>
      </div>
    </div>

    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="轧辊编号" prop="rollNo">
        <el-input v-model="queryParams.rollNo" placeholder="请输入轧辊编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="辊型" prop="rollType">
        <el-select v-model="queryParams.rollType" placeholder="全部" clearable style="width:120px">
          <el-option label="工作辊 WR" value="WR" />
          <el-option label="支撑辊 BR" value="BR" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width:130px">
          <el-option label="在线" value="Online" />
          <el-option label="备用" value="Standby" />
          <el-option label="离线" value="Offline" />
          <el-option label="报废" value="Scrapped" />
        </el-select>
      </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-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <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="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-refresh" size="mini" @click="handleSyncStatus">同步在线状态</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 数据表格 -->
    <KLPTable v-loading="loading" :data="rollList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="轧辊编号" align="center" prop="rollNo" min-width="130">
        <template slot-scope="scope">
          <span class="roll-no-text">{{ scope.row.rollNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="辊型" align="center" prop="rollType" width="100">
        <template slot-scope="scope">
          <span :class="['roll-type-tag', scope.row.rollType === 'WR' ? 'wr-tag' : 'br-tag']">
            {{ scope.row.rollType === 'WR' ? '工作辊 WR' : '支撑辊 BR' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="scope">
          <span :class="['status-dot-wrap', 'status-' + scope.row.status]">
            <span class="status-dot" />
            {{ statusLabel(scope.row.status) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="初始直径(mm)" align="right" prop="initialDia" width="120" />
      <el-table-column label="当前直径(mm)" align="right" prop="currentDia" width="120">
        <template slot-scope="scope">
          <span :class="{ 'warn-text': isDiaLow(scope.row) }">{{ scope.row.currentDia }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最小直径(mm)" align="right" prop="minDia" width="120" />
      <el-table-column label="粗糙度" align="right" prop="roughness" width="90" />
      <el-table-column label="凸度" align="right" prop="crown" width="90" />
      <el-table-column label="材质" align="center" prop="material" width="100" />
      <el-table-column label="磨削次数" align="center" prop="grindCount" width="90" />
      <el-table-column label="累计轧制量(t)" align="right" prop="totalRolledWeight" width="130" />
      <el-table-column label="制造日期" align="center" prop="manufactureDate" width="110" />
      <el-table-column label="备注" align="left" prop="remark" min-width="120" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.status === 'Offline'" size="mini" type="text"
            class="scrap-btn" icon="el-icon-lock" @click="handleScrap(scope.row)">封闭</el-button>
          <el-button v-if="scope.row.status === 'Scrapped'" size="mini" type="text"
            class="del-btn" icon="el-icon-delete" @click="handleDelete(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="620px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="轧辊编号" prop="rollNo">
              <el-input v-model="form.rollNo" placeholder="请输入轧辊编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="辊型" prop="rollType">
              <el-select v-model="form.rollType" placeholder="请选择辊型" style="width:100%">
                <el-option label="工作辊 WR" value="WR" />
                <el-option label="支撑辊 BR" value="BR" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="初始直径(mm)" prop="initialDia">
              <el-input-number v-model="form.initialDia" :precision="2" :min="0" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前直径(mm)" prop="currentDia">
              <el-input-number v-model="form.currentDia" :precision="2" :min="0" style="width:100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="最小直径(mm)" prop="minDia">
              <el-input-number v-model="form.minDia" :precision="2" :min="0" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="材质" prop="material">
              <el-input v-model="form.material" placeholder="请输入材质" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="粗糙度" prop="roughness">
              <el-input-number v-model="form.roughness" :precision="4" :min="0" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="凸度" prop="crown">
              <el-input-number v-model="form.crown" :precision="4" style="width:100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="制造日期" prop="manufactureDate">
              <el-date-picker v-model="form.manufactureDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择制造日期" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!form.rollId">
            <el-form-item label="状态">
              <el-input value="离线" readonly style="width:100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listRollInfo, getRollStats, getRollInfo, addRollInfo, updateRollInfo, delRollInfo, scrapRollInfo, syncRollStatus } from '@/api/mes/roll/rollInfo'

export default {
  name: 'RollOverview',
  data() {
    return {
      loading: false,
      showSearch: true,
      single: true,
      multiple: true,
      total: 0,
      rollList: [],
      stats: {},
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        rollNo: undefined,
        rollType: undefined,
        status: undefined
      },
      form: {},
      rules: {
        rollNo:   [{ required: true, message: '轧辊编号不能为空', trigger: 'blur' }],
        rollType: [{ required: true, message: '请选择辊型', trigger: 'change' }]
      },
      ids: []
    }
  },
  created() {
    this.getStats()
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listRollInfo(this.queryParams).then(res => {
        this.rollList = res.rows
        this.total = res.total
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    getStats() {
      getRollStats().then(res => { this.stats = res.data || {} })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rollId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.title = '新增轧辊'
      this.open = true
    },
    handleUpdate(row) {
      this.reset()
      const rollId = row ? row.rollId : this.ids[0]
      getRollInfo(rollId).then(res => {
        this.form = res.data
        this.title = '修改轧辊'
        this.open = true
      })
    },
    handleDelete(row) {
      const rollIds = row ? [row.rollId] : this.ids
      this.$modal.confirm('确认删除该报废轧辊记录？此操作不可恢复。').then(() => {
        return delRollInfo(rollIds.join(','))
      }).then(() => {
        this.getList()
        this.getStats()
        this.$modal.msgSuccess('删除成功')
      })
    },
    handleScrap(row) {
      this.$modal.confirm('确认封闭轧辊【' + row.rollNo + '】？封闭后状态将变为报废，不可恢复。').then(() => {
        return scrapRollInfo(row.rollId)
      }).then(() => {
        this.getList()
        this.getStats()
        this.$modal.msgSuccess('封闭成功')
      })
    },
    handleSyncStatus() {
      this.$modal.confirm('将从换辊记录中重新推算各机架当前在机轧辊的状态，确认执行？').then(() => {
        return syncRollStatus()
      }).then(() => {
        this.getList()
        this.getStats()
        this.$modal.msgSuccess('状态同步完成')
      })
    },
    handleExport() {
      this.download('/mes/rollInfo/export', this.queryParams, '轧辊数据.xlsx')
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        const action = this.form.rollId ? updateRollInfo : addRollInfo
        action(this.form).then(() => {
          this.$modal.msgSuccess(this.form.rollId ? '修改成功' : '新增成功')
          this.open = false
          this.getList()
          this.getStats()
        })
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = { status: 'Offline' }
      this.resetForm('form')
    },
    statusLabel(status) {
      const map = { Online: '在线', Standby: '备用', Offline: '离线', Scrapped: '报废' }
      return map[status] || status
    },
    // 当前直径接近最小直径时高亮警告
    isDiaLow(row) {
      if (!row.currentDia || !row.minDia) return false
      return Number(row.currentDia) - Number(row.minDia) < 5
    }
  }
}
</script>

<style scoped>
/* ─── 调色板（金属灰工业风） ─────────────────────────────────── */
/* 主背景 #f4f5f7  卡片底 #fff  边框 #dcdee0  文字主 #1f2329  文字副 #646a73 */
/* 在线=深绿 #0a7c42  备用=深琥珀 #8b5c00  离线=钢灰 #5f6368  报废=深红 #a61c00 */

.roll-overview { background: #f4f5f7; }
.mb16 { margin-bottom: 16px; }

/* ─── 统计看板 ──────────────────────────────────────────────── */
.stat-panel {
  display: flex;
  background: #fff;
  border: 1px solid #dcdee0;
  border-radius: 4px;
  padding: 0;
  overflow: hidden;
}
.stat-item {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 18px 20px;
  gap: 14px;
  position: relative;
}
.stat-divider {
  position: absolute;
  right: 0; top: 16px; bottom: 16px;
  width: 1px;
  background: #e4e6ea;
}
.stat-icon-wrap {
  width: 42px; height: 42px;
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.total-icon    { background: #edf0f3; color: #3d4b5c; }
.online-icon   { background: #e8f5ef; color: #0a7c42; }
.standby-icon  { background: #fdf3e3; color: #8b5c00; }
.offline-icon  { background: #f0f1f2; color: #5f6368; }
.scrapped-icon { background: #fdecea; color: #a61c00; }

.stat-body { display: flex; flex-direction: column; }
.stat-num {
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
  color: #1f2329;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
}
.online-num   { color: #0a7c42; }
.standby-num  { color: #8b5c00; }
.offline-num  { color: #5f6368; }
.scrapped-num { color: #a61c00; }
.stat-label {
  font-size: 12px;
  color: #8f9099;
  margin-top: 4px;
  letter-spacing: 1px;
}

/* ─── 表格内样式 ─────────────────────────────────────────────── */
.roll-no-text {
  font-family: 'Consolas', 'Courier New', monospace;
  font-weight: 600;
  color: #1f2329;
  letter-spacing: .3px;
}

/* 辊型标签 */
.roll-type-tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 2px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .3px;
}
.wr-tag { background: #e8edf5; color: #2b4c8c; border: 1px solid #c5d0e8; }
.br-tag { background: #f0eae0; color: #6b4c1e; border: 1px solid #dccfb8; }

/* 状态指示 */
.status-dot-wrap {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 500;
}
.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.status-Online   { color: #0a7c42; }
.status-Online   .status-dot { background: #0a7c42; box-shadow: 0 0 0 2px #c8ead8; }
.status-Standby  { color: #8b5c00; }
.status-Standby  .status-dot { background: #d4860a; box-shadow: 0 0 0 2px #f5e2b8; }
.status-Offline  { color: #5f6368; }
.status-Offline  .status-dot { background: #9aa0a6; box-shadow: 0 0 0 2px #e0e2e4; }
.status-Scrapped { color: #a61c00; }
.status-Scrapped .status-dot { background: #c5221f; box-shadow: 0 0 0 2px #f5c6c4; }

/* 直径接近下限警告 */
.warn-text { color: #a61c00; font-weight: 600; }

/* 封闭按钮 */
.scrap-btn { color: #8b5c00 !important; }
/* 删除按钮单独着色 */
.del-btn { color: #c5221f !important; }
</style>
