<template>
  <div class="page">
    <!-- Header -->
    <TopNavBar title="最近任务" color="white" :showHistory="false" @back="onBack" />

    <!-- 列表 -->
    <div class="list">
      <div v-for="(item, index) in list" :key="`${item.name}-${index}`" class="card">
        <!-- 排队中 -->
        <template v-if="item.status == 0">
          <div class="left queued-box">排队中</div>

          <div class="right">
            <div class="name">{{ item.name }}</div>
            <div class="status-text">排队中</div>
            <div class="time">{{ item.time }}</div>
          </div>
        </template>

        <!-- 生成中 -->
        <template v-else-if="item.status == 1">
          <div class="left processing-box">生成中</div>

          <div class="right">
            <div class="name">{{ item.name }}</div>

            <div class="progress">
              <div class="bar" :style="{ width: (item.progress || 0) + '%' }"></div>
            </div>

            <div class="status-text">生成中 {{ item.progress ? item.progress + '%' : '' }}</div>
          </div>
        </template>

        <!-- 已完成 -->
        <template v-else-if="item.status == 2">
          <img class="thumb" :src="item.cover" />

          <div class="right">
            <div class="name">{{ item.name }}</div>
            <div class="time">{{ item.time }}</div>
            <div class="status-text">已完成</div>
          </div>

          <button class="view-btn" @click="lookPicture(item)">查看</button>
        </template>

        <!-- 失败 -->
        <template v-else>
          <div class="left failed-box">失败</div>

          <div class="right">
            <div class="name">{{ item.name }}</div>
            <div class="time">{{ item.time }}</div>
            <div class="status-text">生成失败</div>
          </div>
        </template>
      </div>
      <div class="tip"> {{ list.length == 0 ? '暂无记录' : `仅保留最近20天生成记录` }}</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';
// @ts-ignore
import TopNavBar from '../components/TopNavBar.vue';
// @ts-ignore
import { generatorPhotoTaskList } from '@api';
import { closeToast, showLoadingToast } from 'vant';

interface TaskItem {
  name: string
  status: 0 | 1 | 2 | 3 // 0: 排队中, 1: 生成中, 2: 已完成, 3: 失败
  progress?: number
  cover?: string
  time?: string
}

const list: TaskItem[] = []

const router = useRouter();

const onBack = () => {
  router.back();
};

const lookPicture = (item: TaskItem) => {
  console.log('查看图片');
  const encoded = encodeURIComponent(JSON.stringify(item));
  router.push({ path: '/prepicture', query: { data: encoded } });
}

const getTaskList = () => {
  showLoadingToast('加载中...');
  generatorPhotoTaskList({ pageNum: 1, pageSize: 20 }).then(res => {
    closeToast(); // 关闭加载提示
    if (res.code === 0) {
      const data = res.data || [];
      list.splice(0, list.length, ...data.map(item => {
        let statusNum: number = item.taskStatus as unknown as number; // 先尝试直接转换为数字
        return {
          name: item.sceneName,
          status: statusNum as 0 | 1 | 2 | 3,
          progress: statusNum === 1 ? 50 : undefined, // 若后端有真实进度请替换
          cover: item.generatorPhoto,
          time: item.createTime,
        } as TaskItem;
      }));
    }
  });
}

onMounted(() => {
  getTaskList();
});

</script>

<style scoped>
.page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #000;
  color: #fff;
}

/* 列表（放在 NavBar 下面并可滚动） */
.list {
  padding: 16px;
  margin-top: 72px;
  /* 留出 NavBar 空间（56px header + 24px margin） */
  flex: 1 1 auto;
  overflow: auto;
}

/* 卡片 */
.card {
  display: flex;
  align-items: center;
  background: #1a1a1a;
  border-radius: 16px;
  padding: 12px;
  margin-bottom: 16px;
}

/* 左侧生成中 */
.processing-box {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  background: #1f2a36;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ccc;
  font-size: 14px;
}

/* 排队中 */
.queued-box {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  background: #23313e;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9fb7d0;
  font-size: 14px;
}

/* 失败 */
.failed-box {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  background: #3a1f22;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ff9b9b;
  font-size: 14px;
}

/* 已完成缩略图 */
.thumb {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  object-fit: cover;
}

/* 右侧 */
.right {
  flex: 1;
  margin-left: 12px;
}

.name {
  font-size: 16px;
  margin-bottom: 8px;
}

/* 进度条 */
.progress {
  height: 4px;
  background: #333;
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 6px;
}

.bar {
  height: 100%;
  background: linear-gradient(90deg, #6ec1ff, #b7f8c8);
}

/* 状态 */
.status-text {
  font-size: 12px;
  color: #888;
}

/* 时间 */
.time {
  font-size: 12px;
  color: #888;
}

/* 查看按钮 */
.view-btn {
  background: #2a2a2a;
  border: none;
  color: #fff;
  border-radius: 14px;
  padding: 6px 12px;
  font-size: 12px;
}

/* 底部提示 */
.tip {
  text-align: center;
  color: #666;
  font-size: 12px;
  margin-top: 20px;
}
</style>