<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in customerList" :key='index'>
				<view class="item">
					<view>客户姓名:{{item.nickName}}</view>
					<view>客户电话:{{item.mobile}}</view>
					<view>加入时间:{{item.serviceTime | dateToYYmmdd}}</view>
				</view>
			</block>
		</view>
		<no-data v-if="!customerList.length" :showText="'暂无客户'"></no-data>
		<loading-text v-if="customerList.length"  :loading="loading" :noMore="noMore" ></loading-text>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	
	export default {
		data() {
			return {
				customerList: [],
				pageNum: 1,
				pageSize: 10,
				noMore: false,
				loading: false,
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad() {
			this.getCustomerList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getCustomerList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getCustomerList(1);
		},
		
		methods: {
			// 获取商品列表
			getCustomerList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/user/customer',
					method: 'get',
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						userId: this.userId,
					},
					isLoading: !loadMore
				}).then(res => {
					let _list = res.data.records;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.customerList = this.customerList.concat(_list);
						this.loading = false;
					} else {
						this.customerList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
	}
	.list-container {
		padding: 20rpx;
		.item {
			background: #FFFFFF;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			padding: 20rpx;
			font-size: 28rpx;
			height: 180rpx;
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			justify-content: space-between;
		}
	}
</style>