<script setup lang="ts">
import type { LocationResult } from '@af-mobile-client-vue3/components/data/XOlMap/types'
import LocationPicker from '@af-mobile-client-vue3/components/data/XOlMap/XLocationPicker/index.vue'
import NormalDataLayout from '@af-mobile-client-vue3/components/layout/NormalDataLayout/index.vue'
import { showNotify } from 'vant'
import { ref } from 'vue'

const selectedLocation = ref<LocationResult>()

// 处理位置选择
function handleLocationConfirm(location: LocationResult) {
  // console.log('选择的位置:', location)
  // selectedLocation.value = location
  showNotify({ type: 'success', message: '位置已选择' })
}
</script>

<template>
  <NormalDataLayout id="XLocationPicker" title="XOlMap地址选择器">
    <template #layout_content>
      <div class="location-picker-demo">
        <!-- 页面标题 -->
        <div class="page-header">
          <div class="title">
            位置选择
          </div>
        </div>

        <!-- 选择结果展示 -->
        <div v-if="selectedLocation" class="location-result">
          <div class="label">
            已选位置:
          </div>
          <div class="value">
            {{ selectedLocation.address }}
          </div>
          <div class="coordinates">
            经度: {{ selectedLocation.longitude.toFixed(6) }},
            纬度: {{ selectedLocation.latitude.toFixed(6) }}
          </div>
        </div>

        <!-- 地图组件 -->
        <div class="map-container">
          <LocationPicker
            v-model="selectedLocation"
            :default-center="[108.948024, 34.263161]"
            :default-zoom="12"
            :readonly="true"
            @confirm="handleLocationConfirm"
          />
        </div>
      </div>
    </template>
  </NormalDataLayout>
</template>

<style scoped lang="less">
.location-picker-demo {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  background-color: #f7f8fa;
}

.page-header {
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;

  .title {
    font-size: 16px;
    color: #333;
    font-weight: 500;
  }
}

.location-result {
  background: white;
  padding: 12px 16px;
  margin: 10px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);

  .label {
    font-size: 14px;
    color: #666;
    margin-bottom: 4px;
  }

  .value {
    font-size: 16px;
    color: #333;
    margin-bottom: 8px;
    word-break: break-all;
  }

  .coordinates {
    font-size: 12px;
    color: #999;
  }
}

.map-container {
  flex: 1;
  position: relative;
  margin: 0 10px 10px 10px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
</style>
