var windowId,sysUrl = top.sysUrl,toEdit;
$(function(){
//	initSelect();
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
//	sParam.LIKExh = $('#LIKExh').textbox("getValue");
	sParam.LIKEbzlx = $('#LIKEbzlx').textbox("getValue");
	sParam.LIKEbzmc = $('#LIKEbzmc').textbox("getValue");
	sParam.LIKEbzbh = $('#LIKEbzbh').textbox("getValue");/*
	sParam.LIKEbzpp = $('#LIKEbzpp').textbox("getValue");
	sParam.LIKEggxh = $('#LIKEggxh').textbox("getValue");
	sParam.LIKEsszbx = $('#LIKEsszbx').textbox("getValue");
	sParam.LIKEjd = $('#LIKEjd').textbox("getValue");
	sParam.LIKEwd = $('#LIKEwd').textbox("getValue");
	sParam.LIKEazsj = $('#LIKEazsj').textbox("getValue");
	sParam.LIKEazdw = $('#LIKEazdw').textbox("getValue");
	sParam.LIKEazfs = $('#LIKEazfs').textbox("getValue");
	sParam.LIKEgjlx = $('#LIKEgjlx').textbox("getValue");
	sParam.LIKEsjzp = $('#LIKEsjzp').textbox("getValue");
	sParam.LIKEgxdwbm = $('#LIKEgxdwbm').textbox("getValue");
	sParam.LIKEgxdwmc = $('#LIKEgxdwmc').textbox("getValue");
	sParam.LIKElxr = $('#LIKElxr').textbox("getValue");
	sParam.LIKElxdh = $('#LIKElxdh').textbox("getValue");
	sParam.LIKEgysbh = $('#LIKEgysbh').textbox("getValue");
	sParam.LIKEgysmc = $('#LIKEgysmc').textbox("getValue");
	sParam.LIKEbzjj = $('#LIKEbzjj').textbox("getValue");
	sParam.LIKEsl = $('#LIKEsl').textbox("getValue");
	sParam.LIKEcjry = $('#LIKEcjry').textbox("getValue");
	sParam.LIKEcjrq = $('#LIKEcjrq').textbox("getValue");
	sParam.LIKExgry = $('#LIKExgry').textbox("getValue");
	sParam.LIKExgrq = $('#LIKExgrq').textbox("getValue");
	sParam.LIKEbz = $('#LIKEbz').textbox("getValue");
	sParam.LIKEkz1 = $('#LIKEkz1').textbox("getValue");
	sParam.LIKEkz2 = $('#LIKEkz2').textbox("getValue");
	sParam.LIKEkz3 = $('#LIKEkz3').textbox("getValue");
	sParam.LIKEkz4 = $('#LIKEkz4').textbox("getValue");
*/
	/*
	var xx1 = $("#xx1").textbox("getValue");//文本
	var xx2 = $("#xx2").combobox("getValue");//下拉
	var xx3 = $("#xx3").datetimebox("getValue");//日期
	var xx4 = $("#xx4").numberbox("getValue");//数字
	var LIKEtime1 = $('#LIKEtime1').datetimebox("getValue");	
	var LIKEtime2 = $('#LIKEtime1').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND time > '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND time < '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	*/
	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
    }/*,{
        text	:	'模板',
        iconCls	:	'icon-export-excel',
        handler	:	doTemplate
    },{
        text	:	'导入',
        iconCls	:	'icon-update',
        handler	:	doImport
    },{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }*/];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'nhywjtbzglAction!listNhywjtbzglByPage.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:'bzmc',title:'标志名称',hidden:false,width:120},
 			{field:'bzbh',title:'标志编号',hidden:false,width:120,
 				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doDetail(\""+record.xh+"\")'>"+value+"</a>";
				}
 			},
			{field:'bzlx',title:'标志类型',hidden:false,width:120},
// 			{field:'bzlxname',title:'标志类型',hidden:false,width:120},
			{field:'bzpp',title:'标志品牌',hidden:false,width:120},
			{field:'ggxh',title:'规格型号',hidden:false,width:120},
			{field:'sszbx',title:'所属坐标系',hidden:true,width:120},
			{field:'jd',title:'经度',hidden:false,width:120},
			{field:'wd',title:'维度',hidden:false,width:120},
			{field:'azsj',title:'安装时间',hidden:false,width:120},
			{field:'azdw',title:'安装单位',hidden:false,width:120},
			{field:'azfs',title:'安装方式',hidden:false,width:120},
			{field:'gjlx',title:'杆件类型',hidden:false,width:120},
//			{field:'gjlxname',title:'杆件类型',hidden:false,width:120},
			{field:'sjzp',title:'实景照片',hidden:true,width:120},
			{field:'gxdwbm',title:'管辖单位编码',hidden:true,width:120},
			{field:'gxdwmc',title:'管辖单位名称',hidden:false,width:120},
			{field:'lxr',title:'联系人',hidden:true,width:120},
			{field:'lxdh',title:'联系电话',hidden:true,width:120},
			{field:'gysbh',title:'供应商编号',hidden:true,width:120},
			{field:'gysmc',title:'供应商名称',hidden:true,width:120},
			{field:'sl',title:'数量',hidden:false,width:120},
			{field:'bzjj',title:'标志简介',hidden:false,width:120},
			{field:'bz',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:'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}*/
		]]
	});
}

function doDetail(xh) {
	windowId = top.$.createWin({
		title	: 	'详情信息',
		width	:  	600,
		height	: 	430,
		data   	:   xh,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		url	:	sysUrl+"/yw/nhyw/nhywjtbzgl/nhywjtbzgl_show_detail.jsp"
	 });
}

//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtbzglAction!delete_Nhywjtbzgl.do',
			data:{
				xh	:	selRows.xh,
				sjzp : selRows.sjzp
			},
			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:'nhywjtbzglAction!batch_delete_Nhywjtbzgl.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	: 	600,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjtbzgl/nhywjtbzgl_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	600,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjtbzgl/nhywjtbzgl_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	var gysbh = jq("#gysbh").combotree("getValue");
	if (gysbh != null && gysbh != "") {
		model.gysmc = jq("#gysbh").combotree("getText");
	}
	var gxdwbm = jq("#gxdwbm").combotree("getValue");
	if (gxdwbm != "-1" && gxdwbm != null && gxdwbm != "") {
		model.gxdwmc = jq("#gxdwbm").combotree("getText");
	} else {
		model.gxdwbm= "";
	}
	// 验证数字
	/*
	var reg = /^[0-9]+\.?[0-9]*$/;
	if (model.sl != null && model.sl != "") {
		if (!reg.test(model.sl)) {
			$.showMsg("数量请输入正确数字！");
			return ;
		}
	}
	if (model.jd != null && model.jd != "") {
		if (!reg.test(model.jd)) {
			$.showMsg("经度请输入正确数字！");
			return ;
		}
	}
	if (model.wd != null && model.wd != "") {
		if (!reg.test(model.wd)) {
			$.showMsg("纬度请输入正确数字！");
			return ;
		}
	}*/
	console.log("增加",model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	var frame   =   top.$("#"+windowId).children("iframe")[0];
	var winf     =   frame.contentWindow||frame.window;
	var file 	= 	winf.$("#myfile").prop('files');
	if (!file.length) {
    	//$.showMsg('请选择要上传的文件！');
    	//return;
    }else{
    	var fileStr = uploadFile();
		var fileInfo = JSON.parse(fileStr);
		var flag = fileInfo.flag;
		if(flag=="success"){			
			model.sjzp = fileInfo.url;
//			model.wdmc = fileInfo.fileName;
//			model.kz1 = fileInfo.kz1;
			console.log("参数：",model);
		}else{
			$.showMsg('文件上传失败！');
			return;
		}
    }
	if(toEdit){//修改
		model.XGTJxh = jq('#xh').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjtbzglAction!update_Nhywjtbzgl.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:'nhywjtbzglAction!insert_Nhywjtbzgl.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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/im/xx/nhywjtbzgl_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'nhywjtbzglAction!count_Nhywjtbzgl.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 = 'nhywjtbzglAction!exp_Nhywjtbzgl.do?'+downParam;
	document.excelForm.submit(); 
}

//初始化加载下拉框
function initSelect(){
	//加载下拉框
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		data : {"csdl":"nhywBzlx"},
		success:function(res){
		//	console.log(res);
			if(res && res.length>0){
				var nhywBzlxArr = [{"id":"","text":"全部","selected":true}];
				for(i in res){
					if(res[i].csdl=="nhywBzlx"){
						nhywBzlxArr.push({"id":res[i].csdm,"text":res[i].csmc});
					}
				}
				$("#LIKEbzlx").combobox("loadData", nhywBzlxArr);
			}
		}
	});
}

function uploadFile(){
	var win		=	$.getTopWindowById(windowId);
	var res = '';
	win.$("#childform").ajaxSubmit({
	    type: 'post', // 提交方式 get/post
	    cache:false,
        dataType:'text',
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        async : false,
	    url: getUrl('UploadNhywSjzp'), // 需要提交的 url
	    success: function(responseText, statusText, xhr, $form) { 
//	    	 console.log(responseText);
	    	res = responseText;
//	    	console.log("res：",res);
	    }
	});
	return res;
}

function getUrl(ele) {
	var addr = "http://" + window.location.host + "/impnhyw"
//	console.log("addr: ",addr);
	var httpUrl = addr + '/' + ele;
//	console.log("httpUrl: ",httpUrl);
	return httpUrl;
}