<template>
	<view class="wrap">
		<view v-if="flowList.length">
			<view
				class="flex-box"
				v-for="i in flowList"
				v-if="i.id != id"
				:key="i.id"
			>
				<u-image
					:src="i.face"
					class="flxleft"
					width="120"
					height="120"
					shape="circle"
					loading-icon="/static/missing-face.png"
					error-icon="/static/missing-face.png"
					style="display: flex; align-items: center"
				></u-image>

				<view class="flxcenter">
					<view class="nkname">{{ i.nickname }}</view>
					<!-- <view class="fans">粉丝：125.2万</view> -->
				</view>
				<view class="flxright">
					<u-button
						type="error"
						@click="follow(i)"
						size="mini"
						v-if="i.followStatus == '未关注'"
					>
						关注
					</u-button>
					<u-button
						v-else
						type="plain"
						@click="cancelFollow(i)"
						size="mini"
					>
						{{ i.followStatus }}
					</u-button>
				</view>
			</view>
			<u-loadmore
				style="padding: 10px 0"
				bg-color="#f8f8f8"
				:status="loadStatus"
				@loadmore="getData"
			></u-loadmore>
		</view>
		<u-empty
			v-else
			style="margin-top: 20%"
			text="暂无数据"
			mode="data"
		></u-empty>
	</view>
</template>

<script>
import storage from '@/utils/storage.js'; //缓存
import { vlogSearchUser, vlogFansFollow, vlogFansCancel } from '@/api/vlog';
export default {
	props: {
		keywords: {
			default: ''
		}
	},
	data() {
		return {
			flag: 2, // 在tabs中的索引
			loadStatus: 'loadmore',
			flowList: [],
			page: 0,
			totalPage: 0,
			search: '',
			id: ''
		};
	},
	created() {
		this.initData();
	},
	methods: {
		async getData() {
			if (this.loadStatus !== 'loadmore') return;
			var info = storage.getVlogUserInfo();
			var id = '';
			if (info != null) {
				id = info.id;
			}
			this.id = id;
			this.loadStatus = 'loading';
			this.page += 1;
			console.log('加载用户数据');

			var params = {
				id: id,
				nickname: this.search,
				page: this.page,
				pageSize: 10
			};
			console.log(params);
			var result = await vlogSearchUser(params);
			console.log(result);
			if (result.data.status == 200) {
				var data = result.data.data || [];
				if (!data.length) {
					this.loadStatus = 'nomore';
				} else {
					if (data.length < 10) {
						this.loadStatus = 'nomore';
					} else {
						this.loadStatus = 'loadmore';
					}
					data.forEach((i) => {
						i.doIflow = false;
						if (i.followStatus == null) {
							// 处理未登录时的状态
							i.followStatus = '未关注';
						}
						this.flowList.push(i);
					});
					// this.flowList.push(...data);
				}
			}
		},
		initData() {
			this.search = this.keywords;
			this.page = 0;
			this.totalPage = 0;
			this.flowList = [];
			this.loadStatus = 'loadmore';
			console.log('初始化用户数据');
			this.getData();
		},
		async follow(item) {
			let userInfo = storage.getVlogUserInfo();
			if (userInfo == null) {
				uni.navigateTo({
					url: '/pages/passport/login',
					animationType: 'slide-in-bottom'
				});
				return;
			}
			let data = {
				myId: userInfo.id,
				vlogerId: item.id
			};
			var result = await vlogFansFollow(data);
			console.log(result);
			if (result.data.status == 200) {
				item.followStatus = '已关注';
			} else {
				uni.showToast({
					title: result.data.msg,
					icon: 'none',
					duration: 3000
				});
			}
		},
		async cancelFollow(item) {
			let userInfo = storage.getVlogUserInfo();
			if (userInfo == null) {
				uni.navigateTo({
					url: '/pages/passport/login',
					animationType: 'slide-in-bottom'
				});
				return;
			}
			let data = {
				myId: userInfo.id,
				vlogerId: item.id
			};
			var result = await vlogFansCancel(data);
			console.log(result);
			if (result.data.status == 200) {
				item.doIflow = false;
			} else {
				uni.showToast({
					title: result.data.msg,
					icon: 'none',
					duration: 3000
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.flex-box {
	display: flex;
	align-items: center;
	margin-bottom: 10rpx;
	padding: 10rpx 20rpx;
}
.flxleft {
	width: 120rpx;
	height: 120rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 5px;
}
.flxcenter {
	flex: 1;
}
.nkname {
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 1;
	overflow: hidden;
	font-weight: 500;
	font-size: 26rpx;
}
.fans {
	font-size: 24rpx;
	color: #999;
}
.flxright {
	margin-left: 10px;
}
</style>
