<template>
	<el-dialog v-model="state.dialogVisible" width="500px" draggable title="修改甄别提起时限" @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef">
			<el-form-item label="甄别提起时限" prop="screenByEndTime" :rules="[{ required: true, message: '请选择甄别提起时限', trigger: 'change' }]">
				<el-date-picker
					v-model="state.ruleForm.screenByEndTime"
					type="datetime"
					placeholder="请选择甄别提起时限"
					value-format="YYYY-MM-DD[T]HH:mm:ss"
					class="w100"
					:disabled-date="disabledDate"
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { discernUpdateTime } from '@/api/business/discern';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		screenByEndTime: null, // 时间
		id: null,
	},
});

const disabledDate = (time: Date) => {
	return time.getTime() <= Date.now() - 8.64e7;
};
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = (val: any) => {
	state.ruleForm.id = val.id ?? '';
	state.dialogVisible = true;
	loading.value = false;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		discernUpdateTime(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				emit('updateList');
				closeDialog();
				loading.value = false;
			})
			.catch((error) => {
				console.log(error);
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
