<template>
	<view>
		<view class="list">
			<view class="item" v-for="item in listData.list" :key="item.id">
				<view class="top">
					<text class="t">用户ID：{{ item.userId }}</text>
				</view>
				<view class="user-info">
					<view class="left">
						<u-avatar :src="item.headImg" shape="square" :size="50"></u-avatar>
						<view class="info">
							<text class="t">{{ item.nickName }}</text>
							<text class="t">{{ item.phone }}</text>
						</view>
					</view>
					<view class="right">
						<text class="t1">当前积分</text>
						<text class="t2">{{ item.pointBalance }}</text>
					</view>
				</view>
				<view class="footer-wrap">
					<view class="btn" @click="toDetail(item)">
						<u-text type="primary" text="查看明细"></u-text>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="listData.status"></u-loadmore>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { pointUserPage } from '@/http/api/market/point.js';

const listData = reactive({
	page: 1,
	size: 10,
	status: 'loading',
	list: []
});

function reachBottom() {
	if (listData.status != 'nomore') {
		listData.page++;
		pointUserPageAjax();
	}
}

// 获取用户所有门店下积分列表
async function pointUserPageAjax(page = listData.page, isPull = false) {
	try {
		const res = await pointUserPage({
			page: page,
			size: listData.size
		});

		if (listData.page == 1) {
			listData.list = res.records;
		} else {
			listData.list.push(...res.records);
		}

		if (res.pageNumber >= res.totalPage) {
			listData.status = 'nomore';
		}
	} catch (error) {
		console.log(error);
	}
	if (isPull) {
		setTimeout(() => {
			uni.showToast({
				title: '刷新成功',
				icon: 'none'
			});
			uni.stopPullDownRefresh();
		}, 300);
	}
}

// 去积分详情
function toDetail(item) {
	uni.navigateTo({
		url: `/pageMarket/points/userPointDetail?id=${item.id}&nickName=${item.nickName}&phone=${item.phone}&point=${item.pointBalance}`
	});
}

defineExpose({
	reachBottom,
	pointUserPageAjax
});

onMounted(() => {
	pointUserPageAjax();
});
</script>

<style scoped lang="scss">
.list {
	padding-bottom: 28upx;
	.item {
		background-color: #fff;
		border-radius: 20upx;
		padding: 28upx;
		&:not(:first-child) {
			margin-top: 28upx;
		}
		.top {
			display: flex;
			align-items: center;
			justify-content: space-between;
			.t {
				font-size: 28upx;
				color: #999;
			}
		}
		.user-info {
			display: flex;
			padding: 28upx 0;
			.left {
				flex: 1;
				display: flex;
				.info {
					flex: 1;
					display: flex;
					justify-content: center;
					flex-direction: column;
					padding-left: 10px;
					.t {
						font-size: 28upx;
						color: #333;
					}
				}
			}
			.right {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				.t1 {
					font-size: 28upx;
					color: #666;
				}
				.t2 {
					color: #333;
					font-size: 32upx;
					font-weight: bold;
				}
			}
		}
		.footer-wrap {
			display: flex;
			justify-content: flex-end;
		}
	}
}
</style>
