<template>
    <el-dialog
        v-model="visible"
        :title="selectedIds.length === 1 ? '审核' : '批量审核'"
        width="500px"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="审核结果" prop="auditResult">
                <el-radio-group v-model="form.auditResult">
                    <el-radio :label="2">通过</el-radio>
                    <el-radio :label="3">拒绝</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="handleCancel">取消</el-button>
                <el-button
                    type="primary"
                    :loading="loading"
                    @click="handleConfirm"
                >
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import { ElMessage } from 'element-plus';
    import request from '@/utils/request';

    const visible = ref(false);
    const loading = ref(false);
    const formRef = ref(null);

    // 表单数据
    const form = reactive({
        auditResult: 2 // 默认选择通过
    });

    // 表单验证规则
    const rules = {
        auditResult: [
            { required: true, message: '请选择审核结果', trigger: 'change' }
        ]
    };

    // 选中的记录ID列表
    const selectedIds = ref([]);

    // 打开弹窗
    const open = (ids = []) => {
        if (!ids || ids.length === 0) {
            ElMessage.warning('请先选择要审核的记录');
            return;
        }
        selectedIds.value = ids;
        form.auditResult = 2; // 重置为默认值
        visible.value = true;
    };

    // 取消
    const handleCancel = () => {
        visible.value = false;
        formRef.value?.resetFields();
    };

    // 确认审核
    const handleConfirm = async () => {
        try {
            await formRef.value?.validate();

            loading.value = true;

            const params = {
                idList: selectedIds.value,
                auditResult: form.auditResult
            };

            const res = await request.post(
                '/order/orderCompensationLog/audit',
                params
            );

            if (res.data.code === 200) {
                const resultText = form.auditResult === 2 ? '通过' : '拒绝';
                const actionText =
                    selectedIds.value.length === 1 ? '审核' : '批量审核';
                ElMessage.success(`${actionText}${resultText}成功`);
                visible.value = false;
                // 触发父组件刷新
                emit('success');
            } else {
                ElMessage.error(res.data.msg || '审核失败');
            }
        } catch (error) {
            console.error('批量审核失败:', error);
            ElMessage.error('批量审核失败');
        } finally {
            loading.value = false;
        }
    };

    // 定义事件
    const emit = defineEmits(['success']);

    // 暴露方法给父组件
    defineExpose({
        open
    });
</script>

<style scoped>
    .dialog-footer {
        text-align: right;
    }
</style>
