<template>
	<view class="content">
		<!-- 达人信息 -->
		<view class="info">
			<image class="info-bg" src="https://cdn.his.cdwjyyh.com/images/expert_top_bg.png"></image>
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
			<view class="back-box">
				<image class="img" @click="goBack" src="https://cdn.his.cdwjyyh.com/images/back_white_icon.png"></image>
			</view>
			<view class="info-block">
				<image class="head" :src="expert.avatar"></image>
				<view class="right">
					<view class="user">
						<text class="name">{{expert.nickname}}</text>
						<view class="level">
							<image class="icon" src="https://cdn.his.cdwjyyh.com/images/intelligent_icon.png"></image>LV.2
						</view>
					</view>
					<view class="txt">{{expert.introduction}}</view>
					<view class="card">
						<view class="card-item">
							<image class="img" src="https://cdn.his.cdwjyyh.com/images/jinbang_font.png"></image>
							<text class="ranking">达人榜 第15名</text>
						</view>
						<image class="go" src="https://cdn.his.cdwjyyh.com/images/jb_arrow_right_icon.png"></image>
					</view>
				</view>
			</view>
			<view class="data-block">
				<view class="number-block">
					<view class="item">
						<text class="number-item">{{expert.fansNum}}</text>
						<text class="txt-item">粉丝</text>
					</view>
					<view class="item">
						<text class="number-item">{{expert.followNum}}</text>
						<text class="txt-item">关注</text>
					</view>
					<view class="item">
						<text class="number-item">{{expert.likes}}</text>
						<text class="txt-item">获赞</text>
					</view>
				</view>
				<!-- 关注 -->
				<view class="button" :class="isFollow ? 'follow' : ''" @click="toFollow">
					<image v-if="!isFollow" class="icon" src="https://cdn.his.cdwjyyh.com/images/guanzhu_green_icon.png" />
					<text>{{ isFollow ? '已关注' : '关注' }}</text>
				</view>
			</view>
		</view>
		<view class="vedio-block">
			<view class="tab-wrapper">
				<!-- 生活号标签 -->
				<view class="tab-item" :class="{ active: activeTab === 0 }" @click="switchTab(0)">
					<text class="tab-text">生活号(22)</text>
					<view v-if="activeTab === 0" class="underline"></view>
				</view>

				<!-- 直播标签 -->
				<view class="tab-item" :class="{ active: activeTab === 1 }" @click="switchTab(1)">
					<text class="tab-text">直播</text>
					<view v-if="activeTab === 1" class="underline"></view>
				</view>
			</view>

			<!-- 视频文章列表 -->
			<mescroll-body bottom="0" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:down="downOption" :up="upOption">
				<view class="list">
					<view class="list-item" @click="goLive(item)" v-for="(item,index) in list" :key="index">
						<image class="img" v-if="item.liveImgUrl" :src="item.liveImgUrl" mode="widthFix"></image>
						<image class="video-icon" src="https://cdn.his.cdwjyyh.com/images/video_icon.png"></image>
						<image class="suspension-icon" @click="onTask" src="https://cdn.his.cdwjyyh.com/images/suspension.png"></image>
						<view class="info-block">
							<view class="title">冰乳绿豆糕 冰冰凉凉谁吃谁迷糊! 谁懂，...</view>
							<view class="item">
								<view class="flex">
									<image class="head" src="https://cdn.his.cdwjyyh.com/images/img.png"></image>
									<text class="name">崔医生</text>
								</view>
								<view class="flex">
									<image class="icon" src="https://cdn.his.cdwjyyh.com/images/zan_icon.png"></image>
									<text>141</text>
								</view>
							</view>
							<view class="card">
								<view class="card-item">
									<image class="img" src="https://cdn.his.cdwjyyh.com/images/jinbang_font.png"></image>
									<text class="ranking">达人榜 第15名</text>
								</view>
								<image class="go" src="https://cdn.his.cdwjyyh.com/images/jb_arrow_right_icon.png"></image>
							</view>
						</view>
					</view>


				</view>
			</mescroll-body>
		</view>
		<Task :show="showTask" @close="handleCloseTask"></Task>

	</view>
</template>

<script>
	import Task from '@/components/task.vue'
	import {
		expertHomePage, // 达人主页
		follow, // 关注用户
		listCreatorRank, //达人排行榜
	} from '@/api/life.js'
	// import {
	// 	liveList
	// } from '@/api/living.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	// 点赞/取消点赞常量
	const LIKE = 'LIKE';
	const UNLIKE = 'UNLIKE';
	export default {
		components: {
			Task
		},
		mixins: [MescrollMixin],
		data() {
			return {
				expert:{},//达人的数据
				showTask: false,
				isFollow: false,
				expertId: 5, //达人id
				activeTab: 0,
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				list: [],
				downOption: {
					offset: 80,
					use: true,
					auto: false
				},
				upOption: {
					use: true,
					auto: true,
					page: {
						num: 0,
						size: 10
					}
				},
				mescroll: null,
			}
		},
		onLoad(option) {
			if(option.expertId){
			this.expertId=option.expertId;

			}
			this.getExpertHomePage();
			this.getListCreatorRank();

			
		},
		onUnload() {

		},
		methods: {// 返回上一页，无则返回首页
		goBack() {
			// 获取页面栈
			const pages = getCurrentPages();
			// 如果页面栈长度大于1，说明有上一页
			if (pages.length > 1) {
				// 返回上一页
				uni.navigateBack({
					delta: 1
				});
			} else {
				// 否则返回首页
				uni.reLaunch({
					url: '/pages/home/index'
				});
			}
		},
		//达人排行榜
		getListCreatorRank() {
				listCreatorRank().then(res => {
					if (res.code == 200) {
						console.log("达人排行榜", res)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err => {});
			},

			// 达人主页
			getExpertHomePage() {
				console.log("达人id",this.expertId)
				if(!this.expertId) return;
				expertHomePage(this.expertId).then(res => {
					if (res.code == 200 && res.data) {
						console.log("达人主页", res)
						this.expert=res.data
						// 同步关注状态
						this.isFollow = res.data.isFollow || false;
					} else {
						uni.showToast({
							title: res.msg || '获取达人信息失败',
							icon: 'none'
						});
					}
				}).catch(err => {});
			},
			// 关注用户
			toFollow() {
				console.log("关注取消用户")
				const action = this.isFollow ? UNLIKE : LIKE;
				const data = {
					expertId: this.expertId,
					action: action
				};
				follow(data).then(res => {
					if (res.code == 200) {
						this.isFollow = !this.isFollow;
						this.getExpertHomePage();
						console.log(action + "用户", res)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err => {});
			},
			onTask() {
				this.showTask = true;
			},
			handleCloseTask() {
				this.showTask = false;
			},

			switchTab(tab) {
				if (this.activeTab === tab) return;
				this.activeTab = tab;
				this.$emit('change', tab);
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},

			// 下拉刷新回调
			downCallback(mescroll) {
				this.list = [];
				mescroll.resetUpScroll();
			},

			// 上拉加载回调
			// upCallback(mescroll) {
			// 	const pageNum = mescroll.num;
			// 	const pageSize = mescroll.size;

			// 	let data = {
			// 		pageSize: pageSize,
			// 		pageNum: pageNum,
			// 	}
			// 	liveList(data).then(res => {
			// 		if (!res) {
			// 			mescroll.endErr();
			// 			return;
			// 		}
			// 		if (res.code == 200) {
			// 			let curPageData = Array.isArray(res.data.list) ? res.data.list : [];
			// 			let totalSize = Number(res.data.total) || 0;
			// 			if (pageNum === 1) {
			// 				this.list = [];
			// 			}
			// 			this.list = this.list.concat(curPageData);

			// 			mescroll.endBySize(curPageData.length, totalSize);
			// 		} else {
			// 			mescroll.endErr();
			// 			uni.showToast({
			// 				title: res.msg,
			// 				icon: 'none'
			// 			});
			// 		}
			// 	}).catch(err => {
			// 		mescroll.endErr();
			// 	});
			// },

			goLive(item) {
				uni.navigateTo({
					// &immediate=true
					url: `./living?liveId=${item.liveId}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #111;
		min-height: 100vh;
		// padding: 24rpx;

		// 达人信息
		.info {

			width: 100%;
			height: 600rpx;
			position: relative;
			z-index: 0;

			.info-bg {
				position: absolute;
				z-index: -1;
				width: 100%;
				height: 100%;
			}

			.back-box {
				display: flex;
				align-items: center;
				height: 88rpx;
				margin-left: 24rpx;

				.img {
					width: 48rpx;
					height: 48rpx;
				}
			}

			.info-block {
				margin-top: 32rpx;
				padding: 0 20rpx;
				display: flex;
				align-items: center;

				.head {
					width: 160rpx;
					height: 160rpx;
					margin-right: 20rpx;
					border-radius: 50%;
				}

				.right {
					.user {
						display: flex;

						.name {
							font-weight: 500;
							font-size: 40rpx;
							color: #FFFFFF;
							margin-right: 16rpx;
						}

						.level {
							width: 118rpx;
							height: 40rpx;
							display: flex;
							align-items: center;
							font-weight: 500;
							font-size: 22rpx;
							color: #FEF5EA;
							background: linear-gradient(90deg, #F5CE3C 0%, #E39414 100%);
							border-radius: 20rpx 20rpx 20rpx 20rpx;
							padding: 4rpx 12rpx;
							box-sizing: border-box;

							.icon {
								width: 32rpx;
								height: 32rpx;
								margin-right: 8rpx;
							}
						}
					}

					.txt {
						font-weight: 400;
						font-size: 22rpx;
						color: #FFFFFF;
						margin: 8rpx 0 16rpx;
					}

					.card {
						display: flex;
						justify-content: space-between;
						align-items: center;
						background: #FFF5EB;
						width: 302rpx;
						height: 40rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						border: 1rpx solid #F3E2D0;
						padding: 8rpx;
						box-sizing: border-box;

						.card-item {
							display: flex;
							align-items: center;

							.img {
								width: 48rpx;
								height: 24rpx;
								margin-right: 8rpx;
							}

							.ranking {
								font-size: 22rpx;
								color: #D46C0D;
							}
						}

						.go {
							width: 16rpx;
							height: 16rpx;
						}
					}
				}
			}

			.data-block {
				padding: 0 20rpx;
				margin-top: 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.number-block {
					display: flex;

					.item {
						margin-right: 32rpx;
						display: flex;
						flex-direction: column;
						align-items: center;

						.number-item {
							font-family: Roboto, Roboto;
							font-weight: 600;
							font-size: 36rpx;
							color: #FFFFFF;
						}

						.txt-item {
							margin-top: 6rpx;
							font-size: 26rpx;
							color: rgba(255, 255, 255, 0.7);
						}
					}
				}

				.button {
					width: 144rpx;
					height: 56rpx;
					background: #FFFFFF;
					border-radius: 28rpx 28rpx 28rpx 28rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
					color: #02B176;

					&.follow {
						background: #F0F0F0;
						color: #666666;
					}

					.icon {
						width: 24rpx;
						height: 24rpx;
						margin-right: 4rpx;
					}
				}
			}

		}

		//达人视频
		.vedio-block {
			position: relative;
			z-index: 1;
			background-color: #FFFFFF;
			margin-top: -74rpx;
			border-radius: 32rpx 32rpx 0rpx 0rpx;
			padding: 26rpx 0;

			.tab-wrapper {
				display: flex;
				height: 88rpx;
				padding: 0 40rpx;
				position: relative;

				.tab-item {
					flex: 1;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					position: relative;
					color: #666666;

					&.active {
						font-weight: 600;
						font-size: 32rpx;
						color: #02B176;
					}

					.tab-text {
						font-size: 32rpx;
						transition: all 0.3s ease;


					}

					.underline {
						position: absolute;
						bottom: 0;
						left: 50%;
						transform: translateX(-50%);
						width: 48rpx;
						height: 6rpx;
						background: #02B176;
						border-radius: 3rpx 3rpx 3rpx 3rpx;
						animation: slideIn 0.3s ease;
					}
				}
			}

			@keyframes slideIn {
				from {
					opacity: 0;
					transform: translateX(-50%) scaleX(0.5);
				}

				to {
					opacity: 1;
					transform: translateX(-50%) scaleX(1);
				}
			}

			.list {
				background: #F5F7FA;
				padding: 24rpx 24rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.list-item {
					width: 342rpx;
					margin-bottom: 18rpx;
					position: relative;
					display: flex;
					flex-direction: column;

					.img {
						width: 100%;
						border-radius: 16rpx 16rpx 0rpx 0rpx;

					}

					.video-icon {
						width: 40rpx;
						height: 40rpx;
						position: absolute;
						top: 20rpx;
						right: 20rpx;
					}

					.suspension-icon {
						width: 144rpx;
						height: 144rpx;
						position: absolute;
						top: -14rpx;
						right: 0rpx;
					}

					.info-block {
						border-radius: 0rpx 0rpx 16rpx 16rpx;
						padding: 12rpx 20rpx 20rpx;
						box-sizing: border-box;
						width: 100%;
						background: #FFFFFF;
						display: flex;
						flex-direction: column;

						.title {
							font-weight: 500;
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 20rpx;
						}

						.item {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-bottom: 20rpx;

							.flex {
								display: flex;
								align-items: center;
								font-size: 22rpx;
								color: #999999;
							}

							.icon {
								width: 32rpx;
								height: 32rpx;
								margin-right: 4rpx;
							}

							.head {
								width: 32rpx;
								height: 32rpx;
								border-radius: 50%;
								margin-right: 8rpx;
							}

							.name {
								color: #757575;
							}
						}

						text {
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}

						.card {

							display: flex;
							justify-content: space-between;
							align-items: center;
							background: #FFF5EB;
							width: 302rpx;
							height: 40rpx;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							border: 1rpx solid #F3E2D0;
							padding: 8rpx;
							box-sizing: border-box;

							.card-item {
								display: flex;
								align-items: center;

								.img {
									width: 48rpx;
									height: 24rpx;
									margin-right: 8rpx;
								}

								.ranking {
									font-size: 22rpx;
									color: #D46C0D;
								}
							}

							.go {
								width: 16rpx;
								height: 16rpx;
							}
						}
					}

				}

				.list-item:nth-child(2n) {
					margin-right: 0;
				}
			}
		}


	}
</style>