<template>
	<view>
		<view class="passenger-trigger" @tap="open">
			<slot>
				<view class="passenger-trigger__default">
					<view class="passenger-trigger__item">
						<text class="passenger-trigger__label">房间</text>
						<text class="passenger-trigger__value">{{ roomCount }}间</text>
					</view>
					<view class="passenger-trigger__divider"></view>
					<view class="passenger-trigger__item">
						<text class="passenger-trigger__label">成人</text>
						<text class="passenger-trigger__value">{{ adultCount }}人</text>
					</view>
					<view class="passenger-trigger__divider"></view>
					<view class="passenger-trigger__item">
						<text class="passenger-trigger__label">儿童</text>
						<text class="passenger-trigger__value">{{ childCount }}人</text>
					</view>
					<text class="passenger-trigger__arrow">▾</text>
				</view>
			</slot>
		</view>

		<view v-if="visible" class="passenger-overlay">
			<view class="passenger-overlay__mask" @tap="close" />
			<view class="passenger-sheet" :style="{ paddingBottom: bottomGap + 'px' }" @tap.stop>
				<view class="passenger-sheet__header">
					<view class="passenger-sheet__title-bar">
						<text class="passenger-sheet__title">{{ title }}</text>
						<text class="passenger-sheet__close" @tap="close">×</text>
					</view>
				</view>

				<view class="passenger-sheet__content">
					<view class="passenger-row">
						<view class="passenger-row__info">
							<text class="passenger-row__title">房间数</text>
							<text class="passenger-row__desc">共{{ roomCount }}间</text>
						</view>
						<view class="passenger-row__counter">
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': roomCount <= minRoom }"
								@tap="decreaseRoom"
							>
								<text class="passenger-row__btn-text">-</text>
							</view>
							<text class="passenger-row__count">{{ roomCount }}</text>
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': roomCount >= maxRoom }"
								@tap="increaseRoom"
							>
								<text class="passenger-row__btn-text">+</text>
							</view>
						</view>
					</view>

					<view class="passenger-divider"></view>

					<view class="passenger-row">
						<view class="passenger-row__info">
							<text class="passenger-row__title">成人</text>
							<text class="passenger-row__desc">12岁以上</text>
						</view>
						<view class="passenger-row__counter">
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': adultCount <= minAdult }"
								@tap="decreaseAdult"
							>
								<text class="passenger-row__btn-text">-</text>
							</view>
							<text class="passenger-row__count">{{ adultCount }}</text>
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': adultCount >= maxAdult }"
								@tap="increaseAdult"
							>
								<text class="passenger-row__btn-text">+</text>
							</view>
						</view>
					</view>

					<view class="passenger-divider"></view>

					<view class="passenger-row">
						<view class="passenger-row__info">
							<text class="passenger-row__title">儿童</text>
							<text class="passenger-row__desc">2-12岁</text>
						</view>
						<view class="passenger-row__counter">
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': childCount <= minChild }"
								@tap="decreaseChild"
							>
								<text class="passenger-row__btn-text">-</text>
							</view>
							<text class="passenger-row__count">{{ childCount }}</text>
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': childCount >= maxChild }"
								@tap="increaseChild"
							>
								<text class="passenger-row__btn-text">+</text>
							</view>
						</view>
					</view>

					<view class="passenger-divider"></view>

					<view class="passenger-row">
						<view class="passenger-row__info">
							<text class="passenger-row__title">婴儿</text>
							<text class="passenger-row__desc">2岁以下（不占床）</text>
						</view>
						<view class="passenger-row__counter">
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': infantCount <= minInfant }"
								@tap="decreaseInfant"
							>
								<text class="passenger-row__btn-text">-</text>
							</view>
							<text class="passenger-row__count">{{ infantCount }}</text>
							<view 
								class="passenger-row__btn" 
								:class="{ 'passenger-row__btn--disabled': infantCount >= maxInfant }"
								@tap="increaseInfant"
							>
								<text class="passenger-row__btn-text">+</text>
							</view>
						</view>
					</view>
				</view>

				<view class="passenger-sheet__footer">
					<view class="passenger-sheet__confirm-btn" @tap="confirm">
						<text class="passenger-sheet__confirm-text">确认</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'PassengerPicker',
	props: {
		title: { type: String, default: '房间和人数' },
		minRoom: { type: Number, default: 1 },
		maxRoom: { type: Number, default: 10 },
		minAdult: { type: Number, default: 1 },
		maxAdult: { type: Number, default: 10 },
		minChild: { type: Number, default: 0 },
		maxChild: { type: Number, default: 5 },
		minInfant: { type: Number, default: 0 },
		maxInfant: { type: Number, default: 3 },
		defaultRoom: { type: Number, default: 1 },
		defaultAdult: { type: Number, default: 2 },
		defaultChild: { type: Number, default: 0 },
		defaultInfant: { type: Number, default: 0 }
	},
	data() {
		return {
			visible: false,
			bottomGap: 0,
			roomCount: 1,
			adultCount: 2,
			childCount: 0,
			infantCount: 0
		}
	},
	created() {
		this.roomCount = this.defaultRoom
		this.adultCount = this.defaultAdult
		this.childCount = this.defaultChild
		this.infantCount = this.defaultInfant
	},
	methods: {
		open() {
			this.visible = true
			const sys = uni.getSystemInfoSync()
			const insetBottom = (sys.safeAreaInsets && sys.safeAreaInsets.bottom) || 0
			this.bottomGap = insetBottom
		},
		close() {
			this.visible = false
		},
		increaseRoom() {
			if (this.roomCount < this.maxRoom) {
				this.roomCount++
				if (this.adultCount < this.roomCount) {
					this.adultCount = this.roomCount
				}
			}
		},
		decreaseRoom() {
			if (this.roomCount > this.minRoom) {
				this.roomCount--
			}
		},
		increaseAdult() {
			if (this.adultCount < this.maxAdult) {
				this.adultCount++
			}
		},
		decreaseAdult() {
			if (this.adultCount > this.minAdult) {
				this.adultCount--
			}
		},
		increaseChild() {
			if (this.childCount < this.maxChild) {
				this.childCount++
			}
		},
		decreaseChild() {
			if (this.childCount > this.minChild) {
				this.childCount--
			}
		},
		increaseInfant() {
			if (this.infantCount < this.maxInfant) {
				this.infantCount++
			}
		},
		decreaseInfant() {
			if (this.infantCount > this.minInfant) {
				this.infantCount--
			}
		},
		confirm() {
			this.$emit('confirm', {
				room: this.roomCount,
				adult: this.adultCount,
				child: this.childCount,
				infant: this.infantCount
			})
			this.close()
		}
	}
}
</script>

<style lang="scss" scoped>
.passenger-trigger {
	display: inline-flex;
	align-items: center;
}

.passenger-trigger__default {
	display: flex;
	align-items: center;
	padding: 12rpx 20rpx;
	background-color: #f5f5f5;
	border-radius: 30rpx;
}

.passenger-trigger__item {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 0 16rpx;
}

.passenger-trigger__label {
	font-size: 20rpx;
	color: #999;
	margin-bottom: 4rpx;
}

.passenger-trigger__value {
	font-size: 26rpx;
	color: #333;
	font-weight: 500;
}

.passenger-trigger__divider {
	width: 1rpx;
	height: 40rpx;
	background-color: #ddd;
	margin: 0 8rpx;
}

.passenger-trigger__arrow {
	font-size: 20rpx;
	color: #999;
	margin-left: 8rpx;
}

.passenger-overlay {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 9990;
}

.passenger-overlay__mask {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
}

.passenger-sheet {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: #fff;
	border-top-left-radius: 24rpx;
	border-top-right-radius: 24rpx;
	max-height: 70vh;
	display: flex;
	flex-direction: column;
}

.passenger-sheet__header {
	padding: 24rpx 30rpx 20rpx;
	border-bottom: 1rpx solid #eee;
}

.passenger-sheet__title-bar {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.passenger-sheet__title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
}

.passenger-sheet__close {
	font-size: 40rpx;
	color: #999;
	padding: 10rpx;
}

.passenger-sheet__content {
	padding: 20rpx 30rpx;
	flex: 1;
	overflow-y: auto;
}

.passenger-row {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20rpx 0;
}

.passenger-row__info {
	flex: 1;
}

.passenger-row__title {
	display: block;
	font-size: 30rpx;
	color: #333;
	font-weight: 500;
	margin-bottom: 8rpx;
}

.passenger-row__desc {
	display: block;
	font-size: 24rpx;
	color: #999;
}

.passenger-row__counter {
	display: flex;
	align-items: center;
	gap: 24rpx;
}

.passenger-row__btn {
	width: 56rpx;
	height: 56rpx;
	border-radius: 50%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
}

.passenger-row__btn--disabled {
	opacity: 0.4;
}

.passenger-row__btn-text {
	font-size: 36rpx;
	color: #333;
	font-weight: 500;
}

.passenger-row__count {
	font-size: 32rpx;
	color: #333;
	font-weight: 600;
	min-width: 48rpx;
	text-align: center;
}

.passenger-divider {
	height: 1rpx;
	background-color: #eee;
	margin: 10rpx 0;
}

.passenger-sheet__footer {
	padding: 20rpx 30rpx;
	padding-bottom: 40rpx;
	border-top: 1rpx solid #eee;
}

.passenger-sheet__confirm-btn {
	background-color: #007aff;
	border-radius: 40rpx;
	padding: 24rpx 0;
	text-align: center;
}

.passenger-sheet__confirm-text {
	font-size: 30rpx;
	color: #fff;
	font-weight: 500;
}
</style>