<template>
  <div class="home">
    <!-- 顶部区域 -->
    <div class="home-top">
      <div class="home-top-inner w1100">
        <div class="home-title">不可移动文物资源数据库</div>
        <!-- 搜索栏：两侧卷轴 -->
        <div class="search-bar">
          <img class="paper paper-left" src="@/assets/img/paper_l.png" alt="left" />
          <el-input
            v-model="keyword"
            placeholder="请输入关键词"
            clearable
            class="search-input"
            @keyup.enter="onSearch"
            @clear="onSearch"
          />
          <img class="paper paper-right" src="@/assets/img/paper_l.png" alt="right" />
        </div>
      </div>
    </div>

    <!-- 列表区域 -->
    <div class="home-list w1100">
      <ul>
        <li v-for="(item, idx) in list" :key="idx" class="list-item">
          <div class="item-left">
            <div class="item-title limit-line">{{ item.title }}</div>
            <div class="item-meta">
              <span class="meta-item"><img src="@/assets/img/icon_time.svg" alt="size" />上传时间：{{ item.uploadTime }}</span>
              <span class="meta-item"><el-icon :size="16"><LocationInformation /></el-icon>点位数：{{ item.shootCount }}</span>
              <span class="meta-item"><img src="@/assets/img/icon_data.svg" alt="size" />数据大小：{{ formatSize(item.space) }}</span>
            </div>
          </div>
          <div class="item-right">
            <el-button link type="primary" @click="onView(item)">查看</el-button>
          </div>
        </li>
      </ul>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          v-model:current-page="pageNum"
          :page-size="pageSize"
          :total="total"
          @current-change="onPageChange"
        />
      </div>
    </div>
  </div>
  
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/api'

const keyword = ref('')
const pageNum = ref(1)
const pageSize = 10
const total = ref(0)
const list = ref([])

const router = useRouter()

async function fetchList() {
  try {
    const res = await api.getSceneListApi({
      pageNum: pageNum.value,
      pageSize,
      orderBy: '',
      sortBy: '',
      title: keyword.value || ''
    })
    const records = res?.pageData
    list.value = Array.isArray(records) ? records : []
    total.value = res?.total || res?.count || res?.page?.total || list.value.length || 0
  } catch (e) {
    console.error('获取场景列表失败:', e)
    list.value = []
    total.value = 0
  }
}

function onSearch() {
  pageNum.value = 1
  fetchList()
}

function onPageChange(page) {
  pageNum.value = page
  fetchList()
}

onMounted(() => {
  fetchList()
})

function formatSize(val) {
  const bytes = Number(val) || 0
  const MB = 1024 * 1024
  const GB = MB * 1024
  if (bytes >= GB) return (bytes / GB).toFixed(2) + 'GB'
  return (bytes / MB).toFixed(2) + 'MB'
}

function onView(item) {
  router.push({ name: 'pushSystem' })
}
</script>

<style lang="scss" scoped>
.home {
  background: #F5F3F0;
  min-height: 100vh;
}

.home-top {
  height: 240px;
  background-image: url('@/assets/img/home-bg.png');
  background-repeat: no-repeat;
  background-position: right top;
  background-size: auto 100%;

  .home-top-inner {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow: visible !important;
  }

  .home-title {
    font-family: 'Source Han Serif CN-Bold';
    font-size: 28px;
    color: #781C0B;
    letter-spacing: 2px;
    margin-bottom: 18px;
  }

  .search-bar {
    position: relative;
    width: 560px;
  }

  .search-input :deep(.el-input__wrapper) {
    height: 40px;
    border-color: #93795D;
    border-left: none;
    border-radius: 0;
  }

  .paper {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 48px;
    pointer-events: none;
    z-index: 1;
  }
  .paper-left { left: -13px; }
  .paper-right { right: -13px; }
}

.home-list {
  padding: 40px 64px;
  height: calc(100vh - 240px);
  background: #fff;
  overflow-y: auto;
  .item-left{
    line-height: 30px;
  }
  .list-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 8px;
    border-bottom: 1px solid #F5F5F5;
  }
  .item-title {
    font-size: 16px;
    color: #333;
    margin-bottom: 16px;
  }
  .item-meta {
    width: 1000px;
    color: #8c8c8c;
    display: flex;
    justify-content: space-between;
    gap: 24px;
    font-size: 13px;
    .meta-item{
      display: flex;
      height: 20px;
      line-height: 20px;
      gap: 4px;
      img{
        width: 16px;
        height: 16px;
      }
    }
  }
  .item-right {
    width: 80px;
    text-align: right;
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    padding-top: 24px;
  }
}
</style>
