var gridManagerBtn = null, gridManagerDisplayList=null,gridManagerSearch=null,refreshFlag = false
$(document).ready(function() {
	initUI();
	initButtonGrid();
	initDisplayListGrid();
	initSearchGrid();
	initializateTab();	
});

function initUI() {
	UICtrl.initDefaultLayout();
}

function initializateTab(){
	 $('#tabPage').tab().bind('click', function (e) {
       var $clicked = $(e.target || e.srcElement);
       if ($clicked.is('li')) {
           var divid = $clicked.attr('divid');
           var div = $('#' + divid);
           curr_gridManager=divid.substr(4);
           eval("var gridName" + "=eval(gridManager_" + curr_gridManager+")");
          if (eval(gridName)==null){ 
             doCallFunc(eval("initGrid_"+curr_gridManager),[]);
           } else {
             doCallFunc(eval("reloadGrid_"+curr_gridManager),[]);
          }
           div.show();
       }
   });
}
//功能按钮配置列表 
function initButtonGrid() {
	
	 var toolbarOptions =[];
	  toolbarOptions = UICtrl.getDefaultToolbarOptions({
		setFormBaseAttribute: {id:'setFormBaseAttribute',img:'fa-cog',text:'添加自定义按钮',click:function(){
			addCustomButton();
		}},
	   saveHandler:saveHandler,
	   deleteHandler:deleteHandler
	   });

	gridManagerBtn = UICtrl.grid('#dli_unit', {
		checkbox: true,
		columns : [ 
		    { display: "按钮名称", name: "buttonName", width: 220, type: "string", align: "left"},
			{ display: "方法名称", name: "buttonValue", width: 200, type: "string", align: "left"},
			
           	{ display: "按钮类型", name: "buttonType", width:220, type: "string", align: "left",
				 render: function (item) {
		               if (item.buttonType=="1"){
		                    return "默认按钮";
		            	} else {
		            		return "自定义按钮";
		            	}
		     		}	
			}
	     ],
		dataAction : 'server',
		toolbar: toolbarOptions,
		url : web_app.name + '/safFormslist/slicedQueryFormButton.ajax',
		parms : {
			id : getId()
	     },
		sortOrder : "asc",
		//toolbar : toolbarOptions,
		width : '99.8%',
		height : '100%',
		heightDiff : -8,
		pageSize:100,
		checkbox : true,
		enabledEdit: false,
		rownumbers : true,
		fixedCellHeight : true,
		selectRowButtonOnly : true,
		isChecked: f_isChecked,
	});
	function f_isChecked(data)
    {
        if (data.seled == 1){
        	return true;
        } 
        return false;
    }
	UICtrl.setSearchAreaToggle(gridManagerBtn);
}
//列表显示字段 
function initDisplayListGrid() {
	
	 var toolbarOptions =[];
	  toolbarOptions = UICtrl.getDefaultToolbarOptions({

	   saveHandler:saveListHandler,
	   deleteHandler:deleteHandler
	   });

	  gridManagerDisplayList = UICtrl.grid('#dli_listing', {
		checkbox: true,
		columns : [ 
		    { display: "字段名称", name: "attrName", width: 180, type: "string", align: "left"},
			{ display: "字段编码", name: "attrCode", width: 150, type: "string", align: "left"},
			{ display: "字段格式", name: "dataFormat", width: 180, type: "string", align: "left"},
			{ display: "标签类型", name: "fieldTag", width: 160, type: "string", align: "left"}
	     ],
		dataAction : 'server',
		toolbar: toolbarOptions,
		url : web_app.name + '/safFormslist/slicedQueryGridFields.ajax',
		parms : {
			type:"list",id:$("#formId").val()
	     },
		sortOrder : "asc",
		width : '99.8%',
		height : '100%',
		heightDiff : -8,
		pageSize:100,
		checkbox : true,
		enabledEdit: false,
		rownumbers : true,
		fixedCellHeight : true,
		selectRowButtonOnly : true,
		isChecked: f_isChecked,
	});
	function f_isChecked(data)
   {
       if (data.seled == 1){
       	return true;
       } 
       return false;
   }
	UICtrl.setSearchAreaToggle(gridManagerDisplayList);
}

//查询字段列表 
function initSearchGrid(){
	 var toolbarOptions =[];
	  toolbarOptions = UICtrl.getDefaultToolbarOptions({
	   saveHandler:saveSearchHandler,
	   deleteHandler:deleteHandler
	   });

	  gridManagerSearch = UICtrl.grid('#dli_search', {
		checkbox: true,
		columns : [ 
		    { display: "字段名称", name: "attrName", width: 180, type: "string", align: "left"},
			{ display: "字段编码", name: "attrCode", width: 150, type: "string", align: "left"},
			{ display: "字段格式", name: "dataFormat", width: 180, type: "string", align: "left"},
			{ display: "标签类型", name: "fieldTag", width: 160, type: "string", align: "left"}
	     ],
		dataAction : 'server',
		toolbar: toolbarOptions,
		url : web_app.name + '/safFormslist/slicedQueryGridFields.ajax',
		parms : {
			type:"search",id:$("#formId").val()
	     },
		sortOrder : "asc",
		//toolbar : toolbarOptions,
		width : '99.8%',
		height : '100%',
		heightDiff : -8,
		pageSize:100,
		checkbox : true,
		enabledEdit: false,
		rownumbers : true,
		fixedCellHeight : true,
		selectRowButtonOnly : true,
		isChecked: f_isChecked,
	});
	function f_isChecked(data)
  {
      if (data.seled == 1){
      	return true;
      } 
      return false;
  }
	UICtrl.setSearchAreaToggle(gridManagerSearch);
}


function getId(){
	return $("#id").val() || "";
}

function addCustomButton(){
	
	 UICtrl.showAjaxDialog({
		  title:'添加自定义按钮',
		  url: web_app.name +'/safFormslist/loadCustomButtonDetail.load',
		      param: {id:$('#id').val(),tableName:$("#tableName").val()},
		      width: 800,
		      height:300,
		      init:function(){
		         initCustomButton();
		      },
		     ok: doSaveCustomButton,
		     close: onDialogCloseHandler 
		 });
}
//保存 选择的按钮
function saveHandler(){
	 var rows = gridManagerBtn.getSelectedRows();
	var _self = this;
	var url = "/safFormslist/saveFormButtonLists.ajax";
    $('#syfFormsForm').ajaxSubmit({
        url: web_app.name + (url),
        param:{detailData1:encodeURI($.toJSON(rows)),id:$("#id").val(),formId:$("#formId").val()},
        success: function () {
            refreshFlag = true;
            gridManagerBtn.loadData();
           // _self.close();
        }
    });
 //
}
 //保存列表显示字段
function saveListHandler(){
	 var rows = gridManagerDisplayList.getSelectedRows();
		var _self = this;
		var url = "/safFormslist/saveDisplayFields.ajax";
	    $('#syfFormsForm').ajaxSubmit({
	        url: web_app.name + (url),
	        param:{detailData1:encodeURI($.toJSON(rows)),id:$("#id").val(),formId:$("#formId").val()},
	        success: function () {
	            refreshFlag = true;
	            gridManagerDisplayList.loadData();
	           // _self.close();
	        }
	    });
	
}
//保存列表查询条件字段
function saveSearchHandler(){
	
	 var rows = gridManagerSearch.getSelectedRows();
		var _self = this;
		var url = "/safFormslist/saveSearchFields.ajax";
	    $('#syfFormsForm').ajaxSubmit({
	        url: web_app.name + (url),
	        param:{detailData1:encodeURI($.toJSON(rows)),id:$("#id").val(),formId:$("#formId").val()},
	        success: function () {
	            refreshFlag = true;
	            gridManagerSearch.loadData();
	           // _self.close();
	        }
	    });
}
function initCustomButton(){ 
} 
	 
//保存自定义按钮方法
function doSaveCustomButton(){
	 var _self = this;
	   var id = getId();
	   var url = '/safFormslist/saveCustomButton.ajax';
	  $('#customButtonForm').ajaxSubmit({
	       url: web_app.name +url,
	       success: function (data) {
	      	  reloadGrid();
	       	  refreshFlag = true;
	          _self.close();
	      }
	   });
	 }

//删除自定义按钮 
function deleteHandler(){
	 var rows = UICtrl.checkSelectedRows(gridManagerBtn);
	 //alert(rows.buttonType);
	 if (rows.buttonType==2) {
	    var action = "/safFormslist/deleteCustomButton.ajax?buttonValue="+rows.buttonValue+"&buttonName="+rows.buttonName;
	    $('#syfFormsForm').ajaxSubmit({
		       url: web_app.name +action,
		       success: function (data) {
		      	  reloadGrid();
		       	  refreshFlag = true;
		         // _self.close();
		      }
		   });
	  /*  DataUtil.delSelectedRows({
	        action: action,
	        idFieldName: 'id',
        gridManager : gridManagerBtn,
	        onSuccess: reloadGrid
	    });*/
	 }else {
		 Public.tip("只能删除自定义按钮!")
	 }
}
function reloadGrid() {
   gridManagerBtn.loadData();
}
//关闭对话框
function onDialogCloseHandler() {
    if (refreshFlag) {
        reloadGrid();
        refreshFlag = false;
    }
}

