<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="代理名称" prop="proxyName">
        <el-input v-model="queryParams.proxyName" placeholder="请输入代理名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="租户名称" prop="tenantName">
        <el-input v-model="queryParams.tenantName" placeholder="请输入租户名称" clearable size="small" @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"
          size="small"
          style="width:240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table border v-loading="loading" :data="recordList" size="small" style="width:100%">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="代理ID" align="center" prop="proxyId" min-width="60" />
      <el-table-column label="代理名称" align="center" prop="proxyName" min-width="100" />
      <el-table-column label="租户ID" align="center" prop="tenantId" min-width="60" />
      <el-table-column label="租户名称" align="center" prop="tenantName" min-width="120" />
      <el-table-column label="消费金额" align="center" prop="amount" min-width="90">
        <template slot-scope="scope">¥{{ scope.row.amount || '0.00' }}</template>
      </el-table-column>
      <el-table-column label="平台成本" align="center" prop="platformCost" min-width="90">
        <template slot-scope="scope">
          <span style="color:#909399">¥{{ scope.row.platformCost || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户售价" align="center" prop="tenantPrice" min-width="90">
        <template slot-scope="scope">¥{{ scope.row.tenantPrice || '0.00' }}</template>
      </el-table-column>
      <el-table-column label="分佣比例" align="center" prop="proxyRatio" min-width="80">
        <template slot-scope="scope">{{ scope.row.proxyRatio || 0 }}%</template>
      </el-table-column>
      <el-table-column label="分佣金额" align="center" prop="proxyProfit" min-width="90">
        <template slot-scope="scope">
          <span style="color:#E6A23C;font-weight:bold">¥{{ scope.row.proxyProfit || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费时间" align="center" prop="consumeTime" min-width="150" />
      <el-table-column label="操作" align="center" width="80" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 详情弹窗 -->
    <el-dialog title="返佣记录详情" :visible.sync="detailVisible" width="550px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="代理ID">{{ detail.proxyId }}</el-descriptions-item>
        <el-descriptions-item label="代理名称">{{ detail.proxyName }}</el-descriptions-item>
        <el-descriptions-item label="租户ID">{{ detail.tenantId }}</el-descriptions-item>
        <el-descriptions-item label="租户名称">{{ detail.tenantName }}</el-descriptions-item>
        <el-descriptions-item label="消费金额">¥{{ detail.amount || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="平台成本">¥{{ detail.platformCost || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="租户售价">¥{{ detail.tenantPrice || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="分佣比例">{{ detail.proxyRatio || 0 }}%</el-descriptions-item>
        <el-descriptions-item label="分佣金额">
          <span style="color:#E6A23C;font-weight:bold">¥{{ detail.proxyProfit || '0.00' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="计费模式">
          <el-tag v-if="detail.billingMode === 'USAGE'" type="primary" size="small">按量</el-tag>
          <el-tag v-else-if="detail.billingMode === 'SUBSCRIPTION'" type="success" size="small">套餐</el-tag>
          <span v-else>{{ detail.billingMode || '-' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="消费时间">{{ detail.consumeTime }}</el-descriptions-item>
        <el-descriptions-item label="消费类型" :span="2">{{ detail.eventType || '-' }}</el-descriptions-item>
        <el-descriptions-item label="关联业务ID" :span="2">{{ detail.bizId || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listCommissionRecords, getCommissionRecord, exportCommissionRecord } from '@/api/admin/commissionRecord'

export default {
  name: 'AdminCommissionRecord',
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      total: 0,
      recordList: [],
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proxyName: null,
        tenantName: null,
        beginTime: null,
        endTime: null
      },
      detailVisible: false,
      detail: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      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
      }
      listCommissionRecords(this.queryParams).then(res => {
        this.recordList = res.rows || []
        this.total = res.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDetail(row) {
      getCommissionRecord(row.id).then(res => {
        this.detail = res.data || {}
        this.detailVisible = true
      })
    },
    handleExport() {
      this.exportLoading = true
      exportCommissionRecord(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
