//var imagePath = "";
var divHeight = ""; // 查询面板高度
var dataGridWidth = ""; // 查询面板宽度
var clientHeight = "";
var dataGridHeight = "";
var bodyWidth = 0;
var bodyHeight = 0;
var editor = null;
var addWinWidth = 0;  //新增窗口宽度
var addWinHeight = 0;  //新增窗口高度
var dataProvince_resource = "中国";
var dataCity_resource="";
$(function() {
	divHeight = $("#queryDiv2").height();
	dataGridWidth = document.documentElement.clientWidth;
	clientHeight = document.documentElement.clientHeight;
	$("#addCentralVisaAgencyWinId").window('close');  //添加会员基础信息窗口
	bodyHeight = document.documentElement.clientHeight;
	//bodyHeight = document.body.clientHeight;
	bodyWidth = document.body.clientWidth;
	addWinWidth = (bodyWidth/5)*3;   
	addWinHeight = (addWinWidth/5)*2;  
	initInputWidth();
	$("#queryDiv").window('close');
	initDataTable();	
});
/**
 * 初始化各输入框宽度
 */
function initInputWidth(){
	$(".easyui-textbox").attr("style","width:"+ 500+"px");
	$("#cityId").attr("style","width:"+ 210+"px");
	$("#provinceId").attr("style","width:"+ 210+"px");
	/*$("#cityId").combobox({
		width : 210,
		valueField : 'id',
		textField : 'text',
	});
		$("#provinceId").combobox({
			width : 210,
			panelHeight : 'auto',
			valueField : 'id',
			textField : 'text',
			url : '/admin/metadataManage/getMetadatasList?dataType='+encodeURI(encodeURI(dataProvince_resource)),
			onSelect: function(rec){    
	            var url = '/admin/metadataManage/getMetadatasList?dataType='+encodeURI(encodeURI(rec.id));    
	            $('#cityId').combobox('reload', url);    
	        }
		});*/
}

//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(){
	$("#visaAgencysTable").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/visaAgencyManage/queryAllVisaAgency',
		columns : [ [ {
			field : 'ck',
			title : 'id',
			width : 15, 
			checkbox : true
		},{ 
			field : 'visaAgencyName',
			title : '签证机构名称',
			width : '400',
			align : 'center',
			formatter : dataStyle
		},{ 
			field : 'province',
			title : '省',
			width : '80',
			align : 'center'
		},{ 
			field : 'city',
			title : '市',
			width : '80',
			align : 'center'
		},{ 
			field : 'contactPerson',
			title : '联系人',
			width : '150',
			align : 'center'
		},{ 
			field : 'phone',
			title : '联系方式',
			width : '400',
			align : 'center'
		},{ 
			field : 'createTime',
			title : '发布日期',
			width : '150',
			align : 'center'
		}, { 
			field : 'creator',
			title : '发布人',
			width : '100',
			align : 'center'
		}
		] ]
	});
	}
	//打开查看窗口   打开tab页
	function viewData(id){ 
		$.getJSON("/business/visaAgencyManage/queryVisaAgencyById/" + id, function(data) {
			$("#addCentralVisaAgencyForm").form('load', data); 	
			//editor.setContent(data.visaAgencyContent);  
		});    
		$("#main_area").height((bodyHeight/5)*4);
		$("#addCentralVisaAgencyWinId").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").hide();
		$("#addCentralVisaAgencyWinId").window('open');
	}

	function dataStyle(val, row) {
		return '<a class="" href="#" onclick="viewData(\'' + row.id + '\')">'+ row.visaAgencyName + '</a>';
	}
	
	function doSearch(value){
	    var queryParams = null;
	    $("#CentralVisaAgencyManageTable").datagrid({
			queryParams : {
				queryValue : value
			}
		});
	}
	
/**
 * 打开新增窗口  基本信息
 */
function openAddWin(){
	initInputWidth();
	operation = "add";
	$("#main_area").height();
	$("#addCentralVisaAgencyWinId").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 
	});  
	//imagePath = ""; 
	//$("#imageID").attr("src",imagePath);
	//$("#imageID").attr("style","width:100px;height:50px");
	$("#addCentralVisaAgencyForm").form('clear');
	$("#buttonId01").show();
	$("#buttonId02").show();
	//editor.setContent('');  
	$("#addCentralVisaAgencyWinId").window('open');
}

/**
 * 打开修改窗口    基本信息
 */
function openEditWin(){
	$("#addCentralVisaAgencyWinId").form('clear');
	operation = "edit";
	var rows = $('#visaAgencysTable').datagrid('getSelections');
	if(rows.length == 1){
		var row = $('#visaAgencysTable').datagrid('getSelected');
		$.getJSON("/business/visaAgencyManage/queryVisaAgencyById/" + row.id, function(data) {
			//$("#imageID").attr("src",data.imagePath);
			//$("#imageID").attr("style","width:100px;height:60px");
			$("#addCentralVisaAgencyForm").form('load', data); 
			editor.setContent(data.visaAgencyContent);  
		});    
		$("#main_area").height((bodyHeight/5)*4);
		$("#addCentralVisaAgencyWinId").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").show();
		$("#buttonId02").show();
		$("#addCentralVisaAgencyWinId").window('open');
	}else {
		$.messager.alert('提示', '请选中一条记录 ', 'info');
	}
}
function openQueryWin(row){
	var data = row
	$("#addCentralVisaAgencyForm").form('load', data); 
	editor.setContent(data.visaAgencyContent);  
	$("#main_area").height((bodyHeight/5)*4);
	$("#addCentralVisaAgencyWinId").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();
	$("#addCentralVisaAgencyWinId").window('open');
}

$("#professorNameId").combobox({
	width:fixShortWidth()+4
})
//保存数据
function saveData(){
	var centralVisaAgencyTitle = $("#visaAgencyNameId").val();
	if('add' == operation){
		$.getJSON("/business/visaAgencyManage/isExistVisaAgency?visaAgencyName="+encodeURI(encodeURI(centralVisaAgencyTitle)),function(json){
			if(json.flag){
				$.messager.alert('提示', json.info, 'error');
			}else { 
				url = "/business/visaAgencyManage/addOrUpdateVisaAgency?operate="+operation;
				$('#addCentralVisaAgencyForm').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 = $('#visaAgencysTable').datagrid('getSelected'); 
		url = "/business/visaAgencyManage/addOrUpdateVisaAgency?operate="+operation+"&visaAgencyId="+row.id;
		$('#addCentralVisaAgencyForm').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 deleteVisaAgency(){
	var rows = $('#visaAgencysTable').datagrid('getChecked');
	if (rows.length !=0 ){
		var jsonIds = arrayTojson(rows);
		$.messager.confirm("删除","确定删除？",function(r){
			if (r){
				var postUrl = '/business/visaAgencyManage/deleteVisaAgencys';
				$.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');
	}
}

//关闭窗口  基础信息
function closeWindow(){
	$("#addCentralVisaAgencyWinId").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 visaAgencyTitle = $("#visaAgencyNameId00").val();
	//var visaAgencyType = $("#visaAgencyTypeId00").combobox('getValue');
	//alert(visaAgencyTitle);
	var queryParams = null;
  		$("#visaAgencysTable").datagrid({
			queryParams : {
				visaAgencyName : visaAgencyTitle,
				//visaAgencyType:visaAgencyType
			}
		});
}
// 清空查询条件框
function clearQueryForm() {
	$("#queryFormId2").form('clear');
	queryData();
}

