<template>
	<view class="map-page">
		<view class="search-card">
			<input v-model="keyword" class="search-input" placeholder="搜索小区、医院、养老院、街道" @input="handleKeywordInput" />
		</view>

		<map
			class="address-map"
			:latitude="latitude"
			:longitude="longitude"
			:markers="markers"
			:show-location="true"
			:scale="16"
		></map>

		<scroll-view class="poi-scroll" scroll-y="true">
			<view class="poi-card">
				<text class="poi-title">附近地址</text>
				<view
					v-for="item in poiList"
					:key="item.id"
					:class="['poi-item', selectedPoiId == item.id ? 'poi-item-selected' : '']"
					@click="selectPoi(item.id)"
				>
					<text class="poi-name">{{ item.name }}</text>
					<text class="poi-address">{{ item.address }}</text>
				</view>
			</view>
		</scroll-view>

		<view class="bottom-bar">
			<button class="confirm-btn" @click="confirmAddress">确认地址</button>
		</view>
	</view>
</template>

<script setup lang="uts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { HomeServiceSelectedAddressType } from '@/types/home-service.uts'

const SELECTED_KEY = 'hss_selected_service_address'
const MAP_DRAFT_KEY = 'hss_service_address_map_draft'

type MapPoiItemType = {
	id: string
	name: string
	address: string
	latitude: number
	longitude: number
}

const keyword = ref('')
const latitude = ref(24.28859)
const longitude = ref(116.12264)
const selectedPoiId = ref('poi-current')
const poiList = ref<Array<MapPoiItemType>>([])
const markers = ref<Array<UTSJSONObject>>([])

function buildMarkers(): Array<UTSJSONObject> {
	const result: Array<UTSJSONObject> = []
	const selected = getSelectedPoi()
	if (selected == null) {
		return result
	}
	const marker = new UTSJSONObject()
	marker.set('id', 1)
	marker.set('latitude', selected.latitude)
	marker.set('longitude', selected.longitude)
	marker.set('title', selected.name)
	result.push(marker)
	return result
}

function createDefaultPoiList(selected: HomeServiceSelectedAddressType | null): Array<MapPoiItemType> {
	const locationName = selected != null && selected.locationName != null && selected.locationName != '' ? selected.locationName : '当前定位'
	const locationAddress = selected != null && selected.locationAddress != null && selected.locationAddress != '' ? selected.locationAddress : '请通过系统地图继续完善地址搜索'
	const baseLatitude = selected != null && selected.latitude != 0 ? selected.latitude : latitude.value
	const baseLongitude = selected != null && selected.longitude != 0 ? selected.longitude : longitude.value
	const result: Array<MapPoiItemType> = []
	result.push({
		id: 'poi-current',
		name: locationName,
		address: locationAddress,
		latitude: baseLatitude,
		longitude: baseLongitude
	})
	result.push({
		id: 'poi-near-1',
		name: locationName + '附近入口',
		address: locationAddress,
		latitude: baseLatitude + 0.0006,
		longitude: baseLongitude + 0.0006
	})
	result.push({
		id: 'poi-near-2',
		name: locationName + '周边推荐',
		address: locationAddress,
		latitude: baseLatitude - 0.0006,
		longitude: baseLongitude - 0.0006
	})
	return result
}

function getSelectedPoi(): MapPoiItemType | null {
	for (let i = 0; i < poiList.value.length; i++) {
		if (poiList.value[i].id == selectedPoiId.value) {
			return poiList.value[i]
		}
	}
	return poiList.value.length > 0 ? poiList.value[0] : null
}

function selectPoi(poiId: string): void {
	selectedPoiId.value = poiId
	const selected = getSelectedPoi()
	if (selected == null) {
		return
	}
	latitude.value = selected.latitude
	longitude.value = selected.longitude
	markers.value = buildMarkers()
}

function handleKeywordInput(): void {
	if (keyword.value.trim() == '') {
		return
	}
	const base = getSelectedPoi()
	if (base == null) {
		return
	}
	const dynamicList: Array<MapPoiItemType> = []
	dynamicList.push({
		id: 'poi-search-1',
		name: keyword.value.trim(),
		address: base.address,
		latitude: base.latitude,
		longitude: base.longitude
	})
	dynamicList.push(base)
	poiList.value = dynamicList
	selectedPoiId.value = 'poi-search-1'
	markers.value = buildMarkers()
}

function confirmAddress(): void {
	const selected = getSelectedPoi()
	if (selected == null) {
		uni.showToast({ title: '请先选择地址', icon: 'none' })
		return
	}
	const draft = {
		addressId: '',
		userId: '',
		isDefault: true,
		contactName: '',
		contactPhone: '',
		phone: '',
		addressName: selected.name,
		locationName: selected.name,
		addressDetail: selected.address,
		locationAddress: selected.address,
		houseNumber: '',
		doorNo: '',
		fullAddress: selected.address,
		latitude: selected.latitude,
		longitude: selected.longitude,
		remark: '',
		coordinateType: 'gcj02',
		createdAt: Date.now(),
		updatedAt: Date.now()
	} as HomeServiceSelectedAddressType
	uni.setStorageSync(MAP_DRAFT_KEY, JSON.stringify(draft))
	uni.navigateBack()
}

onLoad(() => {
	const selected = uni.getStorageSync(SELECTED_KEY) as HomeServiceSelectedAddressType | null
	if (selected != null && selected.latitude != 0) {
		latitude.value = selected.latitude
		longitude.value = selected.longitude
	}
	poiList.value = createDefaultPoiList(selected)
	markers.value = buildMarkers()
})
</script>

<style scoped>
.map-page {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: #f4f6f8;
	display: flex;
	flex-direction: column;
	padding: 24rpx 24rpx 180rpx;
	box-sizing: border-box;
}

.search-card,
.poi-card {
	background: #ffffff;
	border-radius: 28rpx;
	padding: 24rpx;
	box-shadow: 0 12rpx 28rpx rgba(15, 23, 42, 0.05);
}

.search-input {
	background: #f8fafc;
	border-radius: 999rpx;
	padding: 18rpx 24rpx;
	font-size: 26rpx;
	color: #1f2937;
}

.address-map {
	width: 100%;
	height: 45vh;
	border-radius: 28rpx;
	overflow: hidden;
	margin-top: 20rpx;
	margin-bottom: 20rpx;
}

.poi-scroll {
	flex: 1;
	min-height: 0;
}

.poi-title,
.poi-name {
	color: #1f2937;
}

.poi-title {
	font-size: 28rpx;
	font-weight: 600;
	margin-bottom: 12rpx;
}

.poi-item {
	padding: 18rpx 0;
	border-top: 1rpx solid #eef2f7;
	display: flex;
	flex-direction: column;
	gap: 8rpx;
}

.poi-item-selected {
	border-left: 6rpx solid #f97316;
	padding-left: 18rpx;
}

.poi-name {
	font-size: 26rpx;
	font-weight: 600;
}

.poi-address {
	font-size: 24rpx;
	color: #4b5563;
	line-height: 1.6;
}

.bottom-bar {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	padding: 20rpx 24rpx 36rpx;
	background: rgba(244, 246, 248, 0.96);
	box-shadow: 0 -8rpx 24rpx rgba(15, 23, 42, 0.05);
}

.confirm-btn {
	width: 100%;
	height: 88rpx;
	line-height: 88rpx;
	border-radius: 999rpx;
	background: linear-gradient(135deg, #ff8a65 0%, #ff7043 100%);
	color: #ffffff;
	font-size: 30rpx;
	font-weight: 600;
	border: none;
}
</style>