<template>
	<view class="container">
		<view style="flex: 1;">
			<view class="classInfo" style="padding-top:80rpx">
				<!-- #ifdef APP -->
				<view class="iconfont icon-youjiantou zuojiantou" @click="goBack()" style="top:18rpx"></view>
				<!-- #endif -->
				
				<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 class="commonTitle">订单支付明细</h4>
				<view>
					<text class="commonTxt">积分抵扣：</text>
					<text class="price">￥{{dropInfo.integralExchange}}</text>
				</view>
				<view>
					<text class="commonTxt">实际支付：</text>
					<text style="font-size: 26rpx;">￥{{dropInfo.paymentAmount}}</text>
				</view>
			</view>
			<!-- 退款明细 -->
			<view class="dropDetailInfoBox">
				<h4 class="commonTitle">退款明细</h4>
				<view>
					<text class="commonTxt">支付违约金<i class="iconfont icon-31tishi iconStyle" @click="showModal()" style="margin-left: 3rpx;"></i>：</text>
					<text class="price">￥{{dropInfo.breachCommission}}</text>
				</view>
				<view>
					<text class="commonTxt">退款到账金额：</text>
					<text style="font-size: 26rpx;">￥{{dropInfo.refundFee}}</text>
				</view>
				<view>
					<text class="commonTxt">退还积分：</text>
					<text class="commonTxt">{{dropInfo.refundIntegralExchange}}</text>
				</view>
			</view>
			<!-- 退款原因 -->
			<view class="dropInfoBox">
				<view class="dropBox">
					<h4 class="commonTitle">退款原因</h4>
					<view class="pickerBox" @click="showReason=true">
						<commonSelect
							:dataList="dropReasons"
							:visible="showReason"
							:showValue="dropIndex"
							placeholderTxt="26rpx"
							chooseTxt="26rpx"
							@confirm="bindPickerChange"
							@close="showReason=false"
						/>
					</view>
				</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 style="font-size: 28rpx;">违约金规则</h3>
					<i class="iconfont icon-guanbi iconStyle" @click="closePopup()"></i>
				</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";
	import commonSelect from '@/components/commonSelect/commonSelect.vue';
	import dataHandling from "@/util/dataHandling";
	export default {
		components:{
			courseItem,
			commonSelect,
		},
		data() {
			return {
				showReason:false,
				courseInfoItem:{},
				fileId:null,
				orderId:null,
				userId: uni.getStorageSync('cffp_userId'),
				dropInfo:{},
				dropIndex:0,
				dropReasons:[{id:null,name:'请选择'}],
				withdrawal: true,
				readonlyFlag:false
			};
		},
		methods:{
			goBack() {
				uni.navigateBack({
					delta: 1  
				});
			},
			bindPickerChange(value){
				this.dropIndex = value;
				this.showReason = false
			},
			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']
					}
					// console.log('param',param);
					// return
					api.unifiedRefund(param).then(res=>{
						if(res['success']){
							dataHandling.pocessTracking(
							  '退款',
							  `已提交退款申请`,
							  '点击',
							  2,
							  '申请退款',
							  'pages/applyDropClass/applyDropClass'
							)
							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']&&res['data']['dropMasterInfoList'][0]['dropOptionsInfoList']){
						
						let result = res['data']['dropMasterInfoList'][0]['dropOptionsInfoList'].map(item=>{
							item.name = item.dropOptionName
							return item
						})
						this.dropReasons = this.dropReasons.concat(result);
						console.log('this.dropReasons',this.dropReasons );
					}
					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" scoped>
	.container{
		// padding: 10rpx 0;
		height: 100%;
		position: relative;
		.commonTitle{
			font-size: 26rpx;
		}
		.commonTxt{
			font-size: 25rpx;
		}
		.classInfo{
			background-color: #fff;
			padding: 20rpx 14rpx 14rpx 20rpx;
		}
		.payInfoBox,.dropDetailInfoBox{
			padding: 30rpx 14rpx 14rpx 20rpx;
			margin-bottom: 10rpx;
			background-color: #fff;
			// border-radius: 10rpx;
			view{
				margin-top: 22rpx;
				display: flex;
				justify-content: space-between;
			}
			text{
				&.price{
					font-size: 25rpx;
					color: #FA6900;
				}
			}
			
		}
		.submitApply{
			margin-top: 30rpx;
			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;
				font-size: 28rpx;
				
			}
			text.gray{
				background: #666;
			}
		}
		
		.popup-content{
			border-radius: 10rpx;
			view{
				font-size: 28rpx;
				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;
			background-color: #fff;
			padding: 20rpx;
			// border-radius: 10rpx;
			box-sizing: border-box;
			.pickerBox{
				display: flex;
				align-items: baseline;
				.iconfont{
					transform: rotate(90deg);
					margin-left: 20rpx;
					color: #666;
				}
			}
		}
	}
</style>
