var windowId,sysUrl = top.sysUrl,toEdit;
var detailtab;
$(function(){
	initDefault();	
	initDetailTab();
});
//默认
function initDefault(){
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEgysmc = $('#LIKExcdmc').textbox("getValue");
	sParam.order = "order by cjrq 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
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'nhywjtssxcdAction!listNhywjtssxcdByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'xh',title:'xh',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
 			{field:'xcdmc',title:'巡查单名称',hidden:false,width:120},
			{field:'fwsbh',title:'服务商编号',hidden:false,width:120},
			{field:'fwsmc',title:'服务商名称',hidden:false,width:120},
			{field:'fwsqc',title:'服务商全称',hidden:false,width:120},
			{field:'xcr',title:'巡查人',hidden:false,width:120},
			{field:'xcqy',title:'巡查区域',hidden:false,width:120},
			{field:'xcdd',title:'巡查地点',hidden:true,width:120},
			{field:'xcwzjd',title:'巡查位置经度',hidden:true,width:120},
			{field:'xcwzwd',title:'巡查位置维度',hidden:true,width:120},
			{field:'xcsj',title:'巡查时间',hidden:true,width:120},
			{field:'xcnr',title:'巡查内容',hidden:true,width:120},
			{field:'xcjg',title:'巡查结果',hidden:true,width:120},
			{field:'xctp1',title:'巡查图片1',hidden:true,width:120},
			{field:'xctp2',title:'巡查图片2',hidden:true,width:120},
			{field:'xcsp',title:'巡查视频',hidden:true,width:120},
			{field:'xcpjxj',title:'巡查评价星级',hidden:true,width:120},
			{field:'xcpjnr',title:'巡查评价内容',hidden:true,width:120},
			{field:'xcpjr',title:'巡检评价人',hidden:true,width:120},
			{field:'xcpjdw',title:'巡检评价单位',hidden:true,width:120},
			{field:'xcpjsj',title:'巡检评价时间',hidden:true,width:120},
			{field:'cjry',title:'创建人员',hidden:true,width:120},
			{field:'cjrq',title:'创建日期',hidden:true,width:120},
			{field:'xgry',title:'修改人员',hidden:true,width:120},
			{field:'xgrq',title:'修改日期',hidden:true,width:120},
			{field:'bz',title:'备注',hidden:true,width:120},
			{field:'kz1',title:'扩展1',hidden:true,width:120},
			{field:'kz2',title:'扩展2',hidden:true,width:120},
			{field:'kz3',title:'扩展3',hidden:true,width:120},
			{field:'kz4',title:'扩展4',hidden:true,width:120}
		]],
		onClickRow:function(index,row){
			setchecked();
		},
		onLoadSuccess:function(){
			$("#tableGrid").datagrid("selectRow",0);
			setchecked();
			
		}
	});
}

function initDetailTab(){
	detailtab = $("#detailtab").tabs({
		onSelect:function(title,index){
			if(index){
				setTimeout(function(){choseSrc(index)},1000);
			}
		}
	});
	
}

function setchecked(){
	var choseId = $('#detailtab').tabs('getSelected').find("iframe").attr("id");
//	console.log("select：",choseId)
	if("dzjksb" == choseId){
		choseSrc(0);
	}else if("jtsb" == choseId){
		choseSrc(1);
	} 
}

function choseSrc(ifram){
	var selRow = $('#tableGrid').datagrid('getSelected');
	var xcdh = '';
	var xcdmc = '';
	var fwsbh = '';
	if(selRow != null){
		xcdh = selRow.xh;
		xcdmc = selRow.xcdmc;
		fwsbh = selRow.fwsbh;
	}
//	console.log("table======: ",ifram)
	if('0' == ifram){
		$("#dzjksb").prop("src", encodeURI('xcdglsb/dzjksb.jsp?xcdh='+xcdh+'&xcdmc='+xcdmc+'&fwsbh='+fwsbh));		
	}else if('1' == ifram){
		$("#jtsb").prop("src",encodeURI('xcdglsb/jtsb.jsp?xcdh='+xcdh+'&xcdmc='+xcdmc+'&fwsbh='+fwsbh));	
	}
	
}
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtssxcdAction!delete_Nhywjtssxcd.do',
			data:{
				xh	:	selRows.xh

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
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].xh);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'nhywjtssxcdAction!batch_delete_Nhywgysxx.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;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	500,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjtxcrzgl/jtssxcd/nhywjtssxcd_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	500,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjtxcrzgl/jtssxcd/nhywjtssxcd_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
//	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	var fwsxx = model.fwsmc.split("-");
	model.fwsbh = fwsxx[0];
	model.fwsmc = fwsxx[1];
	model.fwsqc = fwsxx[2];
	
	if(toEdit){//修改
		model.XGTJxh = jq('#xh').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtssxcdAction!update_Nhywjtssxcd.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtssxcdAction!insert_Nhywjtssxcd.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		 url	:	sysUrl+"/im/xx/nhywgysxx_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'nhywjtssxcdAction!count_Nhywgysxx.do',
		success : function(value) {
			if(!$.isNull(value)){
				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 = 'nhywjtssxcdAction!exp_Nhywjtssxcd.do?'+downParam;
	document.excelForm.submit(); 
}
