<template>
	<view>
		<view class="inner-box">
			<!-- 药品列表 -->
			<view class="goods-list">
				<view v-for="(item,index) in carts" :key="index" class="item" @click="showDetail(item)">
					<view class="img-box">
						<image :src="source=='audit'?item.image : item.productAttrImage||item.productImage" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view>
							<view class="name-box ellipsis2">
								<view class="myotctxt" style="margin-right: 10rpx;" v-show="item.productType" :style="{background:_background(item.productType)}">{{$getDictLabelName("storeProductType",item.productType)}}</view>
								{{ item.commonName&&item.commonName!=='-'?item.commonName:item.productName }}
							</view>
							<view class="spec ellipsis2">{{item.productAttrName}}</view>
						</view>
						<view class="price-num">
							<view class="price">
								<text class="unit">¥</text>
								<text class="num">{{item.price?item.price.toFixed(2):'0.00'}}</text>
							</view>
							<view class="num">x{{item.cartNum}}</view>
						</view>
					</view>
				</view>
				<!-- 小计 -->
				<view class="sub-total">
					<text class="label">合计金额：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num">{{price.payPrice?price.payPrice.toFixed(2):'0.00'}}</text>
					</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?price.totalPrice.toFixed(2):'0.00'}}</text>
					</view>
				</view>
				<view class="btn" @click="openUpdateMoney()" v-if="source!='audit'">
					实收金额
				</view>
				<view class="btn" v-show="!disabled">
					分享
					<button  class="share" data-name="shareBtn" open-type="share">分享</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>
	</view>
</template>

<script>
	import {getSalesOrder,getSalesOrders,updateSalseOrderMoney,sharePrepare,changeListDetail} from '../api/companyOrder.js'
	export default {
		data() {
			return {
				inputShow:false,
				inputTxt:null,
				orderKey:null,
				price:{
					payPrice:0.00,
					totalPrice:0.00,
				},
				carts:[],
				disabled: false,
				source:'', // audit审核页过来的
				auditId:''
			}
		},
		computed: {
			_background() {
				//productType： 1：OTC，2：Rx，3：非药品，4：器械
				return (productType)=> {
					switch (productType) {
						case 1: return '#37E2EA' // OTC
						case 2: return 'red'     // Rx
						case 3: return '#2583EB' // 非药品
						case 4: return '#999'    // 器械
						default: return '#ccc'
					}
				}
			}
		},
		onLoad(option) {
			this.source = option.source || ''
			if(this.source == 'audit') {
				uni.hideShareMenu()
				this.disabled = true
				this.auditId = option.auditId || ''
				this.sharePrepare()
				this.getDetail()
			} else {
				this.orderKey=option.orderKey;
				uni.showShareMenu()
				this.getSalesOrder();
			} 
		},
		//发送给朋友
		onShareAppMessage(res) {
			return {
				title: "医健宝医药",
				path: '/pages_company/order/confirmOrder?orderKey='+this.orderKey,
				imageUrl: 'https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/sharelogo.png' //分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
			}
			
		},
		methods: {
			openUpdateMoney(){
				if(this.disabled) {
					uni.showModal({
						title: '提示',
						content: '✅ 实收金额改价申请审核中！',
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								console.log('用户点击确认');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					return
				}
				this.inputShow = true
			},
			cancelUpdateMoney(){
				this.inputShow = false
			},
			confirmUpdateMoney(){
				if(parseFloat(this.inputTxt)>0){
					var that=this;
					var data={totalAmount: this.price.totalPrice,createOrderKey:this.orderKey,token:uni.getStorageSync('CompanyUserToken'),money:this.inputTxt}
					updateSalseOrderMoney(data).then(
						res => {
							if(res.code==200){
								this.disabled = res.disabled
								if(this.disabled) {
									uni.hideShareMenu()
								} else {
									uni.showShareMenu()
								}
								this.inputShow = false
								this.getSalesOrder();
								uni.showModal({
									title: '提示',
									content: '✅ 实收金额改价申请已提交！',
									showCancel: false,
									success: function (res) {
										if (res.confirm) {
											uni.navigateBack()
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}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.carts.forEach(function(element) {
								that.price.payPrice+=element.price*element.cartNum;
							});
							that.price.totalPrice=res.totalMoney
							 
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			sharePrepare() {
				const param = {
					token:uni.getStorageSync('CompanyUserToken'),
					id:this.auditId,
				}
				sharePrepare(param).then(res=>{
					if(res.code==200) {
						uni.showShareMenu()
						this.disabled = false
					} else {
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			getDetail() {
				let that = this
				changeListDetail(this.auditId).then(res=>{
					if(res.code==200){
						that.carts=res.data.preparedProductInfoVoList;
						that.price.payPrice = res.data.totalAmount
						that.price.totalPrice=res.data.money
						that.orderKey = res.data.createOrderKey
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.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{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #111111;
						line-height: 40upx;
					}
					.spec{
						margin-top: 10upx;
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 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: flex-end;
				.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: #2583EB;
				border-radius: 44upx;
				position: relative;
				.share{
					display: inline-block;
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%rpx;
					opacity: 0;
				}
			}
		}
	}
</style>
 