<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="tenantName">
        <el-input v-model="queryParams.tenantName" placeholder="请输入租户名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人" prop="createBy">
        <el-input v-model="queryParams.createBy" 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 v-loading="loading" :data="recordList" border size="small" style="width:100%">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="租户ID" align="center" prop="tenantId" width="80" />
      <el-table-column label="租户名称" align="center" prop="tenantName" min-width="140" />
      <el-table-column label="充值金额" align="center" prop="amount" width="120">
        <template slot-scope="scope">
          <span style="color:#67C23A;font-weight:bold">+¥{{ scope.row.amount || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值后余额" align="center" prop="balanceAfter" width="130">
        <template slot-scope="scope">¥{{ scope.row.balanceAfter || '0.00' }}</template>
      </el-table-column>
      <el-table-column label="充值方式" align="center" prop="payMethod" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payMethod === 'ADMIN'" type="warning" size="small">后台充值</el-tag>
          <el-tag v-else-if="scope.row.payMethod === 'ALIPAY'" type="primary" size="small">支付宝</el-tag>
          <el-tag v-else-if="scope.row.payMethod === 'WECHAT'" type="success" size="small">微信支付</el-tag>
          <el-tag v-else-if="scope.row.payMethod === 'BANK'" size="small">银行转账</el-tag>
          <span v-else>{{ scope.row.payMethod || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" width="120" />
      <el-table-column label="充值时间" align="center" prop="createTime" width="160" />
      <el-table-column label="备注" align="center" prop="remark" min-width="150" show-overflow-tooltip />
      <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="500px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="租户ID">{{ detail.tenantId }}</el-descriptions-item>
        <el-descriptions-item label="租户名称">{{ detail.tenantName }}</el-descriptions-item>
        <el-descriptions-item label="充值金额">
          <span style="color:#67C23A;font-weight:bold">+¥{{ detail.amount || '0.00' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="充值后余额">¥{{ detail.balanceAfter || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="充值前余额">¥{{ detail.balanceBefore || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="充值方式">
          <el-tag v-if="detail.payMethod === 'ADMIN'" type="warning" size="small">后台充值</el-tag>
          <el-tag v-else-if="detail.payMethod === 'ALIPAY'" type="primary" size="small">支付宝</el-tag>
          <el-tag v-else-if="detail.payMethod === 'WECHAT'" type="success" size="small">微信支付</el-tag>
          <el-tag v-else-if="detail.payMethod === 'BANK'" size="small">银行转账</el-tag>
          <span v-else>{{ detail.payMethod || '-' }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="操作人">{{ detail.createBy || '-' }}</el-descriptions-item>
        <el-descriptions-item label="充值时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="交易流水号" :span="2">{{ detail.txnNo || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listRechargeRecords, getRechargeRecord, exportRechargeRecord } from '@/api/admin/rechargeRecord'

export default {
  name: 'AdminRechargeRecord',
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      total: 0,
      recordList: [],
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantName: null,
        createBy: 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
      }
      listRechargeRecords(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) {
      getRechargeRecord(row.id).then(res => {
        this.detail = res.data || {}
        this.detailVisible = true
      })
    },
    handleExport() {
      this.exportLoading = true
      exportRechargeRecord(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>
