var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	$('#LIKEalarmdevicetype').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=alarmDeviceDefine&start=0&limit=999999',
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"全部","csdm":""});
				return data;
			}
	});
	$('#LIKEalarmtype').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=ywAlarmType&start=0&limit=999999',
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"全部","csdm":""});
				return data;
			}
	});

	$("#LIKEreceiveinstructionstarttime").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#LIKEreceiveinstructionendtime").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEalarmdevicetype = $('#LIKEalarmdevicetype').combobox("getValue");
//	sParam.LIKEchannelnum = $('#LIKEchannelnum').textbox("getValue");
	sParam.LIKEalarmdevicename = $('#LIKEalarmdevicename').textbox("getValue");
	sParam.LIKEalarmtype = $('#LIKEalarmtype').combobox("getValue");
	var LIKEreceiveinstructionstarttime = $('#LIKEreceiveinstructionstarttime').datetimebox("getValue");
	var LIKEreceiveinstructionendtime = $('#LIKEreceiveinstructionendtime').datetimebox("getValue");
	var tableparam = "";
	if(!(LIKEreceiveinstructionstarttime==null||LIKEreceiveinstructionstarttime==""||LIKEreceiveinstructionstarttime==undefined)){
		tableparam += " AND receiveinstructiontime > '" +LIKEreceiveinstructionstarttime + "'";
	}
	if(!(LIKEreceiveinstructionendtime==null||LIKEreceiveinstructionendtime==""||LIKEreceiveinstructionendtime==undefined)){
		tableparam += " AND receiveinstructiontime < '" +LIKEreceiveinstructionendtime + "'";
	}
	sParam.tableparam = tableparam;
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywalarmsmslogAction!listYwalarmsmslogByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'alarmeventid',title:'告警事件id',hidden:true,width:120},
			{field:'alarmdevicetypeMc',title:'告警设备类型',hidden:false,width:120},
			{field:'alarmdeviceid',title:'告警设备编号',hidden:false,width:120},
			{field:'channelnum',title:'通道编号',hidden:true,width:120},
			{field:'alarmdevicename',title:'告警设备名称',hidden:false,width:120},
			{field:'ywAlarmTypeMc',title:'告警类型',hidden:false,width:120},
			{field:'alarmgrade',title:'告警严重级别',hidden:true,width:120},
			{field:'receiveinstructiontime',title:'发送时间',hidden:false,width:120},
			{field:'callednumber',title:'被叫号码',hidden:false,width:120},
			{field:'smscontent',title:'短信内容',hidden:false,width:120},
			{field:'requestnumber',title:'请求序列号',hidden:true,width:120},
			{field:'statusreportnumber',title:'状态报告序列号',hidden:true,width:120},
			{field:'isstatusreport',title:'是否要求状态报告',hidden:true,width:120},
			{field:'submitstatus',title:'提交状态',hidden:true,width:120},
			{field:'sendstatus',title:'发送状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){
					if (value == '0') {
						return "失败";
					} else if (value == '1') {
						return "成功";
					} else {
						return "";
					}
				}
			},// 0失败，1成功
			{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}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywalarmsmslogAction!delete_Ywalarmsmslog.do',
			data:{
				smslogid	:	selRows.smslogid

			},
			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].smslogid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywalarmsmslogAction!batch_delete_Ywalarmsmslog.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	: 	300,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ptlog/ywalarmsmslog_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ptlog/ywalarmsmslog_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;
	}
	if(toEdit){//修改
		model.XGTJsmslogid = jq('#smslogid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywalarmsmslogAction!update_Ywalarmsmslog.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:'ywalarmsmslogAction!insert_Ywalarmsmslog.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/ywalarmsmslog_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywalarmsmslogAction!count_Ywalarmsmslog.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 = 'ywalarmsmslogAction!exp_Ywalarmsmslog.do?'+downParam;
	document.excelForm.submit(); 
}

function replaceDate(tmpdate){
	var rsdate = "";
	tmpdate = tmpdate+"";
	if(tmpdate&&tmpdate.length==19){
		rsdate = tmpdate.replaceAll("-","").replaceAll(":","").replaceAll(" ","");
	}
	return rsdate;
}
String.prototype.replaceAll = function(s1,s2){ 
	return this.replace(new RegExp(s1,"gm"),s2); 
}
