<template>
  <view class="sub-page">
    <TopBar title="历史任务" show-back />

    <view class="px-4 pt-3">
      <view v-if="taskStore.loading" class="py-10 text-center">
        <text class="empty-text">加载中...</text>
      </view>

      <view v-else-if="completedTasks.length === 0">
        <EmptyState message="暂无历史任务" />
      </view>

      <view v-else class="glass-card">
        <view
          v-for="task in completedTasks"
          :key="task.taskId || task.id"
          class="list-row"
          hover-class="row-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(task.taskId || task.id)"
        >
          <view class="icon-chip mr-3">
            <text class="uni-icons uniui-checkmarkempty chip-glyph"></text>
          </view>
          <view class="flex-1 min-w-0 mr-2">
            <text class="row-title truncate">{{ task.taskName || task.projectName }}</text>
            <text class="row-sub truncate">{{ task.address || '暂无地址' }}</text>
          </view>
          <text class="done-tag flex-shrink-0">已完成</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useTaskStore } from '../../stores/task'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'

const taskStore = useTaskStore()

const completedTasks = computed(() => taskStore.tasks.filter(t => t.status === 'completed'))

function goToDetail(taskId: string) {
  if (!taskId) return
  uni.navigateTo({ url: `/subPackages/pages-construction/taskDetail?id=${taskId}` })
}

onMounted(() => {
  taskStore.fetchMyTasks()
})
</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;
}

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

.list-row:first-child {
  border-top: none;
}

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

.done-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: #368f6f;
  background-color: rgba(164, 216, 152, 0.24);
}

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

.chip-glyph {
  font-size: 20px;
  color: #368f6f;
}

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