<!--模拟演练管理-->
<template>
  <div class="app-container">
    <div class="sim-layout">
      <!-- ===== 左列 ===== -->
      <div class="col-left">
        <!-- 演练项目设置 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><Folder /></el-icon> 演练项目设置</span>
            <el-button type="primary" size="small" @click="handleAddProject">新增</el-button>
          </div>
          <div class="panel-body">
            <div class="project-item" v-for="item in projects" :key="item.id">
              <div class="project-info">
                <div class="project-name">{{ item.name }}</div>
                <div class="project-date">{{ item.date }}</div>
              </div>
              <div class="project-actions">
                <el-button link type="primary" @click="handleEditProject(item)"><el-icon><Edit /></el-icon></el-button>
                <el-button link type="danger" @click="handleDeleteProject(item)"><el-icon><Delete /></el-icon></el-button>
              </div>
            </div>
          </div>
        </div>

        <!-- 场景设置 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><PictureFilled /></el-icon> 场景设置</span>
            <el-button type="primary" size="small" @click="handleEditScene">编辑</el-button>
          </div>
          <div class="panel-body">
            <div class="scene-section">
              <div class="scene-label">当前场景元素：</div>
              <div class="tag-list">
                <el-tag v-for="tag in currentSceneTags" :key="tag" size="small" closable @close="removeSceneTag(tag)">{{ tag }}</el-tag>
              </div>
            </div>
            <div class="scene-section">
              <div class="scene-label">场景元素库：</div>
              <div class="tag-list">
                <el-tag v-for="tag in sceneLibrary" :key="tag" size="small" type="info" class="clickable-tag" @click="addSceneTag(tag)">{{ tag }}</el-tag>
              </div>
            </div>
          </div>
        </div>

        <!-- 搜索 + 统计 -->
        <div class="panel">
          <div class="search-bar">
            <el-input v-model="projectSearch" placeholder="查询项目名称..." size="small" style="width: 160px" />
            <el-button type="primary" size="small" @click="handleSearchProject">查询</el-button>
          </div>
          <div class="stat-line">项目总数：{{ projects.length }}</div>
        </div>
      </div>

      <!-- ===== 中列 ===== -->
      <div class="col-center">
        <!-- 事件设置 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><Warning /></el-icon> 事件设置（突发事件制作）</span>
            <el-button type="primary" size="small" @click="handleAddEvent">新增事件</el-button>
          </div>
          <div class="panel-body">
            <div class="event-card" v-for="evt in events" :key="evt.id">
              <div class="event-info">
                <div class="event-name">
                  <el-icon style="color: #e6a23c"><Warning /></el-icon>
                  {{ evt.name }}
                </div>
                <div class="event-desc">{{ evt.desc }}</div>
              </div>
              <el-button type="primary" size="small" @click="addToScene(evt)">添加到场景</el-button>
            </div>
            <div class="tip-text">提示：可随时向场景中添加新事件，培养应变能力</div>
          </div>
        </div>

        <!-- 角色设置 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><User /></el-icon> 角色设置（救援/指挥/观摩）</span>
            <el-button type="primary" size="small" @click="handleAddRole">新增角色</el-button>
          </div>
          <div class="panel-body">
            <div class="role-item" v-for="role in roles" :key="role.id">
              <div class="role-name">{{ role.name }}（{{ role.type }}）</div>
              <div class="role-detail">技能：{{ role.skills }} | 装备：{{ role.equipment }}</div>
            </div>
          </div>
        </div>

        <!-- 任务设置 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><List /></el-icon> 任务设置（任务模型库）</span>
            <el-button type="primary" size="small" @click="handleAddTask">新增任务</el-button>
          </div>
          <div class="panel-body">
            <div class="task-item" v-for="task in tasks" :key="task.id">
              <div class="task-name">{{ task.name }}</div>
              <div class="task-desc">{{ task.desc }}</div>
              <div class="task-bind">绑定角色：{{ task.bindRole }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- ===== 右列 ===== -->
      <div class="col-right">
        <!-- 过程管理 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><VideoPlay /></el-icon> 过程管理</span>
          </div>
          <div class="panel-body">
            <div class="process-buttons">
              <el-button type="primary" size="small" @click="startDrill">启动</el-button>
              <el-button type="warning" size="small" @click="pauseDrill">暂停</el-button>
              <el-button type="danger" size="small" @click="exitDrill">退出</el-button>
            </div>
            <div class="event-log">
              <div class="log-item" v-for="(log, idx) in eventLogs" :key="idx">
                <span class="log-time">[{{ log.time }}]</span>
                <span class="log-text">{{ log.text }}</span>
              </div>
            </div>
            <div class="event-control">
              <div class="control-label">事件控制</div>
              <div class="control-buttons">
                <el-button type="primary" size="small" @click="triggerEvent">
                  <el-icon><Promotion /></el-icon>触发事件(发展)
                </el-button>
                <el-button size="small" @click="endEvent">
                  <el-icon><Document /></el-icon>结束事件
                </el-button>
              </div>
            </div>
          </div>
        </div>

        <!-- 结果分析与评估 -->
        <div class="panel">
          <div class="panel-header">
            <span><el-icon><DataAnalysis /></el-icon> 结果分析与评估</span>
          </div>
          <div class="panel-body">
            <div class="metric-grid">
              <div class="metric-item">
                <div class="metric-label">任务完成率</div>
                <div class="metric-value">33%</div>
              </div>
              <div class="metric-item">
                <div class="metric-label">平均响应时间</div>
                <div class="metric-value">30s</div>
              </div>
              <div class="metric-item">
                <div class="metric-label">应急评估等级</div>
                <div class="metric-value highlight">良好</div>
              </div>
              <div class="metric-item">
                <div class="metric-label">建议</div>
                <div class="metric-value small">加强指挥调度协同</div>
              </div>
            </div>
            <div class="stat-cards">
              <div class="stat-card">
                <div class="stat-title">应急统计</div>
                <div class="stat-content">事件响应时间 28秒</div>
              </div>
              <div class="stat-card">
                <div class="stat-title">应急评估</div>
                <div class="stat-content">任务完成率 67%</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Folder, Edit, Delete, PictureFilled, Warning, User, List,
  VideoPlay, Promotion, Document, DataAnalysis
} from '@element-plus/icons-vue'

// ---------- 演练项目 ----------
let projectId = 3
const projects = ref([
  { id: 1, name: '化工园区毒气泄漏演练', date: '2025-01-01' },
  { id: 2, name: '海上溢油应急联合演练', date: '2025-01-01' },
  { id: 3, name: '化工园区综合应急演练', date: '2025-03-15' }
])

const projectSearch = ref('')
const handleSearchProject = () => { ElMessage.success('查询项目（Mock）') }
const handleAddProject = () => { ElMessage.success('新增演练项目（Mock）') }
const handleEditProject = (item) => { ElMessage.success(`编辑项目: ${item.name}`) }
const handleDeleteProject = (item) => {
  ElMessageBox.confirm('确认删除该项目?', '提示', { type: 'warning' }).then(() => {
    projects.value = projects.value.filter(p => p.id !== item.id)
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// ---------- 场景设置 ----------
const currentSceneTags = ref(['控制室', '泄漏点', '氨气泄露', '火灾蔓延'])
const sceneLibrary = ref(['控制室', '泄漏点', '储罐区', '氨气泄露', '火灾蔓延', '风向标'])

const addSceneTag = (tag) => {
  if (!currentSceneTags.value.includes(tag)) {
    currentSceneTags.value.push(tag)
  }
}
const removeSceneTag = (tag) => {
  currentSceneTags.value = currentSceneTags.value.filter(t => t !== tag)
}
const handleEditScene = () => { ElMessage.success('编辑场景（Mock）') }

// ---------- 事件设置 ----------
const events = ref([
  { id: 1, name: '氯气泄漏', desc: '突发事件: 储罐区发生泄漏，需紧急处置' },
  { id: 2, name: '火灾蔓延', desc: '次生灾害: 爆炸引起火灾' }
])

const handleAddEvent = () => { ElMessage.success('新增事件（Mock）') }
const addToScene = (evt) => { ElMessage.success(`已将"${evt.name}"添加到场景`) }

// ---------- 角色设置 ----------
const roles = ref([
  { id: 1, name: '现场指挥员', type: '指挥类', skills: '消防指挥、泄漏处置', equipment: '防护服、指挥终端' },
  { id: 2, name: '消防队员', type: '救援类', skills: '灭火、堵漏', equipment: '防护服、灭火器' }
])

const handleAddRole = () => { ElMessage.success('新增角色（Mock）') }

// ---------- 任务设置 ----------
const tasks = ref([
  { id: 1, name: '启动应急响应', desc: '指挥员需下达疏散命令', bindRole: '现场指挥员' },
  { id: 2, name: '演练整体评估', desc: '完成评价报告', bindRole: '现场指挥员' }
])

const handleAddTask = () => { ElMessage.success('新增任务（Mock）') }

// ---------- 过程管理 ----------
const eventLogs = ref([
  { time: '16:11:05', text: '演练启动' },
  { time: '16:11:06', text: '演练暂停' },
  { time: '16:11:07', text: '触发事件: 氯气泄漏 - 影响范围扩大，角色已分配' },
  { time: '16:11:10', text: '触发事件: 氯气泄漏 - 影响范围进一步扩大' }
])

const startDrill = () => {
  const now = new Date()
  const time = now.toTimeString().slice(0, 8)
  eventLogs.value.push({ time, text: '演练启动' })
  ElMessage.success('演练已启动')
}
const pauseDrill = () => {
  const now = new Date()
  const time = now.toTimeString().slice(0, 8)
  eventLogs.value.push({ time, text: '演练暂停' })
  ElMessage.warning('演练已暂停')
}
const exitDrill = () => {
  ElMessageBox.confirm('确认退出演练?', '提示', { type: 'warning' }).then(() => {
    const now = new Date()
    const time = now.toTimeString().slice(0, 8)
    eventLogs.value.push({ time, text: '演练退出' })
    ElMessage.info('演练已退出')
  }).catch(() => {})
}

const triggerEvent = () => {
  const now = new Date()
  const time = now.toTimeString().slice(0, 8)
  eventLogs.value.push({ time, text: '触发事件(发展): 事态发展变化' })
  ElMessage.success('事件已触发')
}
const endEvent = () => {
  const now = new Date()
  const time = now.toTimeString().slice(0, 8)
  eventLogs.value.push({ time, text: '事件结束' })
  ElMessage.info('事件已结束')
}
</script>

<style scoped lang="scss">
.sim-layout {
  display: flex;
  gap: 12px;
  min-height: calc(100vh - 160px);
}

.col-left {
  width: 25%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.col-center {
  width: 50%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.col-right {
  width: 25%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  background: #fff;
  overflow: hidden;

  .panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: #f5f7fa;
    border-bottom: 1px solid #e4e7ed;
    font-weight: bold;
    font-size: 14px;
    color: #303133;

    span {
      display: flex;
      align-items: center;
      gap: 6px;
    }

    .el-icon {
      color: #409eff;
    }
  }

  .panel-body {
    padding: 12px 14px;
  }
}

// 项目列表
.project-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px dashed #ebeef5;

  &:last-child { border-bottom: none; }

  .project-info {
    .project-name { font-size: 13px; color: #303133; font-weight: 500; }
    .project-date { font-size: 12px; color: #909399; margin-top: 2px; }
  }

  .project-actions {
    display: flex;
    gap: 2px;
  }
}

// 场景设置
.scene-section {
  margin-bottom: 12px;

  .scene-label {
    font-size: 12px;
    color: #909399;
    margin-bottom: 6px;
  }
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;

  .clickable-tag {
    cursor: pointer;
    transition: all 0.2s;

    &:hover {
      color: #409eff;
      border-color: #409eff;
    }
  }
}

.search-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.stat-line {
  font-size: 12px;
  color: #909399;
  text-align: right;
}

// 事件卡片
.event-card {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 10px;
  margin-bottom: 8px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background: #fafafa;

  .event-info {
    flex: 1;
    margin-right: 10px;

    .event-name {
      font-size: 14px;
      font-weight: 500;
      color: #303133;
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .event-desc {
      font-size: 12px;
      color: #909399;
      margin-top: 4px;
    }
  }
}

.tip-text {
  font-size: 12px;
  color: #909399;
  padding: 8px;
  background: #f0f9eb;
  border-radius: 4px;
  margin-top: 4px;
}

// 角色列表
.role-item {
  padding: 8px 0;
  border-bottom: 1px dashed #ebeef5;

  &:last-child { border-bottom: none; }

  .role-name {
    font-size: 13px;
    font-weight: 500;
    color: #303133;
  }

  .role-detail {
    font-size: 12px;
    color: #909399;
    margin-top: 4px;
  }
}

// 任务列表
.task-item {
  padding: 8px 0;
  border-bottom: 1px dashed #ebeef5;

  &:last-child { border-bottom: none; }

  .task-name {
    font-size: 13px;
    font-weight: 500;
    color: #303133;
  }

  .task-desc {
    font-size: 12px;
    color: #909399;
    margin-top: 2px;
  }

  .task-bind {
    font-size: 12px;
    color: #409eff;
    margin-top: 2px;
  }
}

// 过程管理
.process-buttons {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.event-log {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 8px;
  margin-bottom: 10px;
  background: #fafafa;

  .log-item {
    font-size: 12px;
    line-height: 1.8;
    color: #606266;

    .log-time {
      color: #409eff;
      margin-right: 6px;
      font-family: monospace;
    }
  }
}

.event-control {
  border-top: 1px solid #ebeef5;
  padding-top: 10px;

  .control-label {
    font-size: 13px;
    font-weight: bold;
    color: #303133;
    margin-bottom: 8px;
  }

  .control-buttons {
    display: flex;
    gap: 8px;
  }
}

// 结果分析
.metric-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.metric-item {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 10px;
  text-align: center;

  .metric-label {
    font-size: 12px;
    color: #909399;
    margin-bottom: 4px;
  }

  .metric-value {
    font-size: 18px;
    font-weight: bold;
    color: #303133;

    &.highlight { color: #67c23a; }
    &.small { font-size: 13px; font-weight: normal; color: #606266; }
  }
}

.stat-cards {
  display: flex;
  gap: 10px;
}

.stat-card {
  flex: 1;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 10px;
  text-align: center;
  background: #fafafa;

  .stat-title {
    font-size: 12px;
    color: #909399;
    margin-bottom: 4px;
  }

  .stat-content {
    font-size: 14px;
    font-weight: bold;
    color: #409eff;
  }
}
</style>
