<template>
  <view class="list-page-container">
    <view class="search-header">
      <uv-input placeholder="搜索公告标题" v-model="queryParams.noticeTitle" prefixIcon="search" clearable shape="circle"
        @confirm="onSearch"></uv-input>
    </view>

    <view class="list-container">
      <scroll-view scroll-y class="scroll-list" refresher-enabled :refresher-triggered="isTriggered"
        @refresherrefresh="onRefresh" @scrolltolower="loadMore">
        <uv-empty v-if="!list.length && loadStatus !== 'loading'" mode="list"></uv-empty>

        <view class="notice-card-item" v-for="item in list" :key="item.id" @click="toDetail(item.id)">
          <view class="notice-main">
            <view class="notice-title-row">
              <text class="tag" v-if="item.isTop === '10'">置顶</text>
              <text class="title">{{ item.noticeTitle }}</text>
            </view>
            <view class="notice-info">
              <text class="author">{{ item.createdName }}</text>
              <text class="time">{{ formatDate(item.noticeTime, 'YYYY-MM-DD HH:mm') }}</text>
            </view>
          </view>
          <uv-icon name="arrow-right" color="#ccc" size="16"></uv-icon>
        </view>

        <uv-load-more v-if="list.length > 0 || loadStatus === 'loading'" :status="loadStatus"
          @loadmore="loadMore"></uv-load-more>
      </scroll-view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { debounce } from 'lodash-es';
import { useSystemApi } from '@/api/system/index';
import { onRouterPush } from '@/utils/router';
import { formatDate } from '@/utils/date';
import { DEFAULT_PAGE_SIZE } from '@/constants/index';

const systemApi = useSystemApi();

const queryParams = ref({
  noticeTitle: '',
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
  noticeStatus: "20"
});

const list = ref<any[]>([]);
const loadStatus = ref<'loadmore' | 'loading' | 'nomore'>('loadmore');
const isTriggered = ref(false);

const fetchListData = async (reset = false) => {
  if (reset) {
    queryParams.value.pageNum = 1;
  }
  loadStatus.value = 'loading';

  try {
    const res: any = await systemApi.getNoticeList(queryParams.value);
    if (res.code == 200) {
      const records = res.data?.list || [];
      list.value = reset ? records : [...list.value, ...records];

      if (list.value.length >= (res.data?.total || 0)) {
        loadStatus.value = 'nomore';
      } else {
        loadStatus.value = 'loadmore';
        queryParams.value.pageNum += 1;
      }
    } else {
      loadStatus.value = 'nomore';
    }
  } catch (error) {
    console.error(error);
    loadStatus.value = 'nomore';
  } finally {
    if (reset) isTriggered.value = false;
  }
};

const onSearch = () => {
  fetchListData(true);
};

const debouncedSearch = debounce(() => {
  fetchListData(true);
}, 500);

watch(() => queryParams.value.noticeTitle, () => {
  debouncedSearch();
});

const onRefresh = async () => {
  isTriggered.value = true;
  await fetchListData(true);
};

const loadMore = () => {
  if (loadStatus.value !== 'loadmore') return;
  fetchListData(false);
};

const toDetail = (id: number) => {
  onRouterPush(`/pages/notice/detail?id=${id}`);
};

onLoad(() => {
  fetchListData(true);
});
</script>

<style lang="scss" scoped>
.list-page-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.search-header {
  background-color: #fff;
  padding: 20rpx 30rpx;
  position: sticky;
  top: 0;
  z-index: 10;
}

.list-container {
  flex: 1;
  overflow: hidden;
  padding: 20rpx;
}

.scroll-list {
  height: 100%;
}

.notice-card-item {
  background-color: #fff;
  padding: 30rpx;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.03);

  .notice-main {
    flex: 1;
    margin-right: 20rpx;

    .notice-title-row {
      display: flex;
      align-items: flex-start;
      margin-bottom: 16rpx;

      .tag {
        background-color: #ff4d4f;
        color: #fff;
        font-size: 20rpx;
        padding: 2rpx 8rpx;
        border-radius: 4rpx;
        margin-right: 12rpx;
        flex-shrink: 0;
        margin-top: 6rpx;
      }

      .title {
        font-size: 30rpx;
        font-weight: bold;
        color: #333;
        line-height: 1.4;
      }
    }

    .notice-info {
      display: flex;
      font-size: 24rpx;
      color: #999;

      .author {
        margin-right: 20rpx;
      }
    }
  }
}
</style>
