<template>
	<view class="list-content">
		<pageHead pageTitle="议价审批" :showBack="true" :showHistory="true" @jumpHistory="jumpPage" style="margin-bottom: 10px;"></pageHead>
		<view class="list-card">
			<u--form ref="form" :model="bargainInfo" labelPosition="left" labelWidth="80" :labelStyle="{'color': '#6B7688','fontSize':'16px'}" >

				<u-form-item label="项目名称" prop="projectName">
				 <view class="val-posi">{{bargainInfo.projectName}}</view>
				</u-form-item>
				<u-form-item label="报价编号" prop="quotationNo">
				 <view class="val-posi">{{bargainInfo.quotationNo}}</view>
				</u-form-item>
			  <u-form-item label="报价人" prop="quoter">
			   <view class="val-posi">{{bargainInfo.quoter}}</view>
			  </u-form-item>
			  <u-form-item label="地区" prop="nickName" >
			    <view class="val-posi">{{bargainInfo.countryName+'/'+bargainInfo.city}}</view>
			  </u-form-item>
			 <!-- <u-form-item label="销售" prop="saler" >
			    <view class="val-posi">{{bargainInfo.saler}}</view>
			  </u-form-item> -->
			  <u-form-item label="代理商" prop="agentName" >
			    <view class="val-posi">{{bargainInfo.agentName}}</view>
			  </u-form-item>

			</u--form>
		</view>


		<view class="list-card" >
			<view class="card-title">电梯列表</view>
			<view v-for="(item, index) in bargainInfo.elevatorList">
				<u--form ref="form" :model="item" labelPosition="left" labelWidth="80" :labelStyle="{'color': '#6B7688','fontSize':'16px'}" >
				  <u-form-item label="型号" prop="elevatorModel">
				   <view class="val-posi">{{item.elevatorModel}}</view>
				  </u-form-item>
				  <u-form-item label="编号" prop="elevatorCode">
					<!--  <uni-tooltip :content="item.elevatorCode" placement="bottom">
						<view class="val-posi" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">{{item.elevatorCode}}</view>
					  </uni-tooltip> -->
					  <view class="val-posi">{{item.elevatorCode}}</view>
				  </u-form-item>
				  <u-form-item label="规格" prop="specification" >
				    <view class="val-posi">{{item.specification}}</view>
				  </u-form-item>
				  <u-form-item label="数量" prop="elevatorNumber" >
				    <view class="val-posi">{{item.elevatorNumber}}</view>
				  </u-form-item>
				  <u-form-item label="价格" prop="price" >
				    <view class="val-posi">{{stateFormatLow(item.price,0)}}</view>
				  </u-form-item>
				 <u-form-item label="基价折率" prop="discountRate">
				   <u-input v-model="item.discountRate" placeholder="请输入基价折率" type="digit" inputAlign="right" border="surround"
				   style="font-size: 16px;" :maxlength="30" @blur="changeRate(item)" :customStyle="{width:'150px'}"></u-input>
					<span style="margin-left: 5px;">%</span>
				 </u-form-item>
				 <u-form-item label="折后价格" prop="price" >
				   <view class="val-posi">{{stateFormatLow(item.discountPrice,2)}}</view>
				 </u-form-item>
				</u--form>
				<u-divider class="divider"></u-divider>
			</view>
		</view>


		<view class="list-card" style="padding: 16px 10px;">
			<view class="card-title">价格信息</view>
			<u-row  gutter="4" style="margin-bottom: 8px;">

			    <u-col span="4" >
			       <view class="card__price">
					 <view class="txt-label">价格合计</view>
					 <view class="txt-value">¥{{stateFormatLow(bargainInfo.priceTotal,0)}}</view>
			       </view>
			    </u-col>
			    <u-col span="4">
					<view class="card__price">
						<view class="txt-label">基价合计</view>
						<view class="txt-value">¥{{stateFormatLow(bargainInfo.basicPriceTotal,0)}}</view>
					</view>
			    </u-col>
			    <u-col span="4">
			       <view class="card__price">
			       	<view class="txt-label">选配合计</view>
			       	<view class="txt-value">¥{{stateFormatLow(bargainInfo.optionalPriceTotal,0)}}</view>
			       </view>
			    </u-col>

			</u-row>
			<u-row gutter="4">
				<u-col span="4">
					<view class="card__price">
						<view class="txt-label">希望价格</view>
						<view class="txt-value2">¥{{stateFormatLow(bargainInfo.expectedPrice,0)}}</view>
					</view>
				</u-col>
				<u-col span="4">
					<view class="card__price">
						<view class="txt-label">希望基价折率</view>
						<view class="txt-value2">{{bargainInfo.basicPriceRate}}%</view>
					</view>
				</u-col>
				<u-col span="4">
					<view class="card__price">
						<view class="txt-label">实际基价折率</view>
						<view class="txt-value2">{{bargainInfo.discountRate}}%</view>
					</view>
				</u-col>
			</u-row>
		</view>

		<view style="height: 75px;"></view>

		<view class="card_bottom ">
			<view class="flex-center" style="width: 100%;">
				<view class="list-card__price" >
					<view class="txt-label" style="font-size: 14px;">同意价格</view>
					<view class="txt-value2" style="font-size: 20px;">¥{{stateFormatLow(bargainInfo.acceptPrice,0)}}</view>
				</view>
				<view class="flex-center" style="width: 60%;padding: 0px 8px;">
					<view class="button-left flex-center-middle" @click="toggle('02')">
						<img class="button-icon" src="@/static/img/approve/reject.png" alt="" />
						<view class="reject-text">驳回</view>
					</view>
					<view class="button-right flex-center-middle" @click="toggle('01')">
						<img class="button-icon" src="@/static/img/approve/pass.png" alt="" />
						<view class="pass-text">通过</view>
					</view>
				</view>
			</view>
		</view>


		<!-- 弹窗 -->
		<view>
			<!-- 驳回弹窗 -->
			<uni-popup ref="popup" type="bottom" background-color="#fff" style="border-radius: 8rpx;">
				<view class="popup-content" >
					<view class="return_title">
						<text style="color: rgba(0, 0, 0, 0.85);">驳回原因：</text>
					</view>
					<view class="uni-textarea">
						<textarea v-model.trim="returnMsg" placeholder-style="color:#6B7688" placeholder="请输入驳回原因…" class="return_content" maxlength="200"/>
					</view>
					<view class="flex-center mt32">
						<view class="button-left flex-center-middle" @click="closePup()">
							<img class="button-icon" src="@/static/img/approve/reject.png" alt="" />
							<view class="reject-text">取消</view>
						</view>
						<view class="button-right flex-center-middle" @click="doSP('02')">
							<img class="button-icon" src="@/static/img/approve/pass.png" alt="" />
							<view class="pass-text">提交</view>
						</view>
					</view>
				</view>
			</uni-popup>

			<!-- 通过弹窗 -->
			<uni-popup ref="popupPass" background-color="#fff" borderRadius="10px 10px 10px 10px" style="width: 90%;">
				<view class="popup-content" style="padding: 20rpx 40rpx" >
					<view class="flex-center" style="justify-content: center;margin-bottom: 20rpx 0 30rpx">
						<view><img style="height: 100%;width: 300rpx;" src="@/static/img/approve/nomessage.png" alt="" /></view>
					</view>
					<view style="text-align: center;margin-bottom: 30rpx">
						<img class="button-icon" src="@/static/img/approve/warning.png" alt="" />
						<text style="color: rgba(0, 0, 0, 0.85);">确认无误并通过审核？</text>
					</view>
					<view class="flex-center mt32" style="margin-bottom: 30rpx;">
						<view class="button-left-one flex-center-middle" @click="closePup()">
							<img class="button-icon" src="@/static/img/approve/reject.png" alt="" />
							<view class="reject-text">取消</view>
						</view>
						<view style="width: 50rpx;"></view>
						<view class="button-right-one flex-center-middle" @click="doSP('01')">
							<img class="button-icon" src="@/static/img/approve/pass.png" alt="" />
							<view class="pass-text">通过</view>
						</view>
					</view>
				</view>
			</uni-popup>

			<!-- 确认弹窗 -->
			<uni-popup ref="popupConfirm" background-color="#fff" borderRadius="10px 10px 10px 10px" style="width: 90%;">
				<view class="popup-content" style="padding: 20rpx 40rpx" >
					<view style="text-align: center;margin: 50rpx 0 30rpx">
						<img class="button-icon" src="@/static/img/approve/warning.png" alt="" />
						<text style="color: rgba(0, 0, 0, 0.85);">该报价单的折扣超出您的折扣范围</text>
					</view>
					<view style="text-align: center;margin: 30rpx 0">
						<text style="color: rgba(0, 0, 0, 0.85);">是否确认?</text>
					</view>

					<view class="flex-center mt32" style="margin-bottom: 30rpx;">
						<view class="button-left-one flex-center-middle" @click="closePup()">
							<img class="button-icon" src="@/static/img/approve/reject.png" alt="" />
							<view class="reject-text">取消</view>
						</view>
						<view style="width: 50rpx;"></view>
						<view class="button-right-one flex-center-middle" @click="handleApprove('01')">
							<img class="button-icon" src="@/static/img/approve/pass.png" alt="" />
							<view class="pass-text">确定</view>
						</view>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>

</template>

<script>
	import * as ApproveApi from '@/api/work/approve'
	import { stateFormat,stateFormatLow } from '@/utils/common.js'

	export default {
		data() {
			return {
				projectPageParam: {
					pageNum: 1,
					pageSize: 5
				},

				bargainInfo:{},
				bargainLoadAll: false,
				bargainId:'',
				quotationId:'',
				returnMsg:"",
				jumpUrl:"/pages/work/approve/bargainAduit?businessId="
			}
		},
		onLoad (params) {
			this.quotationId = params.quotationId
			this.jumpUrl = this.jumpUrl+this.quotationId
			this.getBargainInfo()
		},
		methods: {
			jumpPage(){
				uni.navigateTo({
					url: this.jumpUrl
				})
			},
			closePup(){
				this.returnMsg = "";
				this.$refs.popup.close()
				this.$refs.popupPass.close()
				this.$refs.popupConfirm.close()
			},
			toggle(status) {
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				if(status  == '02'){
					this.$refs.popup.open()
				}else{
					this.$refs.popupPass.open()
				}
			},
			handleApprove(status){
				if(status == '02' && this.returnMsg == ""){
					uni.showToast({
						icon: 'error',
						mask: true,
						title: '请输入驳回原因..',
					})
					return
				}
				//设置实体
				let bargainObj = {}
				let auditForm = {}

				if (status == '02') {
					//驳回
					bargainObj = {
					        quotationId: this.quotationId,
					      }
					auditForm = {
					  // 审批流
					  auditFlow: 2,
					  // 业务id  报价id
					  businessId: this.quotationId,
					  // 审批结果
					  status: 30,
					  // 备注
					  remark: '基价折率:' + this.bargainInfo.discountRate + '%',
					  // 业务参数
					  businessObj: bargainObj
					}
				}else{
					//通过
					bargainObj = {
					        bargainId: this.bargainInfo.bargainId,
					        quotationId: this.quotationId,
					        tableList: this.bargainInfo.elevatorList,
					        discountRate: this.bargainInfo.discountRate
					}
					auditForm = {
					  // 审批流
					  auditFlow: 2,
					  // 业务id  报价id
					  businessId: this.quotationId,
					  // 审批结果
					  status: 200,
					  // 备注
					  remark: '基价折率:' + this.bargainInfo.discountRate + '%',
					  // 业务参数
					  businessObj: bargainObj
					}
				}

				if (status == '02') {
					auditForm.remark += '，驳回原因：'+ this.returnMsg
				}
				ApproveApi.audit(auditForm).then(res => {
					if (res.code == 200) {
						setTimeout(() => {
							uni.showToast({
								icon: 'none',
								mask: true,
								title: '操作成功',
							})

						}, 500)
						//调回上一页
						this.goBack()
					}
				})
			},

			changeRate(item){
				if(item.discountRate < 1){
					item.discountRate = 1
				}
				if(item.discountRate > 100){
					item.discountRate = 100
				}
				//格式化
				item.discountRate = Number(item.discountRate).toFixed(4)
				//重新计算该梯形的折后价
				item.discountPrice = ((item.originalBasicPrice * item.discountRate) / 100 ) + item.originalOptionalPrice
				console.log("item.discountPrice"+item.discountPrice)
				//重新计算同意价格
				let sum = 0
				//计算折扣前后的基价
				let basicPricePre = 0
				let basicPriceAft = 0
				let elevators = this.bargainInfo.elevatorList
				elevators.some(it=>{
					sum += it.discountPrice * it.elevatorNumber
					basicPricePre +=  it.originalBasicPrice * it.elevatorNumber
					basicPriceAft += ((it.originalBasicPrice * it.discountRate * it.elevatorNumber) / 100 )
				})
				//重新计算折扣率
				let discountRate = (basicPriceAft / basicPricePre) * 100
				//重新设置接受价格
				this.bargainInfo.acceptPrice = sum
				//折扣率格式化
				this.bargainInfo.discountRate = Number(discountRate).toFixed(4)
			},
			stateFormat,
			stateFormatLow,
			getBargainInfo() {
				ApproveApi.bargainById(this.quotationId).then(res => {
					if (res.code == 200) {
						this.bargainInfo = res.data
						if(this.bargainInfo.elevatorList){
							let list = this.bargainInfo.elevatorList
							//this.bargainInfo.discountRate = Number(this.bargainInfo.discountRate).toFixed(4)
							this.bargainInfo.elevatorList.some(item=>{
								item.discountRate = Number(item.discountRate).toFixed(4)
							})
							//赋值完成后重新计算一下折率
							this.changeRate(this.bargainInfo.elevatorList[0])
						}
					}
				})
			},

			async doSP(status){
				const that = this

				let res = await ApproveApi.getSalesDiscount(this.quotationId);
				let minRate = res.data.minDiscount
				let maxRate = res.data.maxDiscount
				let realRate = this.bargainInfo.discountRate
				console.log(this.bargainInfo.priceTotal)
				console.log(this.bargainInfo.originOptionalPrice)
				console.log(this.bargainInfo.originBasicPrice)
				console.log(Math.round((this.bargainInfo.priceTotal - this.bargainInfo.originOptionalPrice)))
				//20240828新算法
				let ratePoint = Math.round((this.bargainInfo.priceTotal - this.bargainInfo.originOptionalPrice) * 100000 /this.bargainInfo.originBasicPrice) / 1000 - Number(realRate)

				console.log('ratePoint', ratePoint)
				console.log('minRate', minRate)
				console.log('maxRate', maxRate)
				if ((ratePoint >= 0 && minRate >= ratePoint) || (ratePoint < 0 && maxRate >= Math.abs(ratePoint))) {
					//可以掉通过
					this.handleApprove(status)
				}else {
					//不行还需确认
					this.$refs.popupConfirm.open()
				}
			},

			// 审批
			handleApproveOld(status) {
				let bargainObj = {}
				let auditForm = {}

				if (status == '02') {
					//驳回
					bargainObj = {
					        quotationId: this.quotationId,
					      }
					auditForm = {
					  // 审批流
					  auditFlow: 2,
					  // 业务id  报价id
					  businessId: this.quotationId,
					  // 审批结果
					  status: 30,
					  // 备注
					  remark: '基价折率:' + this.bargainInfo.discountRate + '%',
					  // 业务参数
					  businessObj: bargainObj
					}
				}else{
					//通过
					bargainObj = {
					        bargainId: this.bargainInfo.bargainId,
					        quotationId: this.quotationId,
					        tableList: this.bargainInfo.elevatorList,
					        discountRate: this.bargainInfo.discountRate
					}
					auditForm = {
					  // 审批流
					  auditFlow: 2,
					  // 业务id  报价id
					  businessId: this.quotationId,
					  // 审批结果
					  status: 200,
					  // 备注
					  remark: '基价折率:' + this.bargainInfo.discountRate + '%',
					  // 业务参数
					  businessObj: bargainObj
					}
				}


				const that = this
				uni.showModal({
					title: status == '02' ? '是否确认驳回' : '是否确认通过',
					editable: status == '02',
					placeholderText: status == '02' ? '请输入驳回原因' : null,
					cancelColor: '#6B7688',
					confirmColor: '#E60114',
					success: function(res) {
						if (res.confirm) {
							if (status == '02') {
								auditForm.remark += '，驳回原因：'+res.content
							}
							ApproveApi.audit(auditForm).then(res => {
								if (res.code == 200) {
									setTimeout(() => {
										uni.showToast({
											icon: 'none',
											mask: true,
											title: '操作成功',
										})

									}, 500)
									//调回上一页
									that.goBack()
								}
							})
						}
					}
				})
			},
			goBack () {
			  //uni.navigateBack({ delta: 1});
			  uni.navigateTo({
			  	url: '/pages/work/approve/list'
			  })

			}
		}
	}
</script>

<style>
	page {
		background-color: #EFF2F6;
	}
</style>

<style lang="scss" scoped>
	.list-card__price {
		width: 40%;
	}
	.card_bottom{
		display:flex;
		width: 100%;
		align-items: center;
		height: 75px;
		position:fixed;
		bottom: 0px;
		margin-left: 0px;
		background: #FFFFFF;
		.button-left {
			width: 48%;
			height: 84rpx;
			background: #FFF8F8;
			border-radius: 200rpx 0rpx 0rpx 200rpx;
			box-shadow: 0px 5px 6px 0px #EADEDF;
			.reject-text {
				font-size: 32rpx;
				color: #E60114;
				line-height: 44rpx;
			}
		}

		.button-right {
			width: 48%;
			height: 84rpx;
			background: linear-gradient(90deg, #DC0736 0%, #E60114 100%);
			box-shadow: 0px 5px 6px 0px #EADEDF;
			border-radius: 0rpx 200rpx 200rpx 0rpx;

			.pass-text {
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 44rpx;
			}
		}

	}
	.card-title{
		font-weight: 600;
		font-size: 16px;
		margin-bottom: 10px;
	}
	.txt-label {
		color: #3C485B;
		font-weight: 400;
		font-size: 12px;
		text-align: center;
		margin-bottom: 5px;
	}
	.txt-value {
		color: #000000;
		text-align: center;
		font-size: 16px;
		font-weight: 600;
	}
	.txt-value2 {
		color: #E60012;
		text-align: center;
		font-size: 16px;
		font-weight: 600;
	}
	.card__price{
		text-align: center;
		width: 90%;
		background-color: #FFF5F6;
		border-radius: 4px;
		padding:7px 5px;
	}
	.val-posi{
		width: 100%;
		text-align: right;
		font-size: 16px;
	}

	::v-deep .u-badge--error {
		background-color: #E60114;
	}

	.flex-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.flex-center-between {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 10px;
	}

	.flex-center-middle {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.ml40 {
		margin-left: 40rpx
	}

	.mt32 {
		margin-top: 32rpx;
	}

	.list-content {
		// padding: 20rpx 24rpx;

		.button-icon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 8rpx;
		}
		.divider {
			margin: 32rpx 0;
			border: 2rpx solid #E9EEF3;
		}

		.list-card {
			margin: 0px 24rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			padding: 32rpx;
			margin-bottom: 20rpx;

			&__title {
				font-weight: 600;
				font-size: 34rpx;
				color: #000000;
				line-height: 48rpx;
				width: 480rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			&__area {
				font-size: 32rpx;
				color: rgba(0, 0, 0, 0.85);
				line-height: 32rpx;
			}

			&__text {
				font-size: 32rpx;
				color: #6B7688;
				line-height: 40rpx;
			}

			.developer {
				color: rgba(0, 0, 0, 0.85);
				width: 490rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.button-icon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 8rpx;
			}

			.button-left {
				width: 350rpx;
				height: 84rpx;
				background: #FFF8F8;
				border-radius: 200rpx 0rpx 0rpx 200rpx;
				box-shadow: 0rpx 4rpx 16rpx 0rpx #DFDFDF;
				.reject-text {
					font-size: 32rpx;
					color: #E60114;
					line-height: 44rpx;
				}
			}

			.button-right {
				width: 350rpx;
				height: 84rpx;
				background: linear-gradient(90deg, #DC0736 0%, #E60114 100%);
				box-shadow: 0rpx 4rpx 16rpx 0rpx #DFDFDF;
				border-radius: 0rpx 200rpx 200rpx 0rpx;

				.pass-text {
					font-size: 32rpx;
					color: #FFFFFF;
					line-height: 44rpx;
				}
			}
		}

		.popup-content {
			padding: 20rpx 20rpx;

			.return_content{
				width: 92%;
				padding: 20rpx 28rpx;
				background: #EFF2F6;
				border-radius: 4px;
			}
			.return_title{
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 16px;
				color: rgba(0,0,0,0.85);
				margin-bottom: 10rpx;
			}
			.button-icon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 8rpx;
			}

			.button-left {
				width: 350rpx;
				height: 84rpx;
				background: #FFF8F8;
				border-radius: 200rpx 0rpx 0rpx 200rpx;
				box-shadow: 0px 2px 8px 0px #DFDFDF;
				.reject-text {
					font-size: 32rpx;
					color: #E60114;
					line-height: 44rpx;
				}
			}

			.button-right {
				width: 350rpx;
				height: 84rpx;
				background: linear-gradient(90deg, #DC0736 0%, #E60114 100%);
				border-radius: 0rpx 200rpx 200rpx 0rpx;
				box-shadow: 0px 2px 8px 0px #DFDFDF;

				.pass-text {
					font-size: 32rpx;
					color: #FFFFFF;
					line-height: 44rpx;
				}
			}
			.button-left-one {
				width: 220rpx;
				height: 84rpx;
				background: #FFF8F8;
				border-radius: 200rpx;
				box-shadow: 0px 2px 8px 0px #DFDFDF;
				.reject-text {
					font-size: 32rpx;
					color: #E60114;
					line-height: 44rpx;
				}
			}

			.button-right-one {
				width: 220rpx;
				height: 84rpx;
				background: linear-gradient(90deg, #DC0736 0%, #E60114 100%);
				border-radius: 200rpx;
				box-shadow: 0px 2px 8px 0px #DFDFDF;

				.pass-text {
					font-size: 32rpx;
					color: #FFFFFF;
					line-height: 44rpx;
				}
			}
		}
	}
</style>
