var windowId,sysUrl = top.sysUrl,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){

	$('#xcpjxj').combobox({  
		url:'xtclAction!query_Xtcl.do?csdl=nhywXcdpjxj&page=1&rows=1000', 
		textField:"csmc",  
		valueField:"csdm",
		panelWidth:148,
		loadFilter:function(data){
			data.unshift({"csmc":"请选择","csdm":""});
			return data;
		}
	});
	$("#datetimeStart").datetimebox("setValue",$.getTodayStart('00:00:00'));
	$("#datetimeEnd").datetimebox("setValue",$.getTodayStart('23:59:59'));
	/*
	$("#xx6").textbox("setValue",records.xx6);
	$("#xx7").combobox("setValue",records.xx7);
	$("#xx8").combobox("setValue",records.xx8);
	$("#xx9").datetimebox("setValue",$.getNowTime());
	$("#xx10").datetimebox("setValue",$.getTodayStart('00:00:00',-3));//往前推3天  +'00:00:00'
	$("#xx11").datetimebox("setValue",$.getTodayEnd());//当天 +'23:59:59'	
	$("#xx12").datetimebox("setValue",$.getDateNumToStr(records.xx12));
	$("#xx13").numberbox("setValue",records.xx13);
	*/
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKExcdmc = $('#LIKExcdmc').textbox("getValue");
	sParam.LIKEfwsmc = $('#LIKEfwsmc').textbox("getValue");
	sParam.LIKExcr = $('#LIKExcr').textbox("getValue");
	sParam.LIKExcdd = $('#LIKExcdd').textbox("getValue");
	sParam.xcpjxj = $('#xcpjxj').combobox("getValue");
	var datetimeStart = $('#datetimeStart').datetimebox("getValue");	
	var datetimeEnd = $('#datetimeEnd').datetimebox("getValue");	
	var tableparam = "";
	if(!(datetimeStart==null||datetimeStart==""||datetimeStart==undefined)){
		tableparam += " AND xcsj >= '" +datetimeStart + "'";
	}
	if(!(datetimeEnd==null||datetimeEnd==""||datetimeEnd==undefined)){
		tableparam += " AND xcsj <= '" +datetimeEnd + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by xcsj desc ";
	/*
	sParam.LIKExh = $('#LIKExh').textbox("getValue");
	sParam.LIKEfwsbh = $('#LIKEfwsbh').textbox("getValue");
	sParam.LIKEfwsmc = $('#LIKEfwsmc').textbox("getValue");
	sParam.LIKEfwsqc = $('#LIKEfwsqc').textbox("getValue");
	sParam.LIKExcr = $('#LIKExcr').textbox("getValue");
	sParam.LIKExcqy = $('#LIKExcqy').textbox("getValue");
	sParam.LIKExcdd = $('#LIKExcdd').textbox("getValue");
	sParam.LIKExcwzjd = $('#LIKExcwzjd').textbox("getValue");
	sParam.LIKExcwzwd = $('#LIKExcwzwd').textbox("getValue");
	sParam.LIKExcsj = $('#LIKExcsj').textbox("getValue");
	sParam.LIKExcnr = $('#LIKExcnr').textbox("getValue");
	sParam.LIKExcjg = $('#LIKExcjg').textbox("getValue");
	sParam.LIKExctp1 = $('#LIKExctp1').textbox("getValue");
	sParam.LIKExctp2 = $('#LIKExctp2').textbox("getValue");
	sParam.LIKExcsp = $('#LIKExcsp').textbox("getValue");
	sParam.LIKExcpjxj = $('#LIKExcpjxj').textbox("getValue");
	sParam.LIKExcpjnr = $('#LIKExcpjnr').textbox("getValue");
	sParam.LIKExcpjr = $('#LIKExcpjr').textbox("getValue");
	sParam.LIKExcpjdw = $('#LIKExcpjdw').textbox("getValue");
	sParam.LIKExcpjsj = $('#LIKExcpjsj').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 time 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 : '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:true,width:120},
			{field:'fwsmc',title:'服务商名称',hidden:false,width:120},
			{field:'fwsqc',title:'服务商全称',hidden:true,width:120},
			{field:'xcr',title:'巡查人',hidden:false,width:120},
			{field:'xcqy',title:'巡查区域',hidden:true,width:120},
			{field:'xcdd',title:'巡查地点',hidden:false,width:120},
			{field:'xcwzjd',title:'巡查位置经度',hidden:true,width:120},
			{field:'xcwzwd',title:'巡查位置维度',hidden:true,width:120},
			{field:'xcsj',title:'巡查时间',hidden:false,width:120},
			{field:'xcnr',title:'巡查内容',hidden:false,width:120},
			{field:'xcjg',title:'巡查结果',hidden:false,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:false,width:120},
			{field:'xcpjnr',title:'巡查评价内容',hidden:false,width:120},
			{field:'xcpjr',title:'巡检评价人',hidden:false,width:120},
			{field:'xcpjdw',title:'巡检评价单位',hidden:false,width:120},
			{field:'xcpjsj',title:'巡检评价时间',hidden:false,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},
			{field:'xh',title:'操作',hidden:false,width:120, 
            	formatter:function showClick(value,record,num){ 
            		return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="appraise(\''+value+'\')" group="" id=""><span class="l-btn-left"><span class="l-btn-text">评价</span></span></a>';
				}
            }
		]]
	});
}
function appraise(data){
	console.log(data);
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	windowId = top.$.createWin({
		title	: 	'评价',
		width	:  	650,
		height	: 	300,
		data 	: selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveAppraise
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjtxcrzgl/jtssxcd/appraise_show.jsp"
	 });
}
function saveAppraise(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	model.XGTJxh = jq('#xh').val();
	console.log(model)
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'nhywjtssxcdAction!appraise_Nhywjtssxcd.do',
		data:model,
		success:function(value){
			if(value=="success"){
				$.showMsg("操作成功！");
				top.$("#"+windowId).dialog('close');
				doSearch();
			}else if(value=="same"){
				$.showMsg("已存在！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//删除
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_Nhywjtssxcd.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	: 	450,
		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	: 	450,
		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/nhywjtssxcd_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'nhywjtssxcdAction!count_Nhywjtssxcd.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(); 
}
