<!-- 部门管理 -->
<template>
	<div class='departmentManage'>
		<div style="border: 1px solid #dedede;height: 100%;">
			<el-row :gutter="0" style="height: 100%;">
				<el-col :span="4">
					<div class="leftTreeTitle">部门列表</div>
					<div class="leftTree">
						<el-tree :data="data" @current-node-key="currentDepartment" :default-expand-all="true" :expand-on-click-node="false" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
					</div>
				</el-col>
				<el-col :span="20" style="height: 100%;">
					<div style="border-left: 1px solid #dedede;height: 100%;">
						<div style="float: right;margin: 20px 30px 100px 30px;">
							<el-button type="primary" icon="el-icon-plus" @click="addDepartment('departmentForm')">新增</el-button>
						</div>
						<div class="details" v-show="showDetail === true">
							<div style="padding: 35px 100px 20px 100px;">
								<el-form :model="departmentForm" :rules="rules" ref="departmentForm" label-width="100px" class="demo-ruleForm">
									<el-form-item label="当前选择部门">
										<span>{{currentDepartment.name}}</span>
									</el-form-item>
									<el-form-item label="部门名称" prop="name">
										<el-input v-model="departmentForm.name"></el-input>
									</el-form-item>
									<!--<el-form-item label="部门等级" prop="level">-->
										<!--<el-input v-model="departmentForm.level"></el-input>-->
									<!--</el-form-item>-->
									<el-form-item label="部门描述" prop="description">
										<el-input type="textarea" v-model="departmentForm.description"></el-input>
									</el-form-item>
								</el-form>
								<div style="text-align: center;">
									<el-button type="primary" v-show="!isShowDelete" @click="submitForm('departmentForm')">保存</el-button>
									<el-button type="primary" v-show="isShowDelete" @click="editSubmitForm('departmentForm')">保存</el-button>
									<el-button v-show="isShowDelete" @click="deleteDepartment('departmentForm')">删除</el-button>
								</div>
							</div>
						</div>
					</div>
				</el-col>
			</el-row>
		</div>
	</div>
</template>

<script>
	//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
	//例如：import 《组件名称》 from '《组件路径》';

	export default {
		name: "departmentMAnage",
//import引入的组件需要注入到对象中才能使用
		components: {},
		data () {
//这里存放数据
			return {
				defaultProps: {
					children: 'departments',
					label: 'name'
				},
				data: [],
				departmentForm: {
					name: '',
					// level: '',
					description: '',
				},
				rules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					],
					// level: [
					// 	{ required: true, message: '请输入部门等级', trigger: 'blur' }
					// ],
					description: [
						{ required: true, message: '请输入部门描述', trigger: 'blur' }
					]
				},
				showDetail: false,
				currentDepartment: '', // 当前
				authorityLists: [],
				isShowDelete: false,
			};
		},
//监听属性 类似于data概念
		computed: {},
//监控data中的数据变化
		watch: {},
//方法集合
		methods: {
			postDepartment() {
				this.$axios.postDepartmentList().then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						this.data = res.data;
					}
				})
			},
			handleNodeClick(data) {
				console.log(data);
				this.showDetail = true;
				this.isShowDelete = true;
				this.departmentForm = JSON.parse(JSON.stringify(data));
				// this.departmentForm.name = data.name;
				// // this.departmentForm.level = data.level;
				// this.departmentForm.description = data.description;
				this.currentDepartment = JSON.parse(JSON.stringify(data));
			},
			addDepartment(formName) {
				this.showDetail = true;
				this.isShowDelete = false;
				let obj = {
					name: '',
					description: ''
				}
				this.departmentForm = obj;
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						console.log(this.departmentForm)
						let data = JSON.parse(JSON.stringify(this.departmentForm));
						data.parentId = this.currentDepartment.id
						console.log(data)
						this.$axios.postAddDepartment(data).then((res) => {
							console.log(res);
							if (res.request.status === 200) {
								this.$message({
									message: '新增部门成功!',
									type: 'success'
								});
								this.postDepartment();
								this.showDetail = false;
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			editSubmitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						console.log(this.departmentForm)
						this.$axios.putDepartmentList(this.departmentForm).then((res) => {
							console.log(res);
							if (res.request.status === 200) {
								this.$message({
									message: '新增部门成功!',
									type: 'success'
								});
								this.postDepartment();
								this.showDetail = false;
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			deleteDepartment(formName) {
				console.log(this.currentDepartment)
				this.$axios.deleteDepartmentList(this.currentDepartment.id).then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						this.$message({
							message: '删除部门成功!',
							type: 'success'
						});
						this.postDepartment();
						this.showDetail = false;
					}
				})
				this.$refs[formName].resetFields();
			}
		},
//生命周期 - 创建完成（可以访问当前this实例）
		created () {

		},
//生命周期 - 挂载完成（可以访问DOM元素）
		mounted () {
			this.postDepartment()
		},
		beforeCreate () {
		}, //生命周期 - 创建之前
		beforeMount () {
		}, //生命周期 - 挂载之前
		beforeUpdate () {
		}, //生命周期 - 更新之前
		updated () {
		}, //生命周期 - 更新之后
		beforeDestroy () {
		}, //生命周期 - 销毁之前
		destroyed () {
		}, //生命周期 - 销毁完成
		activated () {
		}, //如果页面有keep-alive缓存功能，这个函数会触发
	}
</script>
<style scoped>
	.departmentManage{
		height: 94%;
		margin: 20px 100px;
	}
	.leftTreeTitle{
		height: 60px;
		text-align: center;
		border-bottom: 1px solid #dedede;
		line-height: 60px;
		font-size: 30px;
	}

	.details{
		clear: both;
		margin: 40px 100px;
		border: 1px solid #dedede;
	}
</style>
