<template>
	<view>
		<view class="edgeInsetTop"></view>
		<view class="wanl-goods-comment">
			<empty v-if="listData.length === 0 && status !== 'loading'" :text="$t('comment.empty')" />
			<view class="list padding-bj radius-bock" v-for="item in listData" :key="item.id">
				<view class="userinfo">
					<view class="avatar">
						<view class="cu-avatar round margin-right-xs" :style="{ backgroundImage: 'url(' + $appKit.oss(user.avatar, 35, 35, 2, 'avatar') + ')' }"></view>
						<view class="text-sm">
							<view>{{ user.nickname }}</view>
							<view class="text-gray">{{ $appKit.timeToDate(item.createtime) }}</view>
						</view>
					</view>
					<view class="rate-stars">
						<text v-for="n in 5" :key="n" class="rate-star" :class="n <= item.score ? 'filled' : ''">★</text>
					</view>
				</view>
				<view class="details">
					<view class="margin-bottom-sm">{{ item.content || $t('comment.noContent') }}</view>
				</view>
				<view class="grid flex-sub grid-square" :class="[item.images && item.images.length > 3 ? 'col-3' : 'col-' + (item.images ? item.images.length : 0)]" v-if="item.images && item.images.length !== 0">
					<view class="bg-img" v-for="(image, index) in item.images" :key="index" @tap="previewImage(item.images, index)" :style="{ backgroundImage: 'url(' + $appKit.oss(image, 248, 0) + ')' }"></view>
				</view>
				<view class="goods margin-bottom-bj" @tap="onGoods(item.goods_id)">
					<view class="cu-avatar" :style="{ backgroundImage: 'url(' + $appKit.oss(item.order_goods.image, 66, 66) + ')' }"></view>
					<view class="content margin-left-bj">
						<view class="text-cut">
							{{ item.order_goods.title }}
						</view>
						<view class="margin-top-sm">
							<text class="text-price margin-right-bj">{{ item.order_goods.price }}</text>
							<text class="text-orange text-min">{{ item.state_text }}</text>
						</view>
					</view>
				</view>
			</view>
			<uni-load-more :status="status" :content-text="contentText" />
		</view>
	</view>
</template>

<script>
import { mapState } from 'vuex'
import Empty from '@/components/empty/empty.vue'
import { pullMyComments } from '@/features/mall/gateway/product'

export default {
	components: { Empty },
	computed: {
		...mapState(['user'])
	},
	data() {
		return {
			listData: [],
			reload: false,
			current_page: 1,
			last_page: 1,
			status: 'loading',
			contentText: {
				contentdown: ' ',
				contentrefresh: this.$t('comment.loading'),
				contentnomore: this.$t('comment.noMore')
			}
		}
	},
	onLoad() {
		this.loadData()
	},
	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.status = 'loading'
			this.loadData()
		}
	},
	methods: {
		async loadData() {
			try {
				const res = await pullMyComments({ page: this.current_page })
				uni.stopPullDownRefresh()
				this.listData = this.reload ? res.list : this.listData.concat(res.list)
				this.current_page = res.currentPage
				this.last_page = res.lastPage
				this.status = res.total === 0 ? 'noMore' : 'more'
			} catch (e) {
				uni.stopPullDownRefresh()
				this.status = 'noMore'
			}
		},
		previewImage(images, index) {
			const imgArr = images.map(img => this.$appKit.oss(img, 500))
			uni.previewImage({
				urls: imgArr,
				current: imgArr[index],
				indicator: 'number'
			})
		},
		onGoods(id) {
			if (id) {
				uni.navigateTo({ url: '/pages/product/goods?id=' + id })
			}
		}
	}
}
</script>

<style>
.wanl-goods-comment .list {
	background-color: #ffffff;
	margin-bottom: 20rpx;
}
.wanl-goods-comment .userinfo {
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.wanl-goods-comment .userinfo .avatar {
	display: flex;
	align-items: center;
}
.wanl-goods-comment .userinfo .cu-avatar {
	width: 70rpx;
	height: 70rpx;
}
.rate-stars {
	display: flex;
}
.rate-star {
	font-size: 28rpx;
	color: #ddd;
	margin-left: 4rpx;
}
.rate-star.filled {
	color: #ffca28;
}
.wanl-goods-comment .details {
	margin-top: 16rpx;
}
.wanl-goods-comment .goods {
	display: flex;
	align-items: center;
	background-color: #f8f8f8;
	padding: 20rpx;
	border-radius: 16rpx;
	margin-top: 16rpx;
}
.wanl-goods-comment .goods .cu-avatar {
	width: 100rpx;
	height: 100rpx;
	border-radius: 12rpx;
}
.wanl-goods-comment .goods .content {
	flex: 1;
	min-width: 0;
}
.wanl-goods-comment .goods .content .text-cut {
	font-size: 26rpx;
}
</style>