<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="contractInfo" labelPosition="left" labelWidth="80" :labelStyle="{'color': '#6B7688','fontSize':'16px'}" >

				<u-form-item label="合同名称" prop="contractName">
				 <view class="val-posi">{{contractInfo.contractName}}</view>
				</u-form-item>
				<u-form-item label="合同编号" prop="contractNo">
				 <view class="val-posi">{{contractInfo.contractNo}}</view>
				</u-form-item>
			
			  <u-form-item label="地区" prop="nickName" >
			    <view class="val-posi">{{contractInfo.countryName+'/'+contractInfo.city}}</view>
			  </u-form-item>
			  <u-form-item label="业务员" prop="salesmanName">
			   <view class="val-posi">{{contractInfo.salesmanName}}</view>
			  </u-form-item>
			 <!-- <u-form-item label="销售" prop="saler" >
			    <view class="val-posi">{{contractInfo.saler}}</view>
			  </u-form-item> -->
			  <u-form-item label="代理商" prop="agentId" >
			    <view class="val-posi">{{contractInfo.agentName}}</view>
			  </u-form-item>
			  
			  <u-form-item label="合同金额" prop="amount" >
			    <view class="val-posi">{{stateFormatLow(contractInfo.amount,0)}}</view>
			  </u-form-item>
			  <u-form-item label="装船期限" prop="shipmentDeadline" >
			    <view class="val-posi">{{contractInfo.shipmentDeadline}}</view>
			  </u-form-item>
			  <u-form-item label="贸易条款" prop="tradeTerms" >
			    <view class="val-posi">{{contractInfo.tradeTermsName}}</view>
			  </u-form-item>
			  <u-form-item label="付款方式" prop="paymentMethod" >
			    <view class="val-posi">{{contractInfo.paymentMethodName}}</view>
			  </u-form-item>
			  <u-form-item label="付款比例" prop="paymentRatio" >
			    <view class="val-posi">{{contractInfo.paymentRadioName}}</view>
			  </u-form-item>
			  <u-form-item label="销售模式" prop="salesModel" >
			    <view class="val-posi">{{contractInfo.salesModelName}}</view>
			  </u-form-item>
			</u--form>
		</view>


		<view class="list-card" >
			<view class="card-title">电梯列表</view>
			<view v-for="(item, index) in contractInfo.elevatorList">
				<u--form ref="form" :model="item" labelPosition="left" labelWidth="80" :labelStyle="{'color': '#6B7688','fontSize':'16px'}" >
					<u-form-item label="产品类型" prop="elevatorType">
					 <view class="val-posi">{{item.elevatorTypeName}}</view>
					</u-form-item>
				  <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="count" >
				    <view class="val-posi">{{item.count}}</view>
				  </u-form-item>
				  <u-form-item label="单价" prop="unitPrice" >
				    <view class="val-posi">{{stateFormatLow(item.unitPrice,0)}}</view>
				  </u-form-item>
				  <u-form-item label="附加费用" prop="additionalCost" >
				    <view class="val-posi">{{stateFormatLow(item.additionalCost,0)}}</view>
				  </u-form-item>
				</u--form>
				<u-divider class="divider"></u-divider>
			</view>
			<view class="val-posi">
				<view>总价</view>
				<view>{{stateFormatLow(totalPrice,0)}}</view>
			</view>
		</view>


		<view class="list-card" style="padding: 16px 10px;">
			<view class="card-title">备注</view>
			<view>{{contractInfo.remark}}</view>
			<view class="card-title">附件信息</view>
<!-- 			<view @click="getFile(contractInfo.file)">{{contractInfo.file}}</view> -->
			<view  v-if="contractInfo.file" style="width: 100%;word-wrap: break-word;">
				<u-link ref="myLink" :href='getFile(contractInfo.file)' class="Links"
			 :text='getFileName(contractInfo.file)' target="_blank" ></u-link></view>
			<view v-else>暂无附件</view>
		</view>

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

		<view class="card_bottom ">
			<view class="flex-center" style="width: 100%;">
				<view class="flex-center" style="width:100%;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="handleApprove('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="handleApprove('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 {editFlow,updateContract} from '@/api/work/contract/contract.js'
	import { stateFormat,stateFormatLow } from '@/utils/common.js'
	const { environment } = require('../../../config/environment.js')
	export default {
		data() {
			return {
				projectPageParam: {
					pageNum: 1,
					pageSize: 5
				},

				contractInfo:{},
				returnMsg:"",
				contractId:'',
				totalPrice:0,
				jumpUrl:"/pages/work/approve/contractAduit?businessId="
			}
		},
		onLoad (params) {
			this.contractId = params.contractId
			this.jumpUrl = this.jumpUrl+this.contractId
			this.getBargainInfo()
		},
		methods: {
			jumpPage(){
				uni.navigateTo({
					url: this.jumpUrl
				})
			},
			getFile(url){
				// uni.downloadFile({
				//         url: environment.baseURL + url, //仅为示例，并非真实的资源
				//         success: function (res) {
				//             if (res.statusCode === 200) {
				//                 console.log('下载成功');
				//             }
				//         }
				//     });
				return environment.baseURL + url;
			},
			getFileName(url){
				let file = url.split('/');
				return file[file.length-1]
			},
			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 = {}
					auditForm = {
					  // 审批流
					  auditFlow: this.contractInfo.paymentMethod === '1' || this.contractInfo.paymentRatio !=='1' ? 3 :4,
					  // 业务id  合同id
					  businessId: this.contractId,
					  // 审批结果
					  status: 30,
					  // 备注
					  remark: '合同审批打回',
					  // 业务参数
					  businessObj: bargainObj
					}
				}else{
					//通过
					bargainObj = {
						paymentMethod:this.contractInfo.paymentMethod,
				        paymentRatio:this.contractInfo.paymentRatio
					}
					auditForm = {
					  // 审批流
					  auditFlow: this.contractInfo.paymentMethod === '1' || this.contractInfo.paymentRatio !=='1' ? 3 :4,
					  // 业务id  报价id
					  businessId: this.contractId,
					  // 审批结果
					  status: 200,
					  // 备注
					  remark: '合同审批通过',
					  // 业务参数
					  businessObj: bargainObj
					}
				}

				if (status == '02') {
					auditForm.remark += '，驳回原因：'+ this.returnMsg
				}
				ApproveApi.audit(auditForm).then(res => {
					let nowTitle = ''
					if (res.code == 200){
						if (status == '02') {
							console.log("还是合同打回的操作")
							this.$set(this.contractInfo,"approveStatus","1")
							updateContract({...this.contractInfo}).then(res1=>{
								if(res1 && res1.code == 200){
								  nowTitle = "合同打回成功"
								  this.approveOpen = false
								}else{
								  nowTitle = "合同打回失败"
								}
							})
						}else{
							console.log("开始合同审批的操作")
							this.$set(this.contractInfo,"approveStatus","2")
							editFlow({...this.contractInfo}).then(res2=>{
								if(res2 && res2.code == 200){
								  nowTitle = "合同审批成功"
								}else{
								  nowTitle = "合同审批失败"
								}
							})
						}
						setTimeout(() => {
							uni.showToast({
								icon: 'none',
								mask: true,
								title: nowTitle,
							})
						
						}, 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
				//重新计算同意价格
				let sum = 0
				//计算折扣前后的基价
				let basicPricePre = 0
				let basicPriceAft = 0
				let elevators = this.contractInfo.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.contractInfo.acceptPrice = sum
				//折扣率格式化
				this.contractInfo.discountRate = Number(discountRate).toFixed(4)
			},
			stateFormat,
			stateFormatLow,
			getBargainInfo() {
				ApproveApi.contractById(this.contractId).then(res => {
					if (res.code == 200) {
						this.contractInfo = res.data
						//再次查询电梯信息
						this.getBargainLift();
					}
				})
			},
			
			getBargainLift() {
				ApproveApi.contractLiftByNo(this.contractInfo.contractNo).then(res => {
					if (res.code == 200) {
						this.$set(this.contractInfo,'elevatorList',res.rows)
						//计算总价
						if(res.rows){
							res.rows.some(item=>{
								this.totalPrice += ((item.unitPrice + item.additionalCost)  * item.count)
							})
						}
					}
				})
			},

			goBack () {
			  //uni.navigateBack({ delta: 1});
			  uni.navigateTo({
			  	url: '/pages/work/approve/list'
			  })

			}
		}
	}
</script>

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

<style lang="scss" scoped>
	::v-deep .Links {
		span {
			width: 100%;
		}
	}
	.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>
