<template>
    <z-paging
        auto-show-back-to-top
        :auto="i == index"
        ref="paging"
        v-model="dataList"
        :data-key="i"
        @query="queryList"
        :fixed="false"
        height="100%"
    >
        <block v-for="(item, index) in dataList" :key="index">
            <Card
                :comment="item.comment"
                :commentUrlList="item.commentUrlList"
                :reply="item.reply"
                :createTime="item.createTime"
                :goodsId="item.goodsId"
                :serviceComment="item.serviceComment"
                :user="item.userInfoVo"
            ></Card>
        </block>
    </z-paging>
</template>

<script lang="ts" setup>
import { ref, watch, nextTick, shallowRef, unref } from 'vue'
import Card from './card.vue'
import { apiEvaluateGoodsLists } from '@/api/goods'

const paging = shallowRef<any>(null)
const dataList = ref<any>([])
const isFirst = ref<boolean>(true)

const props = withDefaults(
    defineProps<{
        goodsId: number | string
        i: number
        index: number
        cid: number
    }>(),
    {
        goodsId: 0,
        cid: 0
    }
)

watch(
    () => props.index,
    async () => {
        await nextTick()
        if (props.i == props.index && unref(isFirst)) {
            isFirst.value = false
            paging.value?.reload()
        }
    },
    { immediate: true }
)

const queryList = async (pageNo: number, pageSize = 100) => {
    try {
        const { lists } = await apiEvaluateGoodsLists({
            goodsId: +props.goodsId,
            commentLevel: props.i,
            pageNo,
            pageSize: 100
        })
        paging.value.complete(lists)
    } catch (e) {
        console.log('报错=>', e)
        //TODO handle the exception
        paging.value.complete(false)
    }
}
</script>

<style scoped></style>
