<template>
  <view class="Mall4j page-message-detail">
    <view class="tit">
      {{ noticeDetails.title }}
    </view>
    <view class="time">
      {{ shopName }} {{ noticeDetails.publishTime }}
    </view>
    <view class="con">
      <uv-parse :content="content" />
    </view>
  </view>
</template>

<script setup>
import UvParse from '@/components/uv-parse/components/uv-parse/uv-parse.vue'

const noticeDetails = ref({})
const content = ref('')
const shopName = ref('')

onLoad((options) => {
  onQueryDetailsOfNotice(options.id)
  shopName.value = options.shopName || ''
})

/**
 * 请求公告详情
 */
const onQueryDetailsOfNotice = (id) => {
  http.request({
    url: '/tmerclub_admin/ma/notice/info',
    method: 'GET',
    data: {
      id
    }
  }).then(res => {
    noticeDetails.value = res
    if (!res.content) {
      return ''
    }
    content.value = res.content
    if (res.shopId !== 0) {
      onQueryShopInfo(res.shopId)
    }
    if (res.status === 0) onErrBack()
  }).catch(() => {
    onErrBack()
  })
}

const onQueryShopInfo = (id) => {
  // 获取店铺头部信息
  http.request({
    url: '/tmerclub_admin/ua/shop_detail/head_info',
    method: 'GET',
    data: {
      shopId: id
    }
  }).then(res => {
    const startDate = new Date(res.contractStartTime).getTime()
    const endtDate = new Date(res.contractEndTime).getTime()
    const timestamp = Date.parse(new Date())
    if (res.shopStatus === 0) {
      let msg = ''
      if (timestamp < startDate) {
        msg = '商家尚未营业'
      }
      if (timestamp > endtDate) {
        msg = '商家已暂停营业'
      }
      uni.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        success (res) {
          if (res.confirm) {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }
        }
      })
    }
    if (res.shopStatus === 2 || res.shopStatus === 6) {
      uni.showModal({
        title: '提示',
        content: '店铺已下线',
        showCancel: false,
        success (res) {
          if (res.confirm) {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }
        }
      })
    }
  }).catch(res => {
    // 如果不存在店铺或其他错误
    uni.showModal({
      title: '提示',
      content: res.msg,
      showCancel: false,
      success (res) {
        if (res.confirm) {
          uni.switchTab({
            url: '/pages/index/index'
          })
        }
      }
    })
  })
}

const onErrBack = () => {
  uni.showToast({
    title: '公告不存在',
    icon: 'none',
    duration: 2000
  })
  setTimeout(() => {
    uni.navigateBack({ delta: 1 })
  }, 500)
}

</script>

<style lang="scss" scoped>
@use "./message-detail.scss";
</style>
