<template>
	<view class="body">
		<view class="header" @click="addressList">
			<view class="model" v-if="ProductList.address.name? true :false ">
				<view style="font-size: 1rem; font-weight: 550;">{{ProductList.address.area}} {{ProductList.address.addr}} </view>
				<view style="margin-top: 2vw;"> <span>{{ProductList.address.name}}</span> <span style="margin-left: 2vw;">{{ProductList.address.tel}}</span> </view>
			</view>
			<view   v-if="ProductList.address.name? false : true " >
				<p style="font-size: 0.75rem; color: grey; ">您还没有收获地址哦~</p>
				<p
					style="width: 20vw; height: 8vw; margin-top: 2vw; text-align: center; line-height: 8vw; border-radius: 5px; color: red; border: 1px solid red; background-color:#fff5f5;">
					添加地址</p>
			</view>
		</view>
		<!--确认订单 -->
		<view class="content">
			<view class="shopCon">
				<p>
					<image style="width: 25vw; height: 25vw; border-radius: 8px;" :src="ProductList.product.cover.url"></image>
				</p>
				<view style="padding-left: 6vw;">
					<p style="font-size: 1rem; font-weight: 550;">{{ProductList.product.name}}</p>
					<!-- <p style="font-size: 0.75rem; color: grey; margin-top: 2vw;">五香味45g*7</p> -->
					<view style=" width: 50vw; display: flex;  margin-top: 2vw; ">
						<p style="font-size: 1.4rem; font-weight: 550; color: red;">￥{{ProductList.product.price}}</p>
						<view style=" width: 40vw; display: flex;  margin-left: 8vw;">
							<p class="minus_quantity" @click="decrease">-</p>
							<input class="input_quantity" type="number" v-model="quantity" @input="handleInput" />
							<p class="add_quantity" @click="increase">+</p>
						</view>
					</view>
				</view>
			</view>
			<p class="ProductIntroduction"> <span>商品金额</span> <span style="font-weight: 550;">￥{{ Number(ProductList.product.price*quantity) }}</span> </p>
		<!-- 	<p
				class="ProductIntroduction">
				<span>优惠金额</span> <span style="color: red; font-weight: 550;">-￥0</span>
				 </p> -->
		<!-- 	<p
				class="ProductIntroduction">
				<sapn>恒信豆</sapn> <span style="color: red; font-weight: 550;">-￥0</span></sapn>
			</p> -->
		<!-- 	<p
				class="ProductIntroduction" >
				<span>运费金额</span>
				<sapn>￥0</sapn>
			</p> -->
		<!-- 	<view
				class="ProductIntroduction">
				<span>订单备注</span>
				<view style="display: flex;">
					<p
						style=" width: 50vw; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; text-align: center; font-size: 0.8rem; color: grey; ">
						{{ProductList.product.notes}}</p>
					<p>
						<image style="width: 5vw; height: 5vw; " src="../../static/youjian.png" mode=""></image>
					</p>
				</view>
			</view> -->
		</view>
		<!-- 已选择 -->
			<u-toast ref="uToast" />
		<view class="foot">
			<view  style="width: 28vw;">合计: <span style="color: red; font-size: 1.2rem;">￥{{Number(ProductList.product.price*quantity)}}</span> </view>
			<view class="button" @click="purchase">下单</view>
		</view>
			
	</view>
</template>

<script>
	export default {
		data() {
			return {
				quantity: 1,
				ProductList:{
					product:{price:0}
				},
			}
		},
		onLoad(option) {
			if(option.storeId){
				this.storeId=option.storeId
				this.planProductList(option.storeId)
			}
		},
		onShow(){
			this.planProductList(this.storeId)
		},
		methods: {
			addressList(){
				uni.navigateTo({
					url:'/pages/shopStore/Address'
				})
			},
			// 数据信息
			planProductList(id){
				this.$api.postshopdetial(id,res=>{
					if(res.code==0){
						this.ProductList=res.data
					}
				})
			},
			handleInput(event) {
				const value = parseInt(event.target.value);
				if (!isNaN(value) && value >= 1) {
					this.quantity = value;
				} else {
					this.quantity = 1;
				}
			},
			increase() {
				this.quantity++;
			},
			decrease() {
				if (this.quantity > 1) {
					this.quantity--;
				}
			},
			purchase(){
				let that=this
				if(!that.$db.get('token')){
					uni.navigateTo({
						url:'/pages/login/login'
					})
					return
				}
				that.$api.postPlaceOrder({product:that.ProductList.product.id,customer_address:that.ProductList.address.id,count:that.quantity,price:that.ProductList.product.price},res=>{
					if(res.code==0){
						uni.requestPayment({
							provider:'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(e){
								that.successToast('充值成功')
								setTimeout(() => {
									uni.switchTab({
										url:'/pages/shopStore/index'
									})
								}, 1000)
							},
							faill:function(e){
								console.log(e)
								that.errorToast(e)
							}
						})
					}
				})
			},
			successToast(msg) {
				this.$refs.uToast.show({
					title: msg,
					type: 'success',
					duration: 1000
				})
			},
			errorToast(msg) {
				this.$refs.uToast.show({
					title: msg,
					type: 'error',
					duration: 1000
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.body {
		width: 100vw;
		height: 100vh;
		background-color: #f8f8f8;
		box-sizing: border-box;
		padding: 0 5vw;

		.header {
			width: 90vw;
			height: 10vh;
			background-color: white;
			border-radius: 10px;
			display: flex;
			justify-content: center;
			align-items: center;
			.model{
				width: 90vw;
				height: 10vh;
				border-radius: 5px;
				background-color: white;
				box-sizing: border-box;
				padding: 2vw;
			}
		}

		.content {
			width: 90vw;
			height: 30vh;
			margin-top: 4vw;
			background-color: white;
			border-radius: 10px;

			.shopCon {
				width: 90vw;
				height: 20vh;
				display: flex;
				box-sizing: border-box;
				padding: 4vw;

				.minus_quantity {
					border: 1px solid #f8f8f8;
					text-align: center;
					width: 20%;
					background-color: #f8f8f8;
					margin-right: 2vw;

				}

				.input_quantity {
					// border-bottom: 1px solid #74bfe7;
					text-align: center;
					width: 20%;
					height: 8vw;
					background-color: #f8f8f8;
				}

				.add_quantity {
					border: 1px solid #f8f8f8;
					background-color: #f8f8f8;
					text-align: center;
					width: 20%;
					margin-left: 2vw;
				}
			}

			.ProductIntroduction {
				width: 90vw;
				margin-top: 2vw;
				box-sizing: border-box;
				padding: 0 4vw;
				display: flex;
				justify-content: space-between;
			}
		}

		.foot {
			width: 90vw;
			height: 20vw;
			border-top: 1px solid lightgray;
			position: fixed;
			bottom: 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.button {
			width: 50vw;
			height: 12vw;
			text-align: center;
			line-height: 12vw;
			background-color: #f83036;
			color: white;
			border-radius: 32px;
		}
	}
</style>