new Vue({
	el:"#configPage",
		data: function(){
	  		return {
	  			form: {
	  				appCode: '',
	  				packageName: '',
	  				dataSourceName: '',
	  				dataSources: [],
	  				tableNames: [],
	  				tableName: '',
	  				className: '',
	  				parentName: '',
	  				author:'',
	  				auditData:'false',
	  				propertys:[
	  					{
	  						label: '',
	  						name: '',
	  						type: '',
	  						length: '',
	  						required: '',
	  						conditionType: '',
	  						pageQueryProperty: '',
	  						pageSortProperty: '',
	  						dictionariesType: ''
	  					}
	  				],
	  				tableProps:{
	  					label: 'name',
	  					children: 'tables'
	  				}
	  			},
	  			uploadId: '',
	  			yesNos: [
	  			         {value:'1', label:'是'},
	  			         {value:'0', label:'否'}
	  			],
	  			conditionTypes: [
	  			         {value:'EQ', label:'等于'},
	  			         {value:'LK', label:'包含'}
	  			],
	  			sortType: [
	  			         {value:'1', label:'默认排序'},
	  			         {value:'2', label:'支持排序'},
	  			         {value:'3', label:'不支持排序'}
	  			],
	  			propertyTypes: [
	  			         {value:'String', label:'String'},
	  			         {value:'Integer', label:'Integer'},
					     {value:'Long', label:'Long'},
	  			         {value:'Double', label:'Double'},
	  			         {value:'Date', label:'Date'},
	  			         {value:'Boolean', label:'Boolean'},
	  			         {value:'Text', label:'文本域'}
	  			],
	  			rules: {
	  				packageName: [{ required: true, message: '请输入包名', trigger: 'blur' }],
	  				dataSourceName: [{ required: true, message: '请输入数据源', trigger: 'blur' }],
	  				tableName: [{ required: true, message: '请输入表名', trigger: 'blur' }],
	  				parentName: [{ required: true, message: '请输入继承类名', trigger: 'blur' }],
	  				className: [{ required: true, message: '请输入类名', trigger: 'blur' }]
	  	        },
	  		};
	  	},
	  	mounted: function(){
	  		this.$http.get("/entitygenerator/dataSources").then(res=>{
	  			if(res.data.code == "200"){
                     console.log(res.data.data);
	  				this.form.dataSources = res.data.data;
	  			}
	  		});
		},
		methods:{
  	     	save: function(formName){
 	  	     	 this.$refs[formName].validate((valid) => {
 			     	if (valid) {
 			     		var defaultSortCount = 0;
 			     		var propertyNames = [];
 			     		for(var i=0;i<this.form.propertys.length;i++){
 			     			if(this.form.propertys[i].pageSortProperty == '1'){
 			     				defaultSortCount++;
 			     			}
 			     			propertyNames.push(this.form.propertys[i].name);
 			     		}
 			     		var nameIsRight = true;
 			     		let orderedProperties=propertyNames.slice().sort();
 			     		for(let i=0;i<orderedProperties.length;i++){
 			     		    if (orderedProperties[i]==orderedProperties[i+1]){
 			     		    	nameIsRight = false;
 			     				break;
 			     		    }
 			     		}
 			     		if(!nameIsRight){
 			     			this.$message({
		       	     	    	type: 'warning',
		       	     	    	message: "属性不能重复！"
	 			        	});
 			     			return;
 			     		}
 			     		if(defaultSortCount == 0){
 			     			this.$message({
		       	     	    	type: 'warning',
		       	     	    	message: "请指定一个默认排序属性！"
	 			        	});
 			     			return;
 			     		}
 			     		if(defaultSortCount > 1){
 			     			this.$message({
		       	     	    	type: 'warning',
		       	     	    	message: "默认排序属性只能有一个，请重新设置！"
	 			        	});
 			     			return;
 			     		}
 			        	this.$http.post("/codegenerator", this.form).then(res=>{
	 			        	if(res.body.code == "200"){
	 			        		this.uploadId = '/codegenerator/export/'+res.body.data;
	 			        		let vm = this;
	 			        		setTimeout(function(){
	 			        			vm.$refs['exportForm'].submit();
	 			        			setTimeout(function(){
	 			        				loading.close();
	 			        			}, 100);
	 			        		},300);
	 			        		return true;
	 			        	}else{
	 			        		this.$message({
			       	     	    	type: 'warning',
			       	     	    	message: "生成失败！"
		 			        	});
	 			        		return false;
	 			        	}
       	     	    	});
 			        } else {
 			            return false;
 			        }
		        });
 	     	},
 	     	addProperty: function(){
 	     		this.form.propertys.push(
 	     				{
	  						label:'',
	  						name:'',
	  						type:'',
	  						length:'',
	  						required:''
	  					}
 	     		);
 	     	},
 	     	deleteProperty: function(index){
 	     		this.form.propertys.splice(index, 1);
 	     	},
 	     	getTables: function(node, resolve){
 	     		if (node.level === 0) {
 	     			return;
 	            }
 	            if (node.level > 1) return resolve([]);
 	     		let dataSource = node.data.name;
 	     		this.$http.get("/entitygenerator/tables/"+dataSource).then(res=>{
 		  			if(res.body.code == "200"){
 		  				resolve(res.body.data);
 		  			}
 		  		});
 	     	},
 	     	getColumns: function(node, data){
 	     		if(!data.isLeaf){
 	     			return;
 	     		}
 	     		this.form.tableName = node.name;
 	     		let dataSource = data.parent.data.name;
 	     		let table = node.name;
 	     		this.$http.get("/entitygenerator/columns/"+dataSource+"/"+table).then(res=>{
 	     			if(res.body.code == "200"){
 	     				let columns = res.body.data;
 	     				for(let i=0; i<columns.length; i++){
 	     					if(columns[i].type=='int' || columns[i].type=='tynyint' || columns[i].type=='smallint'){
 	     						columns[i].type = 'Integer';
 	     					}else if(columns[i].type=='varchar' || columns[i].type=='char'){
 	     						columns[i].type = 'String';
 	     					}else if(columns[i].type=='text'){
 	     						columns[i].type = 'String';
 	     					}else if(columns[i].type=='date' || columns[i].type=='timestamp' || columns[i].type=='datetime'){
 	     						columns[i].type = 'Date';
 	     					}else if(columns[i].type=='double'){
 	     						columns[i].type = 'Double';
 	     					}else if(columns[i].type=='bit' || columns[i].type=='tinyint'){
 	     						columns[i].type = 'Boolean';
 	     					}
 	     					columns[i].conditionType = 'EQ';
 	     					columns[i].pageQueryProperty = '0';
 	     					columns[i].pageSortProperty = '2';
 	     				}
 	     				this.form.propertys = columns;
 	     			}
 	     		});
 	     	}
		
		}
	})