<template>
  <view class="city-selector" @tap="openSelector">
    <text class="city-name">{{ city }}</text>
    <text class="city-arrow">▾</text>
  </view>

  <uni-popup ref="popupRef" type="bottom">
    <view class="city-panel">
      <view class="panel-header">
        <text class="panel-title">选择城市</text>
        <text class="panel-close" @tap="closeSelector">✕</text>
      </view>
      <view class="hot-cities">
        <text class="section-title">热门城市</text>
        <view class="city-grid">
          <view
            v-for="item in hotCities"
            :key="item"
            :class="['city-item', { active: city === item }]"
            @tap="selectCity(item)"
          >
            {{ item }}
          </view>
        </view>
      </view>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const props = defineProps<{
  city: string;
}>();

const emit = defineEmits<{
  (e: 'change', city: string): void;
}>();

const popupRef = ref();

const hotCities = [
  '上海', '北京', '广州', '深圳',
  '杭州', '成都', '重庆', '西安',
  '三亚', '厦门', '丽江', '大理',
  '南京', '武汉', '长沙', '青岛',
];

function openSelector() {
  popupRef.value?.open();
}

function closeSelector() {
  popupRef.value?.close();
}

function selectCity(name: string) {
  emit('change', name);
  closeSelector();
}
</script>

<style lang="scss" scoped>
.city-selector {
  display: flex;
  align-items: center;
  padding: 20rpx 24rpx;
  background: #fff;
}

.city-name {
  font-size: 32rpx;
  font-weight: 700;
  color: #333;
  max-width: 120rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.city-arrow {
  font-size: 24rpx;
  color: #999;
  margin-left: 4rpx;
}

.city-panel {
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  max-height: 60vh;
  padding-bottom: env(safe-area-inset-bottom);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 28rpx 32rpx;
  border-bottom: 1rpx solid #eee;
}

.panel-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.panel-close {
  font-size: 32rpx;
  color: #999;
  padding: 8rpx;
}

.hot-cities {
  padding: 24rpx 32rpx;
}

.section-title {
  font-size: 26rpx;
  color: #999;
  margin-bottom: 20rpx;
  display: block;
}

.city-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
}

.city-item {
  width: calc(25% - 12rpx);
  text-align: center;
  padding: 16rpx 0;
  font-size: 28rpx;
  color: #333;
  background: #f5f5f5;
  border-radius: 12rpx;

  &.active {
    color: #FF6B35;
    background: #fff1eb;
    font-weight: 600;
  }
}
</style>
