<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>组织架构</title>
		<script src="../../../js/vue/vue.js"></script>
		<script src="../../../js/vue/element-ui/lib/index.js"></script>
		<script src="../../../js/myelement.js"></script>
		<script src="../../../js/vue/page.js"></script>
		<script src="../setting.js"></script>
		
		<link href="../../../js/vue/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
		<link href="../../../css/myelement.css" rel="stylesheet">
		<link href="../../../css/iconfont.css" rel="stylesheet">
		<link href="../../../css/page.css" rel="stylesheet">
	</head>
	
	<body>
		<div id="vbody">
			<div id="page_root">
				<!-- <div class="topbar">
					<span>{{title}}</span>
					<div style="float: right; margin-right: 24px;">
						<el-button-group style="margin-left: 3px;">
							<el-button  @click="openTree">角色管理</el-button>
						</el-button-group>
					</div>
				</div> -->
				<div style="width: 100%; height: 100%; padding: 20px; box-sizing: border-box;">
					<div :style="{width: '51%', height: tableHeight2 + 50+'px', float: 'left', border: '1px solid #ccc'}">
						<div v-if="treeData.length > 0" style="height: 50px; border-bottom: 1px solid #aaa;">
							<div class="topbar" style="float: left; line-height: 50px;">
								<span>{{title}}</span>
								<!-- <template v-if="page.isVersionActive">
									<span class="versionNo">版本号: {{currentVersion.label}}</span>
									<h-select :datalist="versionList" v-on:itemselect="onVersionSelect"></h-select>							
								</template> -->
							</div>
							<div style="float: right; margin: 10px;">
								<el-button-group>
									<!-- <el-button v-if="buttons.coverup" @click="onCoverup">封版</el-button>
									<el-button v-if="buttons.download" icon="el-icon-download" @click="onDownload" >导出</el-button>
									<el-upload action="https://jsonplaceholder.typicode.com/posts/" class="z_upload_" style="float: right;">
										<el-button v-if="buttons.upload" size="mini" type="primary" icon="el-icon-upload2" style="display: flex;">导入</el-button> -->
									</el-upload>
								</el-button-group>
								<el-button-group style="margin-left: 3px; ">
									<!-- <el-button icon="el-icon-refresh" @click="onRefreshTree">刷新</el-button>
							
									<el-button v-if="buttons.append" :disabled="buttons.append.disabled"  icon="el-icon-plus"  @click="onNewTreeRecord">添加</el-button>-->
									<el-button v-if="buttons.edit" :disabled="buttons.edit.disabled" @click="isEditTreeData = !isEditTreeData">{{isEditTreeData ? '取消编辑' : '在线编辑'}}</el-button> 
								</el-button-group>
							</div>
						</div>
						<div style=" padding: 20px;">
							<el-input
							  placeholder="输入搜索字"
							  v-model="filterText">
							</el-input>
						</div>
						<div v-if="treeData.length > 0 && istree" :style="{height: tableHeight2 - 68 +'px', padding: '0px 20px 20px 20px', 'box-sizing': 'border-box'}">
							<div :style="{height: tableHeight2 - 80  +'px', 'overflow-y': 'auto', 'font-size': '14px'}">
								<el-tree ref="tree" :data="treeData" :props="defaultProps" node-key="id" 
								:filter-node-method="filterNode" 
								highlight-current
								@node-click="editnode"
								>
									<span class="custom-tree-node" slot-scope="{ node, data }">
										<span>{{ data.md_description }} </span>
										<!-- <span v-if="isEditTreeData">
											<el-button type="danger" size="mini" style="padding: 2px; font-size: 10px;" 
											@click="() => remove(node, data)">删除</el-button>
										</span> -->
									</span>
								</el-tree>
							</div>
						</div>
					</div>
					
					<div v-if="istree" :style="{width: '48%', height: tableHeight2 + 50+'px', float: 'right', border: '1px solid #ccc'}">
						<div style="height: 50px; border-bottom: 1px solid #aaa;">
							<div class="topbar" style="float: left; line-height: 50px;">
								<span>详情</span>
							</div>
							<div v-if="isEditTreeData" style="float: right; margin: 10px;">
								<el-button @click="saveRowNoe">保 存</el-button>
							</div>
						</div>
						<div style="margin: 20px;">
							<h-form
								ref="form2"
								:form-attr="formNodeAttr"
								:table-fields="treeFields"
								:disabled="!isEditTreeData"
								:form-data="formnodeData"
								v-on:show-popup="showPopup_employee"
							>
							</h-form>
							<!--  -->
						</div>
					</div>
				</div>
			</div>
			
			<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 100vh;">
				<div class="spinner">
				  <div class="cube1"></div>
				  <div class="cube2"></div>
				</div>
			</div>
		</div>	
		
		<script type="text/javascript">
			var setting = getSetting("hierarchy_list");
			var initlized = false;
			let id = 1000;
			new ListVue({
				el: "#vbody",
				data: {
					dataname: "hierarchy",
					tabaleFieldsName: "hierarchy_",
					title: "组织架构",
					
					page: setting.page,
					buttons: setting.buttons,
					
					currentVersion: {
						value: "2021-3",
						label: "2021-3"
					},
					versionList: [
					  {
					    value: "2021-3",
					    label: "2021-3"
					  }, 
					  {
					    value: "2021-2",
					    label: "2021-2"
					  },
					  {
					    value: "2021-1",
					    label: "2021-1"
					  }
					],
					
					tableHeight2: 500,
					isEditData:false,
					isEditTreeData: false,
					
					selectFormField:{},
					formNodeAttr: {
						istitle: false,
						columnnumber: 1,
						labelwidth: "200px",
						labelposition: "right",
						align: "left",
						border: "10px solid #c6c6c600"
					},
				
					treeFields: [],
					istree: false,
					treeDataObj: {},
					treeData: [],
					defaultExpandedKeys: [],
					defaultProps: {
						children: 'children',
						label: 'label'
					},
					salesStructure: [],
					filterText: '',
					selectnode: {},
					formnodeData: {},
					 
				},
				created() {
					
				},
				mounted() {
					this.initData();
					this.getTree();
					this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
						hideLoading();
					});
				},
				watch: {
				  filterText(val) {
					this.$refs.tree.filter(val);
				  }
				},
				methods:{
					initData() {
						//1. run only
						if (initlized) { return; }
						initlized = true;
					},
					/* loadNode(node, resolve) {
					   setTimeout(() => {
						 const data = [{
						   name: 'leaf',
						   leaf: true
						 }, {
						   name: 'zone'
						 }];
									
						 resolve(data);
					   }, 500);
					}, */
					onServerInitData(data) {
						var me = this.data;
						me.treeFields = data.tableFields;
						
						if(me.tableFields.length > 0) {
							for(var i=0; i < me.tableFields.length; i++) {
								let fieldObj_ = me.tableFields[i];
								
								me.tableFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}
					},
					getTree() {
						let me = this;
						let filter_ = "1=1 and md_hierarchy.long_code like '01!0101!010102!010102628%'";
						this.filterList = [];
						
						
						let param_ = {
							isClientMode: false,
							dataname: this.dataname,
							filterList: this.filterList,
							filter: filter_,
							orderby: this.orderby,
						}
						Server.call("root/data/getEntitySet", param_, function(result) {
							console.log(result);
							me.total = 0;
							me.saleManor = [];
							if (result && result.data) {
								var data_ = result.data.entityset;
								me.saleManor = data_;
							}
							
							me.defaultExpandedKeys = [];
							me.istree = false;
							let treeDataList = me.saleManor;
							treeDataList.map(e=>{
								me.treeDataObj[e.id] = e;
							});
							let treeData_ = me.ArrayToTree_(treeDataList, "md_description", "parent_code");
							treeData_.map(e=>{
								me.defaultExpandedKeys.push(e.id);
								if (e.children) {
									e.children.map(ec=>{
										
									});
								}
							});
							me.treeData = treeData_;
							me.istree = true;
						});
					},
					ArrayToTree_(array_, name, parentId, parId, disabled_field, isencode) {
						if(!parentId) {
							parentId = "parent_id";
						}
						if(!name) {
							name = "label";
						}
						if(!parId) {
							parId = "";
						}
						
						let obj = {};
						let result = [];
						let list = JSON.parse(JSON.stringify(array_));
						list.map(el => {
							obj[el.md_code] = el;
						})
						let openId = "";
						for (let i = 0, len = list.length; i < len; i++) {
							let parentId_ = list[i][parentId];
							//如果存在判断只读字段,并且该字段有值则设置该节点为只读
							if (disabled_field) {
								if(list[i][disabled_field]) {
									list[i].disabled = true;
								}
							}
							
							//设置显示字段
							if (isencode) {
								list[i].label = decodeURI(encodeURI( list[i][name]));//数据如果带“%”的需要编码再解码,否则会存在格式错误
								list[i].name = decodeURI(encodeURI( list[i][name]));
							}
							else {
								list[i].label = decodeURI(list[i][name]);
								list[i].name = decodeURI(list[i][name]);
							}
							if (parentId_ == "202009290028") {
								if(!obj[list[i].md_code].children) {
								    obj[list[i].md_code].children = null;
								}
								result.push(list[i]);
								continue;
							} else if (obj[parentId_]) {
								if (!obj[parentId_].children) {
									obj[parentId_].children = [];
								}
								obj[parentId_].children.push(list[i]);
							}
						}
						return result;
					},
					
					onNewTreeRecord() {
						//1.
						this.operationtype = "add";
						
						//2. get config and go
						var config = new Config(), go = true;
						
						if (this.onAddTreeData) {
							 go = this.onAddTreeData.call(this, config);
						}
						
						//3. popup
						if (go) {
							this.doPopup(config);
						}
					},
					onAddTreeData(config) {
						var me = this;
						config.combine({
							url: "../md/plan/area_edit.html",
							sceneCode: "add",
							data: {},
							delta:  me.treeFields ? me.treeFields : {},
							callback: function(obj) {
								me.saveRowTree_popup(obj);
							}
						});
						
						return true;
					},
					remove(node, data) {
						let me = this;
						Root.confirm('确定删除-' + data.name + '-吗?', '删除提示', {
						  confirmButtonText: '删除',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							let param = {
								id: data.id,
								isClientMode: false,
							}
							Server.call("root/data/deleteEntity/"+ this.dataname, param, function(result) {
								console.log(result);
								Root.message({
									type: 'success',
									message: '删除成功!'
								});
								me.getTree();
							});
						}).catch(() => {
							Root.message({
								type: 'info',
								message: '已取消删除'
							});          
						});
					},
					editnode(data, node, tree) {
						this.operationtype = "edit";
						this.selectnode = data;
						let formnodeData_ = clone(data);
						var parentname_ = [];
						if (data.parentid) {
							let parents_ = [];
							parentname_.push(data.parentid);
							parents_ = this.getparent(data.parentid, parentname_);
						}
						this.formnodeData = formnodeData_; 
					},
					saveRowTree_popup: function(obj) {
						var me = this;
						var formData_ = obj.row;
						var operationtype_ = this.operationtype;
						
						if (operationtype_ == "add") {//修改
							var param = formData_;
							param.isClientMode = false;
							Server.call("root/data/insertEntity/" + this.dataname, param, function(result) {
								console.log(result);
								me.getTree();
							});
						}
					},
					saveRowNoe() {
						let me = this;
						var formData_ = this.formnodeData;
						var operationtype_ = this.operationtype;
						
						if (operationtype_ == "edit") {//修改
							var paramObj = {};
							for (var k in formData_) {
								paramObj[k] = formData_[k];
							}
							var param = paramObj;
							param.isClientMode = false;
							Server.call("root/data/updateEntity/" + this.dataname, param, function(result) {
								console.log(result);
								me.getTree();
							});
						}
					},
					openTree(){
						//2. get config and go
						var config = new Config(), go = true;
						
						if (this.onTreeData) {
							 go = this.onTreeData.call(this, config);
						}
						//3. popup
						if (go) {
							if (config.totab) {
								var parames = {
									url: config.url,
									sceneCode: config.sceneCode,
									delta: config.delta,
								};
								
								Root.popupParames = parames;
								window.top.tab.open(config);
							}
							else {
								this.doPopup(config);
							}
						}
					},
					onTreeData(config) {
						var me = this;
						config.combine({
							totab: true,
							icon: "icon-product",
							text: "用户管理",
							id: "hierarchy_tree",
							url: "../md/plan/hierarchy_tree.html",
							data: {},
							delta:  me.dataname,
							
						});
						return true;
					},
					showPopup(obj) {
						this.selectFormField = obj.obj;
					},
					onVersionSelect(obj) {
						this.currentVersion = obj;
					},
					onDownload() {
						dealExportByPath("../../../template/hierarchy_list.xlsx","商务架构");
					},
					onCoverup() {
						Root.confirm('确定封版为新2021-3吗?', '封版提示', {
						  confirmButtonText: "封版",
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							Root.message({
								type: 'success',
								message: '封版成功!'
							});
						}).catch(() => {
							Root.message({
								type: 'info',
								message: '已取消'
							});          
						});
					},
					onRefreshTree() {
						this.getTree();
					},
					
					filterNode(value, data) {
						if (!value) return true;
						return data.label.indexOf(value) !== -1;
					},
					
					getparent(parentid, parents) {
						if (this.treeDataObj[parentid].parentid){
							parents.unshift(this.treeDataObj[parentid].parentid);
							this.getparent(this.treeDataObj[parentid].parentid, parents);
						}
						else {
							return parents;
						}
					},
					showPopup_employee(obj){
						let aa = [
							{field: "field11", name: "员工代码", isshow: "T", align: "center", width: "130", required: true},
							{field: "field2", name: "员工姓名", isshow: "T", align: "center", width: "100", required: true},
							{field: "field13", name: "性别", isshow: "T", align: "center",required: true, type:"select", options: [{label: "男",value: "Y"}, {label: "女",value: "N"}]},
							{field: "field14", name: "所属公司", isshow: "T", width: "240", required: true},
							{field: "field1", name: "所属部门", isshow: "T", align: "center"},
							{field: "field16", name: "职务", isshow: "T", align: "center"},
							{field: "field17", name: "E-Mail", isshow: "T", align: "center"},
							{field: "field10", name: "状态", isshow: "T", align: "center", required: true, type:"select", options: [{label: "激活",value: "Y"}, {label: "不激活",value: "N"}]},
						];
						this.selectFormField = obj.obj;
						if (this.selectFormField.field == "field2") {
							let me = this;
							Root.popupParames = {
								width: "900px",
								height: "500px",
								url: "../org/employee/employee_edit.html",
							};
							Root.popupParames.disabled = true;
							Root.popupParames.data =this.formnodeData;
							Root.popupParames.tableFields = aa;
							Root.showPopup(Root.popupParames);
						}
					},
				}
			});
		</script>
		<style>
			.el-dialog__body{
				border-top: 1px solid #ccc;
				border-bottom: 1px solid #ccc;
				padding: 20px;
			}
			.custom-tree-node {
			    flex: 1;
			    display: flex;
			    align-items: center;
			    justify-content: space-between;
			    font-size: 14px;
			    padding-right: 8px;
			  }
		</style>
	</body>
</html>