<template>
  <view class="container">
    <!-- 数据概览 -->
    <view class="overview-section">
      <view class="overview-card">
        <view class="overview-item">
          <text class="item-label">当前分成比例</text>
          <view class="item-value">
            <text class="value-num">15.5</text>
            <text class="value-unit">%</text>
          </view>
        </view>
        <view class="divider"></view>
        <view class="overview-item">
          <text class="item-label">累计实际收入</text>
          <view class="item-value">
            <text class="value-unit">¥</text>
            <text class="value-num">11,164.28</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 功能说明 -->
    <view class="info-section">
      <view class="info-card">
        <text class="info-title">功能说明</text>
        <text class="info-desc">一级、二级渠道可导入电费报表（模板），导入后，系统根据相应数据计算出结算金额、分成比例、实际利润等，可反导出成excel表格保存到手机上</text>
      </view>
    </view>

    <!-- 操作按钮区 -->
    <view class="action-section">
      <button class="action-btn primary-btn" @click="downloadTemplate">下载模板</button>
      <button class="action-btn upload-btn" @click="uploadReport">导入报表</button>
    </view>

    <!-- 报表列表 -->
    <view class="report-section">
      <view class="section-title">
        <text>历史报表</text>
        <text class="section-count">({{ reportList.length }})</text>
      </view>
      
      <view class="report-list">
        <view 
          class="report-item" 
          v-for="(item, index) in reportList" 
          :key="index"
          @click="viewReport(item)"
        >
          <view class="report-info">
            <text class="report-name">{{ item.name }}</text>
            <text class="report-time">{{ item.uploadTime }}</text>
            <view class="report-stats">
              <text class="stats-item">数据条数：{{ item.dataCount }}</text>
              <text class="stats-item">结算金额：¥{{ item.totalAmount }}</text>
            </view>
          </view>
          <view class="report-actions">
            <text class="action-text export" @click.stop="exportReport(item)">导出</text>
            <text class="action-text delete" @click.stop="deleteReport(item)">删除</text>
          </view>
        </view>
      </view>

      <!-- 空状态 -->
      <view class="empty-state" v-if="reportList.length === 0">
        <image src="/static/logo.png" class="empty-icon" />
        <text class="empty-text">暂无报表数据</text>
        <text class="empty-desc">请先下载模板并导入电费数据</text>
      </view>
    </view>

    <!-- 报表详情弹窗 -->
    <view class="report-modal" v-if="showModal" @click="closeModal">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">{{ selectedReport.name }}</text>
          <text class="modal-close" @click="closeModal">×</text>
        </view>
        <view class="modal-body">
          <view class="detail-row">
            <text class="detail-label">上传时间：</text>
            <text class="detail-value">{{ selectedReport.uploadTime }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">数据条数：</text>
            <text class="detail-value">{{ selectedReport.dataCount }}条</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">结算金额：</text>
            <text class="detail-value">¥{{ selectedReport.totalAmount }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">分成比例：</text>
            <text class="detail-value">{{ selectedReport.shareRatio }}%</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">实际利润：</text>
            <text class="detail-value">¥{{ selectedReport.actualProfit }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">处理状态：</text>
            <text class="detail-value status" :class="selectedReport.status">
              {{ getStatusText(selectedReport.status) }}
            </text>
          </view>
        </view>
        <view class="modal-footer">
          <button class="modal-btn export-btn" @click="exportReport(selectedReport)">导出Excel</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showModal: false,
      selectedReport: {},
      totalIncome: 11164.28,
      shareRatio: 15.5,
      reportList: [
        {
          id: 1,
          name: '2024年1月电费报表',
          uploadTime: '2024-01-15 14:30:25',
          dataCount: 156,
          totalAmount: 25680.50,
          shareRatio: 15.5,
          actualProfit: 3980.48,
          status: 'completed'
        },
        {
          id: 2,
          name: '2023年12月电费报表',
          uploadTime: '2024-01-02 09:15:10',
          dataCount: 142,
          totalAmount: 23456.80,
          shareRatio: 15.5,
          actualProfit: 3635.80,
          status: 'completed'
        },
        {
          id: 3,
          name: '2023年11月电费报表',
          uploadTime: '2023-12-05 16:45:33',
          dataCount: 138,
          totalAmount: 22890.30,
          shareRatio: 15.5,
          actualProfit: 3548.00,
          status: 'processing'
        }
      ]
    }
  },
  methods: {
    downloadTemplate() {
      uni.showLoading({ title: '下载中...' })
      
      // 模拟下载过程
      setTimeout(() => {
        uni.hideLoading()
        uni.showToast({
          title: '模板下载成功',
          icon: 'success'
        })
      }, 2000)
    },
    uploadReport() {
      uni.chooseFile({
        count: 1,
        extension: ['.xlsx', '.xls'],
        success: (res) => {
          this.processUpload(res.tempFilePaths[0])
        },
        fail: () => {
          uni.showToast({
            title: '请选择Excel文件',
            icon: 'none'
          })
        }
      })
    },
    processUpload(filePath) {
      uni.showLoading({ title: '上传处理中...' })
      
      // 模拟上传和处理过程
      setTimeout(() => {
        const newReport = {
          id: Date.now(),
          name: `电费报表_${new Date().toLocaleDateString()}`,
          uploadTime: new Date().toLocaleString(),
          dataCount: Math.floor(Math.random() * 200) + 100,
          totalAmount: (Math.random() * 30000 + 20000).toFixed(2),
          shareRatio: 15.5,
          actualProfit: (Math.random() * 5000 + 3000).toFixed(2),
          status: 'completed'
        }
        
        this.reportList.unshift(newReport)
        uni.hideLoading()
        uni.showToast({
          title: '导入成功',
          icon: 'success'
        })
      }, 3000)
    },
    viewReport(item) {
      this.selectedReport = item
      this.showModal = true
    },
    closeModal() {
      this.showModal = false
      this.selectedReport = {}
    },
    exportReport(item) {
      uni.showLoading({ title: '导出中...' })
      
      setTimeout(() => {
        uni.hideLoading()
        uni.showToast({
          title: '导出成功',
          icon: 'success'
        })
      }, 2000)
    },
    deleteReport(item) {
      uni.showModal({
        title: '确认删除',
        content: `确定要删除"${item.name}"吗？`,
        success: (res) => {
          if (res.confirm) {
            const index = this.reportList.findIndex(report => report.id === item.id)
            if (index > -1) {
              this.reportList.splice(index, 1)
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              })
            }
          }
        }
      })
    },
    getStatusText(status) {
      const statusMap = {
        'processing': '处理中',
        'completed': '已完成',
        'failed': '处理失败'
      }
      return statusMap[status] || '未知状态'
    },
    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;
}

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

.overview-card {
  background-color: #007aff;
  border-radius: 15px;
  padding: 20px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.2);
}

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

.item-label {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 8px;
  display: block;
}

.item-value {
  display: flex;
  justify-content: center;
  align-items: baseline;
}

.value-num {
  font-size: 24px;
  font-weight: bold;
  color: white;
}

.value-unit {
  font-size: 14px;
  color: white;
  margin: 0 2px;
}

.divider {
  width: 1px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
}

.info-section {
  margin: 15px;
}

.info-card {
  background-color: #e3f2fd;
  border-radius: 10px;
  padding: 15px;
  border-left: 4px solid #007aff;
}

.info-title {
  font-size: 14px;
  font-weight: bold;
  color: #007aff;
  display: block;
  margin-bottom: 8px;
}

.info-desc {
  font-size: 12px;
  color: #666;
  line-height: 1.5;
}

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

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

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

.upload-btn {
  background-color: #2ed573;
  color: white;
}

.report-section {
  margin: 15px;
}

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

.section-count {
  color: #999;
  font-weight: normal;
}

.report-list {
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
}

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

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

.report-info {
  flex: 1;
}

.report-name {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 5px;
}

.report-time {
  font-size: 12px;
  color: #999;
  display: block;
  margin-bottom: 8px;
}

.report-stats {
  display: flex;
  gap: 15px;
}

.stats-item {
  font-size: 12px;
  color: #666;
}

.report-actions {
  display: flex;
  gap: 15px;
}

.action-text {
  font-size: 14px;
  padding: 5px 10px;
  border-radius: 15px;
}

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

.delete {
  background-color: #ff4757;
  color: white;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  width: 80px;
  height: 80px;
  opacity: 0.3;
  margin-bottom: 20px;
}

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

.empty-desc {
  font-size: 14px;
  color: #ccc;
}

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

.modal-content {
  background-color: white;
  border-radius: 15px;
  margin: 20px;
  max-width: 90%;
  max-height: 70vh;
  overflow: hidden;
}

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

.modal-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.modal-close {
  font-size: 24px;
  color: #999;
  width: 30px;
  text-align: center;
}

.modal-body {
  padding: 20px;
  max-height: 40vh;
  overflow-y: auto;
}

.detail-row {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

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

.detail-value {
  font-size: 14px;
  color: #333;
  flex: 1;
}

.status.completed {
  color: #2ed573;
}

.status.processing {
  color: #ffa502;
}

.status.failed {
  color: #ff4757;
}

.modal-footer {
  padding: 20px;
  border-top: 1px solid #f0f0f0;
}

.modal-btn {
  width: 100%;
  height: 45px;
  border-radius: 25px;
  font-size: 16px;
  border: none;
}

.export-btn {
  background-color: #007aff;
  color: white;
}
</style>
