<template>
	<view class="category">
		<view class="search" @click="goSearch">
			<image src="../../static/home/search.png" mode="" class="searchIcon"></image>
			<text class="searchInfo">iPone15</text>
		</view>
		<view class="main">
			<view class="menu">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper"
					@scrolltolower="lower" @scroll="scroll">
					<view id="demo1" class="menuList" v-for="(item,index) in secondMenu" :key="index"
						:style="{'color':secondChose==item.category_id?'#F22029':'#333'}" @click="choseSecondMenu(item)">{{item.name}}</view>
				</scroll-view>
			</view>
			<view class="goods">
				<view class="goodsTop">
					<scroll-view class="suggest" scroll-x="true">
						<view id="demo1" class="sugItem" v-for="(item,index) in menuList" :key="index" @click="choseFirstMenu(item)"
							:style="{'border':isChose==item.category_id?'2rpx solid #F22029':none,'color':isChose==item.category_id?'#F22029':'#333'}">
							{{item.name}}
						</view>
					</scroll-view>
				</view>
				<view class="goodsCenter">
					<view class="hotType">
						热门分类
					</view>
					<view class="goodsList">
						<view class="listItem" v-for="(item,index) in hotList" :key="index" @click="choseSecondMenu(item)">
							<image :src="item.image.preview_url" mode="" class="goodsImg" v-if="item.image.preview_url"></image>
							<view class="goodsName">
								{{item.name}}
							</view>
						</view>
					</view>
				</view>
				<view class="goodsCenter">
					<view class="hotType">
						{{typeName}}
					</view>
					<view class="goodsList">
						<view class="listItem" v-for="(item,index) in goodsList" :key="index"
							@click="goodsDetail(item)">
							<image :src="item.goods_image" mode="" class="goodsImg"></image>
							<view class="goodsName">
								{{item.goods_name}}
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>
<script>
	import * as cstegory from '@/api/category'
	export default {
		data() {
			return {
				secondChose:'',
				menuList: [],
				typeName: '',
				goodsList: [],
				isChose: '',
				hotList: [],
				secondMenu: [],
			}
		},
		onShow() {
			this.getGoodsTypeList();
			// this.getSonList()
		},
		methods: {
			choseFirstMenu(item){
				this.isChose = item.category_id;
				this.secondMenu = item.children;
				this.secondChose=''
				this.hotList = item.children
				// this.secondChose = this.secondMenu[0].category_id;
				this.getSonList(this.isChose)
			},
			choseSecondMenu(item){
				this.secondChose = item.category_id;
				this.typeName = item.name
				
				this.getSonList(this.secondChose)
			},
			getGoodsTypeList() {
				const that = this;
				console.log(cstegory.list(), '接口地址')
				return new Promise((resolve, reject) => {
					cstegory.list({
							is_hot: 0
						})
						.then(res => {
							// 合并新数据
							console.log(res, '分类数据')
							that.menuList = res.data.list;
							that.isChose = that.menuList[0].category_id;
							that.secondMenu = that.menuList[0].children;
							that.secondChose = that.secondMenu[0].category_id
							that.getSonList(that.secondChose)
							// that.hotList = that.menuList[0].
						})
						.catch(reject)
				})
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/search/index'
				})
			},
			getSonList(val) {
				const that = this;
				let params = {
					categoryId: val,
					page: 1
				}
				return new Promise((resolve, reject) => {
					cstegory.sonList(params)
						.then(res => {
							// 合并新数据
							console.log(res, '子数据')
							this.goodsList = res.data.list.data
						})
						.catch(reject)
				})
			},
			goodsDetail(item) {
				// 跳转商品详情
				uni.navigateTo({
					url: '/pages/goods/detail?goodsId=' + item.goods_id
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.category {
		width: 100%;
		height: 100%;
		background: #F5F5F5;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		opacity: 1;

		.search {
			width: 678rpx;
			height: 64rpx;
			background: #FFFFFF;
			border-radius: 68rpx 68rpx 68rpx 68rpx;
			opacity: 1;
			margin: 32rpx 0 0 34rpx;

			.searchIcon {
				width: 28rpx;
				height: 28rpx;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				opacity: 1;
				margin: 18rpx 0 0 26rpx;
			}

			.searchInfo {
				height: 64rpx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #8D8D8D;
				line-height: 64rpx;
				margin-left: 22rpx;
			}
		}

		.main {
			display: flex;
			margin-top: 28rpx;

			.menu {
				width: 172rpx;
				height: 100%;
				padding-top: 90rpx;

				.scroll-Y {
					height: 100%;
				}

				.menuList {
					margin-bottom: 54rpx;
					text-align: center;
					width: 172rpx;
					height: 40rpx;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #434343;
					line-height: 40rpx;
				}
			}

			.goods {
				.goodsTop {
					width: 500rpx;

					.suggest {
						width: 100%;
						white-space: nowrap;

						.sugItem {
							display: inline-block;
							width: 146rpx;
							height: 54rpx;
							background: #FEF6F6;
							border-radius: 82rpx 82rpx 82rpx 82rpx;
							opacity: 1;
							// border: 2rpx solid #F22029;
							line-height: 54rpx;
							margin-right: 34rpx;
							text-align: center;
						}
					}
				}

				.goodsCenter {
					width: 566rpx;
					min-height: 700rpx;
					background: #FFFFFF;
					border-radius: 12rpx 12rpx 12rpx 12rpx;
					opacity: 1;
					margin-top: 22rpx;
					padding-top: 30rpx;

					.hotType {
						width: 112rpx;
						height: 40rpx;
						font-size: 28rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						color: #434343;
						line-height: 40rpx;
						margin: 0rpx 0 0 34rpx;
					}

					.goodsList {
						width: 544rpx;
						margin: 26rpx 0 0 8rpx;
						display: flex;
						flex-wrap: wrap;
					}

					.listItem {
						width: 180rpx;
						height: 200rpx;
						margin-bottom: 34rpx;

						.goodsImg {
							width: 154rpx;
							height: 154rpx;
						}

						.goodsName {
							width: 180rpx;
							height: 40rpx;
							font-size: 28rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							color: #434343;
							line-height: 40rpx;
							text-align: center;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
				}
			}

		}
	}
</style>