<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="formName">
				<el-input v-model="form.formName" placeholder="请输入表单名称"></el-input>
			</el-form-item>
			<el-form-item label="业务类型:" prop="busType">
				<el-input v-model="form.busType" placeholder="请输入业务类型"></el-input>
			</el-form-item>
			<el-form-item label="表单id:" prop="formId">
				<el-input v-model="form.formId" placeholder="请输入表单id"></el-input>
			</el-form-item>
			<el-form-item label="表单编号:" prop="formCode">
				<el-input v-model="form.formCode" placeholder="请输入表单编号"></el-input>
			</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,
					formName: null,
					busType: null,
					formId: null,
					formCode: null,
				},
				rules: {
					formName: [{ required: true, message: "请输入表单名称" }],
					busType: [{ required: true, message: "请输入业务类型" }],
					formId: [{ required: true, message: "请输入表单id" }],
					formCode: [{ required: true, message: "请输入表单编号" }]
				}
			}
		},

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

			// 表单注入数据
			setData(data) {
				this.form.id = data.id;
				this.form.formName = data.formName;
				this.form.busType = data.busType;
				this.form.formId = data.formId;
				this.form.formCode = data.formCode;
			},
			
			// 表单提交方法
			submit() {
				this.$refs.dialogForm.validate(valid => {
					if (valid) {
						this.isSaveing = true;
						this.$API.system.approveInfo[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>