<template>
  <div class="app-container">
    <!-- ===== KPI 概览卡片 ===== -->
    <el-row :gutter="16" class="mb16">
      <el-col :span="6" v-for="card in overviewCards" :key="card.label">
        <el-card shadow="hover" class="overview-card">
          <div class="card-inner">
            <div class="card-icon" :style="{ background: card.bg }"><i :class="card.icon"></i></div>
            <div class="card-info">
              <span class="card-value">{{ card.value }}</span>
              <span class="card-label">{{ card.label }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- ===== 搜索栏 ===== -->
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="租户名称" prop="tenantName">
          <el-input v-model="queryParams.tenantName" placeholder="请输入租户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="消费时间">
          <el-date-picker v-model="dateRange" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期"
            value-format="yyyy-MM-dd" style="width:240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- ===== 模块消费交叉汇总表 ===== -->
    <el-table border v-loading="loading" :data="tenantList" size="small" style="width:100%" show-summary :summary-method="getSummary">
      <el-table-column type="index" label="序号" width="55" align="center" fixed="left" />
      <el-table-column label="租户名称" align="center" prop="tenantName" min-width="120" fixed="left" />
      <!-- 动态模块列 -->
      <el-table-column v-for="mod in moduleColumns" :key="mod.code" :label="mod.name" align="center" :min-width="mod.minWidth || 90">
        <template slot-scope="scope">
          <span style="color:#1890ff">¥{{ getModuleAmount(scope.row, mod.code) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费总额" align="center" prop="totalAmount" width="120" fixed="right">
        <template slot-scope="scope">
          <span style="color:#f5222d;font-weight:bold">¥{{ scope.row.totalAmount || '0.00' }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- ===== 模块消费汇总表（模块维度） ===== -->
    <el-card shadow="never" class="mt16">
      <div slot="header"><span>各模块消费汇总</span></div>
      <el-table border :data="moduleBreakdown" size="small" style="width:100%">
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column label="模块" align="center" prop="moduleName" min-width="120" />
        <el-table-column label="消费笔数" align="center" prop="count" width="100" />
        <el-table-column label="消费金额" align="center" prop="totalAmount" width="140">
          <template slot-scope="scope">
            <span style="color:#1890ff;font-weight:bold">¥{{ scope.row.totalAmount || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="占比" align="center" width="100">
          <template slot-scope="scope">
            <el-progress :percentage="getPercent(scope.row.totalAmount)" :stroke-width="16" :show-text="true" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getConsumptionReport } from '@/api/admin/consumeReport'

export default {
  name: 'AdminConsumeReport',
  data() {
    return {
      loading: false,
      dateRange: [],
      // 模块列定义（动态生成）
      moduleColumns: [],
      // 模块维度汇总
      moduleBreakdown: [],
      // 租户维度汇总
      tenantList: [],
      // 各模块总额（用于占比计算）
      moduleTotalMap: {},
      queryParams: {
        tenantName: null,
        beginTime: null,
        endTime: null
      },
      overviewCards: [
        { label: '消费总额', value: '¥0.00', icon: 'el-icon-wallet', bg: '#e6f7ff' },
        { label: '消费笔数', value: 0, icon: 'el-icon-document', bg: '#fff7e6' },
        { label: '涉及租户数', value: 0, icon: 'el-icon-office-building', bg: '#f6ffed' },
        { label: '涉及模块数', value: 0, icon: 'el-icon-data-line', bg: '#f9f0ff' }
      ]
    }
  },
  created() {
    // 默认查询当月
    const now = new Date()
    const y = now.getFullYear()
    const m = String(now.getMonth() + 1).padStart(2, '0')
    this.dateRange = [y + '-' + m + '-01', y + '-' + m + '-' + String(new Date(y, now.getMonth() + 1, 0).getDate()).padStart(2, '0')]
    this.getReport()
  },
  methods: {
    getReport() {
      this.loading = true
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.beginTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      } else {
        this.queryParams.beginTime = null
        this.queryParams.endTime = null
      }
      getConsumptionReport(this.queryParams).then(res => {
        const data = res.data || {}
        this.buildModuleColumns(data.moduleBreakdown || [])
        this.moduleBreakdown = data.moduleBreakdown || []
        this.tenantList = data.tenantBreakdown || []
        this.updateOverview(data.summary || {})
        this.loading = false
      }).catch(() => { this.loading = false })
    },

    /** 根据模块明细动态生成表格列 */
    buildModuleColumns(modules) {
      this.moduleColumns = modules.map(m => ({
        code: m.moduleCode,
        name: m.moduleName,
        minWidth: m.moduleName.length > 4 ? 90 : 80
      }))
      // 构建模块总额映射
      this.moduleTotalMap = {}
      modules.forEach(m => {
        this.moduleTotalMap[m.moduleCode] = parseFloat(m.totalAmount) || 0
      })
    },

    /** 从租户的 modules 映射中取指定模块金额 */
    getModuleAmount(row, moduleCode) {
      const modules = row.modules || {}
      const val = modules[moduleCode]
      return val != null ? parseFloat(val).toFixed(2) : '0.00'
    },

    /** 计算模块占比 */
    getPercent(amount) {
      const val = parseFloat(amount) || 0
      const total = parseFloat((this.overviewCards[0].value || '¥0').replace('¥', '')) || 1
      return Math.round(val / total * 100)
    },

    updateOverview(summary) {
      this.overviewCards[0].value = '¥' + (parseFloat(summary.totalAmount) || 0).toFixed(2)
      this.overviewCards[1].value = summary.totalCount || 0
      this.overviewCards[2].value = summary.tenantCount || 0
      this.overviewCards[3].value = summary.moduleCount || 0
    },

    /** 表格合计行 */
    getSummary({ columns, data }) {
      const sums = []
      columns.forEach((col, idx) => {
        if (idx === 0) { sums[idx] = '合计'; return }
        if (idx === 1) { sums[idx] = ''; return }
        const prop = col.property
        if (prop === 'totalAmount') {
          const val = data.reduce((s, r) => s + (parseFloat(r[prop]) || 0), 0)
          sums[idx] = '¥' + val.toFixed(2)
        } else {
          sums[idx] = ''
        }
      })
      // 动态模块列求和
      this.moduleColumns.forEach((mod, mi) => {
        const colIdx = mi + 2 // 2 = 序号 + 租户名称
        const val = data.reduce((s, r) => {
          return s + parseFloat(this.getModuleAmount(r, mod.code))
        }, 0)
        sums[colIdx] = '¥' + val.toFixed(2)
      })
      return sums
    },

    handleQuery() {
      this.getReport()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>

<style scoped>
.overview-card { cursor: default; }
.card-inner { display: flex; align-items: center; }
.card-icon {
  width: 46px; height: 46px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  margin-right: 14px; font-size: 22px; color: #fff;
}
.card-value { font-size: 22px; font-weight: bold; color: #333; display: block; }
.card-label { font-size: 12px; color: #999; }

.filter-card { padding-bottom: 0; }
.mb16 { margin-bottom: 16px; }
.mt16 { margin-top: 16px; }
</style>
