var grid,bool=false;
var fieldsdata = {},changeid=0,win;
function clicksetwhere(){
	var a = grid.changedata;
	wherewindows.show('工作流['+a.name+']对应表上记录',a.table,'flowset_'+a.num+'', 1);
}
function clicksetfield(){
	var lx=0;
	if(!win){
		var cans = winopt({title: '选择字段',width:400,border:false,
			items:getistess([]),
			buttons:[{
				text:'确定',icon:gicons('ok'),handler:quesngsho
			},{
				text:'取消',icon:gicons('cancel'),handler:function(){win.close()}
			}]
		});
		win = Ext.create('Ext.Window',cans);
		lx = 1;
	}
	if(lx==1){
		grid.destroypanel.push(win);
	}
	changeid = grid.changedata.id;
	if(!fieldsdata[changeid])fieldsdata[changeid]=grid.changedata.fields;
	win.show();
	win.setTitle('选择['+grid.changedata.name+']字段');
	getfielshoe();
}
function quesngsho(){
	if(bool)return;
	var a = getcmp('itemsel_'+rand+'');
	var val = a.getValue().join(',');
	fieldsdata[changeid] = val;
	js.msg('wait','保存中...');
	bool = true;
	$.post(js.getajaxurl('savefields',mode,dir),{id:changeid,fields:val},function(){
		js.msg('success','保存成功');
		bool=false;
	});
}
function getistess(a){
	return {
		xtype:'itemselector',
		height:300,
		id:'itemsel_'+rand+'',
		store: a,
		fromTitle: '可选字段',
		toTitle: '已选择'
	};
}

var _olidtabl;
function getfielshoe(){
	var table = grid.changedata.table;
	if(_olidtabl == table){
		getcmp('itemsel_'+rand+'').setValue(fieldsdata[changeid]);
		return;
	}	
	$.post(js.getajaxurl('getfields','public'),{table:table,atype:0},function(da){
		var d = js.decode(da);
		win.removeAll();
		win.add(getistess(d));
		var fields = fieldsdata[changeid];
		if(isempt(fields))fields='';
		var a = getcmp('itemsel_'+rand+'');
		a.setValue(fields);
	});
	_olidtabl = table;
}

function clickpipei(){
	var sid = grid.getSelectValue();
	if(sid==''){
		js.msg('msg','没有选择行')
		return;
	}
	getcmp('msg_'+rand+'').setText(js.getmsg('匹配中...', '#ff6600'));
	$.post(js.getajaxurl('pipei',mode,dir),{id:sid},function(da){
		getcmp('msg_'+rand+'').setText(js.getmsg(da,'#000000'));
	});
}
function setcoursename(){
	var da = grid.changedata;
	rockoption.setlist('['+da.name+']进程名称', 'flowcoursename_'+da.id);
}

function btns(bo){
	getcmp('setwheres_'+rand+'').setDisabled(bo);
	getcmp('setwhere_'+rand+'').setDisabled(bo);
	getcmp('setcourse_'+rand+'').setDisabled(bo);
}

var panel = {
	xtype:'rockgridform',tablename:'flow_set',formtitle:'工作流配置',searchtools:false,
	checkcolumns:true,delbool:false,
	clickgrid:function(){
		btns(false);
	},
	dblclick:function(o,v){
		var a = v.raw;
		addtabs('['+a.name+']进程管理','flow,course,setid='+a.id+',table='+a.table+',index='+index+'','flowset'+a.id+'');
	},
	tbar:[{
		text:'字段设置',icon:gicons('table_gear'),id:'setwheres_'+rand+'',handler:clicksetfield,disabled:true
	},'-',{
		text:'设置条件',handler:clicksetwhere,id:'setwhere_'+rand+'',disabled:true
	},'-',{
		text:'设置进程名称',handler:setcoursename,id:'setcourse_'+rand+'',disabled:true
	},'-',{
		text:'工作流匹配',handler:clickpipei
	},'双击编辑管理流程进程',{
		xtype:'tbtext',id:'msg_'+rand+''
	}],
	columns:[{
		xtype: 'rownumberer',
		width: 40
	},{
		text:'编号',dataIndex:'num',width:100
	},{
		text:'名称',dataIndex:'name',width:150
	},{
		text:'对应表',dataIndex:'table',width:100
	},{
		text:'对应菜单',dataIndex:'menunum',width:100
	},{
		text:'序号',dataIndex:'sort',width:80
	},{
		text:'摘要',align:'left',dataIndex:'summary',flex:1
	},{
		text:'ID',dataIndex:'id',width:60
	}],
	formparams:{
		submitfields:'name,num,sort,table,uidfields,statusfields,menunum,summary',width:500,
		params:{int_filestype:'sort',otherfields:'optdt={now}'},
		items:[{
			fieldLabel:'id号',value:'0',name:'idPost',hidden:true
		},{
			fieldLabel:''+bitian+'编号',name:'numPost',allowBlank: false
		},{
			fieldLabel:''+bitian+'名称',name:'namePost',allowBlank: false
		},{
			fieldLabel:'序号',name:'sortPost',value:'0',minValue:0,xtype:'numberfield'
		},{
			fieldLabel:'对应的表',name:'tablePost'
		},{
			fieldLabel:'人员字段',name:'uidfieldsPost'
		},{
			fieldLabel:'状态字段',name:'statusfieldsPost'
		},{
			fieldLabel:'菜单编号',name:'menunumPost'
		},{
			fieldLabel:'摘要',name:'summaryPost',xtype:'textareafield',height:60
		}]
	}
};


return {
	panel:panel,
	init:function(){
		grid = rock[index];
	}
};