<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="idCardNumber">
                    <el-input
                        v-model="queryParams.idCardNumber"
                        placeholder="请输入身份证号码"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="真实姓名" prop="realName">
                    <el-input
                        v-model="queryParams.realName"
                        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="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>
                    <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:userVerification:add']"
                    >
                        新增
                    </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>
                <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="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:userVerification:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:userVerification: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="userVerificationList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="主键ID" align="center" prop="id" />
                <el-table-column label="用户ID" align="center" prop="userId" />
                <el-table-column label="身份证号码" align="center" prop="idCardNumber" />
                <el-table-column label="真实姓名" align="center" prop="realName" />
                <el-table-column
                    label="身份证正面照URL"
                    align="center"
                    prop="idCardFrontUrl"
                    width="100"
                >
                    <template #default="scope">
                        <image-preview :src="scope.row.idCardFrontUrl" :width="50" :height="50" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="身份证反面照URL"
                    align="center"
                    prop="idCardBackUrl"
                    width="100"
                >
                    <template #default="scope">
                        <image-preview :src="scope.row.idCardBackUrl" :width="50" :height="50" />
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="{ row }">
                        {{ row.status == 1 ? '待处理' : row.status == 2 ? '审核拒绝' : '审核通过' }}
                    </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remarks" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['service:userVerification:edit']"
                        >
                            审核
                        </el-button>
                        <!-- <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:userVerification: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="userVerificationRef"
                :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="idCardNumber">
                    <el-input v-model="form.idCardNumber" disabled placeholder="请输入身份证号码" />
                </el-form-item>
                <el-form-item label="真实姓名" prop="realName">
                    <el-input v-model="form.realName" disabled placeholder="请输入真实姓名" />
                </el-form-item>
                <el-form-item label="身份证正面照URL" prop="idCardFrontUrl">
                    <image-upload v-model="form.idCardFrontUrl" :limit="1" disabled />
                </el-form-item>
                <el-form-item label="身份证反面照URL" prop="idCardBackUrl">
                    <image-upload v-model="form.idCardBackUrl" :limit="1" disabled />
                </el-form-item>
                <el-form-item label="审核状态" prop="status">
                    <el-select v-model="form.status" 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="remarks">
                    <el-input v-model="form.remarks" 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="UserVerification">
import {
    listUserVerification,
    getUserVerification,
    delUserVerification,
    addUserVerification,
    updateUserVerification,
    updateAppUserRealVerificationByIds,
} from '@/api/service/userVerification';

const { proxy } = getCurrentInstance();

const userVerificationList = 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,
        idCardNumber: null,
        realName: null,
        idCardFrontUrl: null,
        idCardBackUrl: null,
        status: null,
        remarks: null,
    },
    rules: {
        userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        idCardNumber: [{ required: true, message: '身份证号码不能为空', trigger: 'blur' }],
        realName: [{ required: true, message: '真实姓名不能为空', trigger: 'blur' }],
        status: [
            {
                required: true,
                message: '状态：1待处理；2审核拒绝；3通过不能为空',
                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;
    listUserVerification(queryParams.value).then(response => {
        userVerificationList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        userId: null,
        idCardNumber: null,
        realName: null,
        idCardFrontUrl: null,
        idCardBackUrl: null,
        status: null,
        remarks: null,
        createTime: null,
        updateTime: null,
    };
    proxy.resetForm('userVerificationRef');
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = '添加实名认证信息';
}

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

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value;
    getUserVerification(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改实名认证信息';
    });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
        .confirm('是否确认删除实名认证信息编号为"' + _ids + '"的数据项？')
        .then(function () {
            return delUserVerification(_ids);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

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

getList();
</script>
