<template>
	<view class="page flex flex-col ">
		<!-- <commonTitle title="搜索酒款名称" bg="#FDFDFD"></commonTitle> -->
		<view class="search-box">
			<!-- <text class="cuIcon-search" style="font-size: 40rpx;color: #A2A2A2;margin-right: 24rpx;"
				@click="search">222233</text> -->
			<input v-model="keyword" type="text" placeholder="搜索酒款/品牌" @confirm="search" @input="changeSearch">
		</view>
		<!-- 热门啤酒风格 -->
		<template v-if="!keyword">
			<view class="hot-title">热门啤酒风格</view>
			<view class="hot-box">
				<view v-for="(item, index) in popularStyleList" :key="index" class="hot-item flex justify-between"
					@click="searchByStyle(item)">
					<view class="flex-1">
						<view class="title word-all">{{ item.beerStyles}}</view>
						<view class="sub">全系列{{item.popular}}款产品在售</view>
					</view>
					<image src="@/static/right-arrow.png" style="width: 48rpx;height: 48rpx;"></image>
				</view>
			</view>
		</template>
		<template v-else>
			<view>
				<view class="padding-lr flex justify-start">
					<view class="text-sm tag" :class="{'active-tag': curTag == 0}" @click="changeTag(0)">最佳匹配</view>
					<view class="text-sm tag" :class="{'active-tag': curTag == 1}" @click="changeTag(1)">酒款</view>
					<view class="text-sm tag" :class="{'active-tag': curTag == 2}" @click="changeTag(2)">酒厂</view>
				</view>
			</view>
		</template>
		<template v-if="curTag == 0">
			<scroll-view v-if="commonBeer.length > 0" scroll-y="true" class="my-brandSide">
				<view class="brandSide-box" v-for="(item, index) in commonBeer" :key="index" @click="toBeer(item)">
					<view class="flex align-center justify-start">
						<image :src="item.cover" class="beerCover"></image>
						<view>
							<view class="title">{{ item.beerName }}</view>
							<view class="desc" style="margin-bottom: 10rpx;">{{item.beerStyles}}</view>
							<view class="desc flex align-center">
								<image :src="item.brandLogo" style="width: 40rpx;height: 40rpx;margin-right: 10rpx;">
								</image>
							{{item.brandName}}</view>
						</view>
					</view>
					
					<view class="flex flex-col justify-between">
						<view class="desc margin-bottom-xs">
							{{ item.beerOverallRating || 0 }}
							<text class="cuIcon-favorfill" style="font-size: 30rpx;margin-left: 10rpx;color:#FEE034"></text>
						</view>
						<view class="desc" style="text-align: right;">
							{{ item.beerReviewsCount || 0}}
							<text class="cuIcon-friend" style="font-size: 30rpx;margin-left: 10rpx;"></text>
						</view>
												
					</view>
				</view>
			</scroll-view>
			<scroll-view v-if="commonBreweries.length > 0" scroll-y="true" class="my-brandSide">
				<view class="brandSide-box" v-for="(item, index) in commonBreweries" :key="index" @click="toBrand(item)">
					<view class="flex align-center justify-start">
						<image :src="item.brandLogo" class="logo"></image>
						<view>
							<view class="title">{{ item.brandName }}</view>
							<!-- <view class="desc">20241223</view> -->
						</view>
					</view>
					<image src="@/static/right-arrow.png" style="width: 48rpx;height: 48rpx;"></image>
				</view>
			</scroll-view>
			
			<view v-if="commonBeer.length == 0 && commonBreweries.length == 0" style="flex:1;width: 100%;" class="flex justify-center align-center">
				<view style="color: #979797;font-size: 24rpx;">暂无结果</view>
				</view>
		</template>
		
		<!-- 酒款列表 -->
		<template v-if="curTag == 1">
			<scroll-view v-if="beers.length > 0" scroll-y="true" class="my-brandSide" style="flex:1">
				<view class="brandSide-box" v-for="(item, index) in beers" :key="index" @click="toBeer(item)">
					<view class="flex align-center justify-start">
						<image :src="item.cover" class="beerCover"></image>
						<view>
							<view class="title">{{ item.beerName }}</view>
							<view class="desc" style="margin-bottom: 10rpx;">{{item.beerStyles}}</view>
							<view class="desc flex align-center">
								<image :src="item.brandLogo" style="width: 40rpx;height: 40rpx;margin-right: 10rpx;">
								</image>
							{{item.brandName}}</view>
						</view>
					</view>
					
					<view class="flex flex-col justify-between">
						<view class="desc margin-bottom-xs">
							{{ item.beerOverallRating || 0 }}
							<text class="cuIcon-favorfill" style="font-size: 30rpx;margin-left: 10rpx;color:#FEE034"></text>
						</view>
						<view class="desc" style="text-align: right;">
							{{ item.beerReviewsCount || 0}}
							<text class="cuIcon-friend" style="font-size: 30rpx;margin-left: 10rpx;"></text>
						</view>
												
					</view>
				</view>
			</scroll-view>
		
			<view v-else style="flex:1;width: 100%;" class="flex justify-center align-center">
				<view style="color: #979797;font-size: 24rpx;">暂无结果</view>
			</view>
		</template>
		<!-- 酒厂列表 -->
		<template v-if="curTag == 2">
			<scroll-view v-if="breweries.length > 0" scroll-y="true" class="my-brandSide" style="flex:1">
				<view class="brandSide-box" v-for="(item, index) in breweries" :key="index" @click="toBrand(item)">
					<view class="flex align-center justify-start">
						<image :src="item.brandLogo" class="logo"></image>
						<view>
							<view class="title">{{ item.brandName }}</view>
							<!-- <view class="desc">20241223</view> -->
						</view>
					</view>
					<image src="@/static/right-arrow.png" style="width: 48rpx;height: 48rpx;"></image>
				</view>
			</scroll-view>
			<view v-else style="flex:1;width: 100%;" class="flex justify-center align-center">
				<view style="color: #979797;font-size: 24rpx;">暂无结果</view>
			</view>
		</template>
	</view>
</template>

<script>
	import {
		commonSearch,
		popularStyle,
		getBeerByStyle,
		searchByBreweriesApi,
		getBeerList
	} from "@/api/platform.js"
	export default {
		components: {
		},
		data() {
			return {
				keyword: '',
				popularStyleList: [],
				curTag: 0,
				breweries: [], // 酒厂列表
				beers: [], // 酒款列表
				commonBeer:[],
				commonBreweries:[],
				timer: null
			};
		},
		mounted() {
			this.getPopularStyle()
		},
		methods: {
			changeSearch() {
			  if(this.keyword.length < 2) return
			  this.commonBeer=[]
			  this.commonBreweries=[]
			  // 节流搜索
			  if (this.timer) {
				  clearTimeout(this.timer)
				  this.timer = null
			  }
			  this.timer = setTimeout(()=>{
				  if(this.keyword.length < 2) return
				  this.search()
			  },500)
			},
			search() {
				commonSearch(this.keyword).then(res => {
					console.log(res)
					this.commonBeer = res.data.beers
					this.commonBreweries = res.data.breweries
				})
			},
			// 获取热门风格
			getPopularStyle() {
				popularStyle().then(res => {
					console.log(res)
					this.popularStyleList = res.data
				})
			},
			searchByStyle(item) {
				// getBeerByStyle(item.beerStyles).then(res => {
				// 	console.log(res)
				// })
				uni.navigateTo({
					url: '/pages/index/styleBeer?beerStyles=' + item.beerStyles
				})
			},
			// 切换标签
			changeTag(key) {
				this.curTag = key
				if (this.curTag == 2) {
					this.searchByBreweries()
				} else if (this.curTag == 1) {
					this.searchByBeer()
				} else if (this.curTag === 0) {
					this.search()
				}
			},
			// 酒厂筛选
			searchByBreweries() {
				searchByBreweriesApi(this.keyword).then(res => {
					console.log(res)
					this.breweries = res.rows
				})
			},
			// 酒款筛选
			searchByBeer() {
				getBeerList(this.keyword).then(res => {
					console.log(res)
					this.beers = res.rows
				})
			},
			// 跳转酒厂详情
			toBrand(item) {
				uni.navigateTo({
					url: '/pages/index/brandHome?breweryId=' + item.id
				})
			},
			// 跳转酒款详情
			toBeer(item) {
				uni.navigateTo({
					url: '/pages/index/review?beerId=' + item.id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background: #FDFDFD;
		padding: 0px 4rpx;
		height: 100vh;
		
		.search-box {
			border-radius: 20rpx;
			box-sizing: border-box;
			// width: 100%;
			height: 100rpx;
			background-color: rgba(196, 196, 196, 0.15);
			display: flex;
			align-items: center;
			padding: 20rpx 18rpx;
			margin: 0 30rpx 20rpx 22rpx;

			input {
				width: 100%;
				height: 100%;
				background-color: transparent;
				font-size: 28rpx;
			}
		}

		.hot-title {
			font-family: Roboto;
			font-size: 48rpx;
			font-weight: 500;
			color: #0B0E26;
			margin: 46rpx 22rpx 46rpx;
		}

		.hot-box {
			border-radius: 12rpx;
			padding: 24rpx 40rpx;
			margin: 0 22rpx;
			background: #FEFEFE;
			box-sizing: border-box;
			border: 1px solid #E3E3E5;
			box-shadow: 0px 25px 40px -10px rgba(34, 24, 63, 0.06);

			.hot-item {
				padding: 20rpx 0;

				.title {
					font-family: Roboto;
					font-size: 32rpx;
					font-weight: 500;
					color: #0B0E26;
					margin-bottom: 8rpx;
				}

				.sub {
					font-family: Roboto;
					font-size: 24rpx;
					font-weight: 500;
					color: #5E5F60;
				}
			}
		}

		.tag {
			background-color: transparent;
			border-radius: 20rpx;
			padding: 12rpx 30rpx;
			border: 1px solid #3C3C3C;
			margin-right: 40rpx;
		}

		.active-tag {
			background-color: #39E5B1;
			font-weight: bold;
			border: 1px solid #39E5B1;
		}

		.my-brandSide {
			padding: 28rpx 36rpx;

			.brandSide-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx 0;
				border-bottom: 1rpx solid #F0F4F9;
				padding: 32rpx 0;

				.logo {
					width: 88rpx;
					height: 88rpx;
					border-radius: 12rpx;
					margin-right: 28rpx;
				}
				.beerCover {
					width: 120rpx;
					height: 140rpx;
					border-radius: 12rpx;
					margin-right: 28rpx;
				}
				.title {
					font-size: 28rpx;
					color: #19191B;
					margin-bottom: 20rpx;
				}

				.desc {
					font-size: 20rpx;
					color: #9C9BA6;
				}
			}
		}
	}
</style>