<template>
	<view class="container" :class="{disableScroll:disableScroll}">
		<view class="info-box order-status">
			<view class="status-text">
				<span class="status">订单状态：{{getRefundTxt(refundInfo.refundStatus)}}</span>
			</view>
		</view>
		<view class="order-info">
			<image :src="orderInfo.hospitalVo.logoUrl" mode="aspectFill"></image>
			<view class="info-content">
				<span class="name">{{orderInfo.hospitalVo.name || '-'}}</span>
				<span class="address">
					<u-icon name="map" color="#999999" size="14"></u-icon>
					<span class="txt">{{orderInfo.hospitalVo.address || '-'}}</span>
				</span>
				<!-- <span class="area">所属区：{{orderInfo.hospitalVo.areaName || '-'}}</span> -->
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">所在科室</span>
				<span class="txt">{{orderInfo.hospitalDepartmentName || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">房床号</span>
				<span class="txt">{{orderInfo.roomNumber || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item info-item-spa">
				<span class="title">备注</span>
				<span class="txt">{{orderInfo.remarks || '-'}}</span>
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">就诊人</span>
				<span class="txt">{{orderInfo.patientVo.name || '-'}}（{{orderInfo.patientVo.mobile || '-'}}）</span>
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">服务产品</span>
				<span class="txt">{{orderInfo.serviceName || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">期望时间</span>
				<span class="txt">{{orderInfo.expectedTime || '-'}}</span>
			</view>
		</view>

		<!-- 基本信息 -->
		<view class="info-box">
			<span class="title-m">基本信息</span>
			<view class="info-item">
				<span class="title">订单号</span>
				<span class="txt">{{orderInfo.orderNo || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">下单时间</span>
				<span class="txt">{{orderInfo.createTime || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">下单数量</span>
				<span class="txt">{{orderInfo.orderNum || '-'}}（{{ getUnitTxt(orderInfo.serviceUnit) }}）</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">销售价格</span>
				<span class="txt">￥{{orderInfo.sellingPrice || "0.00"}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">应付金额</span>
				<span class="txt">￥{{orderInfo.totalPrice || "0.00"}}</span>
			</view>
		</view>

		<!-- 退款信息 -->
		<view class="info-box">
			<span class="title-m">退款信息</span>
			<view class="info-item">
				<span class="title">退款原因</span>
				<span class="txt">{{refundInfo.refundReason || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">退款数量</span>
				<span class="txt">{{refundInfo.refundQuantity || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">退款金额</span>
				<span class="txt">{{refundInfo.refundAmount || "0.00"}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">退款说明</span>
				<span class="txt">{{refundInfo.refundDesc || "-"}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">申请时间</span>
				<span class="txt">{{refundInfo.createTime || "-"}}</span>
			</view>
		</view>


		<!-- 操作信息 退款成功  退款失败-->
		<view class="info-box" v-if="refundInfo.refundStatus == 2 || refundInfo.refundStatus == 3">
			<span class="title-m">操作信息</span>
			<view class="info-item" v-if="refundInfo.refundStatus == 2">
				<span class="title">实退金额</span>
				<span class="txt">￥{{refundInfo.actualRefundAmount || '-'}}</span>
			</view>
			<view class="info-item">
				<span class="title">操作原因</span>
				<span class="txt">{{refundInfo.operationReason || '-'}}</span>
			</view>
			<u-line></u-line>

			<view class="info-item">
				<span class="title">操作时间</span>
				<span class="txt">{{refundInfo.createTime || '-'}}</span>
			</view>
		</view>

		<!-- 底部按钮 -->
		<view class="footer-box" v-if="roleType != 'guanli' && refundInfo.refundStatus == 1">
			<view class="btn-box">
				<!-- 退款中 -->
				<span class="btn " @click.stop="cancleOrder(refundInfo.id)">取消退款</span>
			</view>
		</view>


		<view class="footer-box" v-if="roleType == 'guanli' && refundInfo.refundStatus == 1">
			<view class="btn-box">
				<span class="btn btn1" v-if="refundInfo.refundStatus == 1" @click.stop="opration('refuse')">拒绝退款</span>
				<span class="btn btn2" v-if="refundInfo.refundStatus == 1" @click.stop="opration('agree')">同意退款</span>
			</view>
		</view>

		<!-- 操作 -->
		<u-popup :overlayStyle="{'touch-action':'none'}" closeable mode="bottom" :show="popShow" @close="popClose"
			@open="open" :round="20">
			<view class="pop-box">
				<view class="pop-title">{{popTile}}</view>
				<view class="pop-content">
					<u--form labelWidth="140rpx" labelPosition="top" :model="formData" :rules="rules" ref="uForm">
						<u-form-item label="实退金额" prop="actualRefundAmount" borderBottom required
							v-if="popType == 'agree'">
							<u--input type="number" maxlength="9" v-model="formData.actualRefundAmount"
								placeholder-class="input-placeholder" border="none" clearable
								placeholder="请输入"></u--input>
						</u-form-item>
		
						<u-form-item label="操作原因" prop="operationReason" borderBottom required>
		
							<u--textarea v-model="formData.operationReason" placeholder="请输入内容"></u--textarea>
						</u-form-item>
					</u--form>
					<view class="tip-txt">
						注意：请填写退款失败的原因，如果已提供服务不能申请退款
					</view>
				</view>
				<view class="footer-btn">
					<span class="btn" @click="confirm">确认</span>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getOrderDetail,
		getServiceUnit,
		getOrderStatus,
		cancelOrder,
		getRefundStatus,
		getAfterSaleOrderDetail,
		cancelAfterSaleOrder
	} from '@/api/order.js'
	import {agreeOrRefuseAfterSaleOrder} from '@/api/hospital.js'

	export default {
		components: {},
		data() {
			return {
				orderInfo: {
					serviceUnit: ''
				},
				typeList: [],
				couponList: [],
				disableScroll: false,
				timer: null,
				unitList: [],
				orderId: '',
				evaluateInfo: {
					content: '',
					serviceAttitude: 0,
					dressAppearance: 0,
					communicationAbility: 0,
				},
				refundStatusList: [],
				refundInfo: {},
				roleType: '',
				
				popShow: false,
				popTile: '退款处置',
				popType: '',
				orderId: '',
				
				formData: {
					actualRefundAmount: '',
					operationReason: '',
				},
				rules: {
					actualRefundAmount: [{
						required: true,
						message: '请填写实退金额',
						trigger: ['blur', 'change'],
					}],
					operationReason: [{
						required: true,
						message: '请填写操作原因',
						trigger: ['blur', 'change'],
					}],
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.orderId = options.id;
			}
			if (options.role) {
				this.roleType = options.role
			}
			this.getOrderStatus();
			this.getServiceUnit();
			this.getRefundStatus();
		},
		onShow() {
			this.getInfoById(this.orderId);
		},
		onUnload() {},
		methods: {
			open() {
				this.popShow = true;
			},

			popClose() {
				this.popShow = false;
			},
			confirm() {
				let that = this;
				uni.showLoading({
					title: '操作中',
					mask: true,
				});
				that.$refs.uForm.validate().then((res) => {
					if (res) {
						that.formData.id = that.orderId
						that.formData.refundStatus = that.popType == 'agree' ? 2 : 3
						agreeOrRefuseAfterSaleOrder(this.formData).then(res => {
							uni.$u.toast('操作成功');
							that.formData = {
								actualRefundAmount: '',
								operationReason: '',
							}
							that.popClose();
							that.getInfoById(that.orderId)
						}, err => {
							console.log(err);
						}).finally(() => {
							uni.hideLoading()
						});
					}
				})
			},
			
			getStatusTxt(value) {
				let obj = this.typeList.find(item => item.code == value);;
				return obj ? obj.value : '待确认'
			},
			getUnitTxt(value) {
				let obj = this.unitList.find(item => item.code == value);;
				return obj ? obj.value : ''
			},

			getRefundTxt(value) {
				let obj = this.refundStatusList.find(item => item.code == value);;
				return obj ? obj.value : '-'
			},
			//查询退款状态
			getRefundStatus() {
				let that = this;
				getRefundStatus().then(res => {
						if (res.code == 200) {
							let data = res.data.refundStatus;
							this.refundStatusList = data;
						}

					})
					.catch((err) => {
						console.log(err);
					})
			},
			//查询订单状态
			getOrderStatus() {
				let that = this;
				getOrderStatus().then(res => {
						if (res.code == 200) {
							let data = res.data.orderStatus;
							data.unshift({
								value: '全部',
								code: '',
							})
							this.typeList = data;
						}

					})
					.catch((err) => {
						console.log(err);
					})

			},
			//查询单位
			getServiceUnit() {
				let that = this;
				getServiceUnit().then(res => {
						if (res.code == 200) {
							this.unitList = res.data.serviceUnit;
						}

					})
					.catch((err) => {
						console.log(err);
					})

			},

			getInfoById(id) {
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true,
				});
				getAfterSaleOrderDetail(id).then(res => {
						if (res.code == 200) {
							let data = res.data;
							this.refundInfo = data;
							this.orderInfo = data.orderVo;
						}
					})
					.catch((err) => {
						console.log(err);
					})
					.finally(() => {
						uni.hideLoading();
					});
			},

			//取消订单
			cancleOrder(id) {
				//确认是否删除
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							cancelAfterSaleOrder(id).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '取消成功',
										icon: 'success'
									})
									this.getInfoById(this.orderId);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					},
				})
			},

			//申请退款
			apllyRefund(id) {
				uni.navigateTo({
					url: '/pages/order/applyRefund?id=' + id
				})
			},

			opration(type) {
				this.popShow = true;
				this.popType = type;
			},


			confirmAndCall(phoneNumber) {
				let that = this;
				//获取电话
				getNursePhone(that.orderInfo.orderWorkerChangeRecord.orderId).then(res => {
					if (res.code == 200) {
						phoneNumber = res.msg;
						// 先验证电话号码
						if (!that.isValidPhoneNumber(phoneNumber)) {
							uni.showToast({
								title: '电话号码格式不正确',
								icon: 'none'
							});
							return;
						}

						// 显示确认对话框
						uni.showModal({
							title: '确认拨打电话',
							content: `确定要拨打 ${phoneNumber} 吗？`,
							success: (res) => {
								if (res.confirm) {
									// 用户点击确定，执行拨打电话
									that.makePhoneCall(phoneNumber);
								}
							}
						});
					}
				})

			},

			isValidPhoneNumber(phoneNumber) {
				// 简单的电话号码验证
				return /^1[3-9]\d{9}$/.test(phoneNumber);
			},


			makePhoneCall(phoneNumber) {
				uni.makePhoneCall({
					phoneNumber: phoneNumber,
					success: () => {
						console.log('拨打电话成功');
					},
					fail: (err) => {
						console.error('拨打电话失败', err);
						uni.showToast({
							title: '拨打电话失败',
							icon: 'none'
						});
					}
				});
			},


			//下拉刷新回调函数
			onPullDownRefresh() {
				this.getInfoById(this.orderId);
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 1000);
			},

		},
	}
</script>

<style lang='scss' scoped>
	.container {
		padding: 20rpx 20rpx 200rpx;

		.order-status {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 40rpx 20rpx !important;

			.status-text {
				/* text-align: center; */
				font-weight: bold;
				font-size: 32rpx;
				color: #333;
			}

			.refund-btn {
				font-size: 28rpx;
				color: #4B91D1;
			}
		}

		.order-info {
			display: flex;
			align-items: center;
			padding: 20rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;

			image {
				width: 180rpx;
				height: 180rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
			}

			.info-content {
				display: flex;
				flex-direction: column;
				flex: 1;

				.name {
					word-break: break-all;
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
				}

				.address {
					font-size: 28rpx;
					color: #666;
					margin-top: 20rpx;

					width: 100%;
					font-weight: 500;
					line-height: 35rpx;
					display: flex;
					justify-content: flex-start;
					align-items: flex-start;
					overflow: hidden; //超出的文本隐藏
					text-overflow: ellipsis; //溢出用省略号显示
					white-space: normal; //处理元素中的 空白
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					display: -moz-box;
					-moz-line-clamp: 2;
					-moz-box-orient: vertical;
					overflow-wrap: break-word;
					word-break: break-all;

					::v-deep .u-icon {
						display: inline-block;
					}

					.txt {
						margin-left: 15rpx;
					}
				}

				.area {
					font-size: 28rpx;
					color: #666;
					margin-top: 10rpx;
				}
			}
		}

		.info-box {
			padding: 20rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;

			.info-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx 0;

				.title {
					width: 250rpx;
					font-size: 28rpx;
					color: #333;
				}

				.txt {
					/* flex: 1; */
					max-width: 540rpx;
					text-align: left;
					font-size: 28rpx;
					color: #757575;
				}
			}

			.info-item-spa {
				align-items: flex-start;
				flex-direction: column;

				.txt {
					margin-left: 0;
					margin-top: 20rpx;
					text-align: left;
				}
			}

			.tip-txt {
				padding: 30rpx 0;
				font-size: 28rpx;
				color: #333;
			}
		}

		.title-m {
			/* 竖线和文字对齐 */
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			font-size: 28rpx;
			color: #333;
			font-weight: bold;
			line-height: 28rpx;

			/* 左边加一个竖线 */
			&::before {
				content: '';
				display: inline-block;
				width: 6rpx;
				height: 30rpx;
				background: #4B91D1;
				margin-right: 10rpx;
			}
		}

		.footer-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 50rpx 30rpx;
			box-sizing: border-box;
			font-size: 28rpx;
			background-color: #fff;
			box-shadow: 0 -2rpx 30rpx #c5c5c53a;

			.btn-box {

				display: flex;
				align-items: center;
				justify-content: flex-end;

				.btn {
					width: 170rpx;
					height: 60rpx;
					margin-left: 20rpx;
					line-height: 60rpx;
					text-align: center;
					background-color: #fff;
					border: 1rpx solid #4B91D1;
					color: #4B91D1;
					border-radius: 30rpx;
					font-size: 24rpx;
					margin-left: 30rpx;
				}

				.btn1 {
					background-color: #00aa00;
					border: 1rpx solid #00aa00;
					color: #fff;
				}

				.btn2 {
					background-color: #4B91D1;
					border: 1rpx solid #4B91D1;
					color: #fff;
				}
			}

		}

	}


	.pop-box {
		.pop-title {
			font-size: 32rpx;

			font-weight: bold;
			text-align: center;
			padding: 20rpx 0;

		}

		.pop-content {
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #6F6F6F;
			padding: 0 40rpx;
			width: 100%;
			box-sizing: border-box;

			.info-item {
				display: flex;
				justify-content: space-between;
				padding: 20rpx 0;
				width: 100%;
			}

			.tip-txt {
				margin-top: 20rpx;
				font-size: 24rpx;
				color: #6F6F6F;

				
			}

		}

		.footer-btn {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 30rpx;
			padding: 20rpx;

			.btn {
				width: 100%;
				padding: 20rpx 0;
				border: 1rpx solid #4B91D1;
				background-color: #4B91D1;
				border-radius: 40rpx;
				color: #fff;
				text-align: center;
			}
		}
	}
</style>