<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" :width="500" @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="90px">
			<el-form-item label="岗位名称:" prop="name">
				<el-input v-model="form.name" placeholder="岗位名称"></el-input>
			</el-form-item>
			<el-form-item label="排序:" prop="jobSort">
				<el-input-number v-model="form.jobSort" :min="0" :max="999" step-strictly controls-position="right"></el-input-number>
			</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>
	</sc-dialog>
</template>

<script>
	export default {
		emits: ["success", "closed"],

		data() {
			return {
				mode: "add",
				titleMap: {
					add: "新增岗位",
					edit: "编辑岗位"
				},
				visible: false,
				isSaveing: false,

				form: {
					id: null,
					name: "",
					enabled: true,
					jobSort: 999
				},
				rules: {
					name: [{ required: true, message: "请输入岗位名称" }],
					jobSort: [{ required: true, message: "请输入岗位排序" }]
				}
			}
		},

		methods: {
			// 显示
			open(mode = "add") {
				this.mode = mode;
				this.visible = true;
				return this;
			},

			// 表单注入数据
			setData(data) {
				this.form.id = data.id;
				this.form.name = data.name;
				this.form.jobSort = data.jobSort;
			},
			
			// 表单提交方法
			submit() {
				this.$refs.dialogForm.validate(valid => {
					if (valid) {
						this.isSaveing = true;
						this.$API.system.job[this.mode](this.form).then(() => {
							this.isSaveing = false;
							this.$message.success("操作成功");
							this.visible = false;
							this.$emit("success", this.mode);
						}).catch(() => this.isSaveing = false);
					} else {
						return false;
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.el-form {
  padding: 10px 30px;
}

@media (max-width: 992px) {
  .el-form {
    padding: 10px;
  }
}
</style>