<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16">
      <div slot="header">
        <span>总账号-租户费用明细</span>
      </div>
      <el-form :inline="true" label-width="90px">
        <el-form-item label="租户筛选">
          <el-select v-model="queryTenantId" clearable filterable placeholder="不选=全部租户" style="width: 360px">
            <el-option
              v-for="item in tenantOptions"
              :key="item.id"
              :label="item.tenantName + '（' + item.tenantCode + '）'"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="loadDetails">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never">
      <div slot="header" class="header-row">
        <span>费用明细列表</span>
        <el-button size="mini" type="primary" plain @click="loadDetails">刷新</el-button>
      </div>
      <el-table :data="detailList" v-loading="loading" border>
        <el-table-column label="租户ID" prop="tenantId" width="100" />
        <el-table-column label="事件ID" prop="eventId" min-width="180" />
        <el-table-column label="事件类型" prop="eventType" width="120" />
        <el-table-column label="子类型" prop="subType" width="120" />
        <el-table-column label="计费用量" prop="usageValue" width="120" />
        <el-table-column label="金额" prop="amount" width="120" />
        <el-table-column label="发生时间" prop="occurredAt" min-width="180" />
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { tenantList } from '@/api/tenant/tenant'
import { listBillingDetailsAdmin } from '@/api/saas/billing'

export default {
  name: 'SaasBillingAdmin',
  data() {
    return {
      loading: false,
      queryTenantId: null,
      tenantOptions: [],
      detailList: []
    }
  },
  created() {
    this.loadTenantOptions()
    this.loadDetails()
  },
  methods: {
    loadTenantOptions() {
      tenantList({}).then(res => {
        this.tenantOptions = res.rows || res.data || []
      })
    },
    loadDetails() {
      this.loading = true
      const params = {}
      if (this.queryTenantId) {
        params.tenantId = this.queryTenantId
      }
      listBillingDetailsAdmin(params).then(res => {
        this.detailList = res.rows || []
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
.mb16 {
  margin-bottom: 16px;
}
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>

