<template>
  <div class="detail-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="task-card">
      <div class="card-header">
        <div class="task-type" :class="task.typeClass">
          {{ task.type }}
        </div>
        <div class="task-level" :class="task.levelClass">
          {{ task.level }}
        </div>
      </div>

      <div class="card-body">
        <div class="info-row">
          <van-icon name="location-o" class="row-icon" />
          <span class="label">任务地点：</span>
          <span class="value">{{ task.location }}</span>
        </div>
        <div class="info-row">
          <van-icon name="clock-o" class="row-icon" />
          <span class="label">下发时间：</span>
          <span class="value">{{ task.issueTime }}</span>
        </div>
        <div class="info-row">
          <van-icon name="user-o" class="row-icon" />
          <span class="label">下发人：</span>
          <span class="value">{{ task.issuer }}</span>
        </div>
        <div class="info-row">
          <van-icon name="phone-o" class="row-icon" />
          <span class="label">联系电话：</span>
          <span class="value link">{{ task.contact }}</span>
        </div>
      </div>

      <div class="task-desc">
        <div class="desc-title">任务描述</div>
        <div class="desc-content">{{ task.description }}</div>
      </div>
    </div>

    <!-- 执行记录 -->
    <div class="detail-card" v-if="task.records && task.records.length > 0">
      <div class="card-title">执行记录</div>
      <div class="record-list">
        <div v-for="(record, index) in task.records" :key="index" class="record-item">
          <div class="record-dot" :class="record.status"></div>
          <div class="record-content">
            <div class="record-time">{{ record.time }}</div>
            <div class="record-text">{{ record.content }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 附件 -->
    <div class="detail-card">
      <div class="card-title">附件</div>
      <div class="attachment-list">
        <div v-for="(item, index) in task.attachments" :key="index" class="attachment-item">
          <van-icon name="description" class="attachment-icon" />
          <span class="attachment-name">{{ item.name }}</span>
          <van-icon name="down" class="download-icon" />
        </div>
      </div>
    </div>

    <!-- 底部操作按钮 -->
    <div class="action-bar">
      <van-button block round type="primary" class="action-btn" @click="handleExecute">
        {{ task.status === 'completed' ? '查看完成' : '立即执行' }}
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { Toast } from "vant";

const router = useRouter();
const route = useRoute();

// 任务详情数据
const task = ref({
  id: "",
  type: "紧急疏散",
  typeClass: "type-emergency",
  level: "一级",
  levelClass: "level-high",
  location: "光谷广场地铁站",
  issueTime: "03/28 14:30",
  issuer: "应急指挥中心",
  contact: "027-88888888",
  description: "光谷广场地铁站周边出现紧急情况，需立即组织人员疏散。请相关人员迅速到位，协助维持秩序。",
  status: "pending",
  records: [
    { time: "03/28 14:35", content: "任务已接收，正在前往现场", status: "success" },
    { time: "03/28 14:40", content: "已到达现场，开始执行疏散任务", status: "success" }
  ],
  attachments: [
    { name: "紧急疏散方案.pdf" },
    { name: "现场平面图.jpg" }
  ]
});

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

// 执行任务
function handleExecute() {
  if (task.value.status === "completed") {
    Toast("该任务已完成");
  } else {
    router.push("/dataReport");
  }
}

onMounted(() => {
  const taskData = route.query;
  if (taskData.id) {
    task.value.id = taskData.id;
  }
});
</script>

<style lang="scss" scoped>
.detail-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 100px;
}

.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;
  }
}

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

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.task-type {
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;

  &.type-emergency {
    background: #fee2e2;
    color: #dc2626;
  }
}

.task-level {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;

  &.level-high {
    background: #fef3c7;
    color: #d97706;
  }

  &.level-medium {
    background: #e0e7ff;
    color: #4f46e5;
  }

  &.level-low {
    background: #ecfdf5;
    color: #16a34a;
  }
}

.card-body {
  .info-row {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    font-size: 14px;

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

    .row-icon {
      font-size: 16px;
      color: #999;
      margin-right: 8px;
    }

    .label {
      color: #999;
    }

    .value {
      color: #333;
      flex: 1;

      &.link {
        color: #2563eb;
      }
    }
  }
}

.task-desc {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;

  .desc-title {
    font-size: 14px;
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
  }

  .desc-content {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
  }
}

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

.card-title {
  font-size: 15px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

.record-list {
  position: relative;
  padding-left: 20px;

  &::before {
    content: "";
    position: absolute;
    left: 5px;
    top: 8px;
    bottom: 8px;
    width: 2px;
    background: #e5e5e5;
  }

  .record-item {
    position: relative;
    padding-bottom: 16px;

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

    .record-dot {
      position: absolute;
      left: -17px;
      top: 4px;
      width: 10px;
      height: 10px;
      border-radius: 50%;

      &.success {
        background: #16a34a;
      }

      &.pending {
        background: #ea580c;
      }
    }

    .record-content {
      .record-time {
        font-size: 12px;
        color: #999;
        margin-bottom: 4px;
      }

      .record-text {
        font-size: 14px;
        color: #333;
      }
    }
  }
}

.attachment-list {
  .attachment-item {
    display: flex;
    align-items: center;
    padding: 12px;
    background: #f9fafb;
    border-radius: 12px;
    margin-bottom: 8px;

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

    .attachment-icon {
      font-size: 24px;
      color: #666;
      margin-right: 12px;
    }

    .attachment-name {
      flex: 1;
      font-size: 14px;
      color: #333;
    }

    .download-icon {
      font-size: 18px;
      color: #2563eb;
    }
  }
}

.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-top: 1px solid #e5e5e5;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

.action-btn {
  height: 44px;
  font-size: 16px;
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  border: none;
}
</style>
