<template>
	<view class="study">
		<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>
		</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="'https://oss.dev.zonelife.cn/static/guida/img/logo.jpg' || 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.goodsImg" width="344rpx" height="256rpx" mode="widthFix"></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.goodsImg" width="344rpx" height="256rpx" mode="widthFix"></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}
			}
		},
		methods: {
			// swiper变动
			swiperChange(val) {
			},
			handleType(id,img){
				uni.navigateTo({
					url:'/study/type?id='+id,
					success(res) {
						res.eventChannel.emit('img', img)
					}
				})
			},
			jump(){
				console.log(this.tab);
				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(){
				getMenu({currentPage:1,pageSize:5,userId:this.userId}).then(res=>{
					if(res.state == 'Success'){
						res.content.records.map((item,index)=>{
							if(index == 0){
								this.singleType = item
							}else{
								this.typeList.push(item)
							}
						})
					}
				})
			},
			// 获取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: #FFFFFF;
		padding: 20rpx;
		.type-box{
			display: flex;
			justify-content: space-between;
			.left{
				width: 224rpx;
				
			}
			.right{
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				align-content: space-between;
				padding-left: 20rpx;
				.type-item{
					.icon{
						width: 224rpx;
						height: 124rpx;
						border-radius: 16rpx;
						vertical-align: bottom;
					}
				}
			}
			.type-item{
				position: relative;
				.glass{
					position: absolute;
					bottom: 0%;
					left: 0%;
					width: 100%;
					height: 72rpx;
					padding-left: 16rpx;
					box-sizing: border-box;
					border-radius:0 0 16rpx 16rpx;
					background: linear-gradient(180deg, rgba(0,188,255,0) 0%, var(--colour) 100%);
					.title{
						font-weight: bold;
						color: #FFFFFF;
						font-size: 24rpx;
						margin-top: 8rpx;
					}
					.type{
						font-weight: 400;
						color: #FFFFFF;
						font-size: 16rpx;
						margin-top: 6rpx;
					}
				}
				.icon{
					width: 224rpx;
					height: 268rpx;
					border-radius: 16rpx;
					vertical-align: bottom;
				}
			}
		}
		
		.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>