<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>提现记录管理</span>
      </div>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="用户ID">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户ID"
            clearable
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option label="待审核" :value="0" />
            <el-option label="审核通过" :value="1" />
            <el-option label="审核拒绝" :value="2" />
            <el-option label="打款中" :value="3" />
            <el-option label="已完成" :value="4" />
            <el-option label="已取消" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="list" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="userId" label="用户ID" width="100" />
        <el-table-column label="提现金额" width="120">
          <template slot-scope="scope">
            ¥{{ scope.row.withdrawAmount }}
          </template>
        </el-table-column>
        <el-table-column label="实际到账" width="120">
          <template slot-scope="scope">
            ¥{{ scope.row.actualAmount }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" />
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status === 0"
              type="text"
              @click="handleAudit(scope.row, 1)"
            >
              通过
            </el-button>
            <el-button
              v-if="scope.row.status === 0"
              type="text"
              style="color: #F56C6C"
              @click="handleAudit(scope.row, 2)"
            >
              拒绝
            </el-button>
            <el-button
              v-if="scope.row.status === 1"
              type="text"
              @click="handleTransfer(scope.row)"
            >
              确认打款
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.limit"
        @pagination="getList"
      />
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog :visible.sync="auditDialogVisible" title="审核提现申请" width="500px">
      <el-form :model="auditForm" label-width="100px">
        <el-form-item label="审核状态">
          <el-radio-group v-model="auditForm.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注">
          <el-input
            v-model="auditForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入审核备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="auditDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitAudit">确定</el-button>
      </div>
    </el-dialog>

    <!-- 打款对话框 -->
    <el-dialog :visible.sync="transferDialogVisible" title="确认打款" width="500px">
      <el-form :model="transferForm" label-width="100px">
        <el-form-item label="转账流水号">
          <el-input
            v-model="transferForm.transferNo"
            placeholder="请输入转账流水号"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="transferDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTransfer">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWithdrawRecordList, auditWithdrawRecord, confirmTransfer } from '@/api/shop/shopWithdrawRecord';

export default {
  name: 'ShopWithdrawRecord',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      queryParams: {
        page: 1,
        limit: 20,
        userId: '',
        status: ''
      },
      auditDialogVisible: false,
      auditForm: {
        id: null,
        status: 1,
        remark: ''
      },
      transferDialogVisible: false,
      transferForm: {
        id: null,
        transferNo: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const res = await getWithdrawRecordList(this.queryParams);
        this.list = res.data.list || [];
        this.total = res.data.total || 0;
      } catch (error) {
        console.error('查询失败', error);
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 20,
        userId: '',
        status: ''
      };
      this.getList();
    },
    handleAudit(row, status) {
      this.auditForm = {
        id: row.id,
        status: status,
        remark: ''
      };
      this.auditDialogVisible = true;
    },
    async submitAudit() {
      try {
        await auditWithdrawRecord(this.auditForm.id, {
          status: this.auditForm.status,
          remark: this.auditForm.remark
        });
        this.$message.success('审核完成');
        this.auditDialogVisible = false;
        this.getList();
      } catch (error) {
        console.error('审核失败', error);
      }
    },
    handleTransfer(row) {
      this.transferForm = {
        id: row.id,
        transferNo: ''
      };
      this.transferDialogVisible = true;
    },
    async submitTransfer() {
      try {
        await confirmTransfer(this.transferForm.id, {
          transferNo: this.transferForm.transferNo
        });
        this.$message.success('打款确认完成');
        this.transferDialogVisible = false;
        this.getList();
      } catch (error) {
        console.error('打款确认失败', error);
      }
    },
    getStatusType(status) {
      const map = { 0: 'warning', 1: 'success', 2: 'danger', 3: 'warning', 4: 'success', 5: 'info' };
      return map[status] || 'info';
    },
    getStatusText(status) {
      const map = { 0: '待审核', 1: '审核通过', 2: '审核拒绝', 3: '打款中', 4: '已完成', 5: '已取消' };
      return map[status] || '未知';
    }
  }
};
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
}
</style>
