<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="描述" prop="title">
				<el-input v-model="form.title" placeholder="计划任务标题" clearable></el-input>
			</el-form-item>
			<el-form-item label="执行类" prop="handler">
				<el-select v-model="form.type">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="执行内容" prop="content">
				<el-input v-model="form.content" placeholder="计划任务执行内容" clearable  type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="定时规则" prop="schedule">
				<x-cron v-model="form.schedule" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts"></x-cron>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-radio-group v-model="form.status">
				    <el-radio :label="1">开启</el-radio>
				    <el-radio :label="0">关闭</el-radio>
				  </el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import xCron from '@/components/xCron';
	
	export default {
		components: {
			xCron
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增计划任务',
					edit: '编辑计划任务'
				},
				options: [
					{value: 'url', label:'访问URL'},
					{value: 'sql', label:'执行SQL'},
					{value: 'shell', label:'执行Shell'},
					{value: 'hook', label:'执行本系统函数或钩子'},
				],
				form: {
					title: "",
					type: "",
					content: "",
					schedule: "",
					status: 1
				},
				rules: {
					title:[
						{required: true, message: '请填写标题'}
					],
					type:[
						{required: true, message: '请选择执行类'}
					],
					content:[
						{required: true, message: '请填写执行内容'}
					],
					schedule:[
						{required: true, message: '请填写定时规则'}
					]
				},
				visible: false,
				isSaveing: false,
				shortcuts: [
					{
						text: "每天8点和12点",
						value: "0 8,12 * * *"
					}
				]
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate((valid) => {
					if (valid) {
						this.isSaveing = true;
						this.$emit('success', this.form, this.mode)
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form = Object.assign({}, data);
			}
		}
	}
</script>

<style>
</style>
