dst.defineClass("com.dst.slms.plan.projectyearbudget.projectYearBudgetEdit", dst.framework.coreBillBaseEdit, {
	initalizeDOM:function(){
		com.dst.slms.plan.projectyearbudget.projectYearBudgetEdit.superClass.initalizeDOM.call(this);
		
		
		var billId = $('#id').val();
		var oldId = $('#oldId').val();
		var _self = this;
		$('#editTreeGrid').treegrid({
			url:'getAccountEntryData?id='+billId+'&oldId='+oldId,    
		    idField:'id',    
		    treeField:'name',    
		    cls:'datagrid-simple',
			singleSelect:true,
			rownumbers:true,
			striped:true,
			fit:true,
			editorHeight:30,
			title:'科目年度预算',
			rowStyler: function(row,index){
				if(!row.isLeaf){
					return 'background-color:#cbcccd;';
				}
			},
		    columns:[[    
		        {title:'id',field:'id',hidden:true},    
		        {field:'name',title:'科目名称',width:220},    
		        {field:'number',title:'科目编码',width:120},    
		        {field:'amount',title:'合计金额',width:120,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)}},    
		        {field:'one',title:'1月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'two',title:'2月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'three',title:'3月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'four',title:'4月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'five',title:'5月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'six',title:'6月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'seven',title:'7月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'eight',title:'8月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'nine',title:'9月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'ten',title:'10月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'eleven',title:'11月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		        {field:'twelve',title:'12月*',width:100,align:'right',formatter:function(value,rec,index){return _self.fmt_Amount(value)},editor:{type:'numberbox',options:{prompt:'请输入金额',precision:2,groupSeparator:',',min:-9999999999999,max:9999999999999}}},    
		    ]],
		    onClickCell:function(field,row){
		    	if('ADDNEW' != _self.getOperateState() && 'EDIT' != _self.getOperateState()){
		    		return;
		    	}
		    	if(_self.gridEditId){
		    		$('#editTreeGrid').treegrid('endEdit',_self.gridEditId);
		    		_self.cacl_runAmount();
		    	}
		    	if(field == 'amount' ){
		    		return;
		    	}
		    	if(!row.isLeaf){
		    		return;
		    	}
		    	$('#editTreeGrid').treegrid('beginEdit',row.id);
		    	_self.gridEditId = row.id;
		    },
		    onLoadSuccess:function(data){
		    	_self.cacl_runAmount();
		    }
		});
		
		$('#saleTarget').numberbox({    
		    onChange:function(newValue,oldValue){
				var saleTarget = newValue;      // 预算金额
				var rows = $('#editTreeGrid').treegrid('getData');
				for (var v = 0; v < rows.length; v++) {
					row = rows[v];
					var budget = row.amount;
					if(saleTarget != undefined && saleTarget!=0){
						var rate = Number(budget)/Number(saleTarget);
						$("#rate").numberspinner("setValue",rate*Number(100));
					}else{
						$("#rate").numberspinner("setValue","");
					}
				}
		    } 
		});
		
	},
	
	cacl_runAmount:function(){
		var rows = $('#editTreeGrid').treegrid('getData');
		var row = null;
		for (var v = 0; v < rows.length; v++) {
			row = rows[v];
			this.calAmount(row);
		}
		if(row && row.children){
			var amount = 0;
			var one = 0;
			var two = 0;
			var three = 0;
			var four = 0;
			var five = 0;
			var six = 0;
			var seven = 0;
			var eight = 0;
			var nine = 0;
			var ten = 0;
			var eleven = 0;
			var twelve = 0;
			
			for (var o = 0; o < row.children.length; o++) {
				var ro = row.children[o];
				amount = Number(amount) + Number(ro.amount);
				one = Number(one) + Number(ro.one);
				two = Number(two) + Number(ro.two);
				three = Number(three) + Number(ro.three);
				four = Number(four) + Number(ro.four);
				five = Number(five) + Number(ro.five);
				six = Number(six) + Number(ro.six);
				seven = Number(seven) + Number(ro.seven);
				eight = Number(eight) + Number(ro.eight);
				nine = Number(nine) + Number(ro.nine);
				ten = Number(ten) + Number(ro.ten);
				eleven = Number(eleven) + Number(ro.eleven);
				twelve = Number(twelve) + Number(ro.twelve);
			}
			$('#editTreeGrid').treegrid('update',{id:row.id,row:{amount:amount,one:one,two:two,three:three,four:four,five:five,
				six:six,seven:seven,eight:eight,nine:nine,ten:ten,eleven:eleven,twelve:twelve}});
		}
		
		var saleTarget = $('#saleTarget').numberspinner('getValue');      // 销售目标
		for (var v = 0; v < rows.length; v++) {
			row = rows[v];
			var budget = row.amount;
			if(saleTarget != undefined && saleTarget!=0){
				var rate = Number(budget)/Number(saleTarget);
				$("#rate").numberspinner("setValue",rate*Number(100));
			}else{
				$("#rate").numberspinner("setValue","");
			}
		}
	},
	/**
	 * 逐级汇总父节点
	 */
	calAmount:function(rows){
		for (var v = 0; v < rows.children.length; v++) {
			var row = rows.children[v];
			if(row.children){
				var amount = 0;
				var one = 0;
				var two = 0;
				var three = 0;
				var four = 0;
				var five = 0;
				var six = 0;
				var seven = 0;
				var eight = 0;
				var nine = 0;
				var ten = 0;
				var eleven = 0;
				var twelve = 0;
				
				for (var o = 0; o < row.children.length; o++) {
					var ro = row.children[o];
					var tolAmt = Number(ro.one) + Number(ro.two) + Number(ro.three) + Number(ro.four) + Number(ro.five) + Number(ro.six) 
					+ Number(ro.seven) + Number(ro.eight) + Number(ro.nine) + Number(ro.ten) + Number(ro.eleven) + Number(ro.twelve);
					$('#editTreeGrid').treegrid('update',{id:ro.id,row:{amount:tolAmt}});
					
					amount = Number(amount) + Number(ro.amount);
					one = Number(one) + Number(ro.one);
					two = Number(two) + Number(ro.two);
					three = Number(three) + Number(ro.three);
					four = Number(four) + Number(ro.four);
					five = Number(five) + Number(ro.five);
					six = Number(six) + Number(ro.six);
					seven = Number(seven) + Number(ro.seven);
					eight = Number(eight) + Number(ro.eight);
					nine = Number(nine) + Number(ro.nine);
					ten = Number(ten) + Number(ro.ten);
					eleven = Number(eleven) + Number(ro.eleven);
					twelve = Number(twelve) + Number(ro.twelve);
				}
				
				var tolAmt = Number(one) + Number(two) + Number(three) + Number(four) + Number(five) + Number(six) 
				+ Number(seven) + Number(eight) + Number(nine) + Number(ten) + Number(eleven) + Number(twelve);
				
				$('#editTreeGrid').treegrid('update',{id:row.id,row:{amount:amount,one:one,two:two,three:three,four:four,five:five,
					six:six,seven:seven,eight:eight,nine:nine,ten:ten,eleven:eleven,twelve:twelve}});
			}
		}
	},
	
	fmt_Amount:function(value){
		var number = Number(value);
    	if(isNaN(number)) {
    		return ''
    	}else{
    		var source = String(parseFloat(value).toFixed(2)).split(".");
    		source[0] = source[0].replace(new RegExp('(\\d)(?=(\\d{3})+$)','ig'),"$1,");
    		return source.join(".");
    	}
	},
	verify: function() {
		if(this.gridEditId){
    		$('#editTreeGrid').treegrid('endEdit',this.gridEditId);
    	}
		this.cacl_runAmount();
		var rows = $('#editTreeGrid').treegrid('getData');
    	if($('#editTreeGrid_JSON').length==0){
    		$('#id').append('<input id="editTreeGrid_JSON" name="editTreeGrid_JSON" type="hidden">');
    	}
		$('#editTreeGrid_JSON').val(encodeURIComponent(JSON.stringify(rows)));
		return true;
	},
	
	project_DataChange:function(newValue,oldValue){
		var projectId = $('#project').textbox('getBizValue').id;
		dst.remoteCall({
			method : "getOrg",
			async:true,  
			url: "annualPaymentPlanController",
			param : {projectId:projectId},
			success : function(data, textStatus) {
				if(data.admin==null){
					$("#org").textbox("setBizValue","");
				}else{
					$("#org").textbox("setBizValue",data.admin);
				}
			}
		});
	},
	/*//可编辑表格值改变事件，可对表格进行值代入、带出操作
	entry_change:function(datagridTarget,target,index,field,newValue,oldValue){
		//获取编辑行数据
		var row = datagridTarget.datagrid('getRows')[index];
		row[field]=newValue;
		var v = {};
		if(field=='one'||field=='two'||field=='three'||field=='four'||field=='five'||field=='six'||field=='senven'||field=='eight'||field=='nine'||field=='ten'||field=='eleven'||field=='twelve'){
			//计算金额
			var a =row["one"]==undefined?0:row["one"];
			var b =row["two"]==undefined?0:row["two"];
			var c =row["three"]==undefined?0:row["three"];
			var d =row["four"]==undefined?0:row["four"];
			var e =row["five"]==undefined?0:row["five"];
			var f =row["six"]==undefined?0:row["six"];
			var g =row["senven"]==undefined?0:row["senven"];
			var h =row["eight"]==undefined?0:row["eight"];
			var i =row["nine"]==undefined?0:row["nine"];
			var j =row["ten"]==undefined?0:row["ten"];
			var k =row["eleven"]==undefined?0:row["eleven"];
			var l =row["twelve"]==undefined?0:row["twelve"];
			var xx= Number(a)+ Number(b)+ Number(c)+ Number(d)+ Number(e)+ Number(f)+ Number(g)+ Number(h)+ Number(i)+ Number(j)+ Number(k)+Number(l);
			v["amount"]=xx;
		}
		//更新行数据
		datagridTarget.datagrid("updateRow",{index:index,row:v});
	},*/
});