<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in customerList" :key='index'>
				<view class="item">
					<view class="row">客户姓名：{{item.nickName}}</view>
					<view class="row">客户电话：{{item.mobile}}</view>
					<view class="row">加入时间：{{item.serviceTime ? (item.serviceTime | dateToYYmmdd) : ''}}</view>
					<view class="row">最近访问时间：{{item.lastAccessTime || ''}}</view>
					<view class="title">客户标签</view>
					<view class="tags" @tap="toChooseTag(item.unionId)">
						<view class="left" v-if="item.allTags && item.allTags.length > 0">
							<block v-for="(it, idx) in item.allTags" :key='idx'>
								<text>{{it.tagName}}</text>
							</block>
						</view>
						<view class="no" v-else>添加标签</view>
						<image src="@/static/icon/right.png"></image>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!customerList.length" :showText="'暂无客户'"></no-data>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	
	export default {
		data() {
			return {
				customerList: [],
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onLoad() {
			this.getCustomerList();
			
			uni.$on('refreshCustomerData',() => {
				this.getCustomerList();
			})
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.getCustomerList();
		},
		
		methods: {
			// 获取客户列表
			getCustomerList() {
				this.$axios({
					url: '/tag/wx/customer',
					method: 'get',
					params: {},
				}).then(res => {
					if(res.data && res.data.length > 0) {
						res.data.forEach(item => {
							item.allTags = item.tags.concat(item.customtags);
						})
					}
					this.customerList = res.data || [];
					
					uni.stopPullDownRefresh();
				})
			},
			
			// 去选择标签
			toChooseTag(unionId) {
				uni.navigateTo({
					url: '/pages/mine/customer/tag?unionId=' + unionId
				})
			},
		}
	}
</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;
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			.row {
				margin-bottom: 10rpx;
			}
			.title {
				margin-top: 10rpx;
			}
			.tags {
				margin-top: 8rpx;
				padding: 16rpx 20rpx 6rpx;
				border-radius: 10rpx;
				background: #F1F1F1;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.left {
					text {
						display: inline-block;
						font-size: 24rpx;
						padding: 0 16rpx;
						height: 48rpx;
						line-height: 48rpx;
						border-radius: 10rpx;
						background: #FFD6BB;
						color: #FE781F;
						border: 1px solid rgba($color: #FE781F, $alpha: 0.4);
						margin-right: 10rpx;
						margin-bottom: 10rpx;
						&:last-child {
							margin-right: 0;
						}
					}
				}
				.no {
					color: #666666;
					margin-bottom: 10rpx;
				}
				image {
					flex-shrink: 0;
					width: 14rpx;
					height: 28rpx;
					margin-left: 10rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
