<template>
	<view>
		<view class="main-content" :style="mainContentStyle">
			<!-- 支付宝小程序使用专用组件 -->
			<!-- #ifdef MP-ALIPAY -->
			<MapComponentAlipay v-if="!isLoading && userLocation && !locationPermissionDenied" ref="innerMapRef"
				:userLocation="userLocation" :positionList="positionList" :filteredPositions="filteredPositions"
				:searchKeyword="searchKeyword" :enableMarkers="true" :bannerImages="bannerImages"
				:hideMapOverlays="hideMapOverlays" @relocate="$emit('relocate')" @scan="$emit('scan')"
				@showList="$emit('showList')" @markerTap="$emit('markerTap', $event)"
				@mapCenterChange="$emit('mapCenterChange', $event)" @bannerClick="$emit('bannerClick', $event)"
				@guide="$emit('guide')" />
			<!-- #endif -->

			<!-- 非支付宝小程序使用通用组件 -->
			<!-- #ifndef MP-ALIPAY -->
			<MapComponent v-if="!isLoading && userLocation && !locationPermissionDenied" ref="innerMapRef"
				:userLocation="userLocation" :positionList="positionList" :filteredPositions="filteredPositions"
				:searchKeyword="searchKeyword" :enableMarkers="true" :bannerImages="bannerImages"
				:hideMapOverlays="hideMapOverlays" @relocate="$emit('relocate')" @scan="$emit('scan')"
				@showList="$emit('showList')" @markerTap="$emit('markerTap', $event)"
				@mapCenterChange="$emit('mapCenterChange', $event)" @bannerClick="$emit('bannerClick', $event)"
				@guide="$emit('guide')" />
			<!-- #endif -->

			<!-- 地图加载状态 -->
			<!-- #ifdef MP-ALIPAY -->
			<view v-if="!userLocation" class="location-denied-placeholder">
				<view class="denied-content">
					<text class="denied-text">{{ locationPermissionText }}</text>
					<view class="denied-enable-btn" @click="$emit('enableLocation')">
						<text class="denied-enable-btn-text">{{ enableLocationText }}</text>
					</view>
				</view>
			</view>
			<!-- #endif -->

			<!-- #ifndef MP-ALIPAY -->
			<view v-if="isLoading || !userLocation" class="map-loading-placeholder">
				<view class="loading-content">
					<view class="loading-spinner"></view>
					<text>{{ loadingLocationText }}</text>
				</view>
			</view>
			<!-- #endif -->
		</view>

		<view class="bottom-actions">
			<view class="action-btn secondary small btn-nearby" @click="$emit('buy')">
				<view class="icon-wrap">
					<image src="/static/shop_icon.png" class="action-icon" mode="scaleToFill" lazy-load="true"></image>
				</view>
				<text class="action-label">{{ buyDeviceText }}</text>
			</view>

			<view class="action-btn primary btn-scan" @click="$emit('scan')">
				<view class="icon-wrap">
					<image class="action-icon" src="/static/scan-icon.png" mode="aspectFill" lazy-load="true" />
				</view>
				<text class="primary-label">{{ scanText }}</text>
			</view>

			<view class="action-btn secondary small btn-my" @click="$emit('my')">
				<view class="icon-wrap">
					<image class="action-icon" src="/static/user.png" mode="aspectFit" lazy-load="true" />
				</view>
				<text class="action-label">{{ personalCenterText }}</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { computed, ref } from 'vue'
	import MapComponent from '../MapComponent.vue'
	// #ifdef MP-ALIPAY
	import MapComponentAlipay from '../MapComponentAlipay.vue'
	// #endif

	defineEmits(['relocate', 'scan', 'showList', 'markerTap', 'mapCenterChange', 'bannerClick', 'guide', 'enableLocation', 'buy', 'my'])

	const props = defineProps({
		mainContentStyle: { type: Object, default: () => ({}) },
		isLoading: { type: Boolean, default: false },
		userLocation: { type: Object, default: null },
		locationPermissionDenied: { type: Boolean, default: false },
		positionList: { type: Array, default: () => [] },
		filteredPositions: { type: Array, default: () => [] },
		searchKeyword: { type: String, default: '' },
		bannerImages: { type: Array, default: () => [] },
		hideMapOverlays: { type: Boolean, default: false },
		locationPermissionText: { type: String, default: '' },
		enableLocationText: { type: String, default: '' },
		loadingLocationText: { type: String, default: '' },
		buyDeviceText: { type: String, default: '' },
		scanText: { type: String, default: '' },
		personalCenterText: { type: String, default: '' }
	})

	const innerMapRef = ref(null)

	defineExpose({
		mapCenter: computed(() => innerMapRef.value?.mapCenter || null),
		moveToLocation: (location) => innerMapRef.value && innerMapRef.value.moveToLocation && innerMapRef.value.moveToLocation(location)
	})
</script>

<style lang="scss" scoped>
	.main-content {
		flex: 1;
		position: relative;
		width: 100%;
		height: 100%;
		padding-bottom: 180rpx;
		box-sizing: border-box;
	}

	.map-loading-placeholder {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 180rpx;
		background: #f6f7fb;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 100;

		.loading-content {
			background: #ffffff;
			border-radius: 16rpx;
			padding: 40rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.loading-spinner {
				width: 60rpx;
				height: 60rpx;
				border: 4rpx solid #f0f0f0;
				border-top: 4rpx solid #2196F3;
				border-radius: 50%;
				animation: spin 1s linear infinite;
				margin-bottom: 24rpx;
			}
		}
	}

	@keyframes spin {
		0% { transform: rotate(0deg); }
		100% { transform: rotate(360deg); }
	}

	.location-denied-placeholder {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 180rpx;
		background: #f6f7fb;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 40rpx;
		box-sizing: border-box;
	}

	.denied-content {
		width: 100%;
		background: #ffffff;
		border-radius: 16rpx;
		padding: 40rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
	}

	.denied-text {
		display: block;
		font-size: 28rpx;
		color: #333;
		line-height: 1.6;
	}

	.denied-enable-btn {
		margin-top: 28rpx;
		width: 100%;
		height: 88rpx;
		background: #3EAB64;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.denied-enable-btn-text {
		font-size: 32rpx;
		font-weight: 700;
		color: #ffffff;
	}

	.bottom-actions {
		position: fixed;
		left: 20rpx;
		right: 20rpx;
		bottom: 40rpx;
		z-index: 1200;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		gap: 16rpx;
	}

	.action-btn {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.action-btn.primary {
		background: #3EAB64;
		color: #fff;
		border-radius: 56rpx;
		height: 112rpx;
		flex: 1;
		max-width: 400rpx;
		padding: 0 24rpx;
		flex-direction: row;
		gap: 8rpx;
	}

	.action-btn.secondary {
		color: #333;
		border-radius: 24rpx;
		height: 100rpx;
		width: 140rpx;
		flex-shrink: 0;
		padding: 8rpx 12rpx;
	}

	.icon-wrap {
		width: 36rpx;
		height: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.btn-scan .icon-wrap {
		width: 48rpx;
		height: 48rpx;
	}

	.action-icon {
		width: 36rpx;
		height: 36rpx;
	}

	.btn-scan .action-icon {
		width: 32rpx;
		height: 32rpx;
		filter: brightness(0) invert(1);
	}

	.primary-label {
		color: #ffffff;
		font-size: 28rpx;
		font-weight: 600;
	}

	.action-label {
		line-height: 1.2;
		text-align: center;
	}
</style>
