<template>
  <div class="rm-container">
    <div class="current-project-bar">当前项目：{{ currentProjectName }}</div>
    <div class="rm-panel">
      <div class="rm-panel-header">
        <span>📖 设备说明书和图纸</span>
        <el-button size="small" type="primary" @click="handleAdd">+ 上传说明书/图纸</el-button>
      </div>
      <div class="rm-panel-body">
        <el-table :data="list" v-loading="loading" stripe border highlight-current-row size="small">
          <el-table-column type="index" label="#" width="45" />
          <el-table-column prop="manualName" label="文件名称" min-width="160" />
          <el-table-column prop="docType" label="类型" width="100" align="center">
            <template slot-scope="s">
              <el-tag size="mini" :type="tagType(s.row.docType)">{{ s.row.docType }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="version" label="版本" width="70" align="center" />
          <el-table-column prop="uploadDate" label="上传日期" width="100" align="center" />
          <el-table-column label="操作" width="120">
            <template slot-scope="s">
              <el-button type="text" size="mini" @click="handleEdit(s.row)">编辑</el-button>
              <el-button type="text" size="mini" @click="handleDelete(s.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px" append-to-body @closed="onClosed">
      <el-form ref="form" :model="form" :rules="rules" label-width="0" size="small">
        <div class="form-group"><label>文件名称</label><el-input v-model="form.manualName" placeholder="如：1380mm轧机操作说明书" /></div>
        <div class="form-row">
          <div class="form-group"><label>类型</label>
            <el-select v-model="form.docType" style="width:100%;">
              <el-option label="说明书" value="说明书" />
              <el-option label="图纸" value="图纸" />
              <el-option label="维护手册" value="维护手册" />
              <el-option label="备件清单" value="备件清单" />
            </el-select>
          </div>
          <div class="form-group"><label>版本</label><el-input v-model="form.version" placeholder="V1.0" /></div>
        </div>
        <div class="form-group"><label>上传日期</label><el-input v-model="form.uploadDate" placeholder="yyyy-MM-dd" /></div>
        <div class="form-group"><label>文件链接/路径</label><el-input v-model="form.fileUrl" placeholder="文件URL或路径" /></div>
        <div class="form-group"><label>描述</label><el-input v-model="form.description" type="textarea" :rows="2" /></div>
      </el-form>
      <div slot="footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManualAll, addManual, updateManual, delManual } from '@/api/rm/manual'
import { listProject } from '@/api/rm/project'

export default {
  name: 'RmManual',
  data() {
    const today = new Date().toISOString().slice(0, 10)
    return {
      currentProjectName: this.$route.query.projectName || sessionStorage.getItem('rm_current_project_name') || '',
      loading: false,
      list: [],
      currentProjectId: null,
      dialogVisible: false,
      dialogTitle: '',
      form: { manualName: '', docType: '说明书', version: 'V1.0', uploadDate: today, fileUrl: '', description: '' },
      rules: { manualName: [{ required: true, message: '请填写文件名称', trigger: 'blur' }] }
    }
  },
  created() { this.loadCurrentProject() },
  methods: {
    tagType(type) {
      return { '说明书': 'primary', '图纸': 'success', '维护手册': 'warning', '备件清单': 'info' }[type] || ''
    },
    loadCurrentProject() {
      const pid = this.$route.query.projectId || sessionStorage.getItem('rm_current_project_id')
      if (pid) {
        this.currentProjectId = pid
        this.loadData()
      } else {
        listProject({ pageNum: 1, pageSize: 1 }).then(res => {
          const rows = res.rows || []
          if (rows.length > 0) { this.currentProjectId = rows[0].projectId; this.loadData() }
        })
      }
    },
    loadData() {
      if (!this.currentProjectId) return
      this.loading = true
      listManualAll({ projectId: this.currentProjectId }).then(res => {
        this.list = res.data || []
      }).finally(() => { this.loading = false })
    },
    handleAdd() {
      this.dialogTitle = '上传说明书/图纸'
      this.form = { manualName: '', docType: '说明书', version: 'V1.0', uploadDate: new Date().toISOString().slice(0, 10), fileUrl: '', description: '' }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑'
      this.form = {
        manualId: row.manualId,
        manualName: row.manualName,
        docType: row.docType,
        version: row.version,
        uploadDate: row.uploadDate,
        fileUrl: row.fileUrl,
        description: row.description
      }
      this.dialogVisible = true
    },
    save() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        const data = { ...this.form, projectId: this.currentProjectId }
        const action = data.manualId ? updateManual(data) : addManual(data)
        action.then(() => {
          this.$message.success('已保存')
          this.dialogVisible = false
          this.loadData()
        })
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除？', '提示', { type: 'warning' }).then(() => {
        delManual(row.manualId).then(() => { this.loadData() })
      }).catch(() => {})
    },
    onClosed() { this.$refs.form?.clearValidate() }
  }
}
</script>

<style scoped>
.rm-container { padding: 8px; }
.rm-panel { background: #fff; border: 1px solid #d0d7de; border-radius: 8px; overflow: hidden; }
.rm-panel-header { padding: 8px 12px; font-size: 14px; font-weight: 600; border-bottom: 1px solid #d0d7de; display: flex; align-items: center; justify-content: space-between; }
.rm-panel-body { padding: 12px; }
.form-group { margin-bottom: 6px; }
.form-group label { display: block; font-size: 12px; color: #555; margin-bottom: 3px; font-weight: 500; }
.form-row { display: flex; gap: 10px; }
.form-row .form-group { flex: 1; }
</style>
