<template>
  <div class="region-detail-overlay" v-if="visible" @click="closeOverlay">
    <div class="region-detail-card" @click.stop>
      <div class="card-header">
        <h2>{{ regionData.region?.name }} 详情</h2>
        <button class="close-button" @click="closeOverlay">×</button>
      </div>
      
      <div class="card-content">
        <div class="region-stats">
          <div class="stat-item">
            <div class="stat-label">牛只数量</div>
            <div class="stat-value">{{ regionData.region?.cattle_count || 0 }}</div>
          </div>
          
          <div class="stat-item">
            <div class="stat-label">牧场数量</div>
            <div class="stat-value">{{ regionData.region?.farm_count || 0 }}</div>
          </div>
          
          <div class="stat-item">
            <div class="stat-label">产值</div>
            <div class="stat-value">¥{{ formatNumber(regionData.region?.output_value || 0) }}</div>
          </div>
          
          <div class="stat-item">
            <div class="stat-label">趋势</div>
            <div class="stat-value" :class="regionData.region?.trend">
              {{ regionData.region?.trend === 'up' ? '↑ 上升' : '↓ 下降' }}
            </div>
          </div>
        </div>
        
        <div class="farms-section" v-if="regionData.farms && regionData.farms.length > 0">
          <h3>牧场列表</h3>
          <div class="farms-list">
            <div 
              class="farm-item" 
              v-for="farm in regionData.farms" 
              :key="farm.id"
            >
              <div class="farm-name">{{ farm.name }}</div>
              <div class="farm-details">
                <span>牛只: {{ farm.cattle_count }}</span>
                <span>产值: ¥{{ formatNumber(farm.output_value) }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="no-farms" v-else>
          <p>暂无牧场数据</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, defineProps, defineEmits } from 'vue';

export default {
  name: 'RegionDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    regionData: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const closeOverlay = () => {
      emit('close');
    };
    
    const formatNumber = (num) => {
      if (num >= 100000000) {
        return (num / 100000000).toFixed(2) + '亿';
      }
      if (num >= 10000) {
        return (num / 10000).toFixed(2) + '万';
      }
      return num.toLocaleString();
    };

    return {
      closeOverlay,
      formatNumber
    };
  }
};
</script>

<style scoped>
.region-detail-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.region-detail-card {
  background: linear-gradient(135deg, #0f2027, #20555d);
  border-radius: 12px;
  width: 600px;
  max-width: 90%;
  max-height: 90%;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(76, 175, 80, 0.3);
}

.card-header {
  padding: 20px;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #4CAF50;
}

.close-button {
  background: none;
  border: none;
  color: #fff;
  font-size: 28px;
  cursor: pointer;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.3s;
}

.close-button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.card-content {
  padding: 20px;
  overflow-y: auto;
  max-height: calc(90vh - 100px);
}

.region-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.stat-item {
  background: rgba(255, 255, 255, 0.05);
  padding: 15px;
  border-radius: 8px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.stat-label {
  font-size: 14px;
  color: #ccc;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 20px;
  font-weight: bold;
  color: #4CAF50;
}

.stat-value.up {
  color: #4CAF50;
}

.stat-value.down {
  color: #F44336;
}

.farms-section h3 {
  color: #4CAF50;
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 18px;
}

.farms-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.farm-item {
  background: rgba(255, 255, 255, 0.05);
  padding: 15px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.farm-name {
  font-weight: bold;
  margin-bottom: 8px;
  color: #fff;
}

.farm-details {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #ccc;
}

.no-farms {
  text-align: center;
  color: #ccc;
  padding: 30px 0;
}

@media (max-width: 768px) {
  .region-stats {
    grid-template-columns: 1fr;
  }
  
  .farm-details {
    flex-direction: column;
    gap: 5px;
  }
}
</style>