<template>
    <div class="app-container">
        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-form
                :model="queryParams"
                ref="queryRef"
                :inline="true"
                v-show="showSearch"
                label-width="68px"
            >
                <el-form-item label="用户ID" prop="userId">
                    <el-input
                        v-model="queryParams.userId"
                        placeholder="请输入用户ID"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="提现单号" prop="withdrawalNo">
                    <el-input
                        v-model="queryParams.withdrawalNo"
                        placeholder="请输入提现单号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="审核状态" prop="status">
                    <el-select v-model="queryParams.status" placeholder="请选择审核状态">
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="处理中" :value="1"></el-option>
                        <el-option label="提现成功" :value="2"></el-option>
                        <el-option label="提现失败" :value="3"></el-option>
                        <el-option label="已取消" :value="4"></el-option>
                        <el-option label="审核通过" :value="5"></el-option>
                        <el-option label="审核拒绝" :value="6"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核时间" prop="auditTime">
                    <el-date-picker
                        clearable
                        v-model="queryParams.auditTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择审核时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="处理时间" prop="processTime">
                    <el-date-picker
                        clearable
                        v-model="queryParams.processTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择处理时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="完成时间" prop="completeTime">
                    <el-date-picker
                        clearable
                        v-model="queryParams.completeTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择完成时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="渠道订单" prop="transactionNo">
                    <el-input
                        v-model="queryParams.transactionNo"
                        placeholder="请输入支付渠道交易号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['service:SerAppUserWithdrawal:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:SerAppUserWithdrawal:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:SerAppUserWithdrawal:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:SerAppUserWithdrawal:export']"
                    >
                        导出
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="singleAll"
                        @click="handleUpdateAll(2)"
                        v-hasPermi="['service:userVerification:edit']"
                    >
                        一键通过
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="singleAll"
                        @click="handleUpdateAll(3)"
                        v-hasPermi="['service:userVerification:edit']"
                    >
                        一键拒绝
                    </el-button>
                </el-col>
                <right-toolbar
                    v-model:showSearch="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-table
                v-loading="loading"
                :data="SerAppUserWithdrawalList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="提现ID" align="center" prop="withdrawalId" />
                <el-table-column label="用户ID" align="center" prop="userId" />
                <el-table-column label="提现单号" align="center" prop="withdrawalNo" />
                <el-table-column label="提现金额" align="center" prop="amount" />
                <el-table-column label="手续费" align="center" prop="feeAmount" />
                <el-table-column label="实际到账金额" align="center" prop="actualAmount" />
                <el-table-column label="提现方式" align="center" prop="withdrawalType">
                    <template #default="scope">
                        {{
                            scope.row.withdrawalType == 1
                                ? '银行卡'
                                : scope.row.withdrawalType == 2
                                  ? '微信'
                                  : '支付宝'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="收款账户" align="center" prop="accountNumber" />
                <!-- <el-table-column label="银行名称" align="center" prop="bankName" /> -->
                <!-- <el-table-column label="银行卡后四位" align="center" prop="cardLastFour" /> -->
                <!-- <el-table-column label="账户名称" align="center" prop="accountName" /> -->
                <!-- <el-table-column label="账号/卡号" align="center" prop="accountNumber" /> -->
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="scope">
                        {{
                            scope.row.status == 0
                                ? '待处理'
                                : scope.row.status == 1
                                  ? '处理中'
                                  : scope.row.status == 2
                                    ? '提现成功'
                                    : scope.row.status == 3
                                      ? '提现失败'
                                      : '已取消'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="失败原因" align="center" prop="failReason" />
                <el-table-column label="审核状态" align="center" prop="auditStatus">
                    <template #default="scope">
                        {{
                            scope.row.auditStatus == 0
                                ? '待审核'
                                : scope.row.auditStatus == 1
                                  ? '审核通过'
                                  : '审核拒绝'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="审核备注" align="center" prop="auditRemark" />
                <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="审核人" align="center" prop="auditor" />
                <el-table-column label="处理时间" align="center" prop="processTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.processTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="完成时间" align="center" prop="completeTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="支付渠道响应" align="center" prop="channelResponse" /> -->
                <!-- <el-table-column label="支付渠道交易号" align="center" prop="transactionNo" /> -->
                <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    fixed="right"
                >
                    <template #default="scope">
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['service:SerAppUserWithdrawal:edit']"
                        >
                            修改
                        </el-button>
                        <!-- <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:SerAppUserWithdrawal:remove']"
                        >
                            删除
                        </el-button> -->
                    </template>
                </el-table-column>
            </el-table>

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

        <!-- 添加或修改用户提现记录对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form
                ref="SerAppUserWithdrawalRef"
                :model="form"
                :rules="rules"
                label-width="80px"
                label-position="top"
            >
                <el-form-item label="用户ID" prop="userId">
                    <el-input v-model="form.userId" disabled placeholder="请输入用户ID" />
                </el-form-item>
                <el-form-item label="提现单号" prop="withdrawalNo">
                    <el-input v-model="form.withdrawalNo" disabled placeholder="请输入提现单号" />
                </el-form-item>
                <el-form-item label="提现金额" prop="amount">
                    <el-input v-model="form.amount" disabled placeholder="请输入提现金额" />
                </el-form-item>
                <el-form-item label="手续费" prop="feeAmount">
                    <el-input v-model="form.feeAmount" disabled placeholder="请输入手续费" />
                </el-form-item>
                <el-form-item label="实际到账金额" prop="actualAmount">
                    <el-input
                        v-model="form.actualAmount"
                        disabled
                        placeholder="请输入实际到账金额"
                    />
                </el-form-item>
                <el-form-item label="收款账户ID" prop="paymentAccountId">
                    <el-input
                        v-model="form.paymentAccountId"
                        disabled
                        placeholder="请输入收款账户ID"
                    />
                </el-form-item>
                <!-- <el-form-item label="银行名称" prop="bankName">
                    <el-input v-model="form.bankName" disabled placeholder="请输入银行名称" />
                </el-form-item>
                <el-form-item label="银行卡后四位" prop="cardLastFour">
                    <el-input v-model="form.cardLastFour" placeholder="请输入银行卡后四位" />
                </el-form-item>
                <el-form-item label="账户名称" prop="accountName">
                    <el-input v-model="form.accountName" placeholder="请输入账户名称" />
                </el-form-item>
                <el-form-item label="账号/卡号" prop="accountNumber">
                    <el-input v-model="form.accountNumber" placeholder="请输入账号/卡号" />
                </el-form-item> -->
                <el-form-item label="失败原因" prop="failReason">
                    <el-input v-model="form.failReason" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="审核备注" prop="auditRemark">
                    <el-input v-model="form.auditRemark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <!-- <el-form-item label="审核时间" prop="auditTime">
                    <el-date-picker
                        clearable
                        v-model="form.auditTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择审核时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="处理时间" prop="processTime">
                    <el-date-picker
                        clearable
                        v-model="form.processTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择处理时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="完成时间" prop="completeTime">
                    <el-date-picker
                        clearable
                        v-model="form.completeTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择完成时间"
                    ></el-date-picker>
                </el-form-item> -->
                <el-form-item label="审核状态（按步骤审核）" prop="status">
                    <el-select v-model="form.status" placeholder="请选择审核状态">
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="处理中 第二步" :value="1"></el-option>
                        <el-option label="提现成功 第三步" :value="2"></el-option>
                        <el-option label="提现失败 第三步" :value="3"></el-option>
                        <!-- <el-option label="已取消" :value="4"></el-option> -->
                        <el-option label="审核通过 第一步" :value="5"></el-option>
                        <el-option label="审核拒绝 第一步" :value="6"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="SerAppUserWithdrawal">
import {
    listSerAppUserWithdrawal,
    getSerAppUserWithdrawal,
    delSerAppUserWithdrawal,
    addSerAppUserWithdrawal,
    updateSerAppUserWithdrawal,
    updateSerAppUserWithdrawalAll,
} from '@/api/service/SerAppUserWithdrawal';
import { el } from 'element-plus/es/locales.mjs';

const { proxy } = getCurrentInstance();

const SerAppUserWithdrawalList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const singleAll = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        withdrawalNo: null,
        amount: null,
        feeAmount: null,
        actualAmount: null,
        withdrawalType: null,
        paymentAccountId: null,
        bankName: null,
        cardLastFour: null,
        accountName: null,
        accountNumber: null,
        status: null,
        failReason: null,
        auditStatus: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        processTime: null,
        completeTime: null,
        channelResponse: null,
        transactionNo: null,
    },
    rules: {
        userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        withdrawalNo: [{ required: true, message: '提现单号不能为空', trigger: 'blur' }],
        amount: [{ required: true, message: '提现金额不能为空', trigger: 'blur' }],
        actualAmount: [{ required: true, message: '实际到账金额不能为空', trigger: 'blur' }],
        withdrawalType: [
            {
                required: true,
                message: '提现方式 (1: 银行卡, 2: 微信, 3: 支付宝)不能为空',
                trigger: 'change',
            },
        ],
        status: [
            {
                required: true,
                message: '状态 (0: 待处理, 1: 处理中, 2: 提现成功, 3: 提现失败, 4: 已取消)不能为空',
                trigger: 'change',
            },
        ],
        auditStatus: [
            {
                required: true,
                message: '审核状态 (0: 待审核, 1: 审核通过, 2: 审核拒绝)不能为空',
                trigger: 'change',
            },
        ],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户提现记录列表 */
function getList() {
    loading.value = true;
    listSerAppUserWithdrawal(queryParams.value).then(response => {
        SerAppUserWithdrawalList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        withdrawalId: null,
        userId: null,
        withdrawalNo: null,
        amount: null,
        feeAmount: null,
        actualAmount: null,
        withdrawalType: null,
        paymentAccountId: null,
        bankName: null,
        cardLastFour: null,
        accountName: null,
        accountNumber: null,
        status: null,
        failReason: null,
        auditStatus: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        processTime: null,
        completeTime: null,
        channelResponse: null,
        transactionNo: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
    };
    proxy.resetForm('SerAppUserWithdrawalRef');
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef');
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.withdrawalId);
    single.value = selection.length != 1;
    singleAll.value = ids.value.length > 0 ? false : true;
    multiple.value = !selection.length;
}

function handleUpdateAll(num) {
    if (ids.value.length == 0) {
        return;
    }
    let params = {
        ids: ids.value,
        status: num,
    };
    updateSerAppUserWithdrawalAll(params).then(res => {
        proxy.$modal.msgSuccess('修改成功');
        singleAll.value = true;
        ids.value = [];
        getList();
    });
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = '添加用户提现记录';
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _withdrawalId = row.withdrawalId || ids.value;
    getSerAppUserWithdrawal(_withdrawalId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改用户提现记录';
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['SerAppUserWithdrawalRef'].validate(valid => {
        if (valid) {
            if (form.value.withdrawalId != null) {
                updateSerAppUserWithdrawal(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功');
                    open.value = false;
                    getList();
                });
            } else {
                addSerAppUserWithdrawal(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功');
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _withdrawalIds = row.withdrawalId || ids.value;
    proxy.$modal
        .confirm('是否确认删除用户提现记录编号为"' + _withdrawalIds + '"的数据项？')
        .then(function () {
            return delSerAppUserWithdrawal(_withdrawalIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'service/SerAppUserWithdrawal/export',
        {
            ...queryParams.value,
        },
        `SerAppUserWithdrawal_${new Date().getTime()}.xlsx`
    );
}

getList();
</script>
