<template>
	<view class="content">
		<view class="content-bgcolor">
			<view class="content-bgcolor-table">
				<view class="">
					<text></text>{{detailInfo.title}}
				</view>
				<view class="">
					维修时间：{{detailInfo.repairTime | formatDate}}
				</view>
			</view>
			<view class="content-bgcolor-matter">
				说明：{{detailInfo.remark ? detailInfo.remark : ''}}
			</view>
			<view class="content-bgcolor-photo">
				<image class="content-bgcolor-photo-graph" :src="v.url" mode=""
					v-for="(v ,index) in detailInfo.pictureList" :key="index"></image>
			</view>
			<view class="content-bgcolor-payment">
				<view class="">
					已沟通审核通过,待支付
				</view>
				<view class="">
					{{detailInfo.actualAmount ? detailInfo.actualAmount : 0}}元
				</view>
			</view>
		</view>
		<view class="content-discounts" @click="jump(1)">
			<view class="">
				选择优惠券
			</view>
			<view class="">
				{{payInfo.fullSubtraction ? payInfo.fullSubtraction : 0}}元
				<image src="@/static/icon/jt.png" mode=""></image>
			</view>
		</view>
		<view class="content-discounts" v-if="discountList.length && discountList[0].discountRatio">
			<view class="">
				您当前可享1单{{Number(discountList[0].discountRatio)*10}}折优惠,是否使用
			</view>
			<view class="">
				<u-switch v-model="isUse" activeColor="#D6E1FF" inactiveColor="#F1F1F1" @change="changeUse"></u-switch>
			</view>
		</view>
		<view class="content-accomplish">
			<view class="">
				截止1小时内支付完成 <text>00:59:42</text>
			</view>
			<button @click="jump(2)">立即支付 {{sumPrice}}元</button>
		</view>
	</view>
</template>

<script>
	import {userDiscountList} from '@/api/system/index.js'
	import apiBaseConfig from '@/config/index.js';
	export default {
		data() {
			return {
				imgBgUrl: apiBaseConfig.imgBgUrl,
				globalData: getApp().globalData,
				scrollTopHeader: 0,
				isUse: true,
				detailInfo: {}, //详情数据
				payInfo:{},
				discountList:[]
				
			};
		},
		created() {
			this.getDisCountList()
		},
		onLoad(option) {
			let that = this
			if (option.detailInfo) {
				this.detailInfo = JSON.parse(decodeURIComponent(option.detailInfo))
				console.log(this.detailInfo,'读书')
			}
			uni.$on('payInfo', res => {
				console.log(res,'打算今年');
				that.payInfo = res
			})
		},
		onUnload() {
			uni.$off('payInfo')
		},
		computed:{
			sumPrice(){
				let that = this
				// 1.判断优惠券是否有值并且价格大于优惠券时
				console.log(that.payInfo.fullSubtraction,that.detailInfo.actualAmount,'第三句话')
				if(that.payInfo.fullSubtraction && Number(that.detailInfo.actualAmount) > Number(that.payInfo.fullSubtraction)){
					console.log('优质',that.detailInfo.actualAmount - that.payInfo.fullSubtraction)
					// 判断是否使用折扣(使用×折扣)
					return that.isUse ? Number((that.detailInfo.actualAmount - that.payInfo.fullSubtraction) * that.discountList[0].discountRatio).toFixed(2): Number(that.detailInfo.actualAmount - that.payInfo.fullSubtraction).toFixed(2)
				}else{
					if(that.discountList.length && that.discountList[0].discountRatio){
						return that.detailInfo.actualAmount && that.isUse ? Number(that.detailInfo.actualAmount * that.discountList[0].discountRatio).toFixed(2) : that.detailInfo.actualAmount
					}else{
						that.isUse = false
						return that.detailInfo.actualAmount && that.isUse ? Number(that.detailInfo.actualAmount * that.discountList[0].discountRatio).toFixed(2) : that.detailInfo.actualAmount
					}
				}
				
			}
		},
		methods: {
			async getDisCountList(){
				const data_back = await userDiscountList({useStatus:0})
				const {code,rows,total} = data_back
				if(code === 200 && total > 0){
					this.discountList = rows
					this.isUse = true
				}else{
					this.isUse = false
				}
			},
			
			jump(val) {
				let that = this
				if (val == 1) {
					this.$uniGo.navigateTo({
						url: `/my/coupon/index?type=pay`
					});
				} else {
					let orderQuery = {
						orderId:that.detailInfo.id,//订单的id
						couponId:that.payInfo.id ? that.payInfo.id : '',//优惠券的id
						discountId:that.isUse && that.discountList.length ? that.discountList[0].id : '',//折扣的id
					}
					this.$uniGo.navigateTo({
						url: `/demand/confirm/index?orderQuery=${encodeURIComponent(JSON.stringify(orderQuery))}&sumPrice=${that.sumPrice}&title=${this.detailInfo.title}`
					});
				}
			},
			changeUse(e) {
				let that = this
				that.isUse = e
			}
		}
	}
</script>
<style>
	page {
		background: #F7F8F9;
	}

	.u-switch__node {
		background: #888888 !important;
	}

	.u-switch__node--on {
		background: #2C66FF !important;
	}
</style>
<style lang="scss" scoped>
	.content {
		padding: 20rpx;

		&-bgcolor {
			background: #ffffff;
			padding: 20rpx;
			border-radius: 8rpx;

			&-table {
				display: flex;
				justify-content: space-between;

				>view:nth-child(1) {
					display: flex;
					font-size: 28rpx;
					font-weight: 500;
					color: #333333;

					>text {
						width: 6rpx;
						height: 36rpx;
						background: #2C66FF;
						border-radius: 3rpx;
						margin-right: 10rpx;
					}
				}

				>view:nth-child(2) {
					font-size: 20rpx;
					font-weight: 400;
					color: #333333;
				}
			}

			&-matter {
				margin-top: 24rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #444444;
				line-height: 36rpx;
			}

			&-photo {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-start;

				&-graph {
					margin-top: 20rpx;
					margin-right: 20rpx;
					width: 210rpx;
					height: 210rpx;
				}

				&-graph:nth-child(3n) {
					margin-right: 0rpx;
				}
			}

			&-payment {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;

				>view:nth-child(1) {
					font-size: 24rpx;
					font-weight: 400;
					color: #444444;
				}

				>view:nth-child(2) {
					font-size: 26rpx;
					font-weight: 600;
					color: #F55726;
				}
			}
		}

		&-discounts {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 23rpx;
			padding: 20rpx 23rpx;
			background: #FFFFFF;
			border-radius: 10rpx;

			>view:nth-child(1) {
				font-size: 24rpx;
				font-weight: 400;
				color: #333333;
			}

			>view:nth-child(2) {
				font-size: 26rpx;
				font-weight: 500;
				color: #F55726;
				display: flex;
				align-items: center;

				>image {
					width: 34rpx;
					height: 34rpx;
				}
			}

		}

		&-accomplish {
			// margin-top: 286rpx;
			// width: ca 100%;
			width: calc(100% - 20rpx * 2);
			text-align: center;
			position: absolute;
			bottom: 98rpx;

			>view:nth-child(1) {
				font-size: 18rpx;
				font-weight: 400;
				color: #333333;

				>text {
					margin-left: 8rpx;
					font-size: 18rpx;
					font-weight: 400;
					color: #FD0100;
				}
			}

			>button {
				margin-top: 20rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: #2C66FF;
				border-radius: 10rpx;
				width: 100%;
				font-size: 26rpx;
				font-weight: 500;
				color: #FFFFFF;
			}
		}

	}
</style>
