<template>
	<view>
		<view class="inner-box">
			<!-- 商品列表 -->
			<view class="goods-list">
				<view v-for="(item,index) in carts" :key="index" class="item">
					<view class="img-box">
						<image :src="item.productImage" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view>
							<view class="name-box ellipsis2">
								<view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>{{item.productName}}
							</view>
							<view class="spec ellipsis2">{{item.productAttrName}}</view>
						</view>
						<view class="price-num">
							<view class="price" @click.stop>
								<text class="unit">¥</text>
								<text class="num">{{item.price.toFixed(2)}}</text>
							</view>
							<view class="num">x{{item.cartNum}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="title">赠品列表</view>
			<view class="goods-list">
				<view v-for="(item,index) in giftCarts" :key="index" class="item" >
					<view class="img-box">
						<image :src="item.productImage" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view>
							<view class="name-box-r">
								<view class="name-box ellipsis2">
									<view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>
									{{item.productName}}
								</view>
								<view class="del" @click="delCart(item)" >删除</view>
							</view>
							<view class="spec ellipsis2">{{item.productAttrName}}</view>
						</view>
						<view class="price-num">
							<view class="price" @click.stop>
								<text class="unit">¥</text>
								<text class="num">{{item.price.toFixed(2)}}</text>
							</view>
							<view class="num">x{{item.cartNum}}</view>
						</view>
					</view>
				</view>
				<!-- 小计 -->
				<view class="sub-total">
					<view class="addgift" @click="addgift">新增赠品</view>
					<view style="flex: 1;display: flex;justify-content: flex-end;">
						<text class="label">合计金额：</text>
						<view class="price">
							<text class="unit">¥</text>
							<text class="num">{{price.payPrice.toFixed(2)}}</text>
						</view>
					</view>
				</view>
			</view>
			 
		</view>
		<!-- 底部按钮 -->
		<view class="btn-foot">
			<view class="right">
				<view class="total" >
					<text class="label">总金额：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num">{{price.totalPrice.toFixed(2)}}</text>
					</view>
				</view>
				<view class="btn" @click="openUpdateMoney()" >
					{{createSalesOrderType==1?'修改金额':'实收金额'}}
				</view>
				<view class="btn"  @click="getshow">
					分享
					<button  class="share" data-name="shareBtn" open-type="share" v-if="isshow">分享</button>
				</view>
			</view>
		</view>
		<modal v-if="inputShow" title="实收金额" confirm-text="保存" cancel-text="取消" @cancel="cancelUpdateMoney" @confirm="confirmUpdateMoney">
			<input type="text" v-model="inputTxt" placeholder="请输入实收金额" class="intxt" maxlength="8" />
		</modal>
		<u-popup :show="show" mode="bottom" :closeOnClickOverlay="false">
			<view class="popbox-title">
				<view style="color:#888" ></view>
				<view>修改商品单价</view>
				<view style="color:red" @click="confirmUpdate">确定</view>
			</view>
			<scroll-view :scroll-y="true" class="popbox" style="height: 70vh;">
				<view class="goods-list">
					<view v-for="(item,index) in cartsNew" :key="index" class="item">
						<view class="img-box">
							<image :src="item.productImage" mode="aspectFill"></image>
						</view>
						<view class="info-box">
							<view>
								<view class="name-box ellipsis2">
									<view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>{{item.productName}}
								</view>
								<view class="spec ellipsis2">{{item.productAttrName}}</view>
							</view>
							<view class="price-num">
								<view class="price" @click.stop>
									<text class="unit">¥</text>
									<u--input
									    :customStyle="{padding: '0 2px',width:'60px'}"
									    placeholder="价格"
										type="digit"
										maxlength="20"
									    border="surround"
									    v-model.trim="item.price"
									    @blur="blur($event,item)"
									  ></u--input>
								</view>
								<!-- <view class="num">x{{item.cartNum}}</view> -->
								<view class="change-num-box">
									<view class="img-box-num" @click="delNum(item)">
										<image v-if="item.cartNum <= 1" src="../../static/images/jian.png" mode=""></image>
										<image v-else src="../../static/images/jian2.png" mode=""></image>
									</view>
									<input  type="number" @change="changeNum($event,item)" :value="item.cartNum"   />
									<view class="img-box-num" @click="addNum(item)">
										<image src="../../static/images/add.png" mode=""></image>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="title">赠品列表</view>
					<view v-for="(item,index) in giftCartsNew" :key="index" class="item">
						<view class="img-box">
							<image :src="item.productImage" mode="aspectFill"></image>
						</view>
						<view class="info-box">
							<view>
								<view class="name-box ellipsis2">
									<view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>{{item.productName}}
								</view>
								<view class="spec ellipsis2">{{item.productAttrName}}</view>
							</view>
							<view class="price-num">
								<view class="price" @click.stop>
									<text class="unit">¥</text>
									<u--input
									    :customStyle="{padding: '0 2px',width:'60px'}"
									    placeholder="价格"
										type="digit"
										maxlength="20"
									    border="surround"
									    v-model.trim="item.price"
									    @blur="blur($event,item)"
									  ></u--input>
								</view>
								<!-- <view class="num">x{{item.cartNum}}</view> -->
								<view class="change-num-box">
									<view class="img-box-num" @click="delNum(item)">
										<image v-if="item.cartNum <= 1" src="../../static/images/jian.png" mode=""></image>
										<image v-else src="../../static/images/jian2.png" mode=""></image>
									</view>
									<input  type="number" @change="changeNum($event,item)" :value="item.cartNum"   />
									<view class="img-box-num" @click="addNum(item)">
										<image src="../../static/images/add.png" mode=""></image>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</u-popup>
	</view>
</template>

<script>
	import {delCart,cartNum} from '@/api/product'
	import {getpro} from "@/api/index.js"
	import {getSalesOrder,addUserCart,updateSalseOrderMoney,updateSalesOrderMoneyByProduct} from '@/api/companyOrder.js'
	export default {
		data() {
			return {
				inputShow:false,
				inputTxt:null,
				orderKey:null,
				 // 原来数据
				price:{
					payPrice:0.00,
					totalPrice:0.00,
				},
				 // 原来数据
				carts:[],
				giftCarts: [],
				cartsNew: [],
				giftCartsNew: [],
				// 类型
				createSalesOrderType: 1,
				show: false,
				isshow:true,
				isSure:false
			}
		},
		onLoad(option) {
			this.orderKey=option.orderKey;
			this.getConfigByKey()
		},
		//发送给朋友
		onShareAppMessage(res) {
			return {
				title: "倍力优",
				path: '/pages_company/order/confirmOrder?orderKey='+this.orderKey,
				imageUrl: this.carts[0].productImage //分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
			}
			
		},
		//分享到朋友圈
		onShareTimeline(res) {
			return {
				title:"倍力优",
				query:'orderKey='+this.orderKey,
				imageUrl:  this.carts[0].productImage //分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
			}
		},
		onShow() {
			this.getSalesOrder();
			
		},
		methods: {
			getshow(){
				if(!this.isshow){
					uni.showToast({
						title: '请修改赠品金额！',
						icon: "none"
					})
				}
			},
			getConfigByKey(){
				const data={
					key:"store.config"
				}
				getpro(data).then(res=>{
					if(res&&res.data) {
						this.createSalesOrderType = JSON.parse(res.data).createSalesOrderType || 1
					} else {
						this.createSalesOrderType = 1
					}
					
				})
			},
			blur(value,item) {
				item.price = typeof item.price === 'string' ? item.price.trim() : String(item.price || 0).trim();
				let price = item.price
				const regex = /^(0|[1-9]\d*)(\.\d{1,2})?$/;
				if(!regex.test(price.trim())) {
					uni.showToast({
						title: '请输入正确的金额',
						icon: "none"
					})
					return 
				}
			},
			confirmUpdate(type) {
				const regex = /^(0|[1-9]\d*)(\.\d{1,2})?$/;
				let flag = this.cartsNew.some(item=> {
					let price = typeof item.price === 'string' ? item.price.trim() : String(item.price || 0).trim()
					return !regex.test(price)
				})
				let flag2 = this.giftCartsNew.some(item=> {
					let price = typeof item.price === 'string' ? item.price.trim() : String(item.price || 0).trim()
					return !regex.test(price)
				})
				if(flag||flag2) {
					uni.showToast({
						title: '请输入正确的金额',
						icon: "none"
					})
					return 
				}
				if(type!=1) {
					this.show = false
				}
				const param = {
					createOrderKey:this.orderKey,
					token:uni.getStorageSync('CompanyUserToken'),
					carts: this.cartsNew,
					giftCarts: this.giftCartsNew
				}
				uni.showLoading({
					title: '处理中'
				})
				updateSalesOrderMoneyByProduct(param).then(
					res => {
						uni.hideLoading()
						if(res.code==200){
							this.isSure=true
							this.getSalesOrder();
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				).catch(()=>{
					uni.hideLoading()
				})
			},
			openUpdateMoney(){
				if(this.createSalesOrderType==1) {
					this.cartsNew = uni.$u.deepClone(this.carts)
					this.giftCartsNew = uni.$u.deepClone(this.giftCarts)
					this.show = true
				} else {
					this.inputShow = true
				}
			},
			cancelUpdateMoney(){
				this.inputShow = false
			},
			confirmUpdateMoney(){
				if(parseFloat(this.inputTxt)>0){
					var that=this;
					var data={createOrderKey:this.orderKey,token:uni.getStorageSync('CompanyUserToken'),money:this.inputTxt}
					updateSalseOrderMoney(data).then(
						res => {
							if(res.code==200){
								this.inputShow = false
								this.getSalesOrder();
							}else{
								uni.showToast({
									icon:'none',
									title: res.msg,
								});
							}
						},
						rej => {}
					);
					
				}
				else{
					uni.showToast({
						icon:'none',
						title: "价格应大于0",
					});
				}
			},
			showDetail(item) {
				uni.navigateTo({
					url: 'productDetails?productId='+item.productId
				})
			},
			getSalesOrder(){
				
				var that=this;
				that.price.payPrice=0;
				that.price.totalPrice=0;
				var data={createOrderKey:this.orderKey}
				getSalesOrder(data).then(
					res => {
						if(res.code==200){
							
							this.carts=res.carts;
							this.giftCarts = res.giftCarts;
							this.carts.forEach(function(element) {
								that.price.payPrice+=element.price*element.cartNum;
							});
							this.giftCarts.forEach(function(element) {
								that.price.payPrice+=element.price*element.cartNum;
							});
							that.price.totalPrice=res.totalMoney
							that.cartsNew = uni.$u.deepClone(that.carts)
							that.giftCartsNew = uni.$u.deepClone(that.giftCarts)
							if(this.giftCarts.length>0&&!this.isSure){
								this.isshow=false
								console.log('酷酷酷',this.isshow)
							}else if(this.isSure){
								this.isshow=true
							}
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			addgift() {
				uni.navigateTo({
					url: '/pages_company/order/productGiftList'
				})
			},
			delCart(item){
				let data = {ids:[item.id]};
				delCart(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
								icon:'success',
								title: "操作成功",
							});
							this.getSalesOrder();
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			changeNum(e,item) {
				item.cartNum = e.detail.value.replace(/\D/g, '')
				if (item.cartNum <= 1) {
				  uni.showToast({
				    title: "已经是底线啦!",
				    icon: "none",
				    duration: 2000
				  });
				  return;
				}
				if(item.cartNum < 1) {
					item.cartNum = 1
				}
				if(item.cartNum>=item.stock){
					item.cartNum=item.stock;
				}
				this.changeCartNum(item)
			},
			changeCartNum(item){
				this.confirmUpdate(1)
			},
			// 购物车减法
			delNum(item) {
				if (item.cartNum <= 1) {
				  uni.showToast({
				    title: "已经是底线啦!",
				    icon: "none",
				    duration: 2000
				  });
				  return;
				}
				item.cartNum --
				if(item.cartNum < 1) {
					item.cartNum = 1
				}
				 
				this.changeCartNum(item)
			},
			// 购物车加法
			addNum(item) {
				console.log(item)
				item.cartNum++
				if(item.cartNum>=item.stock){
					item.cartNum=item.stock;
				}
				this.changeCartNum(item)
			},
		}
	}
</script>

<style lang="scss">
	.popbox {
		background-color: #fff;
		&-title {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 24rpx;
			background-color: #fff;
			border-bottom: 1rpx solid #EDEEEF;
			box-sizing: border-box;
		}
	}
	.title {
		padding: 24rpx 0 0;
		font-weight: bold;
	}
	.inner-box{
		padding: 20upx 20upx 140upx;
	}
	.goods-list{
		margin-top: 20upx;
		padding: 0 30upx;
		background-color: #FFFFFF;
		border-radius: 16upx;
		.item{
			padding: 30upx 0;
			border-bottom: 1px solid #EDEEEF;
			display: flex;
			align-items: center;
			.img-box{
				width: 160upx;
				height: 160upx;
				margin-right: 30upx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.info-box{
				width: calc(100% - 190upx);
				height: 160upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.name-box-r {
					display: flex;
					align-items: flex-start;
					justify-content: space-between;
					.del {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: red;
					}
				}
				.name-box{
					flex: 1;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40upx;
					.tag{
						display: inline-block;
						padding: 0 6upx;
						height: 30upx;
						background: linear-gradient(90deg, #3def30 0%,#018C39 100%);
						border-radius: 4upx;
						margin-right: 10upx;
						font-size: 22upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FFFFFF;
						line-height: 30upx;
						float: left;
						margin-top: 7upx;
					}
				}
				.spec{
					margin-top: 10upx;
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1;
					flex: 1;
				}
				.price-num{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.price{
						display: flex;
						align-items: flex-end;
						.unit{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 1.2;
							margin-right: 4upx;
						}
						.num{
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 1;
						}
					}
					.num{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1;
					}
				}
			}
		}
		.sub-total{
			height: 88upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.addgift {
				padding: 10rpx 14rpx;
				color: #018C39;
				font-size: 28rpx;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				border: 2rpx solid #018C39;
			}
			.label{
				font-size: 24upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
			.price{
				display: flex;
				align-items: flex-end;
				.unit{
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FF6633;
					line-height: 1.2;
					margin-right: 4upx;
				}
				.num{
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FF6633;
					line-height: 1;
				}
			}
		}
	}
	.points{
		height: 88upx;
		padding: 0 30upx;
		background: #FFFFFF;
		border-radius: 16upx;
		 
		display: flex;
		align-items: center;
		justify-content: space-between;
		.left{
			display: flex;
			align-items: center;
			image{
				width: 28upx;
				height: 28upx;
				margin-right: 20upx;
			}
			.text{
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}
		}
		.right{
			display: flex;
			align-items: center;
			.text{
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #111111;
				 
			}
			image{
				margin-left: 15upx;
				width: 14upx;
				height: 24upx;
			}
		}
	}
	.remarks{
		height: 88upx;
		padding: 0 30upx;
		background: #FFFFFF;
		border-radius: 16upx;
		margin-top: 20upx;
		display: flex;
		align-items: center;
		input{
			width: 100%;
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000000;
		}
		.input{
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}
	}
	.btn-foot{
		box-sizing: border-box;
		width: 100%;
		height: 121upx;
		background: #FFFFFF;
		padding: 16upx 30upx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		.right{
			display: flex;
			align-items: center;
			.total{
				display: flex;
				align-items: flex-end;
				margin-right: 15upx;
				.label{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1.5;
				}
				.price{
					display: flex;
					align-items: flex-end;
					.unit{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 10upx;
					}
					.num{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}
			}
			.btn{
				margin-left: 10rpx;
				width: 180upx;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background:#018C39;
				border-radius: 44upx;
				position: relative;
				.share{
					display: inline-block;
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%rpx;
					opacity: 0;
				}
			}
		}
	}
	.change-num-box{
		display: flex;
		align-items: center;
		.img-box-num{
			width: 48upx;
			height: 48upx;
			// border-radius: 4upx;
			border: 1px solid #dddddd;
			display: flex;
			align-items: center;
			justify-content: center;
			image{
				width: 25rpx;
				height: 25rpx;
			}
		}
		input{
			width: 48upx;
			height: 48upx;
			line-height: 48upx;
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #111111;
			// border-radius: 4upx;
			border-top: 1px solid #dddddd;
			border-bottom: 1px solid #dddddd;
			text-align: center;
			// margin: 0 16upx;
		}
	}
</style>
 