<template>
  <view class="container">
    <!-- 月份选择 -->
    <view class="month-selector">
      <view class="selector-item" @click="showMonthPicker">
        <text class="selector-label">查询月份</text>
        <text class="selector-value">{{ selectedMonth }}</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">{{ monthlyData.electricity }}度</text>
          <text class="stats-trend" :class="monthlyData.electricityTrend > 0 ? 'trend-up' : 'trend-down'">
            {{ monthlyData.electricityTrend > 0 ? '↗' : '↘' }} {{ Math.abs(monthlyData.electricityTrend) }}%
          </text>
        </view>
        <view class="stats-item">
          <text class="stats-label">业务结算总金额</text>
          <text class="stats-value">¥{{ monthlyData.amount }}</text>
          <text class="stats-trend" :class="monthlyData.amountTrend > 0 ? 'trend-up' : 'trend-down'">
            {{ monthlyData.amountTrend > 0 ? '↗' : '↘' }} {{ Math.abs(monthlyData.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">{{ monthlyData.customerCount }}户</text>
        </view>
        <view class="detail-item">
          <text class="detail-label">结算笔数</text>
          <text class="detail-value">{{ monthlyData.settlementCount }}笔</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="confirmMonth">确定</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-column>
            <view v-for="(month, index) in months" :key="index" class="picker-item">{{ month }}月</view>
          </picker-view-column>
        </picker-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectedMonth: '',
      showPicker: false,
      pickerValue: [0, 0],
      years: [],
      months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      monthlyData: {
        electricity: 0,
        electricityTrend: 0,
        amount: 0,
        amountTrend: 0,
        peakElectricity: 0,
        valleyElectricity: 0,
        avgDailyElectricity: 0,
        customerCount: 0,
        settlementCount: 0,
        avgPrice: 0
      }
    }
  },
  onLoad() {
    this.initData()
    this.loadMonthlyData()
  },
  methods: {
    initData() {
      // 初始化年份数据（最近3年）
      const currentYear = new Date().getFullYear()
      this.years = [currentYear - 2, currentYear - 1, currentYear]
      
      // 设置默认选择上个月
      const now = new Date()
      const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1)
      this.selectedMonth = `${lastMonth.getFullYear()}年${lastMonth.getMonth() + 1}月`
      
      // 设置picker默认值
      const yearIndex = this.years.indexOf(lastMonth.getFullYear())
      const monthIndex = lastMonth.getMonth()
      this.pickerValue = [yearIndex, monthIndex]
    },
    loadMonthlyData() {
      // 模拟加载月报数据
      uni.showLoading({ title: '加载中...' })
      
      setTimeout(() => {
        this.monthlyData = {
          electricity: 2456.8,
          electricityTrend: 8.5,
          amount: 3256.80,
          amountTrend: -2.3,
          peakElectricity: 1567.2,
          valleyElectricity: 889.6,
          avgDailyElectricity: 79.3,
          customerCount: 156,
          settlementCount: 234,
          avgPrice: 1.32
        }
        uni.hideLoading()
      }, 1000)
    },
    showMonthPicker() {
      this.showPicker = true
    },
    hidePicker() {
      this.showPicker = false
    },
    onPickerChange(e) {
      this.pickerValue = e.detail.value
    },
    confirmMonth() {
      const yearIndex = this.pickerValue[0]
      const monthIndex = this.pickerValue[1]
      this.selectedMonth = `${this.years[yearIndex]}年${this.months[monthIndex]}月`
      this.hidePicker()
      this.loadMonthlyData()
    },
    exportData() {
      uni.showToast({
        title: '导出功能开发中',
        icon: 'none'
      })
    },
    refreshData() {
      this.loadMonthlyData()
    },
    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;
}

.month-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: 5px;
}

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

.trend-up {
  color: #ff4757;
}

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

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

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

.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>
