var dataAuditGridManager = null,existdataNewPlusGridManager,refreshFlag = false,modelId="",newBusinessDataName=null,newBusinessDataName=null;
$(document).ready(function () {
    initUI();
    initDataAuditGrid();
    initExistDataAuditGrid();
});

function initUI() {
	UICtrl.layout("#layout",{
		leftWidth:5,
		rightWidth:5,
		allowLeftCollapse: false, 
		allowRightCollapse: false,
		allowLeftResize: false,
		allowRightResize: false
	});
	//页面布局
	$(".ui-layout-center .btn.btn-primary").parent().parent().css("width","70px").parent().attr("align","center").each(function(){
		var _height = $(this).height();
		$(this).children("[position='center']").css("margin-top",(parseInt(_height)/2)+"px");
	});
	//去边框
	$("#layout").find('div.ui-layout-center>div.ui-layout-warp').css('border',0);

}



function initDataAuditGrid() {
    var toolbarOptions =[];
    dataAuditGridManager = UICtrl.grid('#selectDataListGrid', {
    	  columns: [
    	        	{ display: "字段名称", name: "fieldName", width: 90, minWidth: 60, type: "string", align: "left"},
    				{ display: "字段编码", name: "fieldCode", width: 90, minWidth: 60, type: "string", align: "left" },
			  {
				  display: "字段类型", name: "fieldDataType", width: 60, minWidth: 40, type: "string", align: "left",
				  render: function (item) {
					  if (item.fieldDataType == "number") {
						  return "数值型";
					  } else if (item.fieldDataType == "date") {
						  return "日期型";
					  } else {
						  return "字符型";
					  }
				  },
			  }
    	         ],
        dataAction: 'server',
        url: web_app.name + '/mdmMetaData/slicedQueryOldMdmMetaData.ajax',
        parms:{
    	  	id: $("#id").val()},
        sortName: "fieldName",
        sortOrder: "asc",
        pageSize:200,
        //toolbar: toolbarOptions,
        width: '99.8%',
        height: '95%',
        heightDiff: -2,
        checkbox: true,
        onBeforeCheckRow:function(flag,data){
        	return true;
        },
        onDblClickRow : function(row){ //实现双击进入另一个grid中
			var param = $("#mdmMetaDataCopyForm").formToJSON();
			var newBusinessDataName =param.newBusinessDataName;
			var leftData=dataAuditGridManager.getSelecteds();
			var detailData = DataUtil.getGridData({
				gridManager: existdataNewPlusGridManager,
				isAllData: true,
				onCheck: function () {
					return true;
				}
			});
			if(newBusinessDataName==null || newBusinessDataName==""){
				Public.tip("请先选择目标数据！");
				return;
				// (leftData[i].fieldName==detailData[j].fieldName)&&(leftData[i].fieldCode==detailData[j].fieldCode)
			}else {
				for (var j = 0; j <detailData.length ; j++) {
					if ((row.fieldName==detailData[j].fieldName)&&(row.fieldCode==detailData[j].fieldCode)){
						Public.tip('字段名称和字段编码已经存在!');
						return;
					}
				}
				var g=this,gridManagers=["dataAuditGridManager","existdataNewPlusGridManager"];
				g.selected = []; //清空selected 避免多次选中
				g.select(row);
				gridManagers.splice($.inArray(g.element.id,gridManagers),1); //筛选被加入的gridManager
				UICtrl.moveSelectedRowsToOtherGrid(dataAuditGridManager,existdataNewPlusGridManager);
			}

		},
        rownumbers: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
    });
    UICtrl.setSearchAreaToggle(dataAuditGridManager);
}
function initExistDataAuditGrid(){
	existdataNewPlusGridManager = UICtrl.grid('#existDataListGrid', {
		columns: [
			{ display: "字段名称", name: "fieldName", width: 90, minWidth: 60, type: "string", align: "left"},
			{ display: "字段编码", name: "fieldCode", width: 90, minWidth: 60, type: "string", align: "left" },
			{
				display: "字段类型", name: "fieldDataType", width: 60, minWidth: 40, type: "string", align: "left",
				render: function (item) {
					if (item.fieldDataType == "number") {
						return "数值型";
					} else if (item.fieldDataType == "date") {
						return "日期型";
					} else {
						return "字符型";
					}
				},
			},
			{display: "", name: "isOld", hide: true}
		],
           dataAction: 'server',
        url: web_app.name + '/mdmMetaData/slicedQueryNewMdmMetaData.ajax',
		parms:{
			id: $("#id").val(),
			newBusinessDataName: $("#newBusinessDataName").val()
		},

        sortName: "fieldName",
        sortOrder: "asc",
        pageSize:100,
        width: '99.8%',
        height: '98%',
        isScroll:true,
        heightDiff: -2,
		alternatingRow:false,
		onBeforeCheckRow: function (flag, data) {
			if(data.isOld==1){
				Public.tip("目标数据被不能选择！");
				return false;
			}
			return true;

		},
        onDblClickRow : function(row){ //实现双击进入另一个grid中
			var param = $("#mdmMetaDataCopyForm").formToJSON();
			var id =param.id;
			if (row.classifyId!=id){
				Public.tip('目标数据不能移动到来源数据!');
				return;
			}else {
				var g=this,gridManagers=["existdataNewPlusGridManager","dataAuditGridManager"];
				g.selected = []; //清空selected 避免多次选中
				g.select(row);
				gridManagers.splice($.inArray(g.element.id,gridManagers),1); //筛选被加入的gridManager
				UICtrl.moveSelectedRowsToOtherGrid(existdataNewPlusGridManager,dataAuditGridManager);
			}
			},
		onAfterShowData:function(){
			$("#existDataListGrid .isOld[title='1']").parent().parent().css("background", "#E0FFFF");
		},
        checkbox: true,
        rownumbers: true,
        enabledEdit: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
    });	
	
}
//左表向右表移动
function addData(){
	var param = $("#mdmMetaDataCopyForm").formToJSON();
	var newBusinessDataName =param.newBusinessDataName;
	var leftData=dataAuditGridManager.getSelecteds();
	var detailData = DataUtil.getGridData({
		gridManager: existdataNewPlusGridManager,
		isAllData: true,
		onCheck: function () {
			return true;
		}
	});
	debugger
	if(newBusinessDataName==null || newBusinessDataName==""){
		Public.tip("请先选择目标数据！");
		return;
	}else {
		if(leftData){
			for(var i=0;i<leftData.length;i++){
				for (var j = 0; j < detailData.length; j++) {
					if ((leftData[i].fieldName==detailData[j].fieldName)&&(leftData[i].fieldCode==detailData[j].fieldCode)){
						Public.tip('字段名称和字段编码已经存在!');
						return;
					}
				}
				if(leftData[i].status==0){
					Public.tip('不能选择禁用的数据!');
					return;
				}
			}
			UICtrl.moveSelectedRowsToOtherGrid(dataAuditGridManager,existdataNewPlusGridManager);
		}else{
			Public.tip('请选择左侧功能!');
			return;
		}
	}
	// if(leftData){
	// 	for(var i=0;i<leftData.length;i++){
	// 		for (var j = 0; j < detailData.length; j++) {
	// 			if ((leftData[i].fieldName==detailData[j].fieldName)&&(leftData[i].fieldCode==detailData[j].fieldCode)){
	// 				Public.tip('字段名称和字段编码已经存在!');
	// 				return;
	// 			}
	// 		}
	// 		if(leftData[i].status==0){
	// 			Public.tip('不能选择禁用的数据!');
	// 			return;
	// 		}
	// 	}
	// 	UICtrl.moveSelectedRowsToOtherGrid(dataAuditGridManager,existdataNewPlusGridManager);
	// }else{
	// 	Public.tip('请选择左侧功能!');
	// 	return;
	// }
	
}
//右表向左表移动
function deleteData(){
	var param = $("#mdmMetaDataCopyForm").formToJSON();
	var id =param.id;
	var rightData=existdataNewPlusGridManager.getSelecteds();
	if(rightData){
		for (var j = 0; j < rightData.length; j++) {
			if (rightData[j].classifyId!=id){
				Public.tip('目标数据不能移动到来源数据!');
				return;
			}
		}
		UICtrl.moveSelectedRowsToOtherGrid(existdataNewPlusGridManager,dataAuditGridManager);
	}else{
		Public.tip('请选择右侧功能!');
		return;
	}
	
}
//右表全部删除，并移动到左表
function doDeleteAll(){
	var param = $("#mdmMetaDataCopyForm").formToJSON();
	var id =param.id;
	var records = existdataNewPlusGridManager.records;

	existdataNewPlusGridManager.selected = []; //清空selected 避免多次选中
	debugger;
	for (var rowid in records){
		if(records[rowid].classifyId!=id){
			continue;
		}
		existdataNewPlusGridManager.select(records[rowid]); //循环遍历全选
	}
	UICtrl.moveSelectedRowsToOtherGrid(existdataNewPlusGridManager,dataAuditGridManager);
}

function viewHandler() {
    var row = UICtrl.checkSelectedRows(dataAuditGridManager);
    if (row) {
        viewInfo(row.id);
    }
}
/*
function viewInfo(id) {
    if (!id) {
        var row = dataAuditGridManager.getSelectedRow();
        if (!row) { Public.tip('请选择数据！'); return; }
        id = row.id;
    }
    UICtrl.showAjaxDialog({
        width: 900,
        title: "查看详情",
        url: web_app.name + '/projectFileClassList/showProjectFileClassListDetail.load',
        param: {id:id},
        init:initReadOnly,
        ok: doSaveProjectFileClassList,
        close: onDialogCloseHandler
    });

}*/
function reloadGrid() {
	var params = $("#queryForm").formToJSON();
	{}

    UICtrl.gridSearch(dataAuditGridManager, params);
    //dataAuditGridManager.loadData();
}

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

function getTaskPropertyId(){
	return $("#taskPropertyId").val() || "";
}

function getTaskId(){
	return $("#taskId").val() || "";
}
function initReadOnly(){
	$('input,select,textarea', $('form[name="projectFileClassListForm"]')).attr('readonly', true);
	$("#ok").hide();
}

/*function getSelectData(){
	// var rows = UICtrl.checkSelectedRows(dataAuditGridManager);
	 debugger
	 var rows = dataAuditGridManager.getSelecteds();
	  if (rows) {
	      return rows;   	
	  } else {
		  return "";
	  }
}*/
//获取右表的数据
function getCheckedData(){
	var data = DataUtil.getGridData({gridManager:existdataNewPlusGridManager,isAllData:true});
	if(data){
		return data;
	}else{
		return "";
	}
	
}


//关闭对话框
function onDialogCloseHandler() {
    if (refreshFlag) {
        reloadGrid();
        refreshFlag = false;
    }
}

//启用
function enableHandler() {
    updateStatus('确实要启用选中数据吗?', 1);
}
//禁用
function disableHandler() {
    updateStatus('确实要禁用选中数据吗?', 0);
}

//修改状态
function updateStatus(message, status) {
    DataUtil.updateById({ action: 'projectFileClassList/updateFileClassStatus.ajax',
        gridManager: dataAuditGridManager, param: { status: status },
        message: message,
        onSuccess: function () {
            reloadGrid();
        }
    });
}
//使用方法相当于eval
Public.eval = function(fn) {
	var Fn = Function; // 一个变量指向Function，防止有些前端编译工具报错
	return new Fn('return ' + fn)();
}
//查询
function query(obj) {
    var param = $(obj).formToJSON();
    UICtrl.gridSearch(dataAuditGridManager, param);
}
//重置查询条件
function resetForm(obj) {
    $(obj).formClean();
}