<template>
	<view class="study">
		<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,item.secondaryImg)">
					<image class="icon" :src="item.columnImg" mode=""></image>
					<view class="label">
						{{item.columnName}}
					</view>
				</view>
			</view>
		</view>
<!-- 		<view class="type-box">
			<view class="left">
				<view class="type-item" @click="handleType(singleType.id,singleType.secondaryImg)">
					<view class="glass" :style="{'--colour':singleType.colour}">
						<view class="title">
							{{singleType.columnName}}
						</view>
						<view class="type">
							{{singleType.columnMsg}}
						</view>
					</view>
					<image class="icon" :src="singleType.columnImg" mode="center"></image>
				</view>
				
			</view>
			<view class="right">
					<view class="type-item" v-for="item in typeList" :key="item.id" @click="handleType(item.id,item.secondaryImg)">
						<view class="glass" :style="{'--colour':item.colour}">
							<view class="title">
								{{item.columnName}}
							</view>
							<view class="type">
								{{item.columnMsg}}
							</view>
						</view>
						<image class="icon" :src="item.columnImg" mode="center"></image>
					</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>
		
		<view class="type-title more-title">
			互动社区
			
			<view class="more-box" @click="jump">
				<view>
					更多
				</view>
				<image class="more" src="../static/jiantou-icon.png" mode=""></image>
			</view>
		</view>
		<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" @click="handleTest">
				研学MBTI测试
			</view>
		</scroll-view>
		<view class="list">
			<view class="item" v-for="item in communityList" :key="item.tabId" @click="goDetail(item)">
				<zs-img :src="item.coverImg" width="344rpx" height="256rpx"></zs-img>
				<view class="info">
					<view class="title">
						{{item.communityTitle}}
					</view>
					<view class="user-info">
						<image class="head" :src="require('@/static/logo.png')||item.publishLogo" mode=""></image>
						<view class="user-name">
							{{item.publishUser|| "官方"}}
						</view>
					</view>
				</view>
			</view>
			
		</view>
		
		<view class="type-title">
			精选推荐
		</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.logoPath" 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.shopName}}
							</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.logoPath" 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>
				</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}
			}
		},
		methods: {
			handleTest(){
				uni.navigateTo({
					url:'/study/test'
				})
			},
			// swiper变动
			swiperChange(val) {
			},
			handleType(id,img){
				uni.navigateTo({
					url:'/study/type?id='+id,
					success(res) {
						res.eventChannel.emit('img', img)
					}
				})
			},
			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
						console.log(res.content.records);
					}
				})
			},
			// 获取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
			}
		}
	}
</script>

<style lang="scss" >
	.study {
		background: #F9F9F9;
		padding: 0 20rpx;
		.top-box{
			.bg{
				width: 750rpx;
				height: 480rpx;
				position: relative;
				left: -20rpx;
			}
			.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: 300;
						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;
				color: #AAAAAA;
				background: #EEEEEE;
				font-size: 28rpx;
				margin-right: 20rpx;
				border-radius: 30rpx;
			}
			.tab.active{
				color: #3B83FF;
				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: 424rpx;
				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: 24rpx;
						width: 100%;
						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: 20rpx;
							margin-left: 12rpx;
						}
					}
				}
			}
		}
		
		.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;
				margin-top: 20rpx;
				.info{
					padding: 20rpx;
					.title{
						color: #222222;
						font-size: 24rpx;
						width: 100%;
						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: 20rpx;
							margin-left: 12rpx;
						}
					}
				}
			}
		}
		
	}
</style>