<template>
	<el-dialog
		v-model="state.dialogVisible"
		width="500px"
		draggable
		:title="dialogTitle"
		ref="dialogRef"
		append-to-body
		destroy-on-close
		@close="close"
	>
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-form-item label="班次名称" prop="name" :rules="[{ required: true, message: '请填写班次名称', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.name" placeholder="请填写班次名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="上班时间" prop="workingTime" :rules="[{ required: true, message: '请选择上班时间', trigger: 'change' }]">
				<el-time-picker v-model="state.ruleForm.workingTime" placeholder="请选择上班时间" class="w100" value-format="HH:mm:ss" />
			</el-form-item>
			<el-form-item label="下班时间" prop="offDutyTime" :rules="[{ required: true, message: '请选择下班时间', trigger: 'change' }]">
				<el-time-picker v-model="state.ruleForm.offDutyTime" placeholder="请选择下班时间" class="w100" value-format="HH:mm:ss" />
			</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 { addWorkforceClass, getWorkforceClassDetail, updateWorkforceClass } from '@/api/system/workforce';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
const dialogTitle = ref<string>('新增班次'); // 弹窗标题
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		name: '', // 班次名称
		workingTime: '', // 上班时间
		offDutyTime: '', // 下班时间
	},
});
const loading = ref<boolean>(false); // 确定按钮loading
const isEdit = ref<boolean>(false); // 是否编辑
// 打开弹窗

const openDialog = async (id: string) => {
	if (id) {
		dialogTitle.value = '编辑班次';
		isEdit.value = true;
		try {
			const { result } = await getWorkforceClassDetail(id);
			state.ruleForm = result;
		} catch (e) {
			console.log(e);
		}
	} else {
		state.ruleForm = {
			name: '', // 班次名称
			workingTime: '', // 上班时间
			offDutyTime: '', // 下班时间
		};
		dialogTitle.value = '新增班次';
		isEdit.value = false;
	}
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const ruleFormRef = ref<RefType>();
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};

// 新增
const onSubmit = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		if (isEdit.value) {
			// 编辑
			updateWorkforceClass(state.ruleForm)
				.then(() => {
					ElMessage({
						message: '编辑成功',
						type: 'success',
					});
					loading.value = false;
					closeDialog();
					emit('updateList');
				})
				.catch(() => {
					// 编辑失败
					loading.value = false;
					closeDialog();
				});
		} else {
			addWorkforceClass(state.ruleForm)
				.then(() => {
					ElMessage({
						message: '新增成功',
						type: 'success',
					});
					loading.value = false;
					closeDialog();
					emit('updateList');
				})
				.catch(() => {
					// 新增失败
					loading.value = false;
					closeDialog();
				});
		}
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
