<template>
	<view class="container">
		<view class="sticky">
			<!-- #ifdef MP-WEIXIN -->
			<NavigateBar title="房间选择" control="back" bgcolor="#a09cc4"></NavigateBar>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY -->
			<NavigateBar title="房间选择" bgcolor="#a09cc4"></NavigateBar>
			<!-- #endif -->
		</view>
		<u-modal :show="modalShow" :title="modalTitle" :showCancelButton="true" @confirm="confirmSelectRoom"
			@cancel="modalShow=false"></u-modal>
		<view class="select-room">
			<view class="room-item" v-for="item in roomList" :key=item.room @click="select(item)" id="item.room"
				:style="{'background':' #'+item.color}">
				<view class="room-item-inner">
					<view>
						<text>{{item.room}}</text>
					</view>
					<view>
						<text>({{item.text}})</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import NavigateBar from "../../components/navigateBar/navigate-bar.vue";
	export default {
		components: {
			NavigateBar
		},
		data() {
			return {
				room: ',',
				building: '',
				floor: '',
				roomList: [],
				modalShow: false,
				modalTitle: '',
				roomType: ''
			};
		},
		computed: {
			...mapState('m_business', ['currentHotel', 'order']),
		},
		methods: {
			...mapMutations('m_business', ['updateOrder']),
			async getRoomInfo() {
				let res = await uni.$http.post('/roomInfo', {
					hotelId: this.currentHotel.hotelId,
					roomType: this.roomType,
					startTime: this.order.startTime,
					endTime: this.order.endTime,
					pageNo: 1,
					pageSize: 999
				})
				this.roomList = res.data.data.records
				this.roomList?.forEach(info => {
					if (info.isAvail == true && info.isResv == false && info.status == 0) {
						info.text = '可选'
						info.color = 'a09cc4'
					} else {
						info.text = '不可预订'
						info.color = '999999'
					}
				})
			},
			select(item) {
				if (item.text === '不可预订') {
					uni.$showMsg('该房间不可预订！')
				} else {
					this.modalTitle = "确认选择" + item.room + '房间？'
					this.building = item.building
					this.floor = item.floor
					this.room = item.room
					this.modalShow = true
				}
			},
			confirmSelectRoom() {
				let info = {
					building: this.building,
					floor: this.floor,
					room: this.room,
					status: 'ROOM_SELECTED'
				}
				if (this.order.orderSource === 'MINI_APP') {
					info.isResv = false
				}
				let order = {
					...this.order,
					...info
				}
				console.log("confirm order:", order)
				this.updateOrder(order)
				uni.navigateTo({
					url: '/subpkg_checkin/addGuest/addGuest'
				})
				this.modalShow = false;
			}
		},
		async onLoad(options) {
			//获取从选择房间类型页面传过来的房间类型
			this.roomType = options.roomType
			//查询该房间类型在订单时间段内可用的所有房间
			await this.getRoomInfo();
		},
		// #ifdef MP-WEIXIN
		onShareAppMessage(info) {
			return {
				title: '源享住',
				path: 'pages/login/login',
				imageUrl: "/static/logo.png"
			}
		}
		// #endif
	}
</script>

<style lang="scss">
	page {
		background: #EFEFF4;
		width: 100%;
		display: flex;
		flex-direction: column;

		.container {
			.sticky {
				position: sticky;
				background-color: #fff;
				width: 100%;
				top: 0;
				z-index: 999;
			}

			.select-room {
				display: flex;
				flex-wrap: wrap;
				width: 600rpx;
				margin: 0 auto;

				.room-item {
					width: 180rpx;
					height: 180rpx;
					margin: 10rpx 10rpx;
					text-align: center;
					color: #FFFFFF;
					border-radius: 15rpx;

					.room-item-inner {
						margin-top: 40rpx;
					}
				}
			}
		}
	}
</style>