<template>
  <view>
    <view class="edgeInsetTop"></view>
    <view class="wanl-logistics-list margin-top-bj" v-if="dataList.length != 0">
      <block v-for="(item, index) in dataList" :key="item.id">
        <view class="text-center wanl-gray">
          {{ $appKit.timeToChat(item.createtime) }}
        </view>
        <view class="item margin-lr-bj margin-tb bg-white radius-bock padding-bj" @tap="handleTap(item.url)">
          <view class="title margin-bottom-bj">
            <text class="text-df">{{ item.title }}</text>
            <text class="text-sm wanl-gray-light wlIcon-fanhui2"></text>
          </view>
          <view class="action">
            <image :src="$appKit.oss(item.image, 82, 82)"></image>
            <view class="padding-xs flex flex-wrap align-content-between">
              <view class="text-df margin-bottom-xs">{{ item.content }}</view>
              <text class="text-sm wanl-gray">{{ $t('notice.from') }} {{ item.come }}</text>
            </view>
          </view>
        </view>
      </block>
    </view>
    <wanl-empty src="notice_default3x" :text="$t('notice.noCouponLiveProduct')" v-else />
    <view class="edgeInsetBottom"></view>
  </view>
</template>

<script>
import { pullNoticeList } from '@/features/mall/mall-gateway'
export default {
  data() {
    return {
      dataList: [],
      reload: false,
      total: 0,
      current_page: 1,
      last_page: 1,
      status: 'more',
      contentText: {
        contentdown: ' ',
        contentrefresh: '',
        contentnomore: ''
      }
    }
  },
  onLoad() {
    this.contentText.contentrefresh = this.$t('notice.loading')
    this.loadData()
    // 清除本地角标
    this.$store.commit('statistics/noticec', { notice: 0 })
  },
  onPullDownRefresh() {
    this.current_page = 1
    this.reload = true
    this.loadData()
  },
  onReachBottom() {
    if (this.current_page >= this.last_page) {
      this.status = 'noMore'
    } else {
      this.reload = false
      this.current_page = this.current_page + 1
      this.status = 'loading'
      this.loadData()
    }
  },
  methods: {
    async loadData() {
      try {
        const res = await pullNoticeList({
          type: 'notice',
          page: this.current_page
        })
        uni.stopPullDownRefresh()
        this.dataList = this.reload ? res.data : this.dataList.concat(res.data)
        this.total = res.total
        this.current_page = res.current_page
        this.last_page = res.last_page
        this.status = res.total == 0 ? 'noMore' : 'more'
      } catch (e) {
        uni.stopPullDownRefresh()
        console.error('[notice] 加載通知列表失敗:', e)
      }
    },
    handleTap(url) {
      if (url) {
        uni.navigateTo({ url })
      }
    }
  }
}
</script>

<style>

</style>