<template>
	<view class="container" :class="{disableScroll:disableScroll}">
		<view class="info-box order-status">
			<view class="status-text">
				<span class="status">订单状态：{{getStatusTxt(orderInfo.status)}}</span>
			</view>
			<view class="refund-btn" v-if="orderInfo.hasRefund" @click="viewRefund">查看退款信息</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" v-if="orderInfo.orderWorkerChangeRecord">
			<span class="title-m">护工信息</span>
			<view class="info-item">
				<span class="title">姓名</span>
				<span class="txt">{{orderInfo.orderWorkerChangeRecord.workerName || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">电话</span>
				<span class="txt">{{orderInfo.orderWorkerChangeRecord.workerPhoneNumber || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">性别</span>
				<span class="txt">{{orderInfo.orderWorkerChangeRecord.sex == 1 ? '男' : '女' || '-'}}</span>
			</view>
		</view>

		<!-- 服务信息 -->
		<view class="info-box" v-if="orderInfo.status != '1' || orderInfo.status != '2'">
			<span class="title-m">服务信息</span>
			<view class="info-item">
				<span class="title">开始时间</span>
				<span class="txt">{{orderInfo.orderWorkerChangeRecord.workerStartTime || '-'}}</span>
			</view>
			<u-line></u-line>

			<view class="info-item">
				<span class="title">完成时间</span>
				<span class="txt">{{orderInfo.orderWorkerChangeRecord.workerEndTime || '-'}}</span>
			</view>
		</view>

		<!-- 评价信息 -->
		<view class="info-box" v-if="orderInfo.status == '5'">
			<span class="title-m">评价信息</span>
			<view class="info-item">
				<span class="title">服务态度</span>
				<u-rate :count="count" v-model="orderInfo.orderReviews.serviceAttitude"></u-rate>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">着装仪态</span>
				<u-rate :count="count" v-model="orderInfo.orderReviews.dressAppearance"></u-rate>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">沟通能力</span>
				<u-rate :count="count" v-model="orderInfo.orderReviews.communicationAbility"></u-rate>
			</view>
			<u-line></u-line>
			<view class="tip-txt">
				文字评价：{{orderInfo.orderReviews.content}}
			</view>
		</view>

		<!-- 底部按钮 -->
		<view class="footer-box">
			<!-- 管理 -->
			<view class="btn-box" v-if="roleType == 'guanli'">
				<!-- 待确认 -->
				<span class="btn " v-if="orderInfo.status == 1" @click.stop="cancleOrder(orderInfo.id)">取消订单</span>
				<span class="btn btn2" v-if="orderInfo.status == 1"
					@click.stop="allocation(orderInfo.id,orderInfo.hospitalId,orderInfo.hospitalVo.name)">分配护工</span>
				<!-- 待服务，服务中 -->
				<span class="btn btn1" v-if="orderInfo.status == 2 || orderInfo.status == 3"
					@click.stop="confirmAndCall(orderInfo.orderWorkerChangeRecord.workerPhoneNumber)">联系护工</span>
				<span class="btn btn2" v-if="orderInfo.status == 2 || orderInfo.status == 3"
					@click.stop="changeCaregiver(orderInfo.id,orderInfo.hospitalId,orderInfo.hospitalVo.name)">更换护工</span>
			</view>
			<!-- 护工 -->
			<view class="btn-box" v-if="roleType == 'hugong'">
				<!-- 待服务 -->
				<span class="btn btn2" v-if="orderInfo.status == 2 || orderInfo.status == 3"
					@click.stop="confirmAndCall(orderInfo.orderWorkerChangeRecord.operatingTeacherPhoneNumber)">联系管理老师</span>
				<span class="btn " v-if="orderInfo.status == 2" @click.stop="startService(orderInfo.id)">开始服务</span>
				<!-- 服务中 -->
				<span class="btn " v-if="orderInfo.status == 3" @click.stop="finishService(orderInfo.id)">完成服务</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOrderDetail,
		getServiceUnit,
		getOrderStatus,
	} from '@/api/order.js'
	import {
		getOrderDetailManage,
		getNursePhone,
		cancelOrderManage,
		startService,
		finishService,
		getOrderDetailNurse
	} from '@/api/hospital.js'

	export default {
		components: {},
		data() {
			return {
				count: 5,
				value: 3,
				orderInfo: {
					serviceUnit: ''
				},
				typeList: [],
				couponList: [],
				disableScroll: false,
				timer: null,
				unitList: [],
				orderId: '',
				roleType: uni.getStorageSync('roleType')
			}
		},
		onLoad(options) {
			if (options.id) {
				this.orderId = options.id;
			}
			this.getOrderStatus();
			this.getServiceUnit();
		},
		onShow() {
			this.getInfoById(this.orderId);
		},
		onUnload() {
			// 当页面关闭或卸载时清除定时器

		},
		methods: {
			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 : ''
			},
			//查询订单状态
			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,
				});
				if (this.roleType == 'guanli') {
					getOrderDetailManage(id).then(res => {
							if (res.code == 200) {
								let data = res.data;
								this.orderInfo = data;
							}
						})
						.catch((err) => {
							console.log(err);
						})
						.finally(() => {
							uni.hideLoading();
						});
				} else if (this.roleType == 'hugong') {
					getOrderDetailNurse(id).then(res => {
							if (res.code == 200) {
								let data = res.data;
								this.orderInfo = data;
							}
						})
						.catch((err) => {
							console.log(err);
						})
						.finally(() => {
							uni.hideLoading();
						});
				}

			},

			//分配护工/更换护工
			allocation(orderNo, hospitalId, hospitalName) {
				uni.navigateTo({
					url: '/pages/workbench/caregiverManage?orderNo=' + orderNo + '&type=fenpei' + '&hospitalId=' +
						hospitalId + '&hospitalName=' + hospitalName
				})
			},
			changeCaregiver(orderNo, hospitalId, hospitalName) {
				uni.navigateTo({
					url: '/pages/workbench/caregiverManage?orderNo=' + orderNo + '&type=genghuan' +
						'&hospitalId=' +
						hospitalId + '&hospitalName=' + hospitalName
				})
			},

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

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

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

			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'
						});
					}
				});
			},
			
			//查看退款信息
			viewRefund() {
				let id = this.orderInfo.refundList[0].id;
				uni.navigateTo({
					url: '/pages/order/refundDetails?id=' + id
				})
			},
			
			//下拉刷新回调函数
			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: 30rpx;
				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;
			}
		}

	}
</style>