<template>
	<div>
		<el-row :gutter="5">
			<el-col :span="5">
				<div>
				<el-tree v-loading="loadingTree" :data="dataTree" :props="defaultProps" node-key="id" @node-click="handleNodeClick" default-expand-all class="tree-container"
				:expand-on-click-node="false">
				</el-tree>
				</div>
			</el-col>
					<el-button size="small" type="primary" @click="handleAdd">新增</el-button>
			<el-col :span="19">
				<div style="padding-right: 30px;">
					<el-table v-loading="loading" :data="tableData" border>
						<el-table-column label="部门列表">
						<el-table-column prop="id" label="编号" width="50" v-if="isShow">
						</el-table-column>
						<el-table-column prop="deptName" label="部门名称" width="120" align="center">
						</el-table-column>
						<el-table-column prop="deptDesc" label="部门说明" width="120" align="center">
						</el-table-column>
						<el-table-column prop="fId" label="父id" width="77" align="center">
						</el-table-column>
						<el-table-column label="操作" align="center">
							<template slot-scope="scope">
								<el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
								<el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
						</el-table-column>
					</el-table>
				</div>
			</el-col>
		</el-row>
		<el-dialog title="编辑信息" :visible.sync="dialogVisible" width="500px">
			<el-form :model="form" label-width="100px">
				<el-form-item label="用户编码" v-if="isShow">
					<el-input v-model="form.id"></el-input>
				</el-form-item>
				<el-form-item label="上级部门">
					<el-input v-model="this.fName"></el-input>
				</el-form-item>
				<el-form-item label="部门名称">
					<el-input v-model="form.deptName"></el-input>
				</el-form-item>
				<el-form-item label="部门说明">
					<el-input v-model="form.deptDesc"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="success" @click="handleSave">保存</el-button>
					<el-button @click="handleCancelSave">取消</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>

	</div>
</template>
<script>
	import axios from '@/axios';
	export default {
		data() {
			return {
				dataTree: [],
				defaultProps: {
					children: 'children',
					label: 'text'
				},
				tableData: [],
				fid: '',
				fName: '',
				//录入框
				dialogVisible: false,
				form: {
					id: '',
					deptName: '',
					deptDesc: ''
				},
				isShow: false,
				loading: true,
				loadingTree: true,
			}
		},
		mounted() {
			this.getTableData()
			this.getDataTree()
		},
		methods: {
			// 获取table数据
			getDataTree() {
				//console.log("获取table数据getDataTree")
				var url = 'SysDepartment/queryTree.do?fid=0'
				var param = {}
				axios.get(url, param).then(response => {
					this.dataTree = response.data
					if(response){
						this.loadingTree=false
					} else{
						this.loadingTree=true
					}
				});
			},
			handleNodeClick(data) {
				console.log(data.id)
				this.fid = data.id
				this.fName = data.text
				this.getTableData()
			},
			getTableData() {
				//console.log("获取table数据getTableData")
				var url = 'SysDepartment/queryByFid.do'
				var param = {
					fid: this.fid
				}
				axios.post(url, param).then(response => {
					this.tableData = response.data
					if(response){
						this.loading=false
					} else{
						this.loading=true
					}
				});
			},

			handleAdd() {
				this.dialogVisible = true
				fName: this.fName
			},
			// 编辑按钮事件
			handleEdit(row) {
				this.dialogVisible = true
				this.form = {
					id: row.id,
					fid: this.fid,
					fName: this.fName,
					deptName: row.deptName,
					deptDesc: row.deptDesc
				}
			},
			//保存按钮事件
			handleSave(row) {
				var url = 'SysDepartment/save.do'
				var userId = sessionStorage.getItem('userId');
				var param = {
					id: this.form.id,
					fid: this.fid,
					deptName: this.form.deptName,
					deptDesc: this.form.deptDesc,
					userId: userId,
				}
				axios.post(url, param).then(response => {
					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '修改成功!',
						});
						this.getTableData()
						this.getDataTree()
						this.dialogVisible = false
					} else {
						this.$message({
							type: 'error',
							message: '修改失败' + response.data
						});
					}
				});
			},
			//保存取消按钮事件   关闭编辑对话框
			handleCancelSave(row) {
				this.dialogVisible = false
			},
			// 删除按钮事件
			handleDelete(id) {
				this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					var userId = sessionStorage.getItem('userId');
					var url = 'SysDepartment/remove.do'
					var param = {
						id: id,
						userId: userId
					}
					axios.post(url, param).then(response => {
						if (response.data === 'success') {
							this.$message({
								type: 'success',
								message: '删除成功!',
							});
							this.getTableData()
							this.getDataTree()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败' + response.data
							});
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		}
	};
</script>
<style>
