<template>
	<div class="predict-classify-dialog">
		<el-dialog
		:visible.sync="isOpenDialog"
		:close-on-click-modal="false"
		:modal-append-to-body='false'
		:title="title"
		@close="cancelHandle"
		custom-class="dialog"
		center
		width="560px"
		v-dialogDrag>
			<div class="dialog-main">
				<el-form
				ref="diaForm"
				label-position="left"
				hide-required-asterisk
				label-width="80px"
				:model="formData"
				:rules="formRules">
				
				<!-- 添加一级目录的子分类/编辑二级目录时显示 -->
				<template v-if="(title=='添加'&&formData.Level >= 1)||(title=='编辑'&&formData.Level >= 2)">
					<el-form-item label="一级目录" prop="level_1_Name">
						<span>{{formData.level_1_Name}}</span>
					</el-form-item>
				</template>
				<!-- 添加二级目录的子分类/编辑三级目录时显示 -->
				<template v-if="(title=='添加'&&formData.Level === 2)||(title=='编辑'&&formData.Level === 3)">
					<el-form-item label="二级目录" prop="level_2_Name">
						<span>{{formData.level_2_Name}}</span>
					</el-form-item>
				</template>

				<el-form-item label="分类名称" prop="classify_name">
					<el-input
					v-model="formData.classify_name"
					style="width: 80%"
					placeholder="必填项"></el-input>
				</el-form-item>
				</el-form>
			</div>
			<div class="dia-bot">
				<el-button type="primary" style="margin-right:20px" @click="saveHandle">保存</el-button>
				<el-button type="primary" plain @click="cancelHandle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import * as preDictEdbInterface from '@/api/modules/predictEdbApi.js';
export default {
	props: {
		isOpenDialog: {
			type: Boolean,
		},
		//标题
		title: {
			type: String,
			default: '添加'
		},
		form: {
			type: Object,
		}
	},
	watch: {
		'isOpenDialog': {
			handler(newval) {
        if(newval) {
          this.formData = {...this.form};
        }
			}
		}
	},
	data () {
		return {			
			formData: {},
      formRules: {
        classify_name:[
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
        ]
      },

		};
	},
	methods: {
		async saveHandle() {
			await this.$refs.diaForm.validate()
			const { classify_name,classify_id,Level,ParentId}  = this.formData;
			const { Ret,Msg } = this.title==='添加' 
				? await preDictEdbInterface.classifyAdd({ ClassifyName:classify_name,ParentId,Level})
				: await preDictEdbInterface.classifyEdit({ ClassifyName:classify_name, ClassifyId:classify_id  })
				
			if( Ret !== 200) return
			this.$message.success(Msg);

			this.cancelHandle();
			this.$emit('successCallback')
		},

		/* 取消 */
		cancelHandle() {
      this.formData = {}
			this.$refs.diaForm.resetFields();
			this.$emit('update:isOpenDialog',false)
		},

	},
	created() {},
	mounted() {},
}
</script>
<style lang='scss'>
.predict-classify-dialog {
	.dialog-main {
		padding-left: 50px;
	}
	.el-cascader .el-input {
		width: 100%;
	}
	.dia-bot {
		margin: 52px 0 30px;
		display: flex;
		justify-content: center;

	}
}
</style>