var detailWindow,detailWindowId =$.uniqueId(),windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var companyid = $.getUrlParam("companyid");
var companyname = $.getUrlParam("companyname");

$(function(){
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.companyid = companyid;
	//sParam.LIKEpersontype = $('#LIKEpersontype').textbox("getValue");
	sParam.LIKEpersonname = $('#LIKEpersonname').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	:	doAdd
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete
    },{
    	text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywarchivecompanypersonAction!listYwarchivecompanypersonByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'personid',title:'personid',hidden:true,width:120},
			{field:'companyid',title:'公司id',hidden:true,width:120},
			{field:'companyname',title:'公司名称',hidden:false,width:120},
			{field:'persontype',title:'人员类型',hidden:true,width:120},
			{field:'personname',title:'人员姓名',hidden:false,width:120},
			{field:'personphone',title:'人员手机',hidden:false,width:120},
			{field:'personaddrarea',title:'人员住址区划',hidden:true,width:120},
			{field:'personaddress',title:'人员住址',hidden:false,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:false,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}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelections'),j=0;
	if(selRows.length==0) {
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		for(var i=0;i<selRows.length;i++){
			$.ajax({
				async:false,
				type:'post',
				dataType : 'json',
				url:'ywarchivecompanypersonAction!delete_Ywarchivecompanyperson.do',
				data:{
					personid	:	selRows[i].personid
	
				},
				success:function(flag){
					if(flag == 'success') {
						j++;
					}
				}
			});
			if(j == selRows.length) {
				top.$.dlg.alertInfo('删除成功 ');
				$("#tableGrid").datagrid("reload");
			}
		}
	});
}

//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].personid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywarchivecompanypersonAction!batch_delete_Ywarchivecompanyperson.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}

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

//增加
function doAdd(){
	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/ywarchivecompanyperson_show.jsp');
		detailWindow.dialog('open');
		top.editInfo = {};
	},100);
}

//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	// 根据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/ywarchivecompanyperson_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.companyid = companyid;
	model.companyname=companyname;
	model.personname = jq("#personname").textbox("getValue");
	model.personphone = jq("#personphone").textbox("getValue");
	model.personaddress = jq("#personaddress").textbox("getValue");
	model.remarks = jq("#remarks").textbox("getValue");
	
	/*if (model.persontype == "" || model.persontype == null) {
		top.$.dlg.alertError('人员类型不能为空! ');
    	return;
	}*/
	if (model.personname == "" || model.personname == null) {
		top.$.dlg.alertError('人员名称不能为空! ');
    	return;
	}
	if (model.personphone == "" || model.personphone == null) {
		top.$.dlg.alertError('人员手机不能为空! ');
    	return;
	}

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

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivecompanypersonAction!update_Ywarchivecompanyperson.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:'ywarchivecompanypersonAction!insert_Ywarchivecompanyperson.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					$("#tableGrid").datagrid("reload");
					detailWindow.dialog('close');
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}

//打开修改
function doEdit(){
	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 : 'ywarchivecompanypersonAction!count_Ywarchivecompanyperson.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 = "";
	$("#companyid").val(companyid);
	document.excelForm.action = 'ywarchivecompanypersonAction!exp_Ywarchivecompanyperson.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:590,
		modal:true,
		buttons:[{
				text:$.fn.textbox.defaults.ok,
				handler:saveNodeInfo
			 },{
				text:$.fn.textbox.defaults.close,
				handler:function(){
				detailWindow.dialog('close');
			 }
		}]
	});
}