<template>
	<view class="item" @click="$emit('click')">
		<!-- 商品图片 -->
		<view class="item-image">
			<image :src="item.image &&
			item.image.split(',')[0]" mode="aspectFill">
			</image>
		</view>

		<!-- 商品信息 -->
		<view class="info">
			<!-- 商品标题 -->
			<view class="title">
				{{ item.title }}
			</view>

			<!-- 价格 -->
			<!-- <view class="price">
				<view class="money">
					￥<text>{{ getPriceByRole(item) }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="price-imgs">
					<image v-if="userInfo.role == 1" :src="configList.vip_money_one" mode="aspectFill" class="price-img">
					</image>
					<image v-if="userInfo.role == 2" :src="configList.vip_money_two" mode="aspectFill" class="price-img">
					</image>
					<image v-if="userInfo.role == 3" :src="configList.vip_money_three" mode="aspectFill"
						class="price-img"></image>
				</view>
			</view> -->
			
			<view class="price">
				<view class="money">
					￥<text>{{ item.price }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="oldPrice">
					￥<text>{{ item.oldPrice }}</text>
					<text class="unit">/元</text>
				</view>
			</view>
			
			<!-- <view class="oldPrice">
				<view class="money">
					￥<text>{{ item.oldPrice }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="price-imgs">
				</view>
			</view> -->
			
			<view class="price">
				<view class="money">
					￥<text>{{ item.silverPrice }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="price-imgs">
					<image :src="configList.vip_money_one" mode="aspectFill" class="price-img">
					</image>
				</view>
			</view>
			
			<view class="price">
				<view class="money">
					￥<text>{{ item.goldPrice }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="price-imgs">
					<image :src="configList.vip_money_two" mode="aspectFill"
						class="price-img"></image>
				</view>
			</view>
			
			<view class="price">
				<view class="money">
					￥<text>{{ item.diamondPrice }}</text>
					<text class="unit">/元每件</text>
				</view>
				<view class="price-imgs">
					<image :src="configList.vip_money_three" mode="aspectFill" class="price-img">
					</image>
				</view>
			</view>

			<!-- 销量 -->
			<view class="sales-volume">
				<view class="sales-volume-imgbox">
					<image src="@/static/image/category/sales-volume-icon.png" mode="widthFix" class="sales-volume-img">
					</image>
				</view>

				<view class="sales-volume-number">
					已售出{{ item.payNum }}+单
				</view>
			</view>

			<!-- 购买按钮 -->
			<view @click.stop="purchase(item.id)" class="buy-btn">
				购买
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		props: {
			item: {
				default: {}
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		data() {
			return {
			}
		},
		methods: {
			// 购买商品(创建订单)
			purchase(id) {
				this.$api('getRiceProductDetail', {
					id
				}, res => {
					if (res.code == 200) {
						res.result.num = 1
						this.$store.commit('setPayOrderProduct', [
							res.result
						])
						this.$utils.navigateTo('/pages_order/order/createOrder')
					}
				})
			},
			
			//根据角色获取对应价格
			getPriceByRole(item) {
				let priceFiledList = ['goldPrice','silverPrice','diamondPrice']
				let price = item[priceFiledList[this.userInfo.role - 1]]
				
				return price >= 0 ? price : item.price
			}
		}
	}
</script>

<style scoped lang="scss">
	.item {
		width: 100%;
		padding: 10rpx 20rpx;
		align-items: center;
		display: flex;
		box-sizing: border-box;
		border-bottom: 1rpx solid #00000012;
		background: white;
		border-radius: 20rpx;
		margin-bottom: 20rpx;

		// 商品图片
		.item-image {
			width: 180rpx;
			height: 180rpx;

			image {
				height: 100%;
				width: 100%;
			}
		}

		// 商品信息
		.info {
			padding: 20rpx 0rpx 20rpx 20rpx;
			color: #555;
			width: calc(100% - 180rpx);
			box-sizing: border-box;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			gap: 10rpx;
			// 商品标题
			.title {
				font-size: 28rpx;
				font-weight: bold;
				overflow: hidden; //超出的文本隐藏
				text-overflow: ellipsis; //溢出用省略号显示
				white-space: nowrap; //溢出不换行
			}

			// 价格
			.price {
				display: flex;
				align-items: center;
				color: #f40;
				font-size: 26rpx;
				align-items: flex-end;
				.money {
					font-size: 30rpx;

					.unit {
						font-size: 20rpx;
					}
				}

				.price-imgs {
					display: flex;
					flex-wrap: wrap;

					.price-img {
						width: 80rpx;
						height: 30rpx;
						margin-left: 10rpx;
					}
				}
			}
			.oldPrice{
				color: #999;
				font-size: 18rpx;
				text-decoration: line-through;
			}

			// 销量
			.sales-volume {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.sales-volume-imgbox {
					width: 20rpx;

					.sales-volume-img {
						width: 100%;
					}
				}

				.sales-volume-number {
					color: #B8B8B8;
					font-size: 26rpx;
					padding-left: 10rpx;
				}
			}

			// 购买按钮
			.buy-btn {
				background: $uni-color;
				color: white;
				display: inline-block;
				padding: 10rpx 20rpx;
				border-radius: 10rpx;
				width: fit-content;
			}
		}
	}
</style>