<template>
	<el-dialog v-model="state.dialogVisible" width="500px" draggable title="批量排班" 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="number" :rules="[{ required: true, message: '请选择班次', trigger: 'change' }]">
				<el-select-v2
					v-model="state.ruleForm.number"
					filterable
					:options="numberOptions"
					:props="{
						label: 'name',
						value: 'id',
					}"
					placeholder="请选择班次"
					clearable
					class="w100"
				/>
			</el-form-item>
			<el-form-item label="用户" prop="staff" :rules="[{ required: true, message: '请选择用户', trigger: 'change' }]">
				<el-select-v2
					v-model="state.ruleForm.staff"
					filterable
					:options="staffOptions"
					:props="{
						label: 'userName',
						value: 'id',
					}"
					placeholder="请选择用户"
					clearable
					class="w100"
					multiple
				/>
			</el-form-item>
			<el-form-item label="日期格式" prop="radio" :rules="[{ required: true, message: '请选择日期格式', trigger: 'change' }]">
				<el-radio-group v-model="state.ruleForm.radio">
					<el-radio value="1">时间点</el-radio>
					<el-radio value="2">时间段(时间范围)</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item
				label="时间点"
				prop="dateArray"
				:rules="[{ required: true, message: '请选择时间点', trigger: 'change' }]"
				v-if="state.ruleForm.radio === '1'"
			>
				<el-date-picker v-model="state.ruleForm.dateArray" type="date" placeholder="请选择时间点" value-format="YYYY-MM-DD[T]HH:mm:ss" />
			</el-form-item>
			<el-form-item
				label="时间段"
				prop="datesRange"
				:rules="[{ required: true, message: '请选择时间段', trigger: 'change' }]"
				v-if="state.ruleForm.radio === '2'"
			>
				<el-date-picker
					v-model="state.ruleForm.datesRange"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					value-format="YYYY-MM-DD[T]HH:mm:ss"
					end-placeholder="结束日期"
					:shortcuts="shortcuts"
				/>
			</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 { batchScheduling, getWorkforceClassList, getWorkforceList } from '@/api/system/workforce';
import { shortcuts } from '@/utils/constants';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		radio: '1',
	},
});
const loading = ref<boolean>(false); // 确定按钮loading
// 打开弹窗
const staffOptions = ref<any[]>([]); // 员工列表
const numberOptions = ref<any[]>([]); // 班次列表
const openDialog = async () => {
	try {
		const [staff, number] = await Promise.all([
			getWorkforceList({ PageSize: 999999, PageIndex: 1 }),
			getWorkforceClassList({ PageSize: 999999, PageIndex: 1 }),
		]);
		staffOptions.value = staff.result?.items ?? [];
		numberOptions.value = number.result?.items ?? [];
	} catch (e) {
		console.log(e);
	}
	state.ruleForm = {
		radio: '1',
	};
	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;
		let request;
		if (state.ruleForm.radio === '1') {
			request = {
				userIds: state.ruleForm.staff,
				shiftId: state.ruleForm.number,
				schedulingTime: state.ruleForm.dateArray,
			};
		} else {
			request = {
				userIds: state.ruleForm.staff,
				shiftId: state.ruleForm.number,
				schedulingStartTime: state.ruleForm.datesRange[0],
				schedulingEndTime: state.ruleForm.datesRange[1],
			};
		}
		batchScheduling(request)
			.then(() => {
				ElMessage.success('批量排班成功');
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.finally(() => {
				loading.value = false;
				closeDialog();
				emit('updateList');
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
