<template>
	<view class="container">
		<view v-if="!showChoiceness">
			<view class="top">
				<image class="back-img" src="../../static/img/start.gif" mode=""></image>
				<view class="top-bottom">
				</view>
				<uni-search-bar class="uni-mt-10" placeholder="请输入喜欢的艺术家代号吧~" clearButton="auto" cancelButton="none"
					@confirm="search" />
				<text>热门艺术家</text>
				<view class="user-list">
					<view class="user-list-box" v-for="(item,index) in hotCreatorList" :key='item.id'
						@click="goCreatorDetail(item.scanCode)">
						<image :src="item.img" mode=""></image>
						<text>{{item.scanCode}}</text>
					</view>
				</view>
			</view>
			<view class="middle">
				<text>近期精选</text>
				<image src="../../static/img/slide-top.svg" mode=""></image>
			</view>
		</view>
		<Choiceness v-else></Choiceness>
	</view>
</template>

<script>
	import {
		listHotCreator,
		creatorDetails,
		loginTiktok
	} from '@/api/index.js'
	import Choiceness from './choiceness.vue'
	export default {
		data() {
			return {
				hotCreatorList: [],
				isTarget: true,
				showChoiceness:false
			}
		},
		components:{
			Choiceness,
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.showChoiceness = false
			uni.stopPullDownRefresh()
		},
		created() {
			const userInfo = uni.getStorageSync('userInfo')
			if (!userInfo) {
				console.log('havent userInfo')
				this.getUserInfo()
			} else {
				console.log('have userInfo')
			}
			this.getHotCreatorList()
		},
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件

			if (e.scrollTop > 160) {
				this.showChoiceness = true
				// uni.redirectTo({
				// 	url: '../index/choiceness'
				// })
			}
		},
		methods: {
			// 搜索
			search(res) {
				if (res.value) {
					console.log('search', res.value)
					this.goCreatorDetail(res.value)
				} else {
					uni.showToast({
						title: '请输入艺术家代号',
						icon: 'none'
					})
				}

			},
			// 获取用户信息
			getUserInfo() {
				uni.login({
					force: true,
					success: res => {
						uni.getUserInfo({
							withCredentials: true,
							success: userInfo => {
								const params = {
									code: res.code,
									encryptedData: userInfo.encryptedData,
									iv: userInfo.iv
								}
								// 用户授权登录
								loginTiktok(params).then(res => {
									if (res.data.code === 200) {
										uni.setStorage({
											key: 'userInfo',
											data: res.data.data.userInfo,
										})
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										})
									}
								}).catch(res => {})
								console.log(`getUserInfo 调用成功`, userInfo);
							},
							fail(userInfo) {
								console.log(`getUserInfo 调用失败`);
							},
						});
					},
					fail(res) {
						console.log(`login 调用失败`);
					},
				});

			},
			// 获取热门艺术家列表
			async getHotCreatorList() {
				const res = await listHotCreator()
				if (res.data.code === 200) {
					this.hotCreatorList = res.data.data
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
				console.log('getHotCreatorList', res)
			},
			// 前往艺术家空间
			async goCreatorDetail(scanCode) {
				if (scanCode) {
					const res = await creatorDetails({
						scanCode
					})
					console.log('creatorDetails', res)
					if (res.data.code === 200) {
						uni.setStorage({
							key: 'creatorDetail',
							data: res.data.data,
							success() {
								uni.navigateTo({
									url: '../creator/creatorDetail'
								})
							}
						})
						
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}

				} else {
					uni.showToast({
						title: '搜索码不能为空',
						icon: 'none'
					})
				}
			},
			// 滚动监听
			handleScroll() {
				let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
				console.log(scrollTop)
			},
		},
	}
</script>

<style lang="less">
	.container {
		.top {
			width: 100vw;

			image {
				width: 100vw;
				height: 400rpx;
			}

			.uni-searchbar {
				border: 1px solid #11A8FD;
				margin: 0 40rpx;
				border-radius: 16rpx;
				padding: 0;

				.uni-searchbar__box {
					padding: 0;
					border-radius: 16rpx !important;
				}
			}

			>text {
				font-weight: bold;
				font-size: 14px;
				display: block;
				text-align: center;
				padding-top: 40rpx;
				padding-bottom: 20rpx;
			}

			.user-list {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				overflow-x: auto;

				.user-list-box {
					width: 140rpx;
					padding: 20rpx;
					text-align: center;

					image {
						width: 100rpx;
						height: 100rpx;
						border-radius: 100rpx;
					}

					text {
						font-size: 24rpx;
						color: #1E1E1E;
						text-align: center;
					}
				}
			}
		}

		.middle {
			text-align: center;
			height: calc(100vh - 248rpx);
			padding-top: 40rpx;

			text {
				font-weight: bold;
				font-size: 14px;
				display: block;
			}

			image {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}
</style>
