<template>
  <z-paging
    ref="pagingRef"
    v-model="disNotice"
    :paging-style="{'top': '0','background-color': '#f2f3f7'}"
    empty-view-text="暂无分销通知"
    @query="getDistributionMsg"
  >
    <view class="Mall4j page-distribution-notice">
      <!-- 分销公告 -->
      <view class="notices-box">
        <view
          v-for="item in disNotice"
          :key="item.msgId"
          class="item"
        >
          <view class="tit-box">
            <view class="tit">
              {{ item.msgTitle }}
            </view>
            <view class="time">
              {{ item.startTime.substring(0,10) }}
            </view>
          </view>
          <view class="text-box">
            <rich-text :nodes="item.content" />
          </view>
          <view
            class="text-arrow"
            @tap="toMessageDetail(item.msgId)"
          >
            查看详情
          </view>
        </view>
      </view>
    </view>
  </z-paging>
</template>

<script setup>

onLoad(() => {
  // 分销员信息
  getDisInfo()
})

/**
 * 获取分销员信息
 */
const getDisInfo = () => {
  http.request({
    url: '/tmerclub_marketing/distribution_user/distribution_user_info',
    method: 'GET'
  }).then(res => {
    if (res.state === -1) {
      uni.showModal({
        title: '',
        showCancel: false,
        content: $t('distribution.distributorBanned'),
        confirmColor: '#eb2444',
        success (res) {
          if (res.confirm) {
            uni.switchTab({
              url: '/pages/user/user'
            })
          }
        }
      })
    }

    if (res.state === 2) {
      uni.showToast({
        title: $t('distribution.distributorCleared'),
        mask: true,
        icon: 'none'
      })
      uni.showModal({
        title: '',
        content: $t('distribution.distributorCleared'),
        confirmText: $t('distribution.confirm'),
        confirmColor: '#eb2444',
        showCancel: false
      })
    }
  })
}

// 获取分销公告
const pagingRef = ref(null)
const disNotice = ref([])
const getDistributionMsg = () => {
  http.request({
    url: '/tmerclub_marketing/distribution_msg/page',
    method: 'GET',
    data: {
      pageNum: 1,
      pageSize: 20,
      isTop: ''
    }
  }).then(res => {
    for (let i = 0; i < res.list.length; i++) {
      const element = res.list[i]
      element.content = util.formatHtml(element.content)
    }
    // 将请求结果通过complete传给z-paging处理，同时也代表请求结束，这一行必须调用
    pagingRef.value.complete(res.list)
  })
}

// 公告去详情
const toMessageDetail = (id) => {
  uni.navigateTo({
    url: '/package-activities/pages/distribution-notice-detail/distribution-notice-detail?id=' + id
  })
}

</script>

<style lang="scss" scoped>
@use "./distribution-notice.scss";
</style>
