<template>
  <view class="sub-page">
    <TopBar title="应急班组" show-back />

    <view class="px-4 pt-3">
      <!-- 应急状态概览 -->
      <view class="glass-card p-4">
        <view class="section-head-inner mb-3">
          <view class="section-bar mr-2"></view>
          <text class="section-title flex-1">应急状态</text>
          <text class="realtime-tag">实时</text>
        </view>
        <view class="grid grid-cols-3 gap-3">
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value stat-value-red">{{ standbyTeams }}</text>
            <text class="stat-label">待命班组</text>
          </view>
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value stat-value-gold">{{ standbyVehicles }}</text>
            <text class="stat-label">待命车辆</text>
          </view>
          <view class="stat-card">
            <view class="stat-bar"></view>
            <text class="stat-value">{{ emergencyTasks }}</text>
            <text class="stat-label">应急任务</text>
          </view>
        </view>
      </view>

      <!-- 应急任务列表 -->
      <view class="glass-card">
        <view class="section-head">
          <view class="section-bar mr-2"></view>
          <text class="section-title flex-1">应急任务</text>
        </view>
        <view v-if="loading" class="py-10 text-center">
          <text class="empty-text">加载中...</text>
        </view>
        <view v-else-if="emergencyTaskList.length === 0" class="pb-3">
          <EmptyState message="暂无应急任务" />
        </view>
        <view v-else>
          <view
            v-for="task in emergencyTaskList"
            :key="task.id"
            class="list-row"
            hover-class="row-hover"
            :hover-start-time="0"
            :hover-stay-time="120"
            @click="goToTaskDetail(task.id)"
          >
            <view class="icon-chip icon-chip-red mr-3">
              <text class="uni-icons uniui-notification chip-glyph-red"></text>
            </view>
            <view class="flex-1 min-w-0 mr-2">
              <text class="row-title truncate">{{ task.projectName || '应急任务' }}</text>
              <text class="row-sub truncate">{{ task.address || '暂无地址' }}</text>
            </view>
            <text class="emergency-tag flex-shrink-0 mr-1">应急</text>
            <text class="uni-icons uniui-arrowright row-arrow"></text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import { getTaskList } from '../../api/task'
import { getStaffList } from '../../api/staff'
import { getVehicleList } from '../../api/vehicle'

const loading = ref(false)
const taskList = ref<any[]>([])
const staffList = ref<any[]>([])
const vehicleList = ref<any[]>([])

const emergencyTaskList = computed(() => taskList.value.filter(t => t.type === 'emergency'))

const standbyTeams = computed(() => {
  return staffList.value.filter(s => s.status === 'on_duty').length
})

const standbyVehicles = computed(() => {
  return vehicleList.value.filter(v => v.status === 'available').length
})

const emergencyTasks = computed(() => emergencyTaskList.value.length)

function goToTaskDetail(taskId: string) {
  uni.navigateTo({ url: `/subPackages/pages-dispatch/taskDetail?id=${taskId}` })
}

onMounted(async () => {
  loading.value = true
  try {
    const [taskRes, staffRes, vehicleRes] = await Promise.all([
      getTaskList(),
      getStaffList(),
      getVehicleList(),
    ])
    taskList.value = taskRes.rows || []
    staffList.value = staffRes.rows || []
    vehicleList.value = vehicleRes.rows || []
  } catch (error) {
    console.error('获取数据失败:', error)
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.sub-page {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f6fbf9;
  padding-bottom: 24px;
}

.glass-card {
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(164, 216, 152, 0.35);
  border-radius: 24px;
  box-shadow: 0 18px 50px -12px rgba(54, 143, 111, 0.28);
  overflow: hidden;
  margin-bottom: 12px;
}

.section-head {
  display: flex;
  align-items: center;
  padding: 16px 16px 12px;
}

.section-head-inner {
  display: flex;
  align-items: center;
}

.section-bar {
  width: 4px;
  height: 16px;
  border-radius: 2px;
  background: linear-gradient(180deg, #368f6f 0%, #5ab8d0 100%);
}

.section-title {
  font-size: 16px;
  font-weight: 700;
  color: #1f2937;
}

.realtime-tag {
  font-size: 12px;
  color: #9ca3af;
}

.stat-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 88px;
  padding: 14px 6px 12px;
  border-radius: 16px;
  background-color: rgba(164, 216, 152, 0.14);
  border: 1px solid rgba(164, 216, 152, 0.3);
  overflow: hidden;
}

.stat-bar {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 26px;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: linear-gradient(135deg, #368f6f 0%, #5ab8d0 100%);
}

.stat-value {
  font-size: 24px;
  font-weight: 800;
  line-height: 1.1;
  color: #368f6f;
}

.stat-value-red {
  color: #ef4444;
}

.stat-value-gold {
  color: #b7791f;
}

.stat-label {
  margin-top: 6px;
  font-size: 12px;
  color: #6b7280;
}

.list-row {
  display: flex;
  align-items: center;
  padding: 13px 16px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

.row-title {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #1f2937;
}

.row-sub {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: #9ca3af;
}

.row-hover {
  background-color: rgba(164, 216, 152, 0.1);
}

.row-arrow {
  font-size: 16px;
  color: #c4d0cb;
  flex-shrink: 0;
}

.icon-chip {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(164, 216, 152, 0.2);
}

.icon-chip-red {
  background-color: rgba(239, 68, 68, 0.1);
}

.chip-glyph-red {
  font-size: 20px;
  color: #ef4444;
}

.emergency-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: #ef4444;
  background-color: rgba(239, 68, 68, 0.1);
}

.empty-text {
  font-size: 13px;
  color: #9ca3af;
}
</style>
