<template>
	<view class="container" v-if="totalMoney">
		<!-- 导航栏图片区域 -->
		<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' }">{{ hotelName }}</view>
			<!-- 返回图标区域 -->
			<view class="header_icon" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" @click="handleBack">
				<img src="../../static/index/left.png" />
			</view>
		</view>

		<view class="body">
			<!-- 房间信息区域 -->
			<view class="info">
				<view class="info_time">
					{{ info.startTimeMonth }}月{{ info.startTimeDay }}日
					<text class="gap">{{ info.startTimeWeek }}</text>
					<view class="time_line"></view>
					<view class="time_num">{{ info.nightNum }}晚</view>
					<view class="time_line"></view>
					<view class="gap">{{ info.endTimeMonth }}月{{ info.endTimeDay }}日</view>
					<text>{{ info.endTimeWeek }}</text>
				</view>
				<view class="info_msg">{{ info.item.hName }}</view>
				<view class="info_type">
					<view class="type_item">{{ info.hTypeName }}</view>
				</view>
				<view class="info_tag">
					<view class="tag_item">{{ info.item.hAreas }}㎡</view>
					<view class="tag_item" v-for="tag in info.item.hConfigList" :key="tag.id">{{ tag.name }}</view>
				</view>
			</view>

			<!-- 入住信息区域 -->
			<view class="msg">
				<view class="msg_title">入住信息</view>
				<view class="msg_box">
					<view class="box_key">房间数量</view>
					<view class="box_value">{{ roomCount }}间</view>
					<view class="box_icon">
						<img class="img" src="../../static/index/add.png" @click="handleAdd" />
						<img class="img2" src="../../static/index/minus.png" @click="handleMinus" />
					</view>
				</view>

				<view class="msg_box">
					<view class="box_key">住客姓名</view>
					<view class="box_value">
						<input disabled type="text" placeholder="请选择住客" v-model="clientName" />
					</view>
					<view class="box_icon" @click="handleSelectClient">
						<img class="img2" src="../../static/index/people.png" />
					</view>
				</view>

				<view class="msg_box">
					<view class="box_key">联系电话</view>
					<view class="box_value">
						<input disabled type="number" maxlength="11" placeholder="请选择联系电话" v-model="clientPhone" />
					</view>
				</view>
				<!-- <view class="msg_box" @click="handleSelectTime">
					<view class="box_key">预计到店</view>
					<view class="box_value" :class="{ color: !arriveTime }">{{ arriveTime ? arriveTime : '请选择预计到店时间' }}</view>
					<view class="box_icon">
						<img class="img3" src="../../static/index/right2.png" />
					</view>
				</view> -->

				<uv-datetime-picker
					ref="datetimePicker"
					v-model="timeValue"
					mode="datetime"
					:closeOnClickOverlay="false"
					confirmColor="#096562"
					:formatter="formatter"
					@confirm="confirm"
				></uv-datetime-picker>
			</view>

			<!-- 本单可享区域 -->
			<view class="coupon" v-if="couponNum">
				<view class="coupon_title">本单可享</view>
				<view class="coupon_box">
					<view class="box_key">
						<img src="../../static/index/coupon.png" />
						优惠券
					</view>
					<view class="box_value" @click="handleGoPage">
						<view class="value_count">{{ couponNum }}张可用</view>
						<img src="../../static/index/right2.png" />
					</view>
				</view>
			</view>

			<!-- 费用明细区域 -->
			<view class="price1">
				<view class="price_title">费用明细</view>
				<view class="price_box">
					<view class="box_key">在线支付</view>
					<view class="box_value">
						{{ roomCount }}间{{ info.nightNum }}晚 共
						<text>¥</text>
						<span>{{ totalPrice }}</span>
					</view>
				</view>
			</view>

			<!-- 房费区域 -->
			<view class="all">
				<view class="all_title">
					<text>房费</text>
					<text class="mr_30">¥{{ totalPrice }}</text>
				</view>

				<view class="all_box">
					<view class="box_list">
						<view class="list_item" v-for="(item, index) in dayList" :key="index">
							{{ item.date }}
							<text>{{ item.topinfo }}</text>
						</view>
					</view>
					<view class="box_total">
						{{ roomCount }}间 x {{ info.nightNum }}晚
						<!-- x ¥{{ info.item.price }} -->
					</view>
				</view>
			</view>

			<!-- 优惠区域 -->
			<view class="sale" v-if="chooseObj.discountAmount">
				<view class="sale_top">
					<view class="top_key">优惠</view>
					<view class="top_value">- ¥{{ chooseObj.discountAmount }}</view>
				</view>
				<view class="sale_bottom">
					<view class="bottom_key">
						<img src="../../static/index/coupon.png" />
						{{ chooseObj.typeName }}
					</view>
					<view class="bottom_value">- ¥{{ chooseObj.discountAmount }}</view>
				</view>
			</view>
		</view>

		<!-- 底部提交订单区域 -->
		<view class="foot">
			<view class="foot_left">
				<text>¥</text>
				{{ totalPrice }}
			</view>
			<view class="foot_right" @click="goPagePay">提交订单</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			info: {},
			// 预定房间数量
			roomCount: 1,
			// 住客姓名
			clientName: '',
			// 联系电话
			clientPhone: '',
			// 预计到店时间
			arriveTime: '',
			// 当前时间
			timeValue: Number(new Date()),
			// 民宿名称
			hotelName: '',
			// 状态栏高度
			statusBarH: 0,
			// 胶囊按钮栏高度
			customBarH: 0,
			// 优惠券数量
			couponNum: 0,
			chooseObj: {},
			dayList: [],
			totalMoney: 0,
			// 房间数量的最大数
			maxRooms: 0
		}
	},
	created() {
		// 获取系统信息
		uni.getSystemInfo({
			success: (e) => {
				// 获取状态栏高度
				this.statusBarH = e.statusBarHeight + 10
				// // 获取菜单按钮栏高度
				let custom = uni.getMenuButtonBoundingClientRect()
				this.customBarH = custom.height + 10
			}
		})
	},
	onLoad(options) {
		this.info = JSON.parse(options.info)
		this.maxRooms = this.info.item.remainRooms
		this.hotelName = this.info.hotelName
		this.getDayList()
		uni.$on('change', this.change)
		uni.$on('choose', this.choose)
	},
	// 计算属性
	computed: {
		// 总费用
		totalPrice() {
			if (this.info.item) {
				let tem = this.roomCount * this.totalMoney - (this.chooseObj.discountAmount * 1 || 0)
				let res = tem > 0 ? tem.toFixed(2) : 0
				return res
			}
		}
	},
	// 监听属性
	watch: {
		totalPrice: {
			handler(newVal, oldVal) {
				this.getCouponNum()
			}
		}
	},
	methods: {
		async getDayList() {
			const res = await this.$myRequest({
				url: '/mhotel/house/price/dailyPrice.action',
				data: {
					roomId: this.info.item.id,
					startTime: this.info.queryStartTime,
					endTime: this.info.queryEndTime
				}
			})
			// console.log(res)
			if (res.code === 200) {
				this.dayList = res.data.list
				this.totalMoney = res.data.totalPrice
			}
		},
		async getCouponNum() {
			const res = await this.$myRequest({
				url: '/mhotel/hcusefulCoupon.action',
				data: {
					hotelId: this.info.hotelId,
					page: 1,
					rows: 1,
					userId: uni.getStorageSync('userInfo').id,
					totalPrice: (this.roomCount * this.totalMoney).toFixed(2)
				}
			})
			// console.log(res);
			if (res.code === 200) {
				this.couponNum = res.page.total
			}
		},
		change(e) {
			this.clientName = e.name
			this.clientPhone = e.phone
		},
		choose(e) {
			this.chooseObj = e.data
			// console.log(this.chooseObj)
		},
		handleSelectClient() {
			uni.navigateTo({
				url: '/pages/common/common?type=1'
			})
		},
		confirm(e) {
			// console.log(e.value)
			let date = new Date(e.value)
			let month = date.getMonth() + 1
			let day = date.getDate()
			let H = date.getHours()
			let M = date.getMinutes()
			this.arriveTime = `${month}月${day}日${H}:${M}分之前`
		},
		formatter(type, value) {
			if (type === 'year') {
				return `${value}年`
			}
			if (type === 'month') {
				return `${value}月`
			}
			if (type === 'day') {
				return `${value}日`
			}
			return value
		},
		handleSelectTime() {
			this.$refs.datetimePicker.open()
		},
		handleAdd() {
			if (this.roomCount < this.maxRooms) {
				this.roomCount++
			} else {
				uni.showToast({
					title: `当前房型只有${this.roomCount}间`,
					icon: 'none'
				})
			}
		},
		handleMinus() {
			if (this.roomCount > 1) {
				this.roomCount--
			} else {
				uni.showToast({
					title: '至少需要预定1间',
					icon: 'none'
				})
			}
		},
		// 提交订单按钮回调
		async goPagePay() {
			const reName = /^[\u4e00-\u9fa5]{2,4}$/
			const rePhone = /^[1][3,4,5,7,8,9][0-9]{9}$/
			if (this.totalPrice <= 0) {
				uni.showToast({
					title: '支付金额不能小于等于0',
					icon: 'none',
					mask: true
				})
				return
			}

			if (!this.clientName) {
				uni.showToast({
					title: '请选择住客',
					icon: 'none',
					mask: true
				})
				return
			}
			if (!reName.test(this.clientName)) {
				uni.showToast({
					title: '姓名格式有误',
					icon: 'none'
				})
				return
			}
			if (!this.clientPhone) {
				uni.showToast({
					title: '请选择联系电话',
					icon: 'none',
					mask: true
				})
				return
			}
			if (!rePhone.test(this.clientPhone)) {
				uni.showToast({
					title: '电话号码格式有误',
					icon: 'none'
				})
				return
			}

			const res = await this.$myRequest({
				url: '/mhotel/abkcreateOrder.action',
				data: {
					houseId: this.info.item.id,
					startTime: this.info.queryStartTime,
					endTime: this.info.queryEndTime,
					houseOrderNumber: this.roomCount,
					userName: this.clientName,
					userPhone: this.clientPhone,
					userId: uni.getStorageSync('userInfo').id,
					complaintId: this.chooseObj.complaintId || '',
					useId: this.chooseObj.id || ''
				}
			})
			// console.log(res)
			if (res.code === 200) {
				uni.navigateTo({
					url: `/pages/pay/pay?id=${res.data}`
				})
			}
		},
		handleBack() {
			uni.navigateBack(1)
		},
		handleGoPage() {
			let temTotalPrice = (this.roomCount * this.totalMoney).toFixed(2)
			uni.navigateTo({
				url: `/pages/coupon/coupon?id=${this.info.hotelId}&totalPrice=${temTotalPrice}&complaintId=${this.chooseObj.complaintId}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	position: relative;
	display: flex;
	flex-direction: column;
	height: 100vh;
	overflow: hidden;
	background-color: #ebeced;

	.header {
		position: relative;
		height: 180rpx;
		overflow: hidden;

		img {
			width: 100%;
		}

		.header_title {
			position: absolute;
			top: 0;
			left: 182rpx;
			width: 50%;
			color: #fff;
			text-align: center;
			font-size: 28rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.header_icon {
			position: absolute;
			top: 0;
			left: 5rpx;
			width: 47rpx;
			height: 70rpx;

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

	.body {
		height: calc(100vh - 306rpx);
		overflow-y: auto;

		.info {
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			padding: 0 30rpx;
			margin: 0 auto;
			margin-top: 18rpx;
			width: 710rpx;
			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 30rpx;
				color: #808080;
				font-size: 24rpx;

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

		.msg {
			box-sizing: border-box;
			padding-left: 26rpx;
			margin: auto;
			margin-top: 20rpx;
			width: 710rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.msg_title {
				height: 87rpx;
				line-height: 87rpx;
				font-size: 28rpx;
				font-weight: bold;
				border-bottom: 1rpx solid #e6e6e6;
			}

			.msg_box {
				display: flex;
				align-items: center;
				height: 97rpx;
				font-size: 28rpx;
				border-bottom: 1rpx solid #e6e6e6;

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

				.box_value {
					margin-left: 30rpx;
					flex: 1;

					input {
						box-sizing: border-box;
						padding-right: 30rpx;
						width: 100%;
					}
				}

				.color {
					color: #808080;
				}

				.box_icon {
					display: flex;
					align-items: center;
					height: 100%;

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

					.img2 {
						margin: 0 30rpx;
						width: 37rpx;
						height: 37rpx;
					}

					.img3 {
						margin: 0 30rpx;
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		.coupon {
			box-sizing: border-box;
			padding-left: 26rpx;
			margin: 20rpx auto 0;
			width: 710rpx;
			height: 197rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.coupon_title {
				line-height: 92rpx;
				font-size: 28rpx;
				font-weight: bold;
				border-bottom: 1rpx solid #e6e6e6;
			}

			.coupon_box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 105rpx;

				.box_key {
					display: flex;
					align-items: center;
					font-size: 28rpx;

					img {
						margin-right: 10rpx;
						width: 40rpx;
						height: 40rpx;
					}
				}

				.box_value {
					display: flex;
					align-items: center;

					.value_count {
						display: flex;
						justify-content: center;
						align-items: center;
						width: 105rpx;
						height: 45rpx;
						color: #fff;
						font-size: 24rpx;
						border-radius: 7rpx;
						background-color: #ff5733;
					}

					img {
						margin-right: 17rpx;
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		.price1 {
			box-sizing: border-box;
			padding-left: 26rpx;
			margin: 20rpx auto;
			width: 710rpx;
			height: 197rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.price_title {
				line-height: 92rpx;
				font-size: 28rpx;
				font-weight: bold;
				border-bottom: 1rpx solid #e6e6e6;
			}

			.price_box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 92rpx;
				font-size: 28rpx;

				.box_key {
					color: #808080;
				}

				.box_value {
					display: flex;
					align-items: center;

					text {
						color: #ff5733;
						font-size: 24rpx;
					}

					span {
						margin-right: 30rpx;
						color: #ff5733;
					}
				}
			}
		}

		.all {
			box-sizing: border-box;
			padding-left: 26rpx;
			margin: 0 auto 20rpx;
			width: 710rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.all_title {
				display: flex;
				height: 92rpx;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
				font-weight: bold;
				border-bottom: 1rpx solid #e6e6e6;

				.mr_30 {
					margin-right: 30rpx;
				}
			}

			.all_box {
				box-sizing: border-box;
				padding-right: 26rpx;
				display: flex;
				justify-content: space-between;
				font-size: 28rpx;

				.box_list {
					margin-bottom: 28rpx;
					color: #808080;

					.list_item {
						margin-top: 30rpx;

						text {
							margin-left: 120rpx;
						}
					}
				}

				.box_total {
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}

		.sale {
			margin: 0 auto 20rpx;
			box-sizing: border-box;
			padding: 0 30rpx;
			width: 710rpx;
			height: 209rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.sale_top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 92rpx;
				font-size: 28rpx;
				font-weight: bold;

				.top_key {
				}

				.top_value {
					color: #ff5733;
				}
			}

			.sale_bottom {
				display: flex;
				align-items: center;
				justify-content: space-between;
				box-sizing: border-box;
				padding: 0 26rpx;
				width: 650rpx;
				height: 80rpx;
				font-size: 24rpx;
				border-radius: 7rpx;
				background-color: #f2f2f2;

				.bottom_key {
					display: flex;
					align-items: center;

					img {
						margin-right: 10rpx;
						width: 34rpx;
						height: 34rpx;
					}
				}

				.bottom_value {
				}
			}
		}
	}

	.foot {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 0 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 126rpx;
		background-color: #fff;

		.foot_left {
			margin-top: -10rpx;
			font-size: 40rpx;
			color: #ff5733;

			text {
				font-size: 24rpx;
			}
		}

		.foot_right {
			margin-top: -10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 238rpx;
			height: 80rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 64rpx;
			background-color: #096562;
		}
	}
}
</style>
