<template>
  <view class="w-body">
    <w-public />
    <w-navbar left-back fixed :title="title"></w-navbar>
    <mescroll-body @init="mescrollInit" @down="downCallback" @up="upCallback">
      <u-cell-group>
        <u-cell v-for="item in list" :key="item.noticeId" size="12" :title="item.noticeTitle" isLink :url="'/pages/index/notice?title='+title+'&id='+item.noticeId+'&showTime='+showTime+'&showTitle='+showTitle"></u-cell>
      </u-cell-group>
    </mescroll-body>
  </view>
</template>

<script setup>
import { onLoad, onShow, onReady } from '@dcloudio/uni-app'
import { ref, getCurrentInstance } from 'vue'
import { listNotice } from '@/api/system/notice'

const { proxy } = getCurrentInstance();

const title = ref("");

const showTitle = ref("");//显示文章标题
const showTime = ref("");


const content = ref("");

onLoad(async (e) => {
  title.value = e.title;
  params.value.noticeType = e.type;
  showTitle.value = e.showTitle || '';
  showTime.value = e.showTime || '';
})

const params = ref({ orderByColumn: 'create_time,notice_id', isAsc: 'desc,desc', noticeType: 0, status: 0 });


const doSearch = () => {
  getMescroll().resetUpScroll();
  getMescroll().scrollTo(0,0);
}

onShow(async () => {
  await nextTick();
  doSearch();
});
//分页逻辑
import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
import useMescroll from "@/uni_modules/mescroll-uni/hooks/useMescroll.js"
const { mescrollInit, downCallback, getMescroll } = useMescroll(onPageScroll, onReachBottom)

const list = ref([]);
// 上拉加载的回调: 其中num:当前页 从1开始, size:每页数据条数,默认10
const upCallback = (mescroll) => {
  params.value.current = mescroll.num;
  params.value.size = mescroll.size;
  listNotice(params.value).then(res => {
    console.log(res);
    const curPageData = res.rows || [] // 当前页数据

    if (mescroll.num == 1) list.value = []; // 第一页需手动制空列表
    list.value = list.value.concat(curPageData); //追加新数据

    mescroll.endBySize(curPageData.length, res.total); // 请求成功, 结束加载
  }).catch(() => {
    mescroll.endErr(); // 请求失败, 结束加载
  })
}


</script>

<style lang="scss" scoped>
.w-body {
  --navbar-height: 44px;
  background-color: var(--w-bg);
  font-size: .9rem;
  min-height: 100vh;
}
</style>
