<template>
  <view class="app-container">
    <!-- 顶部渐变区域 -->
    <view class="header-gradient px-4 pt-8 pb-6">
      <view class="flex items-center justify-between mb-4">
        <view class="flex items-center">
          <view class="w-12 h-12 bg-white rounded-full flex items-center justify-center mr-3">
            <text class="uni-icons uniui-person-filled text-primary text-lg"></text>
          </view>
          <view>
            <text class="text-blue-100 text-xs block">调度中心</text>
            <text class="text-white font-medium text-base">控制台</text>
          </view>
        </view>
        <view
          class="w-10 h-10 rounded-full flex items-center justify-center relative"
          style="background-color: rgba(255, 255, 255, 0.2);"
          @click="goToNotification"
        >
          <text class="uni-icons uniui-notification-filled text-white"></text>
          <view
            v-if="notificationCount > 0"
            class="absolute -top-1 -right-1 min-w-5 h-5 bg-danger rounded-full text-white text-xs flex items-center justify-center px-1"
          >
            {{ notificationCount }}
          </view>
        </view>
      </view>
    </view>

    <view class="px-4 -mt-4 pb-6">
      <!-- 统计卡片 -->
      <view class="card p-4">
        <view class="grid grid-cols-3 gap-3">
          <GradientStatCard
            :value="taskStore.total"
            label="今日任务"
            gradient="linear-gradient(135deg, #667eea 0%, #764ba2 100%)"
            @click="goToTaskList"
          />
          <GradientStatCard
            :value="taskStore.pendingCount"
            label="待排班"
            gradient="linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)"
            @click="goToTaskList"
          />
          <GradientStatCard
            :value="emergencyCount"
            label="应急中"
            gradient="linear-gradient(135deg, #f093fb 0%, #f5576c 100%)"
            @click="goToEmergencyTeam"
          />
        </view>
      </view>

      <!-- 快捷操作 -->
      <view class="card p-4">
        <view class="grid grid-cols-4 gap-3">
          <QuickEntry
            label="排班管理"
            icon="uniui-calendar-filled"
            @click="goToSchedule"
          />
          <QuickEntry
            label="任务列表"
            icon="uniui-flag-filled"
            @click="goToTaskList"
          />
          <QuickEntry
            label="车辆管理"
            icon="uniui-cart-filled"
            @click="goToVehicleList"
          />
          <QuickEntry
            label="可视化"
            icon="uniui-map-filled"
            @click="goToVisualization"
          />
        </view>
      </view>

      <!-- 待审核任务 -->
      <view class="card">
        <view class="flex items-center justify-between p-4 border-b border-gray-100">
          <text class="text-base font-semibold text-gray-800">待审核任务</text>
          <text class="text-sm text-primary" @click="goToTaskList">查看全部</text>
        </view>
        <view v-if="pendingTasks.length === 0" class="py-6 text-center">
          <text class="text-desc">暂无待审核任务</text>
        </view>
        <view v-else>
          <view
            v-for="task in pendingTasks"
            :key="task.id"
            class="p-4 border-b border-gray-100 active:bg-gray-50"
            @click="goToTaskDetail(task.id)"
          >
            <view class="flex justify-between items-start mb-2">
              <text class="text-sm font-semibold text-gray-800">{{ task.projectName }}</text>
              <StatusTag :status="task.status" />
            </view>
            <view class="flex items-center text-xs text-gray-500 mb-1">
              <text class="uni-icons uniui-location-filled text-gray-400 mr-1"></text>
              <text class="truncate">{{ task.address || '暂无地址' }}</text>
            </view>
            <view class="flex items-center text-xs text-gray-500">
              <text class="uni-icons uniui-clock-filled text-gray-400 mr-1"></text>
              <text>申请时间：{{ task.applyTime || task.createTime }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useTaskStore } from '@/stores/task'
import GradientStatCard from '@/components/common/GradientStatCard.vue'
import QuickEntry from '@/components/common/QuickEntry.vue'
import StatusTag from '@/components/common/StatusTag.vue'

const taskStore = useTaskStore()

const notificationCount = ref(2)

const emergencyCount = computed(() => taskStore.tasks.filter(t => t.type === 'emergency').length)
const pendingTasks = computed(() => taskStore.tasks.filter(t => t.status === 'pending').slice(0, 3))

function goToTaskList() {
  uni.switchTab({ url: '/pages/task/task' })
}

function goToTaskDetail(taskId: string) {
  taskStore.setCurrentTask(taskId)
  uni.navigateTo({ url: '/subPackages/pages-dispatch/taskDetail' })
}

function goToSchedule() {
  uni.switchTab({ url: '/pages/schedule/schedule' })
}

function goToVehicleList() {
  uni.navigateTo({ url: '/subPackages/pages-dispatch/vehicleList' })
}

function goToVisualization() {
  uni.navigateTo({ url: '/subPackages/pages-dispatch/visualization' })
}

function goToEmergencyTeam() {
  uni.navigateTo({ url: '/subPackages/pages-dispatch/emergencyTeam' })
}

function goToNotification() {
  uni.navigateTo({ url: '/subPackages/pages-dispatch/notification' })
}

onMounted(() => {
  taskStore.fetchTasks()
})
</script>
