<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="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="待审核" :value="0" />
          <el-option label="已通过" :value="1" />
          <el-option label="已拒绝" :value="2" />
          <el-option label="已打款" :value="3" />
        </el-select>
      </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" width="70" />
      <el-table-column label="代理名称" align="center" prop="proxyName" min-width="120" />
      <el-table-column label="可提现金额" align="center" prop="availableAmount" width="120">
        <template slot-scope="scope">¥{{ scope.row.availableAmount || scope.row.balance || '0.00' }}</template>
      </el-table-column>
      <el-table-column label="申请金额" align="center" prop="withdrawAmount" width="120">
        <template slot-scope="scope">
          <span style="color:#E6A23C;font-weight:bold">¥{{ scope.row.withdrawAmount || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">已通过</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">已拒绝</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="primary">已打款</el-tag>
          <el-tag v-else type="info">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="银行名称" align="center" prop="bankName" width="140" show-overflow-tooltip />
      <el-table-column label="银行账号" align="center" prop="bankAccount" width="160" show-overflow-tooltip />
      <el-table-column label="申请时间" align="center" prop="createTime" width="160" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="160" />
      <el-table-column label="审核人" align="center" prop="auditorName" width="100" />
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 0" size="mini" type="success" @click="handleApprove(scope.row)">通过</el-button>
          <el-button v-if="scope.row.status === 0" size="mini" type="danger" @click="handleReject(scope.row)">拒绝</el-button>
          <el-button v-if="scope.row.status === 1" size="mini" type="primary" @click="handlePaid(scope.row)">确认打款</el-button>
          <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="rejectVisible" width="450px" append-to-body>
      <el-form ref="rejectForm" :model="rejectForm" :rules="rejectRules" label-width="100px">
        <el-form-item label="拒绝原因" prop="reason">
          <el-input v-model="rejectForm.reason" type="textarea" :rows="3" placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="rejectVisible = false">取 消</el-button>
        <el-button type="danger" @click="submitReject">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 详情弹窗 -->
    <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="可提现金额">¥{{ detail.availableAmount || detail.balance || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="申请金额">¥{{ detail.withdrawAmount || '0.00' }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="detail.status === 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="detail.status === 1" type="success">已通过</el-tag>
          <el-tag v-else-if="detail.status === 2" type="danger">已拒绝</el-tag>
          <el-tag v-else-if="detail.status === 3" type="primary">已打款</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="银行名称">{{ detail.bankName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="银行账号">{{ detail.bankAccount || '-' }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="审核时间">{{ detail.auditTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="审核人">{{ detail.auditorName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="拒绝原因" :span="2">{{ detail.failReason || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listWithdrawals, getWithdrawal, approveWithdrawal, rejectWithdrawal, confirmPaid, exportWithdrawal } from '@/api/admin/withdrawalManage'

export default {
  name: 'AdminWithdrawal',
  data() {
    return {
      loading: false,
      exportLoading: false,
      showSearch: true,
      total: 0,
      recordList: [],
      dateRange: [],
      rejectVisible: false,
      currentRejectId: null,
      rejectForm: { reason: '' },
      rejectRules: {
        reason: [{ required: true, message: '请输入拒绝原因', trigger: 'blur' }]
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proxyName: null,
        status: 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
      }
      listWithdrawals(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()
    },
    handleApprove(row) {
      this.$confirm(`确认通过代理 "${row.proxyName}" 的提现申请 (¥${row.withdrawAmount})？`, '审核通过', { type: 'warning' }).then(() => {
        approveWithdrawal(row.id).then(() => {
          this.$message.success('审核通过')
          this.getList()
        })
      })
    },
    handleReject(row) {
      this.currentRejectId = row.id
      this.rejectForm.reason = ''
      this.rejectVisible = true
      this.$nextTick(() => { this.$refs.rejectForm && this.$refs.rejectForm.resetFields() })
    },
    submitReject() {
      this.$refs['rejectForm'].validate(valid => {
        if (!valid) return
        rejectWithdrawal(this.currentRejectId, this.rejectForm.reason).then(() => {
          this.$message.success('已拒绝')
          this.rejectVisible = false
          this.getList()
        })
      })
    },
    handlePaid(row) {
      this.$confirm(`确认已向代理 "${row.proxyName}" 打款 ¥${row.withdrawAmount}？`, '确认打款', { type: 'warning' }).then(() => {
        confirmPaid(row.id).then(() => {
          this.$message.success('已确认打款')
          this.getList()
        })
      })
    },
    handleDetail(row) {
      getWithdrawal(row.id).then(res => {
        this.detail = res.data || {}
        this.detailVisible = true
      })
    },
    handleExport() {
      this.exportLoading = true
      exportWithdrawal(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>
