<template>
  <view class="byc-container" :style="{paddingTop: 'calc(150rpx + ' + statusBarHeight + 'px)'}">
    <!-- 顶部导航栏 -->
    <view class="nav-bar" :style="{paddingTop: statusBarHeight + 'px'}">
      <view class="back" @tap="goBack">
        <uni-icons type="left" size="20" color="#fff"></uni-icons>
      </view>
      <text class="nav-title">包邮服务城市</text>
      <view class="nav-icons">
        <!-- 占位元素，保持布局对称 -->
      </view>
    </view>
    <!-- 蓝色banner卡片 -->
    <view class="byc-banner">
      <image 
        class="byc-banner-img" 
        :src="myCityImage" 
        mode="widthFix" 
        @error="onImageError"
        @load="onImageLoad"
      />
      <!-- 备用背景 -->
      <view v-if="!myCityImage || imageLoadError" class="byc-banner-fallback">
        <view class="byc-banner-title">包邮服务城市</view>
        <view class="byc-banner-desc">已开通包邮服务的城市列表</view>
      </view>
    </view>
    <!-- 主内容卡片 -->
    <view class="byc-main-card">
      <view class="byc-main-title">已开通包邮服务的城市</view>
      <view class="byc-main-desc">
        我们很高兴为以下城市的用户提供一键包邮的便捷服务，进一步简化旧衣回收流程，降低参与环保行动的门槛。期待未来能将这一服务拓展至更多地区，邀请全国人民共同投身于旧衣回收的环保事业中。
      </view>
      <view class="byc-dashed-line"></view>
      <view class="byc-province-list">
        <view class="byc-province-item" v-for="(province, idx) in cityList" :key="province.id">
          <view class="byc-province-name">{{ province.name }}</view>
          <view class="byc-city-group" v-for="(city, cidx) in (province.children || [])" :key="city.id">
            <view class="byc-city-name">{{ city.name }}</view>
            <view class="byc-district-list" v-if="city.children && city.children.length > 0">
              <text v-for="(district, didx) in city.children" :key="district.id" class="byc-district">
                <text v-if="didx !== 0" class="byc-dot">·</text>{{ district.name }}
              </text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 0,
				navBarHeight: 88, // 默认
				menuButtonInfo: null,
				cityList: [],
				bannerTop: 0, // banner距离顶部距离
				imageLoadError: false // 图片加载错误标志
			}
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.statusBarHeight = sysInfo.statusBarHeight
			let menuButtonInfo = null
			try {
				menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			} catch (e) {}
			this.menuButtonInfo = menuButtonInfo
			if (menuButtonInfo && menuButtonInfo.height) {
				// 导航栏高度 = 胶囊 bottom + top - 状态栏高度
				this.navBarHeight = menuButtonInfo.bottom + menuButtonInfo.top - sysInfo.statusBarHeight
			} else {
				this.navBarHeight = 88 // 兜底
			}
			this.bannerTop = this.statusBarHeight + this.navBarHeight
			this.$api('getFreeCityList', {}, res => {
				if (res && res.success && Array.isArray(res.result)) {
					this.cityList = res.result.filter(item => item.open === 'Y');
					// console.log(this.cityList);
				}
			});
		},
		computed: {
			city_desc() {
				const item = getApp().globalData.configData.find(i => i.keyName === 'city_desc')
				return item ? item.keyContent : ''
			},
      myCityImage() {
            const item = getApp().globalData.configData.find(i => i.keyName === 'city_banner')
            console.log(item);
            return item ? item.keyContent : ''
        },
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			// 图片加载错误处理
			onImageError(e) {
				console.error('包邮城市背景图加载失败:', e);
				this.imageLoadError = true;
				// 可以在这里添加上报逻辑
			},
			// 图片加载成功处理
			onImageLoad(e) {
				console.log('包邮城市背景图加载成功');
				this.imageLoadError = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
.byc-container {
  min-height: 100vh;
  background: #f8f8f8;
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-bar {
  display: flex;
  align-items: center;
  height: calc(150rpx + var(--status-bar-height));
  padding: 0 32rpx;
  background: #2486f6;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  box-sizing: border-box;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  .back {
    padding: 20rpx;
    margin-left: -20rpx;
  }
  .nav-title {
    flex: 1;
    text-align: center;
    font-size: 32rpx;
    font-weight: 500;
    color: #fff;
  }
  .nav-icons {
    display: flex;
    align-items: center;
    gap: 12px;
  }
}

.byc-banner {
  margin: -60rpx 0 0 0;
  overflow: hidden;
  box-shadow: 0 4rpx 24rpx rgba(60, 167, 250, 0.10);
  background: none;
  position: relative;
  z-index: 1;
  max-height: 400rpx;
}
.byc-banner-left {
  flex: 1;
}
.byc-banner-title {
  color: #fff;
  font-size: 44rpx;
  font-weight: bold;
  margin-bottom: 16rpx;
  text-shadow: 0 4rpx 12rpx rgba(0,0,0,0.12);
}
.byc-banner-desc {
  color: #e3f2fd;
  font-size: 28rpx;
  margin-top: 4rpx;
}
.byc-banner-img {
  width: 100%;
  height: 100%;
  max-height: 400rpx;
  display: block;
  object-fit: cover;
}
.byc-banner-fallback {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #2486f6 0%, #4a9eff 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40rpx;
  box-sizing: border-box;
}
.byc-main-card {
  background: #fff;
  border-radius: 36rpx;
  margin: -40rpx 0 0 0;
  box-shadow: 0 8rpx 32rpx rgba(60, 167, 250, 0.08);
  padding: 48rpx 32rpx 32rpx 32rpx;
  position: relative;
  z-index: 2;
}
.byc-main-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #222;
  margin-bottom: 24rpx;
}
.byc-main-desc {
  color: #888;
  font-size: 28rpx;
  line-height: 1.7;
  margin-bottom: 32rpx;
}
.byc-dashed-line {
  border-bottom: 2rpx dashed #e5e5e5;
  margin-bottom: 32rpx;
}
.byc-province-list {
  .byc-province-item {
    margin-bottom: 36rpx;
    &:last-child { margin-bottom: 0; }
  }
  .byc-province-name {
    font-size: 32rpx;
    font-weight: bold;
    color: #222;
    margin-bottom: 12rpx;
  }
  .byc-city-group {
    margin-bottom: 20rpx;
    padding-left: 16rpx;
    border-left: 3rpx solid #e5e5e5;
    &:last-child { margin-bottom: 0; }
  }
  .byc-city-name {
    font-size: 30rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 8rpx;
  }
  .byc-district-list {
    display: flex;
    flex-wrap: wrap;
    font-size: 26rpx;
    color: #666;
    line-height: 1.6;
    padding-left: 20rpx;
  }
  .byc-district {
    margin-right: 16rpx;
    margin-bottom: 6rpx;
    display: flex;
    align-items: center;
  }
  .byc-dot {
    margin-right: 6rpx;
    color: #bbb;
    font-size: 24rpx;
  }
}
</style> 