<template>
  <view class="container">

    <!-- 年份选择 -->
    <view class="year-selector">
      <view class="selector-item" @click="showYearPicker">
        <text class="selector-label">查询年份</text>
        <text class="selector-value">{{ selectedYear }}年</text>
        <text class="selector-arrow">></text>
      </view>
    </view>

    <!-- 年度统计卡片 -->
    <view class="stats-section">
      <view class="stats-card">
        <view class="stats-item">
          <text class="stats-label">年度用电量</text>
          <text class="stats-value">{{ yearlyData.electricity }}度</text>
          <text class="stats-trend" :class="yearlyData.electricityTrend > 0 ? 'trend-up' : 'trend-down'">
            较去年{{ yearlyData.electricityTrend > 0 ? '增长' : '下降' }} {{ Math.abs(yearlyData.electricityTrend) }}%
          </text>
        </view>
        <view class="stats-item">
          <text class="stats-label">年度结算总金额</text>
          <text class="stats-value">¥{{ yearlyData.amount }}</text>
          <text class="stats-trend" :class="yearlyData.amountTrend > 0 ? 'trend-up' : 'trend-down'">
            较去年{{ yearlyData.amountTrend > 0 ? '增长' : '下降' }} {{ Math.abs(yearlyData.amountTrend) }}%
          </text>
        </view>
      </view>
    </view>

    <!-- 详细统计 -->
    <view class="detail-section">
      <view class="section-title">年度详细统计</view>
      <view class="detail-card">
        <view class="detail-item">
          <text class="detail-label">服务客户总数</text>
          <text class="detail-value">{{ yearlyData.totalCustomers }}户</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">年度结算笔数</text>
          <text class="detail-value">{{ yearlyData.totalSettlements }}笔</text>
        </view>

      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="action-section">
<!--      <button class="action-btn export-btn" @click="exportData">导出年报</button>-->
      <button class="action-btn refresh-btn" @click="refreshData">刷新数据</button>
    </view>

    <!-- 年份选择器 -->
    <view class="picker-modal" v-if="showPicker" @click="hidePicker">
      <view class="picker-content" @click.stop>
        <view class="picker-header">
          <text class="picker-cancel" @click="hidePicker">取消</text>
          <text class="picker-title">选择年份</text>
          <text class="picker-confirm" @click="confirmYear">确定</text>
        </view>
        <picker-view class="picker-view" :value="[pickerValue]" @change="onPickerChange">
          <picker-view-column>
            <view v-for="(year, index) in years" :key="index" class="picker-item">{{ year }}年</view>
          </picker-view-column>
        </picker-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectedYear: 0,
      showPicker: false,
      pickerValue: 0,
      years: [],
      yearlyData: {
        electricity: 0,
        electricityTrend: 0,
        amount: 0,
        amountTrend: 0,
        peakElectricity: 0,
        valleyElectricity: 0,
        avgMonthlyElectricity: 0,
        totalCustomers: 0,
        totalSettlements: 0,
        avgPrice: 0,
        maxMonthElectricity: 0,
        minMonthElectricity: 0
      }
    }
  },
  onLoad() {
    this.initData()
    this.loadYearlyData()
  },
  methods: {
    initData() {
      // 初始化年份数据（最近5年）
      const currentYear = new Date().getFullYear()
      this.years = []
      for (let i = 4; i >= 0; i--) {
        this.years.push(currentYear - i)
      }
      
      // 设置默认选择当前年份
      this.selectedYear = currentYear
      this.pickerValue = this.years.indexOf(currentYear)
    },
    loadYearlyData() {
      // 模拟加载年报数据
      uni.showLoading({ title: '加载中...' })
      
      setTimeout(() => {
        this.yearlyData = {
          electricity: 28456.8,
          electricityTrend: 12.5,
          amount: 38256.80,
          amountTrend: -5.3,
          peakElectricity: 18567.2,
          valleyElectricity: 9889.6,
          avgMonthlyElectricity: 2371.4,
          totalCustomers: 1256,
          totalSettlements: 2834,
          avgPrice: 1.34,
          maxMonthElectricity: 3456.8,
          minMonthElectricity: 1234.5
        }
        uni.hideLoading()
      }, 1000)
    },
    showYearPicker() {
      this.showPicker = true
    },
    hidePicker() {
      this.showPicker = false
    },
    onPickerChange(e) {
      this.pickerValue = e.detail.value[0]
    },
    confirmYear() {
      this.selectedYear = this.years[this.pickerValue]
      this.hidePicker()
      this.loadYearlyData()
    },
    exportData() {
      uni.showToast({
        title: '导出功能开发中',
        icon: 'none'
      })
    },
    refreshData() {
      this.loadYearlyData()
    },
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style scoped>
.container {
  background-color: #f5f5f5;
  min-height: 100vh;
}

.nav-bar {
  background-color: #007aff;
  color: white;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
}

.nav-back {
  font-size: 24px;
  font-weight: bold;
  width: 60px;
}

.nav-title {
  font-size: 18px;
  font-weight: bold;
  flex: 1;
  text-align: center;
}

.nav-right {
  width: 60px;
}

.year-selector {
  background-color: white;
  margin: 15px;
  border-radius: 10px;
}

.selector-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
}

.selector-label {
  font-size: 16px;
  color: #333;
}

.selector-value {
  font-size: 16px;
  color: #007aff;
}

.selector-arrow {
  color: #ccc;
  font-size: 16px;
}

.stats-section {
  margin: 0 15px 15px;
}

.stats-card {
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
}

.stats-item {
  flex: 1;
  text-align: center;
}

.stats-item:first-child {
  border-right: 1px solid #f0f0f0;
  margin-right: 20px;
  padding-right: 20px;
}

.stats-label {
  font-size: 14px;
  color: #666;
  display: block;
  margin-bottom: 10px;
}

.stats-value {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 8px;
}

.stats-trend {
  font-size: 12px;
}

.trend-up {
  color: #ff4757;
}

.trend-down {
  color: #2ed573;
}

.chart-section {
  margin: 0 15px 15px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.chart-card {
  background-color: white;
  border-radius: 10px;
  padding: 40px 20px;
}

.chart-placeholder {
  text-align: center;
}

.chart-text {
  font-size: 16px;
  color: #333;
  display: block;
  margin-bottom: 10px;
}

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

.detail-section {
  margin: 0 15px 15px;
}

.detail-card {
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
}

.detail-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-label {
  font-size: 14px;
  color: #666;
}

.detail-value {
  font-size: 16px;
  color: #333;
  font-weight: bold;
}

.action-section {
  margin: 30px 15px;
  display: flex;
  gap: 15px;
}

.action-btn {
  flex: 1;
  height: 45px;
  border-radius: 25px;
  font-size: 16px;
  border: none;
}

.export-btn {
  background-color: #007aff;
  color: white;
}

.refresh-btn {
  background-color: white;
  color: #007aff;
  border: 1px solid #007aff;
}

.picker-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  z-index: 1000;
}

.picker-content {
  background-color: white;
  width: 100%;
  border-radius: 15px 15px 0 0;
}

.picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.picker-cancel, .picker-confirm {
  font-size: 16px;
  color: #007aff;
}

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

.picker-view {
  height: 200px;
}

.picker-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  font-size: 16px;
}
</style>
