<template>
	<view class="choiceness">
		<uni-search-bar class="uni-mt-10" placeholder="请输入喜欢的艺术家代号吧~" clearButton="auto" cancelButton="none"
			@confirm="search" />
		<view class="choiceness-list">
				<!-- 				<view class="first-box" :style="'width:'+imgWidth" >
					热门作品
				</view> -->
				<view class="ad-view">
					<ad adpid="1111111111" type="banner" @load="onload" @close="onclose" @error="onerror"></ad>
				</view>
				<image v-for="(item,index) in imgList" :key='index' class="img-box"
					:style="'width:'+imgWidth+';height:'+imgHeight" :src="item.imgUrl" @click="targetDetail(item)"
					mode=""></image>
		</view>
	</view>
</template>

<script>
	import {
		imgLists,
		creatorDetails
	} from '@/api/index.js'
	export default {
		data() {
			return {
				imgList: [], //图片列表
				imgWidth: 0, // 图片宽
				imgHeight: 0, // 图片高
				pageNum: 1,
				pageSize: 10,
			}
		},
		created() {
			uni.getSystemInfo({
				success: res => {
					console.log(res)
					this.imgWidth = res.windowWidth - 60 + 'rpx'
					this.imgHeight = (res.windowWidth - 60) * 2 - 30 + 'rpx'
					this.getImgList()
				}
			})
		},
		// 下拉刷新
		onPullDownRefresh() {
			console.log(1111)
			this.pageNum = 1
			this.imgList = []
			this.getImgList()
			uni.stopPullDownRefresh()
		},
		// 上划加载更多
		onReachBottom() {
			console.log(2222)
			if (this.imgList.length > 9) {
				this.pageNum += 1
				this.getImgList()
			}
		},
		methods: {
			// 搜索
			search(res) {
				if (res.value) {
					console.log('search', res.value)
					this.goCreatorDetail(res.value)
				} else {
					uni.showToast({
						title: '请输入艺术家代号',
						icon: 'none',
					})
				}
			},
			// 前往艺术家空间
			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'
					})
				}
			},
			// 图片列表
			async getImgList() {
				const res = await imgLists({
					pageNum: this.pageNum,
					pageSize: this.pageSize
				})
				if (res.data.code === 200) {
					for (let i = 0; i < res.data.rows.length; i++) {
						this.imgList.push(res.data.rows[i])
					}
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			// 前往详情页
			targetDetail(item) {
				console.log('跳转', item)
				if (item.id) {
					uni.setStorage({
						key: 'detailId',
						data: item.id,
						success() {
							console.log(111111)
							uni.navigateTo({
								url: '../creator/imgDetail'
							})
						}
					})
				}
			},
			onload(e) {
				console.log("加载了广告");
			},
			onclose(e) {
				console.log("关闭了广告: " + e.detail);
			},
			onerror(e) {
				console.log("广告错误了: " + e.detail.errCode + " message:: " + e.detail.errMsg);
			}
		},
		// onUnload() {
		// 	uni.switchTab({
		// 		url: '/pages/index/index'
		// 	})
		// },
	}
</script>

<style lang="less">
	.choiceness {
		padding-top: 40rpx;

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

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

		.choiceness-list {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
			.first-box {
				border-radius: 16rpx;
				font-size: 16px;
				margin-top: 40rpx;
				margin-left: 30rpx;
				color: #fff;
				line-height: 60rpx;
				text-align: center;
				height: 60rpx;
				background-color: #11A8FD;
				display: inline-block;
				vertical-align: top;
			}

			image {
				margin-top: 40rpx;
				border-radius: 16rpx;
			}
		}
	}
</style>
