<template>
    <div class="app-container" style="padding: 0">
        <div class="background-color-fff border-radius-8px margin-bottom-20">
            <el-table
                v-loading="loading"
                :data="loansRecordList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column label="记录id" align="center" prop="recordId" />
                <el-table-column label="贷款申请id" align="center" prop="loansId" />
                <el-table-column label="批准额度" align="center" prop="loansAmount" />
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="{ row }">
                        {{ filterStatus(row.status) }}
                    </template>
                </el-table-column>
                <el-table-column label="拒绝原因" align="center" prop="reason" />
                <el-table-column label="审核人" align="center" prop="updateBy" />
                <el-table-column label="审核时间" align="center" prop="createTime" />
            </el-table>
        </div>
    </div>
</template>

<script setup name="LoansRecord">
import {
    listLoansRecord,
    getLoansRecord,
    delLoansRecord,
    addLoansRecord,
    updateLoansRecord,
} from '@/api/service/loansRecord';

const { proxy } = getCurrentInstance();

const props = defineProps({
    loansId: Number,
    loanType: Number,
});

const loansRecordList = 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,
        loansId: null,
        loansAmount: null,
        status: null,
        reason: null,
    },
    rules: {
        loansId: [{ required: true, message: '贷款申请id不能为空', trigger: 'blur' }],
        loansAmount: [{ required: true, message: '批准额度不能为空', trigger: 'blur' }],
    },
});

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

watch(
    () => props.loansId, // 监听 loansId 的变化
    newLoansId => {},
    { immediate: true } // 如果需要立即执行一次
);

/** 查询贷款审核记录列表 */
function getList() {
    loading.value = true;
    listLoansRecord(queryParams.value).then(response => {
        loansRecordList.value = response.data;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        recordId: null,
        loansId: null,
        loansAmount: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        reason: null,
    };
    proxy.resetForm('loansRecordRef');
}

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

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

function filterStatus(status) {
    let name = '';
    switch (status) {
        case 0:
            name = '待提交材料';
            break;
        case 1:
            name = '待初审';
            break;
        case 2:
            name = '初审中';
            break;
        case 3:
            name = '初审通过';
            break;
        case 4:
            name = '初审拒绝';
            break;
        case 5:
            name = '二审待提交';
            break;
        case 6:
            name = '二次待审';
            break;
        case 7:
            name = '二次审核中';
            break;
        case 8:
            name = '同意放款';
            break;
        case 9:
            name = '已拒绝';
            break;
        case 10:
            name = '流水不足拒绝';
            break;
        case 11:
            name = '信用评分不足拒绝';
            break;
        case 12:
            name = '待放款';
            break;
        case 13:
            name = '已放款';
            break;
        case 14:
            name = '已取消';
            break;
        case 15:
            name = '二审签写合同';
            break;
        case 16:
            name = '银行卡信息有误';
            break;
        case 17:
            name = '二审合同提交';
            break;
        default:
            break;
    }
    return name;
}

onMounted(() => {
    queryParams.value.loansId = props.loansId; // 更新 queryParams
    queryParams.value.loanType = props.loanType;
    getList(); // 调用方法
});
</script>
