<template>
  <div class="emergency-tasks-page">
    <!-- 顶部导航栏 -->
    <div class="nav-bar">
      <van-icon name="arrow-left" class="nav-back" @click="goBack" />
      <div class="nav-title">紧急任务</div>
      <div class="nav-placeholder"></div>
    </div>

    <!-- 标签栏 -->
    <div class="tabs-wrapper">
      <div class="tabs-container">
        <div
          v-for="tab in tabs"
          :key="tab.id"
          class="tab-item"
          :class="{ active: activeTab === tab.id }"
          @click="activeTab = tab.id"
        >
          {{ tab.label }}
        </div>
      </div>
    </div>

    <!-- 任务列表 -->
    <div class="tasks-container">
      <div
        v-for="task in filteredTasks"
        :key="task.id"
        class="task-card"
        @click="goToDetail(task)"
      >
        <div class="task-header">
          <div class="task-info">
            <h3 class="task-type">{{ task.type }}</h3>
            <span class="task-level" :class="task.levelClass">{{ task.level }}</span>
          </div>
          <span class="task-status" :class="task.statusClass">{{ task.statusText }}</span>
        </div>

        <div class="task-details">
          <div class="detail-item">
            <van-icon name="location-o" />
            <span>{{ task.location }}</span>
          </div>
          <div class="detail-item">
            <van-icon name="clock-o" />
            <span>{{ task.time }}</span>
          </div>
          <div class="detail-item">
            <van-icon name="user-o" />
            <span>{{ task.issuer }}</span>
          </div>
        </div>

        <van-button
          v-if="task.status !== 'completed'"
          block
          round
          type="primary"
          class="execute-btn"
        >
          立即执行
        </van-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

// 当前激活的标签
const activeTab = ref("all");

// 标签配置
const tabs = [
  { id: "all", label: "全部" },
  { id: "not-executed", label: "未执行" },
  { id: "processing", label: "执行中" },
  { id: "completed", label: "已完成" },
];

// 任务数据
const tasks = ref([
  {
    id: 1,
    type: "紧急疏散",
    level: "一级",
    levelClass: "level-danger",
    location: "光谷广场地铁站",
    time: "03/28 14:30",
    issuer: "应急指挥中心",
    status: "pending",
    statusClass: "status-pending",
    statusText: "待处理",
  },
  {
    id: 2,
    type: "火灾救援",
    level: "一级",
    levelClass: "level-danger",
    location: "科技大厦A座",
    time: "03/28 15:20",
    issuer: "安全管理部门",
    status: "processing",
    statusClass: "status-processing",
    statusText: "执行中",
  },
  {
    id: 3,
    type: "医疗救护",
    level: "二级",
    levelClass: "level-warning",
    location: "人民公园东门",
    time: "03/28 10:00",
    issuer: "急救中心",
    status: "completed",
    statusClass: "status-completed",
    statusText: "已完成",
  },
]);

// 根据标签过滤任务
const filteredTasks = computed(() => {
  if (activeTab.value === "all") {
    return tasks.value;
  }
  if (activeTab.value === "not-executed") {
    return tasks.value.filter((task) => task.status === "pending");
  }
  if (activeTab.value === "processing") {
    return tasks.value.filter((task) => task.status === "processing");
  }
  if (activeTab.value === "completed") {
    return tasks.value.filter((task) => task.status === "completed");
  }
  return tasks.value;
});

// 返回上一页
function goBack() {
  router.back();
}

// 跳转到详情页
function goToDetail(task) {
  router.push({
    path: "/emergency-task-detail",
    query: { id: task.id },
  });
}
</script>

<style lang="scss" scoped>
.emergency-tasks-page {
  min-height: 100vh;
  background: #f5f5f5;
}

.nav-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: white;
  border-bottom: 1px solid #e5e5e5;
  padding: 12px 16px;

  .nav-back {
    font-size: 20px;
    color: #333;
  }

  .nav-title {
    font-size: 16px;
    font-weight: 600;
    color: #333;
  }

  .nav-placeholder {
    width: 20px;
  }
}

.tabs-wrapper {
  background: white;
  padding: 12px 16px;
}

.tabs-container {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;

  &::-webkit-scrollbar {
    display: none;
  }
}

.tab-item {
  flex-shrink: 0;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  white-space: nowrap;
  background: white;
  color: #666;
  border: 1px solid #e5e5e5;
  transition: all 0.2s;

  &.active {
    background: #ee0a24;
    color: white;
    border-color: #ee0a24;
  }
}

.tasks-container {
  padding: 16px;
}

.task-card {
  background: white;
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.task-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 16px;
}

.task-info {
  .task-type {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
  }
}

.task-level {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;

  &.level-danger {
    background: #fef2f2;
    color: #dc2626;
  }

  &.level-warning {
    background: #fffbeb;
    color: #d97706;
  }
}

.task-status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  border: 1px solid;

  &.status-pending {
    background: #fff7ed;
    color: #ea580c;
    border-color: #fed7aa;
  }

  &.status-processing {
    background: #eff6ff;
    color: #2563eb;
    border-color: #bfdbfe;
  }

  &.status-completed {
    background: #f0fdf4;
    color: #16a34a;
    border-color: #bbf7d0;
  }
}

.task-details {
  .detail-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    font-size: 14px;
    color: #666;

    &:last-child {
      margin-bottom: 0;
    }

    .van-icon {
      font-size: 16px;
      color: #999;
    }
  }
}

.execute-btn {
  margin-top: 16px;
  height: 40px;
  font-size: 14px;
  background: linear-gradient(135deg, #ee0a24 0%, #bd2a2a 100%);
  border: none;
}
</style>
