var detailWindow,detailWindowId =$.uniqueId(),windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEcompanyname = $('#LIKEcompanyname').textbox("getValue");
	sParam.order = "ORDER BY createtime desc";
	return sParam;
}
//加载列表
function initGrid(){
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	addCompany
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	updCompany
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	delCompany
    },{
    	text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		pagination:false,
		queryParams:getQueryParams(),
		url : 'ywarchivecompanyAction!listYwarchivecompanyByPage.do',
		columns : [[
			{field:'companynum',title:'公司编号',hidden:false,width:100},
			{field:'companyname',title:'公司全称',hidden:false,width:120},
			{field:'companytype',title:'公司类型',hidden:true,width:120}/*,
			{field:'legalpeople',title:'法定代表人',hidden:false,width:120},
			{field:'establishtime',title:'成立时间',hidden:false,width:120},
			{field:'companycontact',title:'公司联系人',hidden:true,width:120},
			{field:'contackmobilephone',title:'联系手机',hidden:true,width:120},
			{field:'contackphone',title:'联系电话',hidden:true,width:120},
			{field:'contackaddrarea',title:'联系地址区划',hidden:true,width:120},
			{field:'contackaddress',title:'联系地址详址',hidden:true,width:120},
			{field:'companysynopsis',title:'公司简介',hidden:true,width:120},
			{field:'createaccount',title:'创建人员',hidden:true,width:120},
			{field:'createtime',title:'创建日期',hidden:true,width:120},
			{field:'modifyaccount',title:'修改人员',hidden:true,width:120},
			{field:'modifytime',title:'修改日期',hidden:true,width:120},
			{field:'remarks',title:'备注',hidden:true,width:120},
			{field:'extend1',title:'扩展1',hidden:true,width:120},
			{field:'extend2',title:'扩展2',hidden:true,width:120},
			{field:'extend3',title:'扩展3',hidden:true,width:120},
			{field:'extend4',title:'扩展4',hidden:true,width:120}*/
		]],
		onClickRow:function(index,row){
			setchecked();
		},
		onLoadSuccess:function(){
			$("#tableGrid").datagrid("selectRow",0);
			setchecked();
			
		}
	});
}

function setchecked() {
	var selRows = $('#tableGrid').datagrid('getSelected');
	var companyid = '';
	var companyname = '';
	if(selRows != null){
		companyid = selRows.companyid;
		companyname = selRows.companyname;
	}
	var url = encodeURI('ywarchivecompanyperson.jsp?companyid='+companyid+'&companyname='+companyname);
	$("#ywarchivecompanyperson").prop("src",url);
}

//删除
function delCompany(){
	var selRows = $('#tableGrid').datagrid('getSelected');
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivecompanyAction!delete_Ywarchivecompany.do',
			data:{
				companyid	:	selRows.companyid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					$("#tableGrid").datagrid("reload");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}

//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}

//增加
function addCompany(){
	toEdit=false;
	// 根据detailWindowId判断是否已创建了该div，避免创建多余嵌套页面代码
	if(top.$("#" + detailWindowId + "").length > 0) {
		top.$("#" + detailWindowId + "").html("");
	}
	detailWindow = top.$('<div id=' +detailWindowId+'/>');
	createDetailWindow();
	setTimeout(function(){
		detailWindow.children('iframe').attr('src',sysUrl+'/yw/archive/companymanage/ywarchivecompany_show.jsp');
		detailWindow.dialog('open');
		top.editInfo = {};
	},100);
}

//修改
function doUpdate(index){
	toEdit=true;
	// 根据detailWindowId判断是否已创建了该div，避免创建多余嵌套页面代码
	if(top.$("#" + detailWindowId + "").length > 0) {
		top.$("#" + detailWindowId + "").html("");
	}
	detailWindow = top.$('<div id=' +detailWindowId+'/>');
	createDetailWindow();
	setTimeout(function(){
		var selRows = $('#tableGrid').datagrid('getRows')[index];
		detailWindow.children('iframe').attr('src',sysUrl+'/yw/archive/companymanage/ywarchivecompany_show.jsp');
		top.editInfo = selRows;
		detailWindow.dialog('open');
	},100);
}

//保存
function saveNodeInfo(){
	var frame	=	detailWindow.children('iframe')[0],model={};
	var win		=	frame.contentWindow||frame.window;
	var jq		=	win.$;
	// 获取参数
	model.companynum = jq("#companynum").textbox("getValue");
	model.companyname = jq("#companyname").textbox("getValue");
	model.companyabbreviation = jq("#companyabbreviation").textbox("getValue");
	model.establishtime = jq("#establishtime").textbox("getValue");
	model.companycontact = jq("#companycontact").textbox("getValue");
	model.contackphone = jq("#contackphone").textbox("getValue");
	model.contackaddress = jq("#contackaddress").textbox("getValue");
	model.companysynopsis = jq("#companysynopsis").textbox("getValue");

	if (model.companyname == "" || model.companyname == null) {
		top.$.dlg.alertError('公司名称不能为空! ');
    	return;
	}

	if(toEdit){//修改
		model.XGTJcompanyid = jq('#companyid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivecompanyAction!update_Ywarchivecompany.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					$("#tableGrid").datagrid("reload");
					detailWindow.dialog('close');
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivecompanyAction!insert_Ywarchivecompany.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					$("#tableGrid").datagrid("reload");
					detailWindow.dialog('close');
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}

//打开修改
function updCompany(){
	var selRows = $('#tableGrid').datagrid('getSelections');
	if(selRows.length <= 0){
		top.$.dlg.alertInfo('请先选择记录! ');
		return;
	} else if (selRows.length >= 2) {
		top.$.dlg.alertInfo('请先选择单条记录! ');
		return;
	} else {
		var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
		doUpdate(selIndex);
	}
}

function doExport() {
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywarchivecompanyAction!count_Ywarchivecompany.do',
		success : function(value) {
			if(value != null){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}

//导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
//	var model = getQueryParams();
	var downParam = "";
	document.excelForm.action = 'ywarchivecompanyAction!exp_Ywarchivecompany.do?'+downParam;
	document.excelForm.submit(); 
}

function createDetailWindow(){
	detailWindow.append('<iframe frameborder="no" scrolling="no" style="border:none;width:100%;height:100%"></iframe>');
	detailWindow.dialog({
		title:'公司信息管理',
		width:700,
		closed:true,
		height:420,
		modal:true,
		buttons:[{
				text:$.fn.textbox.defaults.ok,
				handler:saveNodeInfo
			 },{
				text:$.fn.textbox.defaults.close,
				handler:function(){
				detailWindow.dialog('close');
			 }
		}]
	});
}
