<template>
	<view class="container" v-if="info">
		<!-- 顶部订单状态信息区域 -->
		<view class="header">
			<!-- 背景图片区域 -->
			<img src="https://chtech.ncjti.edu.cn/hotelReservation/image/18.png" />
			<!-- 标题区域 -->
			<view class="header_title" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" v-if="!headerType">订单详情</view>
			<view class="header_title2" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" v-else>
				<view class="icon" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" @click="handleBack">
					<img src="../../static/index/left2.png" />
				</view>
				订单详情
			</view>

			<!-- 返回图标区域 -->
			<view class="header_icon" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" @click="handleBack">
				<img src="../../static/index/left.png" />
			</view>

			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '1'">待支付</view>
			<view class="header_time" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '1'">
				剩余
				<uv-count-down v-if="info.countDownTime" :time="info.countDownTime" format="mm:ss" @finish="finish(info)"></uv-count-down>
			</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '1'">超时后，订单将会自动取消</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '2'">已支付</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '2'">订单已支付，请等待商家接单</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '3'">待入住</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '3'">订单已被商家接单，请前往办理入住</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '4'">已入住</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '4'">订单已办理入住</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '5'">已消费</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '5'">您的订单已消费，期待您再次预定哦</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '6'">支付超时</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '6'">订单已超过可支付时间，请重新下单</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '7'">已取消</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '7'">订单已取消，请重新下单</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '8'">已退单</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '8'">订单已被商家退单，请重新下单</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '9'">已退款</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '9'">订单已被商家退款</view>
			<view class="header_type" :style="{ paddingTop: statusBarH * 2 + 72 + 'rpx' }" v-if="info.orderStatus === '10'">退款中</view>
			<view class="header_info" :style="{ paddingTop: statusBarH * 2 + 145 + 'rpx' }" v-if="info.orderStatus === '10'">正在退款中...</view>

			<view class="header_btn" :style="{ paddingTop: statusBarH * 2 + 200 + 'rpx' }">
				<view class="btn_box" v-if="info.orderStatus === '1'" @click="handlePay">去支付 ￥{{ info.houseTotalPrice }}</view>
				<!-- hotelIsCanorder  1可取消，0不可 -->
				<view class="btn_box" v-if="(info.orderStatus === '2' || info.orderStatus === '3') && info.hotelIsCanorder === '1'" @click="handleCancel">取消订单</view>
				<view class="btn_box" v-if="info.orderStatus * 1 > 3" @click="handleAgain">再次预定</view>
				<view
					class="btn_box"
					@click="handleDelete"
					v-if="
						info.orderStatus === '1' ||
						info.orderStatus === '5' ||
						info.orderStatus === '6' ||
						info.orderStatus === '7' ||
						info.orderStatus === '8' ||
						info.orderStatus === '9'
					"
				>
					删除订单
				</view>
			</view>
		</view>

		<!-- 主体内容区域 -->
		<view class="body">
			<!-- 费用信息区域 -->
			<view class="body_box">
				<view class="box_title">费用信息</view>
				<view class="box_container">
					<view class="container_key">在线支付</view>
					<view class="container_value price">¥{{ info.houseTotalPrice }}</view>
				</view>
				<view class="box_container">
					<view class="container_key">发票报销</view>
					<view class="container_value">如需发票，请先与酒店确认</view>
				</view>
			</view>

			<!-- 民宿信息区域 -->
			<view class="body_box">
				<view class="box_info">
					<img mode="aspectFill" :src="info.houseFileInfoList[0].url" />
					<view class="info_msg">
						<view class="msg_name">{{ info.hotelName }}</view>
						<view class="msg_address">{{ info.hotelHposition }}</view>
					</view>
				</view>
				<view class="box_btn">
					<view class="btn_item" @click="handleMap">
						<img class="img" src="../../static/my/map.png" />
						地图/导航
					</view>
					<view class="btn_item" @click="handlePhone(info.hotelPhone)">
						<img class="img2" src="../../static/my/phone.png" />
						联系商家
					</view>
				</view>
			</view>

			<!-- 房型信息和用户信息区域 -->
			<view class="body_box2">
				<view class="info_time">
					{{ (info.orderStartTime || '').slice(5, 7) }}月{{ (info.orderStartTime || '').slice(8, 10) }}日
					<view class="time_line"></view>
					<view class="time_num">{{ info.orderLiveTime }}</view>
					<view class="time_line"></view>
					<view class="gap">{{ (info.orderEndTime || '').slice(5, 7) }}月{{ (info.orderEndTime || '').slice(8, 10) }}日</view>
				</view>
				<view class="info_msg">{{ info.houseName }}</view>
				<view class="info_type">
					<view class="type_item">普通型</view>
				</view>
				<view class="info_tag">
					<view class="tag_item" v-if="info.houseAreas">{{ info.houseAreas }}㎡</view>
					<view class="tag_item" v-for="tag in info.houseConfigList" :key="tag.id">{{ tag.name }}</view>
				</view>
				<view class="info_box">
					<view class="box_key">住客姓名</view>
					<view class="box_value">{{ info.userName }}</view>
				</view>
				<view class="info_box">
					<view class="box_key">联系电话</view>
					<view class="box_value">{{ info.userPhone }}</view>
				</view>
				<!-- <view class="info_box">
					<view class="box_key">预计到店</view>
					<view class="box_value">7月26日14:00之前</view>
				</view> -->
			</view>

			<!-- 订单信息区域 -->
			<view class="body_box">
				<view class="box_title">订单信息</view>
				<view class="box_container">
					<view class="container_key">订单号</view>
					<view class="container_value">{{ info.orderNum }}</view>
					<view class="container_copy" @click="handleCopy(info.orderNum)">复制</view>
				</view>
				<view class="box_container">
					<view class="container_key">下单时间</view>
					<view class="container_value">{{ (info.createTime || '').slice(0, 19) }}</view>
				</view>
			</view>

			<!-- 投诉 服务热线区域 -->
			<view class="body_box3">
				<view class="box" @click="handleGoPage">
					<img src="../../static/index/1.png" />
					我要投诉
				</view>
				<!-- @click="handlePhone('18320846714')" -->
				<view class="box">
					<img src="../../static/index/2.png" />
					服务热线
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 详细信息
			info: null,
			// 订单ID
			id: '',
			// 距离差
			distance: '',
			// 状态栏高度
			statusBarH: 0,
			// 胶囊按钮栏高度
			customBarH: 0,
			// 顶部页面标题栏显示隐藏控制
			headerType: false
		}
	},
	onLoad(options) {
		this.id = options.id
		this.distance = options.distance
		this.getData()
	},
	onPageScroll(e) {
		if (e.scrollTop > 50) {
			this.headerType = true
		} else {
			this.headerType = false
		}
	},
	created() {
		// 获取系统信息
		uni.getSystemInfo({
			success: (e) => {
				// 获取状态栏高度
				this.statusBarH = e.statusBarHeight + 10
				// // 获取菜单按钮栏高度
				let custom = uni.getMenuButtonBoundingClientRect()
				this.customBarH = custom.height + 10
			}
		})
	},
	methods: {
		// 获取订单信息
		async getData() {
			const res = await this.$myRequest({
				url: '/mhotel/ampgetBookingById.action',
				data: {
					bookingId: this.id
				}
			})
			// console.log(res)
			if (res.code === 200) {
				this.info = res.data
				// 计算出倒计时时间（毫秒）
				let temLockTime = this.info.lockTime ? this.info.lockTime * 1 : 15
				// 兼容ios部分系统转换时间格式
				let createTime = this.info.createTime.slice(0, 19).replace(/-/g, '/')
				this.info.countDownTime = new Date(createTime).getTime() + temLockTime * 60 * 1000 - new Date().getTime()
			}
		},
		// 点击复制文字回调
		handleCopy(text) {
			uni.setClipboardData({
				data: text,
				success: () => {}
			})
		},
		handleBack() {
			uni.navigateBack(1)
		},
		// 点击地图/导航文字回调
		handleMap() {
			uni.getSetting({
				success: (res) => {
					if (res.authSetting['scope.userLocation']) {
						let lat = this.info.hotelHpositionWens.split(',')[0] * 1
						let lng = this.info.hotelHpositionWens.split(',')[1] * 1
						uni.openLocation({
							latitude: lat,
							longitude: lng,
							name: this.info.hotelName,
							address: this.info.hotelHposition,
							success: () => {}
						})
					} else {
						uni.showModal({
							content: '当前没有定位权限，是否去设置打开？',
							confirmText: '确认',
							cancelText: '取消',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting({
										success: (res) => {
											this.handleMap()
										}
									})
								} else {
									uni.showToast({
										title: '获取定位权限失败',
										icon: 'none',
										mask: true
									})
								}
							}
						})
					}
				}
			})
		},
		// 点击联系商家文字回调
		handlePhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			})
		},
		// 点击去支付按钮回调
		handlePay() {
			uni.navigateTo({
				url: `/pages/pay/pay?id=${this.id}`
			})
		},
		// 点击取消订单按钮回调
		handleCancel() {
			uni.showModal({
				title: '提示',
				content: '确定取消该订单吗？',
				success: async (res) => {
					if (res.confirm) {
						const result = await this.$myRequest({
							url: '/mhotel/abkcancelBooking.action',
							data: {
								bookingId: this.info.id,
								open_id: uni.getStorageSync('openid')
							}
						})
						if (result.code === 200) {
							uni.showToast({
								title: '取消成功,' + result.message,
								icon: 'none',
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack(1)
							}, 1500)
						}
					}
				}
			})
		},
		// 点击再次预定按钮回调
		handleAgain() {
			uni.navigateTo({
				url: `/pages/detail/detail?id=${this.info.hotelId}&distance=${this.distance}`
			})
			// if (this.info.hstatus === 1 && this.info.hotelStatus === 1) {
			// 	uni.showModal({
			// 		title: '提示',
			// 		content: '确定再次预定吗？',
			// 		success: async (res) => {
			// 			if (res.confirm) {
			// 				const result = await this.$myRequest({
			// 					url: '/mhotel/abkcreateOrder.action',
			// 					data: {
			// 						houseId: this.info.houseId,
			// 						startTime: this.info.orderStartTime.slice(0, 10),
			// 						endTime: this.info.orderEndTime.slice(0, 10),
			// 						houseOrderNumber: this.info.houseOrderNumber,
			// 						userName: this.info.userName,
			// 						userPhone: this.info.userPhone,
			// 						userId: uni.getStorageSync('userInfo').id
			// 					}
			// 				})
			// 				if (result.code === 200) {
			// 					uni.navigateTo({
			// 						url: `/pages/pay/pay?id=${result.data}`
			// 					})
			// 				}
			// 			}
			// 		}
			// 	})
			// } else {
			// 	uni.showToast({
			// 		title: '该民宿暂时无法预定',
			// 		icon: 'none',
			// 		mask: true
			// 	})
			// }
		},
		// 点击删除订单按钮回调
		handleDelete() {
			uni.showModal({
				title: '提示',
				content: '确定删除该订单吗？',
				success: async (res) => {
					if (res.confirm) {
						const result = await this.$myRequest({
							url: '/mhotel/abkdelBooking.action',
							data: {
								bookingId: this.info.id,
								open_id: uni.getStorageSync('openid')
							}
						})
						if (result.code === 200) {
							uni.showToast({
								title: '删除成功',
								icon: 'success',
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack(1)
							}, 1500)
						}
					}
				}
			})
		},
		// 倒计时结束回调
		async finish(item) {
			const res = await this.$myRequest({
				url: '/mhotel/abkupdateOrderStatus.action',
				data: {
					bookingId: item.id
				}
			})
			// console.log(res)
			if (res.code === 200) {
				item.orderStatus = '6'
			}
		},
		handleGoPage() {
			uni.navigateTo({
				url: `/pages/complaint/complaint?bookingId=${this.info.id}&hotelId=${this.info.hotelId}&houseId=${this.info.houseId}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	position: relative;
	height: 100vh;
	background-color: #f2f2f2;

	.header {
		height: 480rpx;
		color: #fff;

		img {
			width: 100%;
		}

		.header_title {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			color: #fff;
			font-size: 28rpx;
			text-align: center;
		}

		.header_title2 {
			z-index: 999;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			color: #000;
			text-align: center;
			font-size: 28rpx;
			background-color: #fff;

			.icon {
				position: absolute;
				top: 0;
				left: 0;
			}

			img {
				width: 47rpx;
				height: 47rpx;
			}
		}

		.header_icon {
			z-index: 2;
			position: absolute;
			top: 0;
			left: 10rpx;
			width: 47rpx;

			img {
				width: 47rpx;
				height: 47rpx;
			}
		}

		.header_type {
			position: absolute;
			top: 0;
			left: 30rpx;
			font-size: 40rpx;
			font-weight: bold;
		}

		.header_time {
			position: absolute;
			top: 0;
			left: 530rpx;
			display: flex;
			align-items: center;
			font-size: 30rpx;
			font-weight: bold;
		}

		.header_info {
			position: absolute;
			top: 0;
			left: 30rpx;
			font-size: 24rpx;
		}

		.header_btn {
			position: absolute;
			top: 0;
			left: 30rpx;
			right: 30rpx;
			display: flex;
			justify-content: space-between;

			.btn_box {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 335rpx;
				height: 72rpx;
				border-radius: 10rpx;
				color: #096562;
				font-size: 28rpx;
				background-color: #fff;
			}
		}
	}

	.body {
		position: absolute;
		top: 430rpx;
		left: 0;
		right: 0;
		box-sizing: border-box;
		padding: 20rpx 20rpx 30rpx;
		min-height: calc(100vh - 430rpx);
		border-radius: 20rpx 20rpx 0 0;
		background-color: #ebeced;
		// overflow-y: auto;

		.body_box {
			box-sizing: border-box;
			padding: 0 30rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-direction: column;
			height: 240rpx;
			border-radius: 14rpx;
			background-color: #fff;

			.box_title {
				line-height: 90rpx;
				font-size: 28rpx;
				font-weight: bold;
				border-bottom: 1rpx solid #e5e5e5;
			}

			.box_container {
				display: flex;
				margin-top: 25rpx;
				font-size: 28rpx;

				.container_key {
					width: 160rpx;
					color: #808080;
				}

				.container_value {
				}

				.container_copy {
					margin-left: auto;
					color: #096562;
				}

				.price {
					color: #ff5733;
				}
			}

			.box_info {
				display: flex;
				margin-top: 20rpx;
				height: 130rpx;

				img {
					width: 100rpx;
					height: 100rpx;
					border-radius: 10rpx;
				}

				.info_msg {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					margin-top: -10rpx;
					margin-left: 18rpx;
					width: 540rpx;

					.msg_name {
						font-size: 28rpx;
						font-weight: bold;
					}

					.msg_address {
						color: #808080;
						font-size: 24rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
				}
			}

			.box_btn {
				display: flex;
				justify-content: space-around;
				align-items: center;
				height: 110rpx;

				.btn_item {
					display: flex;
					align-items: center;
					width: 220rpx;

					.img {
						margin-right: 14rpx;
						width: 38rpx;
						height: 38rpx;
					}

					.img2 {
						width: 66rpx;
						height: 66rpx;
					}
				}
			}
		}

		.body_box2 {
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			padding: 0 30rpx;
			margin-bottom: 20rpx;
			height: 408rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.info_time {
				display: flex;
				align-items: center;
				margin-top: 20rpx;
				font-size: 32rpx;
				font-weight: bold;

				.time_line {
					width: 17rpx;
					height: 1rpx;
					background-color: #096562;
				}

				.time_num {
					box-sizing: border-box;
					padding: 0 15rpx;
					height: 46rpx;
					line-height: 46rpx;
					font-size: 24rpx;
					font-weight: 400;
					border-radius: 66rpx;
					border: 1rpx solid #096562;
					background-color: #f0f2f5;
				}

				.gap {
					margin: 0 10rpx;
				}

				text {
					font-size: 24rpx;
					font-weight: 400;
				}
			}

			.info_msg {
				margin-top: 15rpx;
				font-size: 28rpx;
				font-weight: bold;
			}
			.info_type {
				display: flex;
				flex-wrap: wrap;
				margin-top: 15rpx;

				.type_item {
					box-sizing: border-box;
					padding: 0 15rpx;
					margin-right: 20rpx;
					height: 41rpx;
					line-height: 41rpx;
					font-size: 24rpx;
					color: #fff;
					border-radius: 34rpx;
					background-color: #096562;
				}
			}

			.info_tag {
				display: flex;
				flex-wrap: wrap;
				margin: 18rpx 0 0;
				box-sizing: border-box;
				padding-bottom: 30rpx;
				color: #808080;
				font-size: 24rpx;
				border-bottom: 1rpx solid #e5e5e5;

				.tag_item {
					margin-right: 20rpx;
				}
			}

			.info_box {
				display: flex;
				margin-top: 23rpx;
				font-size: 28rpx;

				.box_key {
					width: 160rpx;
					color: #808080;
				}

				.box_value {
				}
			}
		}

		.body_box3 {
			display: flex;
			align-items: center;
			justify-content: space-around;
			box-sizing: border-box;
			padding: 0 110rpx;
			height: 95rpx;
			border-radius: 14rpx;
			background-color: #fff;

			.box {
				display: flex;
				align-items: center;
				color: #096562;
				font-size: 28rpx;

				img {
					width: 36rpx;
					height: 36rpx;
				}
			}
		}
	}
}

// 修改倒计时字体颜色
::v-deep .uv-count-down .uv-count-down__text {
	color: #fff;
}
</style>
