<template>
	<view class="container flex-col" :style="{height: winHeight + 'px'}">
		<!-- #ifdef MP -->
		<!-- 状态栏高度 -->
		<view :style="{ height: `${statusBarHeight}px` }"></view>

		<!-- 导航栏 -->
		<view class="navigationbar flex-center" :style="{ height: `${navigationBarHeight}px` }">
			<view class="back-button" @tap="handBack()"></view>

			<navigator :url="`/pages/merchant_search/index?id=${takeoutId}`" class="input tui-skeleton-rect searchIpt flex-center"
			 hover-class="none"><text class="iconfont icon-xiazai5"></text>
				点击搜索外卖商家</navigator>
		</view>
		<!-- #endif -->
		<view class="box-merchant flex-col">
			<view class="top-merchant">
				<view class="flex-y-center">
					<image style="width: 96rpx; height: 96rpx;border-radius: 6rpx;" :src="recommendObj.avatar"></image>
					<view style="margin-left: 20rpx; flex: 1;">
						<view class="mer-name flex-between-center">
							<view>{{recommendObj.name}}</view>
							<view class='iconfont icon-jiantou' style="font-size: 30rpx;" @tap="merchantTab(recommendObj)"></view>
						</view>
						<view class="flex-between-center" style="padding-right: 53rpx;">
							<text class="mer-tj">今日推荐</text>
							<text class="goods-score">{{recommendObj.score}}</text>
							<text class="goods-info">月销量{{getFormattedSales(recommendObj.salesVolume)}}</text>
							<text v-if="recommendObj.isFreeDeliverFee" class="goods-info" style="color: #E5AD21;margin-left: 18rpx;">免配送费</text>
							<view v-if="recommendObj.isFreeDeliverFee && recommendObj.freeDeliverFee > 0" style="color: #999999;font-size: 21rpx;margin-left: 10rpx;text-decoration: line-through;">¥
								{{recommendObj.freeDeliverFee}}</view>
							<!-- <text v-if="!recommendObj.isFreeDeliverFee" style="color: #999999;font-size: 21rpx;margin-left: 2rpx;">配送费¥
								{{recommendObj.distCosts}}</text> -->
							<text class="goods-info" style="margin-left: 10rpx;">{{recommendObj.distance}}</text>
						</view>
					</view>
				</view>
				<view class="flex-y-center" style="margin: 15rpx 0;">
					<view class="cert-box" v-for="(item, index) in recommendObj.discountLabel" :key="index">{{item}}</view>
				</view>
				<scroll-view :scroll-x="true" scroll-with-animation style="white-space: nowrap;">
					<view class="top-box-item" v-for="(item, index) in recommendObj.proList" :key="index">
						<image style="width: 100%; height: 131rpx;border-radius: 10rpx;" :src="item.image"></image>
						<view class="name">{{item.name}}</view>
						<view style="padding-right: 23rpx">
							<text style="color: #FF6702;">￥</text><text class="bg-color">{{item.price}}</text>
							<!-- <text class="goods-info" style="text-decoration: line-through;">￥6</text> -->
						</view>
					</view>
				</scroll-view>
			</view>
			<scroll-view :scroll-x="true" scroll-with-animation style="white-space: nowrap;margin: 20rpx 0rpx;">
				<view :class="optionIndex == item.id ? 'active-option' : ''" style="color: #141414;display: inline-block;margin-right: 30rpx;width: 96rpx;"
				 v-for="(item, index) in optionData" :key="index" @tap="onOptionClick(item)">
					<view class="flex-col flex-center">
						<image style="width: 100%;height: 96rpx;" :src="item.icon"></image>
						<view style="font-size: 21rpx;margin-top: 10rpx;">{{item.name}}</view>
					</view>
				</view>
			</scroll-view>
			<!-- 销量排行 -->
			<view class="tab-list" style="margin-top: 10rpx;">
				<view :class=" item.checked ? 'tab-item2' : 'tab-item2-false' " v-for="item in labelList" :key="item.id" @click="item2Click(item)">{{item.label}}</view>
			</view>

			<view class="flex-center flex-col" v-if="noDataTip">
				<image :src="urlDomain+'crmebimage/presets/noSearch.png'"></image>
				<text class="no-data-tip">{{noDataTip}}</text>
			</view>

			<scroll-view scroll-y scroll-with-animation style="flex: 1;overflow: hidden;" @scrolltolower="onScrollBottom">
				<view class="content-goods2" v-for="item in merchantList" :key="item.id" @click="merchantTab(item)">
					<view>
						<image :src="item.avatar" style="width: 230rpx;height: 230rpx;"></image>
					</view>

					<view style="margin-left: 20rpx;flex: 1;">
						<view class="">
							<text class="goods-title">{{item.name}}</text>
						</view>

						<view style="margin-top: 10rpx;">
							<text class="goods-score">{{item.score}}</text>
							<text class="goods-info" style="margin-left: 10rpx;">月销 {{getFormattedSales(item.salesVolume)}}</text>
							<text class="goods-info" style="margin-left: 25rpx;">人均 ¥ {{item.perCapita==null?0:item.perCapita}}</text>
						</view>

						<view style="margin-top: 10rpx;display: flex;align-items: center;">
							<text class="goods-info">起送 ¥ {{item.deliveryFee==null?0:item.deliveryFee}}</text>
							<text v-if="item.isFreeDeliverFee" class="goods-info" style="color: #E5AD21;margin-left: 18rpx;">免配送费</text>
							<view v-if="item.isFreeDeliverFee && item.freeDeliverFee > 0" style="color: #999999;font-size: 21rpx;margin-left: 10rpx;text-decoration: line-through;">¥
								{{item.freeDeliverFee}}</view>
							<text v-if="!item.isFreeDeliverFee" class="goods-info" style="color: #999999;margin-left: 40rpx;">配送费¥{{item.distCosts}}</text>
							<text class="goods-info" style="margin-left: 38rpx;">{{item.deliverTime}}分钟</text>
							<text class="goods-info" style="margin-left: 10rpx;">{{item.distance}}</text>
						</view>
						<view style="margin-top: 22rpx;"><text class="goods-sales">已售卖{{getFormattedSales(item.salesAllNum)}}单</text></view>
						<view style="margin-top: 14rpx;">
							<swiper :indicator-dots="false" :autoplay="true" :circular="true" :display-multiple-items="3" style="height: 43rpx;">
							  <swiper-item  v-for="(item2,index2) in item.discountLabel" :key="index2">
							   <text class="goods-labels">{{item2}}</text>
							  </swiper-item>
							</swiper>
						</view>

					</view>
				</view>
				<view class='loadingicon flex-center' :hidden='!loading'>
					<text class='loading iconfont icon-jiazai' style="width: auto;"></text>加载更多
				</view>
				<view class="flex-center no-data-tip" v-if="loadTitle">{{loadTitle}}</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	import navBar from '@/components/navBar';
	import {
		getMerchantList,
		getHomeInfo,
		getMerchantSearchList
	} from '@/api/home.js';
	import {
		getRecommend,
		getProduct
	} from '@/api/takeout.js';
	import tags from '../../mixins/tags';
	let app = getApp();
	var statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'rpx';
	export default {
		components: {
			navBar
		},
		mixins:[tags],
		data() {
			return {
				statusBarHeight: app.globalData.statusBarHeight,
				navigationBarHeight: 0,
				takeoutId: null,
				winHeight: 0,
				recommendObj: {},
				optionData: [],
				optionIndex: 0,
				noDataTip: '',
				urlDomain: this.$Cache.get("imgHost"),
				// labelList: [{
				// 	id: 1,
				// 	label: "销量排行",
				// 	checked: true
				// }, {
				// 	id: 2,
				// 	label: "满减大促",
				// 	checked: false
				// }, {
				// 	id: 3,
				// 	label: "首单立减",
				// 	checked: false
				// }, {
				// 	id: 4,
				// 	label: "减配送费",
				// 	checked: false
				// }],
				merchantList: [],
				loadTitle:'',
				loading: false,
				page: 1,
			}
		},
		onLoad(options) {
			let that = this;
			const custom = wx.getMenuButtonBoundingClientRect()
			// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
			that.navigationBarHeight = custom.height + (custom.top - that.statusBarHeight) * 2
			that.takeoutId = options.id;
			uni.getSystemInfo({
				success: function(res) {
					that.winHeight = res.windowHeight
				},
			});
			const allData = {
				name : '全部',
				id : 0,
				icon :  '/static/img/quanbu.png',
			}; 
			getProduct(that.takeoutId).then(res => {
				const data = res.data;
				// data[0].id = 0;
				// data[0].name = '全部';
				// data[0].icon = '/static/img/quanbu.png';
				// that.optionIndex = data[0].id;
				// that.optionData = data || []  
				if (data.length !== 0) {
					that.optionIndex =0; 
					that.optionData.push(allData)
					data.forEach(function(item) {
					  that.optionData.push(item);
					}); 
				} 
				that.page = 1;
				that.getMatchedData()
				console.log(that.optionData, '12312321321312321321312312')
			})  
			getRecommend({
				latitude: uni.getStorageSync('user_latitude'),
				longitude: uni.getStorageSync('user_longitude'),
				cateId: that.takeoutId
			}).then(recommendRes => {
				that.recommendObj = recommendRes.data
				console.log(that.recommendObj, '00000000000000000000000000')
			})
		},
		methods: {
			getFormattedSales(value) {
						      // 转换成字符串，然后截取前三位，并添加“+”
			  return value!=null&& String(value).length>3?`${String(value).slice(0, 3)}+`:value;
			},
			onScrollBottom() {
				if (!this.loadTitle) {
					this.getMatchedData()
				}
			},
			// getTag(){
			// 	return this.labelList.filter((ite => ite.checked)).map(ite => ite.id).join(',')
			// },
			getMatchedData() {
				if (this.loading) return;
				this.loading = true
				getMerchantSearchList({
					latitude: uni.getStorageSync('user_latitude'),
					longitude: uni.getStorageSync('user_longitude'),
					productType: this.takeoutId,
					typeIds: this.optionIndex==0?"":this.optionIndex,
					page: this.page,
					limit: 10,
					queryLabels: this.getTag()
				}).then(res => {
					let list = res.data.list || [];
					this.merchantList = this.$util.SplitArray(list, this.merchantList);
					if (this.merchantList.length == 0) {
						this.loadTitle = ''
						this.noDataTip = ' 暂无外卖商家 ';
					} else {
						this.noDataTip = ''
						this.loadTitle = list.length < 10 ? "我也是有底线的~" : '';
					}
					this.loading = false
					console.log('商品信息:', this.merchantList);
					this.page += 1;
				}).catch(err => {
					this.loading = false
					console.log(err)
				});
			},
			handBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			goMerchant() {
				this.$util.navigateTo('/pages/merchant_search/index');
			},
			item2Click(item) {
				item.checked = !item.checked;
				this.merchantList = []
				this.page = 1
				this.getMatchedData()
			},
			// getMatchedData() {
			// 	getMerchantList({
			// 		latitude: uni.getStorageSync('user_latitude'),
			// 		longitude: uni.getStorageSync('user_longitude'),
			// 		cateId: this.takeoutId
			// 	}).then(res => {
			// 		this.merchantList = res.data.list;
			// 		this.noDataTip = this.merchantList.length ? '' : ' 暂无外卖商家 ';
			// 		console.log('商品信息:', this.merchantList);
			// 	});
			// },
			onOptionClick(item) {
				if (this.optionIndex === item.id) return
				this.page = 1;
				this.merchantList = [];
				this.optionIndex = item.id;
				//this.optionIndex = item.id;
				this.getMatchedData()
			},
			merchantTab(item) {
				// 修改跳转
				uni.navigateTo({
					url: `/pages/merchant/home/index?merId=${item.id}`
					// url: `/pages/goods_cate/index?id=${item.id}`
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.searchIpt {
		width: 350rpx !important;
		height: 75%;
		background-color: #fff;
		border-radius: 35rpx;
		padding-left: 20rpx;
		justify-content: flex-start;
	}

	.no-data-tip {
		color: #CCC;
	}

	.container {
		overflow: hidden;
		position: relative;
	}

	.search-box {
		background-color: #fff;
		padding: 0 39rpx;
		margin-top: 10rpx;
		box-sizing: border-box;
		height: 96rpx;

		.flex-y-center {
			width: 100%;
			height: 69rpx;
			padding-left: 29rpx;
			border-radius: 35rpx;
			border: 2rpx solid #FF6702;
		}
	}

	.icon-fanhui2 {
		// line-height: 66rpx;
	}

	.search-box input {
		width: 90%;
		font-size: 26rpx;
	}

	.search-box .placeholder {
		color: #bbb;
	}

	.search-box .iconfont {
		color: #000;
		font-size: 35rpx;
	}

	.box-merchant {
		background-color: #F5F5F5;
		border-radius: 30rpx 30rpx 0 0;
		padding: 20rpx 20rpx 0 20rpx;
		// margin-top: 30rpx;
		overflow: hidden;
		flex: 1;
	}

	.top-merchant {
		background-color: #fff;
		position: relative;
		padding: 20rpx;
		border-radius: 20rpx;

		.mer-name {
			font-weight: 600;
			font-size: 27rpx;
			color: #141414;
			margin-bottom: 10rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.mer-tj {
			background: #FAFAFA;
			border-radius: 19rpx;
			font-weight: 400;
			font-size: 17rpx;
			color: #646464;
			padding: 6rpx 10rpx;
		}

		.cert-box {
			font-size: 21rpx;
			color: #FF6702;
			background: #FEEFE5;
			border-radius: 8rpx;
			padding: 4rpx 20rpx;
			margin-right: 10rpx;
		}

		.top-box-item {
			width: 173rpx;
			margin-right: 20rpx;
			display: inline-block;

			.name {
				font-weight: 400;
				font-size: 21rpx;
				color: #141414;
				width: 100%;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}

	.bg-color {
		color: $bg-color-primary;
		font-weight: 600;
		font-size: 31rpx;
	}

	.active-option {
		color: $bg-color-primary !important;
	}

	.tab-list {
		display: flex;
		/* 启用弹性布局 */
		flex-direction: row;
		/* 横向排列 */
		justify-content: space-between;
		/* 均匀分布子项 */
		flex-wrap: wrap;
		/* 允许换行（适用于屏幕宽度不足时） */

		.tab-item2 {
			width: 23%;
			/* 5个元素平分宽度，留2%作为间隙 */
			text-align: center;
			background-color: #FFC127;
			border-radius: 8rpx;
			font-size: 23rpx;
			padding: 6rpx;
			font-weight: 400;
		}

		.tab-item2-false {
			width: 23%;
			/* 5个元素平分宽度，留2%作为间隙 */
			text-align: center;
			background-color: #fff;
			border-radius: 8rpx;
			font-size: 23rpx;
			padding: 6rpx;
			font-weight: 400;
		}
	}

	.content-goods2 {
		background: #FFFFFF;
		border-radius: 23rpx;
		margin-top: 10rpx;
		padding: 20rpx;
		display: flex;
	}

	.goods-title {
		color: #141414;
		font-size: 26rpx;
		font-weight: 600;
	}

	.goods-desc {
		color: #646464;
		font-size: 21rpx;
		margin-top: 10rpx;
	}

	.goods-price {
		color: #FF9805;
		font-size: 34rpx;
		font-weight: 600;
	}

	.goods-score {
		color: $bg-color-primary;
		font-size: 27rpx;
	}

	.goods-info {
		color: #999999;
		font-size: 21rpx;
	}

	.goods-info3 {
		color: #999999;
		font-size: 21rpx;
		margin-left: 10rpx;
		text-decoration: line-through;
	}

	.goods-sales {
		background-color: #FEEFE5;
		color: #FF6702;
		border-radius: 8rpx;
		font-size: 21rpx;
		padding: 4rpx 20rpx;
	}

	.goods-labels {
		background-color: #FFF6F6;
		color: #FF4B4B;
		border: 2rpx solid #FF4B4B;
		border-radius: 8rpx;
		font-size: 21rpx;
		padding: 4rpx 16rpx;
		margin-right: 20rpx;
		margin-left: 10rpx;
	}


	.back-button {

		width: 25rpx;

		height: 25rpx;

		border-right: 4rpx solid #fff;
		border-bottom: 4rpx solid #fff;

		transform: rotate(135deg);

		margin-left: 10rpx;

		display: inline-block;
	}

	.navigationbar {
		position: relative;
		background-color: $bg-color-primary;

		.back-button {
			position: absolute;
			left: 40rpx;
		}
	}
</style>
