<template>
  <tabs :commentNumber="badCommentCount" @switchTab="switchTab"></tabs>
  <scroll-view
    style="height: calc(100vh - 94rpx)"
    scroll-y
    :enable-back-to-top="true"
    @scrolltolower="touchBottom(page)"
  >
    <template v-if="totalCount > 0">
      <commentList
        v-for="(item, index) in commentListData"
        :key="index"
        :type="type"
        :comment="item"
        @TapMsg="TapMsg"
      ></commentList>
    </template>

    <jx-load-more v-show="totalCount >= 5" :isLoad="isLoad" />
  </scroll-view>

  <jx-empty title="暂无评价" v-show="totalCount == 0" />

  <!-- 公共组件 -->
  <jx-loading />
</template>

<script lang="ts" setup>
import commentList from './commentList/commentList.vue'
import tabs from './tabs/tabs.vue'
import { onLoad } from '@dcloudio/uni-app'
import { ref, Ref } from 'vue'
import { getStorage } from '@/utils/storage'
import toast from '@/utils/toast'
import { jxNextPage } from '@/utils/tools'
import merchant from "@/api/https/merchant"
/**
 * 评价数量
 */
const badCommentCount: Ref<number> = ref(0)
onLoad(async (options) => {
  badCommentCount.value = +options?.number
  await getComment()
})

/**
 * 切换tab
 */
const type: Ref<number> = ref(0)
function switchTab(index: number) {
  commentListData.value = []
  type.value = index
  getComment()
}

/**
 * 获取评论内容
 */
interface CommentListType {
  createdAt: string
  id: number
  order_flag: string
  order_id: string
  score4: number
  score4Content: string
  status: number
  updatedScore: string
  updatedScoreContent: string
  updatedVenderTags: string
  userPhone: string
  venderTags: string
  vendorOrderID2: string
  maxModifyTime: string,
  msg:string
}
const page: Ref<number> = ref(1)
const size: Ref<number> = ref(20)
const totalCount: Ref<number> = ref(0) // 总条数
const commentListData: Ref<Array<CommentListType>> = ref([])
async function getComment() {
  isLoad.value = true
  let data = {
    jxStoreId: getStorage('storeID'),
    type: type.value,
    page: page.value,
    size: size.value,
  }
  let commentRes = await merchant.Tmp_get_jx_bad_comments_by_storeId(data)
  if (commentRes.code == 0) {
    totalCount.value = commentRes.data.total
    commentListData.value = commentListData.value.concat(
      commentRes.data.list || []
    )
  } else {
    toast('评论数据异常', 2)
  }
  isLoad.value = false
}

/**
 * 提示信息
 */
const message = ref()
function TapMsg() {
  toast('美团差评暂无订单信息')
}

/**
 *
 * 页面触底
 */
const isLoad = ref<boolean>(false)
function touchBottom(pageNo: number) {
  jxNextPage(pageNo, size.value, totalCount.value, (statu: boolean) => {
    if (statu) {
      // 下一页
      page.value++
      getComment()
    } else {
      // 没有下一页
      isLoad.value = false
    }
  })
}
</script>

<style lang="scss">
.commpent_jx_5648421 {
  position: sticky;
  top: 0;
}
</style>