<template>
	<view class="mpage">
		<view class="line"></view>
		<scroll-view
			scroll-y="true"
			@scrolltolower="pagingFansList()"
		>
			<view
				class="user-wrapper"
				v-for="(f, index) in fansList"
				:key="f.fanId"
			>
				<view
					class="user-info"
					@click="goTovlogerInfo(f.fanId)"
				>
					<image
						class="face"
						:src="f.face"
					/>
					<text class="user-name">
						{{ f.nickname }}
					</text>
				</view>

				<view v-if="!from">
					<view
						v-if="f.bothFriend == 0"
						class="operator-wrapper"
					>
						<text
							class="operator-words"
							style="color: #ef274d"
							@click="followMe(f.fanId)"
						>
							回粉
						</text>
					</view>
					<view
						v-if="f.bothFriend == 1"
						class="operator-wrapper"
					>
						<text
							class="operator-words"
							style="color: #ffffff"
							@click="cancelFollow(f.fanId)"
						>
							互粉
						</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
let system = uni.getSystemInfoSync();
import storage from '@/utils/storage.js'; //缓存
import { vlogFansCancel, vlogFansFollow, vlogQueryMyFans } from '@/api/vlog';
import { isStrEmpty } from '@/utils/tools.js';
export default {
	data() {
		return {
			userId: '',
			screenHeight: 0,
			page: 0,
			totalPage: 0,
			fansList: [],
			from: false
		};
	},
	onLoad(param) {
		if (!isStrEmpty(param.userId)) {
			this.from = true;
			this.userId = param.userId;
		} else {
			var uinfo = storage.getVlogUserInfo();
			this.userId = uinfo.id;
		}
		this.queryMyFansList(0);
	},
	methods: {
		goTovlogerInfo(vlogerId) {
			var id = storage.getVlogUserInfo().id;
			// 是否是当前登录的用户
			if (id == vlogerId) {
				uni.switchTab({
					url: 'me'
				});
			} else {
				uni.navigateTo({
					url: 'vlogerInfo?userPageId=' + vlogerId
				});
			}
		},
		// 关注/取关后的list重新状态刷新设置
		reFreshList(vlogerId, status) {
			let me = this;
			let fansList = me.fansList;
			for (let i = 0; i < fansList.length; i++) {
				let fan = fansList[i];
				if (fan.fanId == vlogerId) {
					fan.bothFriend = status;
					// fansList.splice(i, 1);
				}
			}
			me.fansList = fansList;
		},
		async cancelFollow(vlogerId) {
			let me = this;
			let userId = me.userId;
			let data = {
				myId: userId,
				vlogerId
			};
			var result = await vlogFansCancel(data);
			console.log(result);
			if (result.data.status == 200) {
				me.reFreshList(vlogerId, 0);
			} else {
				uni.showToast({
					title: result.data.msg,
					icon: 'none',
					duration: 3000
				});
			}
		},
		async followMe(vlogerId) {
			let me = this;
			let userId = me.userId;
			let data = {
				myId: userId,
				vlogerId
			};
			var result = await vlogFansFollow(data);
			console.log(result);
			if (result.data.status == 200) {
				me.reFreshList(vlogerId, 1);
			} else {
				uni.showToast({
					title: result.data.msg,
					icon: 'none',
					duration: 3000
				});
			}
		},
		async queryMyFansList(page) {
			let me = this;
			page = page + 1;
			let userId = me.userId;
			let currentUserId = this.currentUserId;
			var data = {
				myId: userId,
				// vlogerId: userId,
				// currentUserId: currentUserId,
				page: page,
				pageSize: 10
			};
			var result = await vlogQueryMyFans(data);
			console.log(result);
			if (result.data.status == 200) {
				let fansList = result.data.data.rows;
				let totalPage = result.data.data.total;
				me.fansList = me.fansList.concat(fansList);
				me.page = page;
				me.totalPage = totalPage;
			}
		},

		// 上滑分页粉丝列表
		pagingFansList() {
			if (this.page >= this.totalPage) {
				return;
			}
			this.queryMyFansList(this.page);
		}
	}
};
</script>

<style scoped>
.user-info {
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
}

.face {
	align-items: center;
	width: 110rpx;
	height: 110rpx;
	border-radius: 100rpx;
	border-width: 2rpx;
	border-color: #f1f1f1;
}

.user-name {
	align-items: center;
	color: #ffffff;
	font-size: 30rpx;
	margin-left: 20rpx;
}

.line {
	height: 1rpx;
	background-color: #393a41;
	width: 750rpx;
}

.mpage {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	background-color: #181b27;
}

.user-wrapper {
	padding-left: 30rpx;
	padding-right: 30rpx;
	width: 750rpx;
	height: 120rpx;
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	margin-top: 20rpx;
	margin-bottom: 20rpx;
}

.operator-wrapper {
	width: 140rpx;
	height: 60rpx;
	display: flex;
	flex-direction: row;
	justify-content: center;
	background-color: #ef274d;
	border-radius: 20rpx;
	align-items: center;
	border-width: 2rpx;
	border-color: #ef274d;
	background-color: #181b27;
}

.operator-words {
	align-items: center;
	font-size: 28rpx;
}
</style>
