<template>
	<view class="detail_main">
		<nav-bar title='提交订单'></nav-bar>
		
		<view class="reser_type" style="margin-top: 24rpx;padding-bottom: 24rpx;">
			<view class="right_nav_item" v-for="(item,index) in selectList" :key="index">
				<image class="goods_img" :src="item.url" mode="aspectFill" @click="previewImage(item.url)"></image>
				<view class="goods_info">
					<view class="goods_name">{{item.greenName}}</view>
					<view class="goods_pice">
						<view class="goods_icon">
							<view v-if="item.num > 0" style="font-size: 30rpx;margin: 0 10rpx;">x {{item.num}}</view>
						</view>
						<view style="font-size: 32rpx;font-weight: bold;">
							<text style="font-size: 24rpx;">￥ </text> {{item.greenPrice}} / <text style="font-size: 24rpx;">{{item.unit}}</text>
						</view>
						
					</view>
				</view>
			</view>
		</view>

		<view class="reser_type" style="margin-top: 24rpx;">
			<view class="reser_tle">订购信息</view>
			<van-form label-width="150rpx">
				<view class="input_row">
					<view class="input_tle_no">商品价格</view>
					<view style="flex: 1;font-size: 28rpx;">{{goodsPrice}}元</view>
				</view>
				<view class="input_row">
					<view class="input_tle_no">配送费</view>
					<view style="flex: 1;font-size: 28rpx;">{{delivery}}元</view>
				</view>
				<view class="input_row">
					<view class="input_tle_no">订购人姓名</view>
					<van-field style="flex: 1;" v-model="userInfo.realName" readonly placeholder="请填写"/>
				</view>
				<view class="input_row">
					<view class="input_tle_no">联系电话</view>
					<van-field
						name="mobile"
						maxlength="11"
						style="flex: 1;" 
						v-model="userInfo.mobile" 
						placeholder="请填写"
						readonly
						:rules="[{ validator: mobileValidator,required: true, message: '请正确填写手机号' }]"/>
				</view>
				<view class="input_row">
					<view class="input_tle_no">单位名称</view>
					<van-field style="flex: 1;" v-model="userInfo.departmentName" readonly placeholder="请填写"/>
				</view>
			</van-form>
		</view>
		
		
		<view class="bottom_btn">
			<view class="info_nav">
				<view style="font-weight: bold;font-size: 30rpx;">合计：
					<text style="font-size: 24rpx;font-weight: 400;color: #F4772D;">￥</text> <text style="color: #F4772D;">{{totalPrice}}</text> 
				</view>
				<view style="font-size: 26rpx;">已选 {{totalNum}} 件</view>
			</view>
			<view class="btn_nav" @click="submitClick()"> 提交订单</view>
		</view>
	</view>
</template>

<script>
	import navBar from "@/components/navBar/index.vue";
	import {accAdd,accMul} from "@/common/util.js"
	import {greenorderAdd} from "@/common/api.js"
	import {
		callbackRequest
	} from '@/common/util.js';
	export default {
		components: {
			navBar
		},
		data() {
			return {
				selectList: [],
				userInfo: null,
				totalPrice: 0,
				totalNum: 0,
				discount: '',
				delivery: 0,
				goodsPrice: 0
			}
		},
		onLoad(options) {
			this.discount = options.discount
			this.delivery = options.delivery
			this.userInfo = uni.getStorageSync('userInfos')
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('selectChange', (data) => {
				this.selectList = data.data
				console.log('this.selectList', this.selectList)
				this.totalNum = 0
				this.totalPrice = 0
				this.selectList.forEach(item => {
					this.totalNum = accAdd(this.totalNum,item.num)
					this.totalPrice = accAdd(this.totalPrice,accMul(item.greenPrice,item.num))
					this.goodsPrice = accAdd(this.goodsPrice,accMul(item.greenPrice,item.num))
				})
				// 有配送费
				if(this.discount == '1') {
					this.totalPrice = accAdd(this.totalPrice,this.delivery)
				}
				// 无配送费
				else if(this.discount == '2'){
					this.delivery = 0
				}
			})
		},
		methods: {
			previewImage(img) {
				uni.previewImage({
					urls: [img]
				});
			},
			
			submitClick() {
				if(!this.mobileValidator(this.userInfo.mobile)) {
					uni.showToast({title: '请完整输入手机号',icon: 'none'});
					return
				}
				let arr = []
				this.selectList.forEach(item => {
					arr.push({
						greenId: item.id,
						greenName: item.greenName,
						greenNum: item.num,
						greenPrice: item.greenPrice,
						isDeleted: '0',
						tenantId: this.userInfo && this.userInfo.tenantId ? this.userInfo.tenantId : 1,
						url: item.url
					})
				})
				let params = {
					method: greenorderAdd,
					name: this.userInfo.realName,
					usePrice: this.totalPrice,
					phone: this.userInfo.mobile,
					unit: this.userInfo.departmentName,
					status: '0',
					isDeleted: '0',
					greenOrderInfoList: arr,
					delivery: this.delivery
				}
				callbackRequest(params).then((res) => {
					if (res.returnCode == 1) {
						uni.showModal({
							title: '提示',
							content: '订购成功，点击返回上一页',
							showCancel: false,
							success: res => {
								if(res.confirm) {
								 let pages = getCurrentPages() 
								 let prevPage = pages[pages.length -2] 
								 prevPage.$vm.clearClick() 
									uni.navigateBack()
								}
							}
						});
					} else {
						uni.showToast({title: res.returnMsg,icon: 'none'});
					}
				})
			},
			mobileValidator(val) {
				 return /^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\d{8}$/.test(val);
			},
		}
	}
</script>

<style scoped lang="scss">
	.detail_main {
		background-color: #F2F6FA;
		padding: 100rpx 24rpx 150rpx 24rpx;
		box-sizing: border-box;
	}

	.reser_date {
		width: 100%;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 0rpx 24rpx 0 24rpx;
		box-sizing: border-box;
	}

	.reser_type {
		width: 100%;
		min-height: 100rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 24rpx 24rpx 0 24rpx;
		box-sizing: border-box;

		.reser_tle {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #0E1A24;
			margin-left: 30rpx;
			position: relative;
			margin-bottom: 28rpx;
		}

		.reser_tle:before {
			position: absolute;
			content: '';
			left: -24rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 8rpx;
			height: 30rpx;
			background: linear-gradient(180deg, #82E7B3 0%, #94B8EF 100%);
			border-radius: 4rpx;
		}
	}

	.input_row_col {
		margin-top: 30rpx;
	}

	.input_row {
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #EBEFF7;
		padding: 32rpx 0;
	}
	.input_tle {
		position: relative;
		width: 160rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #4B5B68;
		text-align: left;
		line-height: 24px;
	}
	.input_tle:before {
		position: absolute;
		right: 40rpx;
		top: 0;
		color: #FB8107;
		font-size: 28rpx;
		content: '*';
	}
	.input_tle_no {
		line-height: 24px;
		width: 160rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #4B5B68;
		text-align: left;
		display: inline-flex;
	}

	.input_tips {
		flex: 1;
		line-height: 24px;
		font-weight: 400;
		font-size: 28rpx;
		color: #0E1A24;
		text-align: left;
		display: inline-flex;
	}

	.bottom_btn {
		width: 100%;
		height: 120rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;

		button {
			width: 654rpx;
			height: 88rpx;
			background: linear-gradient(94deg, #5BCE9D 0%, #00ADA6 100%);
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			font-weight: 400;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 88rpx;
			text-align: center;
		}
	}
	.right_nav_item:last-child {
		margin-bottom: 0;
	}
	.right_nav_item {
		display: flex;
		margin-bottom: 40rpx;
		.goods_img {
			width: 176rpx;
			height: 176rpx;
			border-radius: 20rpx;
			display: block;
		}
		.goods_info {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding: 24rpx;
			box-sizing: border-box;
			.goods_name {
				font-weight: bold;
				font-size: 32rpx;
				color: #0E1A24;
			}
			.goods_pice {
				display: flex;
				justify-content: space-between;
			}
			.goods_icon {
				display: flex;
				// width: 140rpx;
				justify-content: space-between;
				align-items: center;
				image {
					width: 40rpx;
					height: 40rpx;
					display: block;
				}
			}
		}
	}
	.bottom_btn {
		z-index: 99;
		padding-left: 44rpx;
		width: calc(100% - 48rpx);
		height: 100rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 6rpx 16rpx 2rpx #E7EFF9;
		position: fixed;
		bottom: 20rpx;
		border-radius: 50rpx;
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		justify-content: space-between;
		.info_nav {
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
		}
		.btn_nav {
			width: 216rpx;
			height: 100%;
			background: linear-gradient( 180deg, #5BCE9D 0%, #00ADA6 100%);
			border-radius: 0rpx 50rpx 50rpx 0rpx;
			font-weight: bold;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 100rpx;
			text-align: center;
		}
	}
	/deep/.van-cell {
		padding: 0;
	}
</style>