<template>
	<view class="study">
		<zs-header  title="慧研学" color="#000" :background="background"></zs-header>
		<view class="top-box">
			<image class="bg" src="https://hyxhsh.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/06qgjHTlTxWEb95dd5bef869e62f2a78c625ea6bef78.jpg/1.jpg" mode=""></image>
			<view class="type-box">
				<view class="type-item" v-for="item in typeList" :key="item.id" @click="handleType(item.id,{
					img:item.secondaryImg,
					name:item.columnName
				})">
					<image class="icon" :src="item.columnImg" mode=""></image>
					<view class="label">
						{{item.columnName}}
					</view>
				</view>
			</view>
		</view>


		<swiper class="swiper" @change="swiperChange" :indicator-dots="true" circular :autoplay="true"
			:interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in bannerList" :key="index">
				<image class="swiper-item" mode="" :src="item.bannerImg">
				</image>
			</swiper-item>
		</swiper>
		
		<scroll-view  class="tab-box" enable-flex scroll-x >
			<!-- <view class="tab" :class="[tab == item.value?'active':'']" v-for="(item,index) in tabList" :key="index" @click="handleTab(item.value)">
				{{item.label}}
			</view>  -->
			<view class="tab active">
				猜你喜欢
			</view>
			<view class="tab" @click="handleTest">
				MBTI测试
			</view>
		</scroll-view>
		
		<!-- 列表 -->
		<zs-list class="store-box" mt="0" @load="loadMore" :status="status">
			<view class="left">	
				<view class="store-item" v-for="(item,index) in list" :key="index" @click="goGoodsDetail(item.goodsId)">
					<zs-img :src="item.goodsPath" width="344rpx" height="344rpx" mode=""></zs-img>
					<view class="info">
						<view class="title">
							{{item.goodsName}}
						</view>
						<view class="user-info">
							<!-- <image class="head" :src="item.logoPath"></image> -->
							<view class="user-name">
								{{item.goodsDescribe}}
							</view>
						</view>
						<view class="price-box">
						<view class="left">
							<view class="unit">
								￥
							</view>
							<view class="price">
							{{item.realPrice}}
							</view>
							<view class="old-price">
							￥{{item.marketPrice}}
							</view>
						</view>
						
						<view class="right">
							{{item.saleNum}}人已购
						</view>
					</view>
					</view>
				
				</view>
			</view>
			<view class="right">
		
				<view class="store-item" v-for="(item,index) in list1" :key="index" @click="goGoodsDetail(item.goodsId)">
					<zs-img :src="item.goodsPath" width="344rpx" height="344rpx" mode=""></zs-img>
					<view class="info">
						<view class="title">
							{{item.goodsName}}
						</view>
						<view class="user-info">
							<!-- <image class="head" :src="item.logoPath" mode=""></image> -->
							<view class="user-name">
								{{item.shopName}}
							</view>
						</view>
						<view class="price-box">
						<view class="left">
							<view class="unit">
								￥
							</view>
							<view class="price">
							{{item.realPrice}}
							</view>
							<view class="old-price">
							￥{{item.marketPrice}}
							</view>
						</view>
						<view class="right">
							{{item.saleNum}}人已购
						</view>
					</view>
					</view>
				
				</view>
			</view>
		
		</zs-list>
		
		
	</view>
</template>

<script>
	import {getMenu,getTab,getItem,studyGoodsByUser,getResult} from '@/api/study.js';
	import {getBanner} from '@/api/common.js'
	export default {
		data() {
			return {
				status: 'more',
				singleType:{
					columnName: "",
					columnMsg: "",
					columnImg: "",
				},
				typeList:[],
				list: [],
				list1: [],
				communityList:[],
				bannerList:[],
				tab:0,
				tabList:[],
				userId:'',
				query:{userId:this.userId,currentPage:1,pageSize:10,belongType:2},
				background:false
			}
		},
		methods: {
			handleTest(){
				uni.navigateTo({
					url:'/study/test'
				})
			},
			// swiper变动
			swiperChange(val) {
			},
			handleType(id,{img,name}){
				uni.navigateTo({
					url:'/study/type?id='+id,
					success(res) {
						res.eventChannel.emit('img', img, name)
					}
				})
			},
			jump(){
				uni.navigateTo({
					url:'/study/community/index?id='+this.tab
				})
			},
			// 点击社区tab
			handleTab(tab){
				this.tab = tab
				this.getItem(tab)
			},
			// 去社区
			goDetail(item){
				uni.navigateTo({
					url:'/study/community/detail?id='+item.tabId,
					success: function(res) {
					  // 通过eventChannel向被打开页面传送数据
					  res.eventChannel.emit('detail', item)
					}
				})
			},
			// 去商品详情
			goGoodsDetail(id){
				uni.navigateTo({
					url:'/study/studyGoodsDetail?id='+id
				})
			},
			loadMore() {
				this.studyGoodsByUser()
			},
			// 获取顶部栏目
			getMenu(){
				let userId = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).userId:''
				getMenu({currentPage:1,pageSize:5,userId,belongType:2}).then(res=>{
					if(res.state == 'Success'){
						this.typeList = res.content.records
						// const redRoad = this.typeList.find(item=>item.columnName == '红色革命');
						// // 把红色革命放到列表的第一个
						// if(redRoad){
						// 	this.typeList = this.typeList.filter(item=>item.columnName != '红色革命')
						// 	this.typeList.unshift(redRoad)
						// }
					}
				})
			},
			// 获取banner
			getBanner(){
				getBanner({type:2}).then(res=>{
					if(res.state == 'Success'){
						this.bannerList = res.content.records
					}
				})
			},
			getTab(){
				getTab().then(res=>{
					if(res.state == 'Success'){
						for (const key in res.content) {
							if (Object.hasOwnProperty.call(res.content, key)) {
								this.tabList.push({
									value:Number(key),
									label:res.content[key]
								})
							}
						}
						this.tab = this.tabList[0].value
						this.getItem(this.tabList[0].value)
					}
				})
			},
			//获取社区分类列表
			getItem(communityId){
				getItem({communityId,currentPage:1,pageSize:2,state:2}).then(res=>{
					if(res.state == 'Success'){
						this.communityList = res.content.records
					}
				})
			},
			studyGoodsByUser(){
				this.status = 'loading'
				studyGoodsByUser(this.query).then(res=>{
					if(res.state == 'Success'){
						let list = []
						let list1 = []
						res.content.records.map((item,index)=>{
							if(index%2){
								list1.push(item)
							}else{
								list.push(item)
							}
						})
						this.list = this.list.concat(list)
						this.list1 = this.list1.concat(list1)
						let total = this.list.length+this.list1.length
						if(total>=res.content.total){
							this.status = 'noMore'
						}else{
							this.status = 'more'
							this.query.currentPage++
						}
					}
				})
			},
			// 获取测试结果
			getResult(){
				let userId = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).userId:''
				getResult({userId}).then(r=>{
					if(!r.state == 'Success'||!r.content.resultMsg){
						uni.reLaunch({
							url:'/study/test'
						})
					}
				})
			}
			
		},
		created() {
			this.getMenu()
			this.getBanner()
			this.getTab()
			if(uni.getStorageSync('userInfo')){
				this.getResult()
				this.userId = JSON.parse(uni.getStorageSync('userInfo')).userId
			}
		},
		onPageScroll(e) {
				if(e.scrollTop >= 50){
					this.background = true
				}else{
					this.background = false
				}
		},
	}
</script>

<style lang="scss" >
	.study {
		background: #F9F9F9;
		padding: 0 20rpx;
		.top-box{
			.bg{
				width: 750rpx;
				height: 480rpx;
				position: relative;
				left: -20rpx;
				border-radius: 0 0 5% 5%;
			}
			.type-box{
				display: flex;
				align-items: center;
				width: 710rpx;
				height: 228rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				background: #FFFFFF;
				margin-top: -80rpx;
				position: relative;
				z-index: 2;
				.type-item{
					flex: 1;
					text-align: center;
					.icon{
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
					}
					.label{
						font-weight: bold;
						font-size: 24rpx;
						color: #222222;
						margin-top: 15rpx;
					}
				}
			}
		}
		
		.swiper {
			height: 272rpx;
			border-radius: 16rpx;
			margin-top: 28rpx;
			.swiper-item {
				width: 100%;
				height: 100%;
				border-radius: 16rpx;
				object-fit: cover;
			}
		}
		.more-title{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.more-box{
				display: flex;
				align-items: center;
				font-size: 24rpx;
				color: #AAAAAA;
				.more{
					width: 20rpx;
					height: 20rpx;
					display: block;
					margin-left: 6rpx;
				}
			}
		}
		.type-title{
			font-weight: bold;
			color: #222222;
			font-size: 32rpx;
			margin: 30rpx 0 20rpx;
		}
		.tab-box{
			display: flex;
			align-items: flex-start;
			.tab{
				flex-shrink: 0;
				padding: 10rpx 24rpx;
				margin-top: 28rpx;
				color: #AAAAAA;
				background: #EEEEEE;
				font-size: 28rpx;
				margin-right: 20rpx;
				border-radius: 8rpx;
			}
			.tab.active{
				color: #222222;
				// background: #CEE0FF;
			}
		}
		
		.list{
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			.item{
				box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(0,0,0,0.08);
				border-radius: 16rpx;
				width: 344rpx;
				height: 566rpx;
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;
				.info{
					padding: 20rpx;
					height: auto;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.title{
						color: #222222;
						font-size: 28rpx;
						height: 76rpx;
						width: 100%;
						font-weight: bold;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;      /* 显示的最大行数 */
						overflow: hidden;
					}
					.user-info{
						display: flex;
						align-items: center;
						margin-top: 20rpx;
						.head{
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
						}
						.user-name{
							color: #AAAAAA;
							font-size: 24rpx;
							// 超出部分变成...
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1; 
							overflow: hidden;
							text-overflow: ellipsis;
							// margin-left: 12rpx;
						}
					}
					.price-box{
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-top: 12rpx;
						.left{
							display: flex;
							align-items: flex-end;
							.unit{
								font-size: 24rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
							.price{
								font-size: 32rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
							.old-price{
								font-size: 20rpx;
								color: #AAAAAA;
								text-decoration: line-through;
								margin-left: 10rpx;
							}
						}
						.right{
							font-size: 20rpx;
							color: #AAAAAA;
						}
					}
				}
			
			}
		}
		
		.zs-list {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			.left {
				
			}
		
			.right {
				
			}
			
			.store-item{
				box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(0,0,0,0.08);
				border-radius: 16rpx;
				width: 344rpx;
				height: 566rpx;
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;
				.info{
					padding: 20rpx;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.title{
						color: #222222;
						font-size: 28rpx;
						height: 76rpx;
						width: 100%;
						font-weight: bold;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;      /* 显示的最大行数 */
						overflow: hidden;
					}
					.user-info{
						display: flex;
						align-items: center;
						margin-top: 20rpx;
						.head{
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
						}
						.user-name{
							color: #AAAAAA;
							font-size: 24rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1; 
							// margin-left: 12rpx;
						}
					}
					.price-box{
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-top: 12rpx;
						.left{
							display: flex;
							align-items: flex-end;
							.unit{
								font-size: 24rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
							.price{
								font-size: 32rpx;
								color: $uni-color-primary;
								font-weight: bold;
							}
							.old-price{
								font-size: 20rpx;
								color: #AAAAAA;
								text-decoration: line-through;
								margin-left: 10rpx;
							}
						}
						.right{
							font-size: 20rpx;
							color: #AAAAAA;
						}
					}
				}
			
			}
		}
		
	}
</style>