<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="单日限额" prop="dailyLimit">
                    <el-input
                        v-model="queryParams.dailyLimit"
                        placeholder="请输入单日限额"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="是否审核" prop="needAudit">
                    <el-select
                        v-model="queryParams.needAudit"
                        placeholder="请选择是否需要审核"
                        clearable
                    >
                        <el-option
                            v-for="dict in sys_general_yes_no"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否启用" prop="isEnabled">
                    <el-select
                        v-model="queryParams.isEnabled"
                        placeholder="请选择是否启用"
                        clearable
                    >
                        <el-option
                            v-for="dict in sys_general_status"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                </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:appWithdrawalConfig:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:appWithdrawalConfig:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:appWithdrawalConfig:remove']"
                    >
                        删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:appWithdrawalConfig:export']"
                    >
                        导出
                    </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="appWithdrawalConfigList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="配置ID" align="center" prop="configId" />
                <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="minAmount" />
                <el-table-column label="最高提现金额" align="center" prop="maxAmount" />
                <el-table-column label="单日限额" align="center" prop="dailyLimit" />
                <el-table-column label="单日次数限制" align="center" prop="dailyCount" />
                <el-table-column label="手续费类型" align="center" prop="feeType">
                    <template #default="scope">
                        {{ scope.row.feeType == 1 ? '固定金额' : '百分比' }}
                    </template>
                </el-table-column>
                <el-table-column label="手续费值" align="center" prop="feeValue" />
                <el-table-column label="处理时效(小时)" align="center" prop="processHours" />
                <el-table-column label="是否需要审核" align="center" prop="needAudit">
                    <template #default="scope">
                        <dict-tag :options="sys_general_yes_no" :value="scope.row.needAudit" />
                    </template>
                </el-table-column>
                <el-table-column label="审核时效(小时)" align="center" prop="auditHours" />
                <el-table-column label="是否启用" align="center" prop="isEnabled">
                    <template #default="scope">
                        <dict-tag :options="sys_general_status" :value="scope.row.isEnabled" />
                    </template>
                </el-table-column>
                <el-table-column label="排序" align="center" prop="sortOrder" />
                <el-table-column label="配置描述" align="center" prop="configDesc" />
                <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:appWithdrawalConfig:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:appWithdrawalConfig: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="appWithdrawalConfigRef"
                :model="form"
                :rules="rules"
                label-width="80px"
                label-position="top"
            >
                <el-form-item label="提现方式" prop="withdrawalType">
                    <el-select v-model="form.withdrawalType" placeholder="请选择是否需要审核">
                        <el-option label="银行卡" :value="1"></el-option>
                        <el-option label="微信" :value="2"></el-option>
                        <el-option label="支付宝" :value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="最低提现金额" prop="minAmount">
                    <el-input v-model="form.minAmount" placeholder="请输入最低提现金额" />
                </el-form-item>
                <el-form-item label="最高提现金额" prop="maxAmount">
                    <el-input v-model="form.maxAmount" placeholder="请输入最高提现金额" />
                </el-form-item>
                <el-form-item label="单日限额" prop="dailyLimit">
                    <el-input v-model="form.dailyLimit" placeholder="请输入单日限额" />
                </el-form-item>
                <el-form-item label="单日次数限制" prop="dailyCount">
                    <el-input v-model="form.dailyCount" placeholder="请输入单日次数限制" />
                </el-form-item>
                <el-form-item label="手续费类型" prop="feeType">
                    <el-select v-model="form.feeType" placeholder="请选择手续费类型">
                        <el-option label="固定金额" :value="1"></el-option>
                        <el-option label="百分比" :value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手续费值" prop="feeValue">
                    <el-input v-model="form.feeValue" placeholder="请输入手续费值" />
                </el-form-item>
                <el-form-item label="处理时效(小时)" prop="processHours">
                    <el-input v-model="form.processHours" placeholder="请输入处理时效(小时)" />
                </el-form-item>
                <el-form-item label="是否需要审核" prop="needAudit">
                    <el-select v-model="form.needAudit" placeholder="请选择是否需要审核">
                        <el-option label="是" value="1"></el-option>
                        <el-option label="否" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核时效(小时)" prop="auditHours">
                    <el-input v-model="form.auditHours" placeholder="请输入审核时效(小时)" />
                </el-form-item>
                <el-form-item label="是否启用" prop="isEnabled">
                    <el-select v-model="form.isEnabled" placeholder="请选择是否启用">
                        <el-option label="是" value="1"></el-option>
                        <el-option label="否" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sortOrder">
                    <el-input v-model="form.sortOrder" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="配置描述" prop="configDesc">
                    <el-input v-model="form.configDesc" type="textarea" placeholder="请输入内容" />
                </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="AppWithdrawalConfig">
import {
    listAppWithdrawalConfig,
    getAppWithdrawalConfig,
    delAppWithdrawalConfig,
    addAppWithdrawalConfig,
    updateAppWithdrawalConfig,
} from '@/api/service/appWithdrawalConfig';

const { proxy } = getCurrentInstance();
const { sys_general_status, sys_general_yes_no } = proxy.useDict(
    'sys_general_status',
    'sys_general_yes_no'
);

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

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawalType: null,
        minAmount: null,
        maxAmount: null,
        dailyLimit: null,
        dailyCount: null,
        feeType: null,
        feeValue: null,
        processHours: null,
        needAudit: null,
        auditHours: null,
        isEnabled: null,
        sortOrder: null,
        configDesc: null,
    },
    rules: {
        withdrawalType: [
            {
                required: true,
                message: '提现方式 (1: 银行卡, 2: 微信, 3: 支付宝)不能为空',
                trigger: 'change',
            },
        ],
        minAmount: [{ required: true, message: '最低提现金额不能为空', trigger: 'blur' }],
        maxAmount: [{ required: true, message: '最高提现金额不能为空', trigger: 'blur' }],
        dailyLimit: [{ required: true, message: '单日限额不能为空', trigger: 'blur' }],
        dailyCount: [{ required: true, message: '单日次数限制不能为空', trigger: 'blur' }],
        feeType: [
            {
                required: true,
                message: '手续费类型 (1: 固定金额, 2: 百分比)不能为空',
                trigger: 'change',
            },
        ],
        feeValue: [{ required: true, message: '手续费值不能为空', trigger: 'blur' }],
        processHours: [{ required: true, message: '处理时效(小时)不能为空', trigger: 'blur' }],
        needAudit: [
            { required: true, message: '是否需要审核 (0: 否, 1: 是)不能为空', trigger: 'change' },
        ],
        auditHours: [{ required: true, message: '审核时效(小时)不能为空', trigger: 'blur' }],
        isEnabled: [
            { required: true, message: '是否启用 (0: 禁用, 1: 启用)不能为空', trigger: 'change' },
        ],
        sortOrder: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
    },
});

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

/** 查询提现配置列表 */
function getList() {
    loading.value = true;
    listAppWithdrawalConfig(queryParams.value).then(response => {
        appWithdrawalConfigList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        configId: null,
        withdrawalType: null,
        minAmount: null,
        maxAmount: null,
        dailyLimit: null,
        dailyCount: null,
        feeType: null,
        feeValue: null,
        processHours: null,
        needAudit: null,
        auditHours: null,
        isEnabled: null,
        sortOrder: null,
        configDesc: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
    };
    proxy.resetForm('appWithdrawalConfigRef');
}

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

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

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.configId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

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

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _configId = row.configId || ids.value;
    getAppWithdrawalConfig(_configId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改提现配置';
    });
}

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

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

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

getList();
</script>
