<template>
	<view class="container">
		<view class="classInfo" style="padding-top:80rpx">
			<view class="iconfont icon-youjiantou zuojiantou" @click="goBack()" style="top:18rpx">
			</view>
			<course-item :thumbnailPath="courseInfoItem.displayImage" :title="courseInfoItem.fileTitle" :summaryBox="courseInfoItem.fileSynopsis" :dataList="{coursePrice:courseInfoItem.coursePrice,salesNumber:courseInfoItem.salesNumber}" :fileLecturerId="courseInfoItem.fileLecturerId"></course-item>
		</view>
		<!-- 订单支付明细 -->
		<view class="payInfoBox">
			<h4>订单支付明细</h4>
			<view>
				<text>积分抵扣：</text>
				<text class="price">￥{{dropInfo.integralExchange}}</text>
			</view>
			<view>
				<text>实际支付：</text>
				<text>￥{{dropInfo.paymentAmount}}</text>
			</view>
		</view>
		<!-- 退款明细 -->
		<view class="dropDetailInfoBox">
			<h4>退款明细</h4>
			<view>
				<text>支付违约金：<i class="iconfont icon-31tishi" @click="showModal()"></i></text>
				<text class="price">￥{{dropInfo.breachCommission}}</text>
			</view>
			<view>
				<text>退款到账金额：</text>
				<text>￥{{dropInfo.refundFee}}</text>
			</view>
			<view>
				<text>退还积分：</text>
				<text>{{dropInfo.refundIntegralExchange}}</text>
			</view>
		</view>
		<!-- 退款原因 -->
		<view class="dropInfoBox">
			<view class="dropBox">
				<h4>退款原因</h4>
				<view class="pickerBox">
					<picker @change="bindPickerChange" :value="dropIndex" :range="dropReasons" :range-key="'dropOptionName'">
						<view class="uni-input" style="height: 80rpx;">{{dropReasons[dropIndex]['dropOptionName']}}</view>
					</picker>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<!-- 提交申请 -->
		<view class="submitApply" @click="submit()" >
			<text :class="{'gray':readonlyFlag}" >提交申请</text>
		</view>
		<!-- 弹窗 -->
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="popup-content">
				<view class="title">
					<text></text>
					<h3>违约金规则</h3>
					<i class="iconfont icon-guanbi" @click="closePopup()"></i>
				</view>
		<!-- 		<view class="">
					1.购买后14天内未点开学习全额退费； 
				</view>
				<view class="">
					<view class="">2.购买后15-30天未点开学习则：</view>
					<view class="">假如：一个初级课程8000，报名费45 ，违约金比例：15%，那么违约金的费用一共是：8045*15% =1206.75； </view>
				</view>
				<view class="">
					3.已点开课程学习或购买30天后不予退费。
				</view> -->
				<view class="">
					1.购买后7天内未使用全额退费；
				</view>
				<view class="">
					2.购买后已使用或购买7天后不予退费；
				</view>
				<view class="">
					&nbsp;
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import api from "@/api/api";
	import courseItem from "@/components/courseItem/courseItem.vue";
	export default {
		components:{
			courseItem
		},
		data() {
			return {
				courseInfoItem:{},
				fileId:null,
				orderId:null,
				userId: uni.getStorageSync('cffp_userId'),
				dropInfo:{},
				dropIndex:0,
				dropReasons:[''],
				withdrawal: true,
				readonlyFlag:false
			};
		},
		methods:{
			goBack() {
				uni.navigateBack({
					delta: 1  
				});
			},
			bindPickerChange(e){
				this.dropIndex = e.detail.value;
			},
			showModal(){
				this.$refs.popup.open()
			},
			closePopup(){
				this.$refs.popup.close()
			},
			// 课程详情页面
			courseDetail(){
				api.courseDetail({fileId:this.fileId,userId:this.userId}).then(res=>{
					if(res['success']){
						this.courseInfoItem =  res['data']['data'];
					}
				})
			},
			// 退款明细
			userRefundCourseDtl(){
				api.userRefundCourseDtl({userId:this.userId,orderId:this.orderId}).then(res=>{
					if(res['success']){
						this.dropInfo = res['data']
					}else {
						this.withdrawal = false
					}
				})
			},
			// 提交申请
			submit(){
				if(!this.withdrawal){
					uni.showToast({
						title: '已退课不能重新申请',
						icon: 'none',
						duration: 2000
					});
					return false
				}
				uni.showLoading({
					title: '加载中...'
				});
				if(!this.readonlyFlag){
					//提交按钮置灰
					this.readonlyFlag = true;
					const param = {
						breachCommission:this.dropInfo.breachCommission?this.dropInfo.breachCommission: 0,
						orderNo:this.dropInfo.orderNo,
						paymentMethod:this.dropInfo.paymentMethod,
						userId:this.userId,
						totalFee:this.dropInfo.orderPrice,
						refundFee:this.dropInfo.refundFee,
						integralExchange:this.dropInfo.integralExchange,
						refundIntegralExchange:this.dropInfo.refundIntegralExchange,
						refundReason:this.dropReasons[this.dropIndex]['dropOptionName']
					}
					api.unifiedRefund(param).then(res=>{
						if(res['success']){
							uni.navigateTo({
								url:`/pages/afterSaleDetail/afterSaleDetail?orderNo=${this.dropInfo.orderNo}&afterSalesFlag=3`
							})
							uni.$emit("radiobroadcast", {
								type: 'drop'
							});
						}else{
							uni.showToast({
								title: res['message'],
								icon: 'none',
								duration: 2000
							});
						}
						uni.hideLoading()
						this.readonlyFlag = false;
					})
				}
			},
			// 退款原因
			dropOptionsQuery(){
				api.dropOptionsQuery({code:'REFUND_REASON'}).then(res=>{
					if(res['success']){
						this.dropReasons = res['data']['dropMasterInfoList'][0]['dropOptionsInfoList']
					}
					uni.hideLoading()
				})
			}
		},
		onLoad(option) {
			uni.showLoading({
				title: '加载中...'
			});
			this.orderId = option.id;
			this.fileId = option.fileId;
			this.courseDetail();
			this.dropOptionsQuery()
		},
		onShow() {
			this.userRefundCourseDtl();
		}
	}
</script>

<style lang="scss">
	.container{
		padding: 10rpx 20rpx;
		height: 100%;
		position: relative;
		.classInfo{
			background-color: #fff;
			padding: 20rpx 14rpx 14rpx 20rpx;
		}
		.payInfoBox,.dropDetailInfoBox,.dropInfoBox{
			padding: 30rpx 14rpx 14rpx 20rpx;
			margin-bottom: 10rpx;
			background-color: #fff;
			view{
				margin-top: 22rpx;
				display: flex;
				justify-content: space-between;
			}
			text{
				&.price{
					color: #FA6900;
				}
			}
			
		}
		.submitApply{
			position: fixed;
			bottom: 20rpx;
			left: 0;
			width: 100%;
			text{
				display: flex;
				color: #fff;
				background: #20269B;
				border-radius: 80rpx;
				height: 80rpx;
				width: 60%;
				justify-content: center;
				align-items: center;
				margin: 0 auto;
				
			}
			text.gray{
				background: #666;
			}
		}
		
		.popup-content{
			view{
				font-size: 30rpx;
				color: #666;
				margin-bottom: 10rpx;
			}
			.title{
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #333;
				font-size: 32rpx;
			}
		}
		.dropBox{
			display: flex;
			justify-content: space-between;
			align-items: baseline;
			height: 80rpx;
			.pickerBox{
				display: flex;
				align-items: baseline;
				.iconfont{
					transform: rotate(90deg);
					margin-left: 20rpx;
					color: #666;
				}
			}
		}
	}
</style>
