<template>
  <div class="page-container">
    <PageTitle title="专属采购方案" />
    <!-- Tab切换 -->
    <StatusTabs v-model="activeTab" :tabs="tabs" type="pill" />
    <!-- 方案列表 -->
    <div v-loading="loading" class="plan-grid">
      <div v-for="(item, index) in planList" :key="index" class="plan-card">
        <div class="plan-image">
          <el-image :src="item.image" fit="cover">
            <template #error
              ><div class="image-placeholder">
                <el-icon :size="40"><Picture /></el-icon></div
            ></template>
          </el-image>
        </div>
        <div class="plan-info">
          <div class="plan-name">{{ item.name }}</div>
          <div class="plan-desc">{{ item.description }}</div>
          <div class="plan-link" @click="handleDetail(item)">
            了解详情 <el-icon><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>
    <el-empty v-if="planList.length === 0" description="暂无采购方案" />
    <!-- 分页 -->
    <TablePagination
      v-if="planList.length > 0"
      v-model:page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :total="total"
      @change="handleQuery"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import { Picture, ArrowRight } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { PageTitle, StatusTabs, TablePagination } from '@/components';
import { getProcurementProgramProductList } from '@/api/goods/index';

// 采购方案类型映射
const typeMap: Record<string, string> = {
  purchase: '1',      // 采购方案
  exclusive: '2',     // 专属采购方案
  collection: '3'     // 收藏的采购方案
};

const activeTab = ref('purchase');
const tabs = [
  { key: 'purchase', label: '采购方案' },
  { key: 'exclusive', label: '专属采购方案' },
  { key: 'collection', label: '收藏的采购方案' }
];
const queryParams = reactive({ pageNum: 1, pageSize: 10 });
const total = ref(0);
const planList = ref<any[]>([]);
const loading = ref(false);

// 加载数据
const loadData = async () => {
  loading.value = true;
  try {
    const params = {
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize,
      type: typeMap[activeTab.value]
    };
    const res = await getProcurementProgramProductList(params);
    // 根据实际接口返回结构调整
    const data = res.data || res;
    if (data && data.rows) {
      planList.value = data.rows.map((item: any) => ({
        id: item.id,
        name: item.tweetsTitle,
        description: item.programDescribe || item.subtitle,
        image: item.coverImage,
        // 保留原始数据以便详情页使用
        rawData: item
      }));
      total.value = data.total || 0;
    } else if (Array.isArray(data)) {
      planList.value = data.map((item: any) => ({
        id: item.id,
        name: item.tweetsTitle,
        description: item.programDescribe || item.subtitle,
        image: item.coverImage,
        rawData: item
      }));
      total.value = data.length;
    } else {
      planList.value = [];
      total.value = 0;
    }
  } catch (error) {
    ElMessage.error('获取采购方案列表失败');
    planList.value = [];
    total.value = 0;
  } finally {
    loading.value = false;
  }
};

watch(
  activeTab,
  () => {
    queryParams.pageNum = 1;
    loadData();
  },
  { immediate: true }
);

const handleQuery = () => {
  loadData();
};

const handleDetail = (item: any) => {
  // 可以跳转到详情页或打开弹窗
  ElMessage.info('查看方案详情：' + item.name);
};
</script>

<style scoped lang="scss">
.plan-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
.plan-card {
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  .plan-image {
    height: 180px;
    background: #f5f5f5;
    .el-image {
      width: 100%;
      height: 100%;
    }
    .image-placeholder {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, #ffe4c4 0%, #ffd4a3 100%);
      color: #e60012;
    }
  }
  .plan-info {
    padding: 15px;
    .plan-name {
      font-size: 15px;
      font-weight: 500;
      color: #333;
      margin-bottom: 8px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .plan-desc {
      font-size: 13px;
      color: #999;
      margin-bottom: 10px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .plan-link {
      display: flex;
      align-items: center;
      gap: 5px;
      font-size: 13px;
      color: #e60012;
      cursor: pointer;
      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>
