<script setup>
import { ref, reactive, onMounted } from 'vue'
import { showToast } from 'vant'
import { getDemandList } from '../services/api'

// 搜索关键词
const searchKey = ref('')

// 分页参数
const pagination = reactive({
  page: 1,
  size: 3,
  total: 0
})

// 需求列表数据
const demandList = ref([])

// 加载状态
const loading = ref(true)
// 是否为第一次加载
const isFirstLoad = ref(true)

// 详情弹窗
const showDetail = ref(false)
const currentDemand = ref(null)

// 获取需求列表
const fetchDemandList = async () => {
  try {
    loading.value = true
    const response = await getDemandList({
      page: pagination.page,
      size: pagination.size,
      key: searchKey.value
    })
    if (response.code === 1) {
      demandList.value = response.data?.list || []
      pagination.total = response.data?.total || 0
      // 第一次加载完成后设置为false
      if (isFirstLoad.value) {
        isFirstLoad.value = false
      }
    } else {
      showToast('获取需求列表失败')
      // 失败时清空列表
      demandList.value = []
      pagination.total = 0
    }
  } catch (error) {
    console.error('请求需求列表失败:', error)
    showToast('网络错误，请稍后重试')
    // 异常时清空列表
    demandList.value = []
    pagination.total = 0
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  fetchDemandList()
}

// 取消搜索
const handleCancel = () => {
  searchKey.value = ''
  pagination.page = 1
  fetchDemandList()
}

// 分页处理
const handlePageChange = (page) => {
  pagination.page = page
  fetchDemandList()
}

// 查看详情
const handleDetail = (item) => {
  currentDemand.value = item
  showDetail.value = true
}

// 下载清单
const handleDownload = (year) => {
  showToast(`${year}清单下载功能开发中`)
}

// 页面挂载时获取数据
onMounted(() => {
  fetchDemandList()
})
</script>

<template>
  <div class="demand-list">
    <!-- 搜索框 -->
    <div class="search-container">
      <van-search v-model="searchKey" placeholder="请输入关键词搜索" show-action :show-filter="true" :clearable="false"
        wrap-with-form @search="handleSearch" @cancel="handleCancel" shape="round" @click-right-icon="handleSearch">
        <template #right-icon>
          <a style="text-decoration:none">搜索</a>
        </template>
      </van-search>
    </div>

    <!-- 下载按钮 -->
    <div class="download-container">
      <van-button type="default" size="small" @click="handleDownload('2025')">
        2025年清单下载
      </van-button>
      <van-button type="default" size="small" @click="handleDownload('2024')">
        2024年清单下载
      </van-button>
    </div>

    <!-- 列表区域 -->
    <div class="list-container">
      <!-- 首次加载骨架屏 -->
      <div v-if="loading && isFirstLoad" class="loading-container">
        <van-skeleton title :row="5" animated />
      </div>

      <!-- 空数据状态 -->
      <div v-else-if="demandList.length === 0" class="empty-container">
        <van-empty description="暂无数据" />
      </div>

      <!-- 列表内容 -->
      <div v-else-if="Array.isArray(demandList)" class="list-wrapper">
        <!-- 加载遮罩层 -->
        <div v-if="loading" class="loading-mask">
          <van-loading type="spinner" color="#1989fa" />
        </div>

        <div v-for="item in demandList" :key="item?.id || item" class="list-item"
          @click="item && handleDetail(item)">
          <div class="item-content">
            <div class="item-title">{{ item?.project_name || '' }}</div>
            <div class="item-info-row">
              <div class="item-tags">
                <van-tag v-if="item?.tech_maturity" color="#ff9800" size="small" round>{{ item.tech_maturity
                }}</van-tag>
                <van-tag v-if="item?.intellectual_property" color="#9c27b0" size="small" round>{{
                  item.intellectual_property }}</van-tag>
              </div>
              <div class="item-date">{{ item?.create_time || '' }}</div>
            </div>
            <div class="item-intro">{{ item?.demand_overview || '' }}</div>
          </div>
          <div class="item-footer">
            <div class="footer-left">
              <van-tag v-if="item?.field" color="#4caf50" size="small" round>{{ item.field }}</van-tag>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div v-if="demandList.length > 0" class="pagination-container">
        <div class="pagination-info">
          共 {{ pagination.total }} 条
        </div>
        <van-pagination v-model="pagination.page" :total-items="pagination.total" :show-page-size="5"
          :items-per-page="pagination.size" :disabled="loading" @change="handlePageChange">
          <template #prev-text>
            <van-icon name="arrow-left" />
          </template>
          <template #next-text>
            <van-icon name="arrow-right" />
          </template>
          <template #page="{ text }">{{ text }}</template>
        </van-pagination>
      </div>
    </div>

    <!-- 详情弹窗 -->
    <van-popup v-model:show="showDetail" position="bottom" :style="{ height: '80%', borderRadius: '16px 16px 0 0' }">
      <div class="detail-content">
        <div class="detail-header">
          <h3>{{ currentDemand?.project_name }}</h3>
          <van-icon name="cross" size="20" color="#999" @click="showDetail = false" />
        </div>

        <div class="detail-body">
          <div class="detail-section">
            <h4 class="section-title">
              <van-icon name="info-o" color="#2196f3" size="18" />
              基本信息
            </h4>
            <div class="detail-item">
              <span class="item-label">所属领域：</span>
              <span class="item-value">{{ currentDemand?.field }}</span>
            </div>
            <div class="detail-item">
              <span class="item-label">技术成熟度：</span>
              <span class="item-value">{{ currentDemand?.tech_maturity }}</span>
            </div>
            <div class="detail-item">
              <span class="item-label">知识产权要求：</span>
              <span class="item-value">{{ currentDemand?.intellectual_property }}</span>
            </div>
            <div class="detail-item">
              <span class="item-label">预期技术指标：</span>
              <span class="item-value">{{ currentDemand?.expected_tech_target }}</span>
            </div>
          </div>

          <div class="detail-section">
            <h4 class="section-title">
              <van-icon name="user-o" color="#4caf50" size="18" />
              填报单位信息
            </h4>
            <div class="detail-item">
              <span class="item-label">填报企业：</span>
              <span class="item-value">{{ currentDemand?.filling_enterprise }}</span>
            </div>
            <!-- <div class="detail-item">
              <span class="item-label">联系人：</span>
              <span class="item-value">{{ currentDemand?.contact_person }}</span>
            </div>
            <div class="detail-item">
              <span class="item-label">联系电话：</span>
              <span class="item-value">{{ currentDemand?.contact_phone }}</span>
            </div> -->
            <div class="detail-item">
              <span class="item-label">企业意向资金：</span>
              <span class="item-value">{{ currentDemand?.enterprise_intended_fund }}万元</span>
            </div>
          </div>

          <div class="detail-section">
            <h4 class="section-title">
              <van-icon name="comment-o" color="#ff9800" size="18" />
              需求概述
            </h4>
            <div class="detail-content-text">{{ currentDemand?.demand_overview }}</div>
          </div>
          
          <!-- 发布时间 -->
          <div style="font-size: 12px; color: #999; margin-top: 10px; padding-top: 10px; border-top: 1px solid #eee;">
            发布时间：{{ currentDemand?.create_time || '未设置' }}
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<style scoped>
/* 搜索容器 */
.search-container {
  background-color: white;

}

.search-container :deep(.van-search) {
  padding-left: 0;
}

/* 下载按钮容器 */
.download-container {
  display: flex;
  gap: 10px;
  padding: 0 0 15px 0;
  background-color: white;
}

/* 列表容器 */
.list-container {
  padding: 15px;
  background-color: #f5f5f5;
}

/* 加载状态 */
.loading-container {
  background-color: white;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
}

/* 空数据状态 */
.empty-container {
  background-color: white;
  border-radius: 8px;
  padding: 30px 0;
}

/* 列表包装器 */
.list-wrapper {
  display: flex;
  flex-direction: column;
  gap: 15px;
  position: relative;
}

/* 加载遮罩层 */
.loading-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
  z-index: 10;
}

/* 列表项 */
.list-item {
  background-color: white;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.list-item:active {
  transform: translateY(1px);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.05);
}

/* 列表项内容 */
.item-content {
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 15px;
}

/* 标题 */
.item-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
  line-height: 1.4;
}

/* 信息行 */
.item-info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

/* 标签容器 */
.item-tags {
  display: flex;
  gap: 5px;
}

/* 简介 */
.item-intro {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

/* 列表项底部 */
.item-footer {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

/* 底部左侧 */
.footer-left {
  display: flex;
  gap: 5px;
}

/* 日期 */
.item-date {
  font-size: 12px;
  color: #999;
  text-align: right;
}

/* 分页容器 */
.pagination-container {
  margin-top: 15px;
  text-align: center;
  background-color: white;
  border-radius: 8px;
  padding: 15px;
}

/* 分页信息 */
.pagination-info {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
  text-align: center;
}

/* 详情弹窗 */
.detail-content {
  height: 100%;
  overflow-y: auto;
  padding: 15px;
  font-size: 14px;
}

/* 详情头部 */
.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.detail-header h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  color: #333;
  line-height: 1.4;
}

/* 详情主体 */
.detail-body {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* 详情区块 */
.detail-section {
  background-color: #fafafa;
  border-radius: 8px;
  padding: 12px;
}

/* 区块标题 */
.section-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 12px 0;
  color: #333;
  padding-bottom: 6px;
  border-bottom: 1px solid #e0e0e0;
}

/* 详情项 */
.detail-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
  line-height: 1.5;
}

.detail-item:last-child {
  margin-bottom: 0;
}

/* 详情标签 */
.item-label {
  font-weight: 500;
  color: #666;
  min-width: 100px;
  font-size: 14px;
}

/* 详情值 */
.item-value {
  flex: 1;
  color: #333;
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}

/* 详情内容文本 */
.detail-content-text {
  color: #333;
  line-height: 1.6;
  text-align: justify;
  white-space: pre-line;
  font-size: 14px;
}

/* 区块标题 */
.section-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 12px 0;
  color: #333;
  padding-bottom: 6px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* 适配小屏幕 */
@media (max-width: 375px) {
  .search-container :deep(.van-search) {
    padding-left: 0;
  }

  .download-container {
  }

  .list-container {
    padding: 12px;
  }

  .list-item {
    padding: 12px;
  }

  .item-title {
    font-size: 15px;
  }

  .item-intro {
    font-size: 13px;
  }

  .detail-content {
    padding: 15px;
  }

  .detail-header h3 {
    font-size: 17px;
  }

  .section-title {
    font-size: 15px;
  }

  .detail-section {
    padding: 12px;
  }
}
</style>