var imagePath = "";
var divHeight = ""; // 查询面板高度
var dataGridWidth = ""; // 查询面板宽度
var clientHeight = "";
var dataGridHeight = "";
var bodyWidth = 0;
var bodyHeight = 0;
var investStudyInfoId = ""; //初始国别研究id
var dataType_resource = "新闻类型";
var dataType_infoStatus = "新闻状态";
var editor = null;

$(function() {
	divHeight = $("#queryDiv2").height();
	dataGridWidth = document.documentElement.clientWidth;
	clientHeight = document.documentElement.clientHeight;
	$("#addCentralCoApplicationWinId").window('close');  //添加会员基础信息窗口
	initInputWidth();
	$("#queryDiv").window('close');
	initDataTable();
})

/**
 * 初始化各输入框宽度
 */
function initInputWidth(){
	$(".easyui-textbox").attr("style","width:"+ fixShortWidth()+"px");
	$("#reasonId").attr("style","width:"+ fixShortWidth()+"px");
	
}

//js时间戳格式化成日期格式
function timestampformat(timestamp) {
	var date = new Date(timestamp);
	var year = date.getFullYear();
	var month = date.getMonth()+1;
	var day = date.getDate();
	return year+"-"+month+"-"+day;
}   

function initDataTable(){
	$("#coApplicationInfosTable").datagrid({
		height:clientHeight-divHeight-27,
		nowrap : true,
		striped : true,
		pagination : true,
		rownumbers : true,
		pageList : [10,20,30,40,50],
		autoRowHeight : false, 
		fitColumns : false,  
		singleSelect : false, //单选
		checkOnSelect  : true,
		selectOnCheck : true,
		remoteSort : true,
		url : '/business/coApplicationManage/queryAllCoApplicationInfo',
		columns : [ [ {
			field : 'ck',
			title : 'id',
			width : 15, 
			checkbox : true
		}/*,{ 
			field : 'infotatus',
			title : '状态',
			width : '80',
			align : 'center'
		}*/,{ 
			field : 'ccpitCode',
			title : '贸促会编码',
			width : '200',
			align : 'center',
			formatter : dataStyle
		},{ 
			field : 'registNumber',
			title : '企业注册号',
			width : '200',
			align : 'center',
			formatter : dataStyle
		},{ 
			field : 'linkMan',
			title : '联系人',
			width : '100',
			align : 'center'
		},{ 
			field : 'linkPhone',
			title : '联系方式',
			width : '200',
			align : 'center'
		},{ 
			field : 'createTime',
			title : '申请日期',
			width : '150',
			align : 'center'
		}
		] ]
	});
	}
	//打开查看窗口   打开tab页
	function viewData(id){ 
		$.getJSON("/business/coApplicationManage/queryCoApplicationInfoById/" + id, function(data) {
			$("#addCentralCoApplicationForm").form('load', data); 	
		});    
		$("#main_area").height((bodyHeight/5)*4);
		$("#addCentralCoApplicationWinId").window({  
			title : '查看信息',  
			width : addWinWidth/2,
			height : addWinHeight*4/5, 
			left : (dataGridWidth - addWinWidth/2)/2,
			top : (bodyHeight - addWinHeight*4/5)/2,
			closable : true, // 否显示关闭按钮
			collapsible : false, // 是否显示折叠按钮
			minimizable : false, // 是否显示最小化按钮
			maximizable : false, // 是否显示最大化按钮
			close : true, // 初始是否关闭
			modal : true 
		}); 
		$("#buttonId01").hide();
		$("#buttonId02").hide();
		$("#addCentralCoApplicationWinId").window('open');
	}

	function dataStyle(val, row) {
		return '<a class="" href="#" onclick="viewData(\'' + row.id + '\')">'+ row.ccpitCode + '</a>';
	}
	
	function stcikFormat(val, row){
		if(row.whetherStick == '置顶'){
			return '<font style="color:#ff0000">'+row.whetherStick+'</font>';
		}else {
			return '<font style="color:#000000">'+row.whetherStick+'</font>';
		}
	}
	
	function doSearch(value){
	    var queryParams = null;
	    $("#CentralCoApplicationManageTable").datagrid({
			queryParams : {
				queryValue : value
			}
		});
	}
	
/**
 * 打开新增窗口  基本信息
 */
function openAddWin(){
	initInputWidth();
	operation = "add";
	$("#main_area").height();
	$("#addCentralCoApplicationWinId").window({
		title : '新增信息',
		width : addWinWidth/2,
		height : addWinHeight*4/5, 
		left : (dataGridWidth - addWinWidth/2)/2,
		top : (bodyHeight - addWinHeight*4/5)/2,
		closable : true, // 否显示关闭按钮
		collapsible : false, // 是否显示折叠按钮
		minimizable : false, // 是否显示最小化按钮
		maximizable : false, // 是否显示最大化按钮
		close : true, // 初始是否关闭
		modal : true 
	});  
	$("#addCentralCoApplicationForm").form('clear');
	$("#buttonId01").show();
	$("#buttonId02").show();
	$("#addCentralCoApplicationWinId").window('open');
}

/**
 * 打开修改窗口    基本信息
 */
function openEditWin(){
	$("#addCentralCoApplicationWinId").form('clear');
	operation = "edit";
	var rows = $('#coApplicationInfosTable').datagrid('getSelections');
	if(rows.length == 1){
		var row = $('#coApplicationInfosTable').datagrid('getSelected');
		$.getJSON("/business/coApplicationManage/queryCoApplicationInfoById/" + row.id, function(data) {
			$("#addCentralCoApplicationForm").form('load', data); 
			
		});    
		$("#main_area").height((bodyHeight/5)*4);
		$("#addCentralCoApplicationWinId").window({  
			title : '修改信息',  
			width : addWinWidth/2,
			height : addWinHeight*4/5, 
			left : (dataGridWidth - addWinWidth/2)/2,
			top : (bodyHeight - addWinHeight*4/5)/2,
			closable : true, // 否显示关闭按钮
			collapsible : false, // 是否显示折叠按钮
			minimizable : false, // 是否显示最小化按钮
			maximizable : false, // 是否显示最大化按钮
			close : true, // 初始是否关闭
			modal : true 
		}); 
		$("#buttonId01").show();
		$("#buttonId02").show();
		$("#addCentralCoApplicationWinId").window('open');
	}else {
		$.messager.alert('提示', '请选中一条记录 ', 'info');
	}
}
function openQueryWin(row){
	var data = row
	$("#addCentralCoApplicationForm").form('load', data); 
	editor.setContent(data.coApplicationContent);  
	$("#main_area").height((bodyHeight/5)*4);
	$("#addCentralCoApplicationWinId").window({  
		title : '查看信息',  
		width : addWinWidth,
		height : addWinHeight, 
		left : (dataGridWidth - addWinWidth)/2,
		top : (bodyHeight - addWinHeight)/2,
		closable : true, // 否显示关闭按钮
		collapsible : false, // 是否显示折叠按钮
		minimizable : false, // 是否显示最小化按钮
		maximizable : false, // 是否显示最大化按钮
		close : true, // 初始是否关闭
		modal : true 
	}); 
	$("#buttonId01").hide();
	$("#buttonId02").show();
	$("#addCentralCoApplicationWinId").window('open');
}

$("#professorNameId").combobox({
	width:fixShortWidth()+4
})
//保存数据
function saveData(){
	var ccpitCode = $("#ccpitCodeId").val();
	var registNumber = $("#registNumberId").val();
	if('add' == operation){
		$.getJSON("/business/coApplicationManage/isExistCoApplicationInfo?ccpitCode="+encodeURI(encodeURI(ccpitCode))+"&registNumber="+encodeURI(encodeURI(registNumber)),function(json){
			if(json.flag){
				$.messager.alert('提示', json.info, 'error');
			}else { 
				url = "/business/coApplicationManage/addOrUpdateCoApplicationInfo?operate="+operation;
				$('#addCentralCoApplicationForm').form('submit',{
					url: url,
					onSubmit: function(){
						return $(this).form('validate');
					},
					success: function(data){
						var data = eval("("+data+")");
						if (data.flag){
							$.messager.alert('提示', data.info, 'ok');
							closeWindow();			
							initDataTable();
						} else {
							$.messager.alert("提示",data.info,'error');
						}
					}
				}); 
						
			}})   
		}
	if('edit' == operation){ 
		var row = $('#coApplicationInfosTable').datagrid('getSelected'); 
		url = "/business/coApplicationManage/addOrUpdateCoApplicationInfo?operate="+operation+"&coApplicationInfoId="+row.id+"&imagePath="+imagePath;
		$('#addCentralCoApplicationForm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(data){
				var data = eval("("+data+")")
				if (data.flag){
					$.messager.alert('提示',data.info, 'ok');
					closeWindow();
					
					initDataTable();
				} else {
					$.messager.alert("提示",data.info,'error');
				}
			}
		}); 
	}
}

//删除
function deleteCoApplication(){
	var rows = $('#coApplicationInfosTable').datagrid('getChecked');
	if (rows.length !=0 ){
		var jsonIds = arrayTojson(rows);
		$.messager.confirm("删除","确定删除？",function(r){
			if (r){
				var postUrl = '/business/coApplicationManage/deleteCoApplicationInfos';
				$.post(postUrl,{
					"ids":jsonIds},function(result){
					if (result.flag){
						$.messager.alert('提示',result.info,'ok');
						initDataTable(); 
					} else {
						$.messager.alert('提示',result.info,'error');
						initDataTable();
						return;
					}
				},'json');
			}
		});
	}else{ 
		$.messager.alert('提示', '请选中一条要删除记录', 'info');
	}
}
/**
 * 新闻置顶
 * @param stickType
 *//*
function stickCoApplication(stickType){
	var rows = $('#coApplicationInfosTable').datagrid('getChecked');
	if('yes' == stickType){
		if (rows.length !=0 ){
			var jsonIds = arrayTojson(rows);
			$.messager.confirm("置顶","确定置顶？",function(r){
				if (r){
					var postUrl = '/business/coApplicationManage/setStick';
					$.post(postUrl,{
						"ids":jsonIds,"stickType":stickType},function(result){
						if (result.flag){
							$.messager.alert('提示',result.info,'ok');
							initDataTable(); 
						} else {
							$.messager.alert('提示',result.info,'error');
							initDataTable();
							return;
						}
					},'json');
				}
			});
		}else{ 
			$.messager.alert('提示', '请选中一条需要置顶记录', 'info');
		}
	}else if('no' == stickType){
		if (rows.length !=0 ){
			var jsonIds = arrayTojson(rows);
			$.messager.confirm("取消置顶","确定取消置顶？",function(r){
				if (r){
					var postUrl = '/business/coApplicationManage/setStick';
					$.post(postUrl,{
						"ids":jsonIds,"stickType":stickType},function(result){
						if (result.flag){
							$.messager.alert('提示',result.info,'ok');
							initDataTable(); 
						} else {
							$.messager.alert('提示',result.info,'error');
							initDataTable();
							return;
						}
					},'json');
				}
			});
		}else{ 
			$.messager.alert('提示', '请选中一条需要取消置顶的记录', 'info');
		}
	}
}*/

//关闭窗口  基础信息
function closeWindow(){
	$("#addCentralCoApplicationWinId").window('close');
}

/**
 * id数组转换为json字符串
 */
function arrayTojson(arr) { 
	var jsonIds = "[";
	for ( var i = 0; i < arr.length; i++) {
		if (i == arr.length - 1) {
			jsonIds += arr[i].id;
		} else {
			jsonIds += arr[i].id + ",";
		}
	}
	jsonIds += "]";

	return jsonIds; 
}
//查询 
function queryData() {
	var ccpitCode = $("#ccpitCodeId00").val();
	var registNumber = $("#registNumberId00").val();
	//alert(coApplicationTitle);
	var queryParams = null;
  		$("#coApplicationInfosTable").datagrid({
			queryParams : {
				ccpitCode : ccpitCode,
				registNumber:registNumber
			}
		});
}
// 清空查询条件框
function clearQueryForm() {
	$("#queryFormId2").form('clear');
	var queryParams = null;
  		$("#coApplicationInfosTable").datagrid({
  			queryParams:{}
		});
}

//设置为头图

function head2 () {
	var rows = $('#coApplicationInfosTable').datagrid('getChecked');
		if (rows.length !=0 ){
			$.messager.confirm("设置为头图","确定？",function(r){
				if (r){
					var postUrl = '/business/coApplicationManage/setHead/'+rows[0].id;
					$.post(postUrl,function(result){
						if (result.flag){
							$.messager.alert('提示',result.info,'ok');
							initDataTable(); 
						} else {
							$.messager.alert('提示',result.info,'error');
							initDataTable();
							return;
						}
					},'json');
				}
			});
		}else{ 
			$.messager.alert('提示', '请选中一条需要设置为头图的记录', 'info');
		}
}
