<template>
	<div class="system-dept-dialog-container">
		<el-dialog
				:title="state.dialog.title"
				v-model="state.dialog.isShowDialog"
				:close-on-click-modal="false"
				width="769px">
			<el-form ref="deptDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称">
							<el-input v-model="state.ruleForm.name" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="表达式">
							<el-input v-model="state.ruleForm.cron" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="执行类名">
							<el-input v-model="state.ruleForm.className" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="执行方式">
							<el-input v-model="state.ruleForm.methodName" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="描述">
							<el-input  autosize type="textarea" v-model="state.ruleForm.describe" placeholder="请输入"></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="执行状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDeptDialog">
import { reactive, ref } from 'vue';
import {regularApi} from "/@/api/regular";
import {ElNotification} from "element-plus";

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const deptDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id:"",
		name: '', // 部门名称
		describe: '', // 负责人
		cron: '', // 手机号
		className: '', // 邮箱
		methodName: 0, // 排序
		status: false, // 部门状态
	},
	deptData: [] as DeptTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowDeptType) => {
	if (type === 'edit') { //修改
		state.ruleForm = Object.assign({}, row); //强类型赋值
		if(row.status == 1){
			state.ruleForm.status = true
		}else {
			state.ruleForm.status = false
		}
		// state.ruleForm.status = row.status === 1 ? 'true':'false';
		state.dialog.title = '修改定时任务';
		state.dialog.submitTxt = '修 改';
		state.dialog.type = type;
	} else {
		state.dialog.title = '新增定时任务';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		state.ruleForm = {}
		state.dialog.type = type
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	const data = {
		name:state.ruleForm.name,
		describe:state.ruleForm.describe,
		cron:state.ruleForm.cron,
		className:state.ruleForm.className,
		methodName:state.ruleForm.methodName,
		status:state.ruleForm.status == true ? '1':'0',
	}
	if (state.dialog.type === 'add') {//新增接口
		let item = await regularApi().addApi(data)
		if (item.code != 200){
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "error",
				duration: 3000,
			});
		}else {
			ElNotification({
				title: "提示",
				message: item.msg,
				type: "success",
				duration: 3000,
			});
		}
	}else {
		let item = await regularApi().putApi({
			...data,
			id:state.ruleForm.id,
		})
		if (item.code != 200){
			 ElNotification({
				title: "提示",
				message: item.msg,
				type: "error",
				duration: 3000,
			});
		}else {
			 ElNotification({
				title: "提示",
				message: item.msg,
				type: "success",
				duration: 3000,
			});
		}
	}
	emit('refresh');
	closeDialog();
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
