<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="消费时间">
          <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="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="140" />
      <el-table-column label="消费笔数" align="center" prop="count" width="120" />
      <el-table-column label="消费金额" align="center" prop="totalAmount" width="160">
        <template slot-scope="scope">
          <span style="color:#1890ff;font-weight:bold;font-size:15px">¥{{ scope.row.totalAmount || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="占比" align="center" min-width="200">
        <template slot-scope="scope">
          <el-progress :percentage="getPercent(scope.row.totalAmount)" :stroke-width="18" :show-text="true" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

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

export default {
  name: 'CompanyConsumeReport',
  data() {
    return {
      loading: false,
      dateRange: [],
      moduleBreakdown: [],
      queryParams: {
        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-data-line', bg: '#f6ffed' },
        { label: '最高消费模块', value: '-', icon: 'el-icon-top', 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.moduleBreakdown = data.moduleBreakdown || []
        this.updateOverview(data.summary || {}, data.moduleBreakdown || [])
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    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, modules) {
      this.overviewCards[0].value = '¥' + (parseFloat(summary.totalAmount) || 0).toFixed(2)
      this.overviewCards[1].value = summary.totalCount || 0
      this.overviewCards[2].value = summary.moduleCount || 0
      if (modules.length > 0) {
        this.overviewCards[3].value = modules[0].moduleName
      } else {
        this.overviewCards[3].value = '-'
      }
    },
    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; }
</style>
