<template>
	<view class="service-grid">
		<view class="service-item" v-for="(item, index) in services" :style="'width: '+width" :key="services.id"
			@click="onServiceClick(item)">
			<image :src="item.photo[0]" v-if="item.photo && item.photo.length != 0" mode="aspectFill"
				class="service-image" lazy-load :style="'width: '+width"></image>
			<text class="service-tag">{{item.server_kind == 1 ? '到家' : item.server_kind == 2 ? '到店' : ''}}</text>
			<view class="service-info">
				<view class="service-title">
					<image class="service-title-type" mode="widthFix" src="@/static/images/shop/pintuan/group.png"
						v-if="item.team_buy && Object.keys(item.team_buy).length != 0"></image>
					<text class="service-title-text">
						{{ item.title }}
					</text>

				</view>
				<view class="service-row">
					<view class="service-price" v-if="item.team_buy && Object.keys(item.team_buy).length != 0">
						<text class="price-symbol">拼团价：¥</text>
						<text class="price-value" :style="{'font-size': `${formatPriceSize(item.team_buy.price, 36, 5)}rpx`}">{{ item.team_buy.price }}</text>
					</view>
					<view class="service-price" v-else>
						<text class="price-symbol">¥</text>
						<text class="price-value">{{ item.server_price }}</text>
					</view>
					<text class="service-sales">销量{{item.sales_num}}</text>
				</view>
				<view class="service-shop" v-if="item.server_kind == 2">
					<text class="shop-text text-overflow-1">{{item.shop_name}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ServiceGrid',
		props: {
			// 服务列表数据
			services: {
				type: Array,
				default: () => []
			},
			// 是否显示多图布局
			showMultiImages: {
				type: Boolean,
				default: false
			},
			// 是否显示找相似按钮
			showSimilar: {
				type: Boolean,
				default: false
			},
			// 上一页是nvue页面还是vue页面
			pageType: {
				type: String,
				default: 'vue'
			},
			width: {
				type: String,
				default: '334rpx'
			}
		},
		methods: {
			onServiceClick(service) {
				if (this.pageType == 'nvue') {
					console.log(this.pageType)
					this.$emit('serviceclick', service)
				} else {
					this.$emit('service-click', service)
				}

			},
			formatPriceNumber(num) {
			  num = Number(num)
			  let formatted;
			   formatted = num.toFixed(2);
			  // 根据数字大小确定保留的小数位数
			  // if (num >= 100) {
			  //   // 大于等于100时，不保留小数
			  //   formatted = Math.floor(num).toString();
			  // } else if (num >= 10) {
			  //   // 大于等于10时，保留1位小数
			  //   formatted = num.toFixed(1);
			  // } else {
			  //   // 其他情况，保留两位小数
			  //   formatted = num.toFixed(2);
			  // }
			
			  // 处理小数最后一位是0的情况
			  // 如果包含小数点，则检查并移除末尾的0和可能的小数点
			  // if (formatted.includes(".")) {
			  //   formatted = formatted.replace(/0+$/, "").replace(/\.$/, "");
			  // }
			
			  return formatted;
			},
			formatPriceSize(o_num, o_fontSize, type = 1) {
			    let fontSize = o_fontSize;
			    const len = this.formatPriceNumber(o_num).length;
			    //首页拼团立省价格
			    if (len > 7 && type == 1) {
			      fontSize -= 4;
			    }
			    //分享图立省价格
			    if (len > 7 && type == 2) {
			      fontSize -= 3;
			    }
			    //分享图拼团价格
			    if (type === 3) {
			      if (len > 4) {
			        fontSize -= 10;
			      }
			      if (len > 5) {
			        fontSize -= 4;
			      }
			      if (len > 6) {
			        fontSize -= 6;
			      }
			      if (len > 7) {
			        fontSize -= 6;
			      }
			    }
			    //分享图原价
			    if (type === 4) {
			      if (len > 5) {
			        fontSize -= 2;
			      }
			      if (len > 6) {
			        fontSize -= 2;
			      }
			    }
				
			    //首页商家
			    if (type === 5) {
			      if (len > 6) {
			        fontSize -= 6;
			      }
			      if (len > 7) {
			        fontSize -= 4;
			      }
			      if (len > 8) {
			        fontSize -= 4;
			      }
			    }
				
			    return fontSize;
			  },
		}
	}
</script>

<style lang="less">
	.service-grid {
		display: flex;
		flex-flow: row wrap;
		justify-content: space-between;
		align-items: flex-start;
		/* grid-template-columns: repeat(2, 1fr);
  gap: 34rpx; */
	}

	.service-item {
		width: 334rpx;
		background-color: #fff;
		border-radius: 20rpx;
		box-shadow: 0 0 12rpx 0 rgba(0, 0, 0, 0.05);
		overflow: hidden;
		position: relative;
		margin-bottom: 24rpx;
	}

	.service-image {
		width: 334rpx;
		height: 334rpx;
		background-color: #000000;
	}

	.service-tag {
		position: absolute;
		top: 0rpx;
		left: 0rpx;
		background-color: #FF4767;
		color: #FFFFFF;
		font-size: 24rpx;
		padding: 6rpx 14rpx;
		border-radius: 0 0 20rpx 0;
	}

	.service-info {
		padding: 16rpx;
	}

	.service-title {
		margin-bottom: 16rpx;
		display: flex;
		align-items: center;
		flex-direction: row;
	}


	.service-title-type {
		width: 64rpx;
		/* 图片与文字之间的间距 */
		margin-right: 13rpx;
	}

	.service-title-text {
		font-weight: 500;
		font-size: 28rpx;
		color: #000000;
		line-height: 40rpx;
		text-align: left;
		font-style: normal;
		flex: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		/* 显示的行数 */
		lines: 1;
		/* NVUE下要用这个属性，来让文字超出隐藏变省略号 */
	}


	.service-row {
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
	}

	.service-sales {
		font-size: 24rpx;
		color: #808080;
	}

	.service-price {
		display: flex;
		flex-flow: row nowrap;
		justify-content: flex-start;
		align-items: center;
	}

	.price-symbol {
		font-weight: 500;
		font-size: 24rpx;
		color: #FF4767;
		line-height: 33rpx;
		text-align: left;
		font-style: normal;
	}

	.price-value {
		font-weight: 500;
		font-size: 36rpx;
		color: #FF4767;
		line-height: 33rpx;
		text-align: left;
		font-style: normal;
	}

	.shop-text {
		font-size: 24rpx;
		color: #3D3D3D;
		font-weight: 400;
	}



	/* 	
.service-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.service-item {
  background-color: #F8F8F8;
  border-radius: 8rpx;
  overflow: hidden;
  position: relative;
}

.service-images {
  position: relative;
  width: 100%;
  height: 320rpx;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4rpx;
}

.service-image {
  width: 100%;
  height: 100%;
  background-color: #eee;
}

.single-image {
  width: 100%;
  height: 320rpx;
}

.service-tag {
  position: absolute;
  top: 0;
  left: 0;
  background-color: #FF94B4;
  color: #FFFFFF;
  font-size: 24rpx;
  padding: 6rpx 14rpx;
  border-radius: 20rpx 0 20rpx 0;
}

.service-info {
  padding: 16rpx;
}

.service-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 8rpx;
}

.service-price {
  display: flex;
  align-items: baseline;
}

.price-symbol {
  font-size: 24rpx;
  color: #FF4767;
}

.price-value {
  font-size: 32rpx;
  color: #FF4767;
  font-weight: 500;
  margin-right: 12rpx;
}

.find-similar {
  font-size: 24rpx;
  color: #999;
  margin-left: auto;
}

.text-overflow {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
} */

	/* APP适配 */
	/* #ifdef APP-PLUS */
	.service-item {
		transform: translateZ(0);
	}

	/* #endif */
</style>