var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var proName = window.location.pathname.split("/")[1];
var sysUrl = "http://"+window.location.host+"/"+proName;
$(function(){
	initDefault();	
	initTreePanel();
	initSelect();
});
//默认
function initDefault(){
	
	initGrid();
}
function initSelect(){
	var ywRecordStatusArr = [
	    {"id":"","text":"全部"},
	    {"id":"1","text":"正常"},
	    {"id":"0","text":"异常"}];
	
	$("#recordstate").combobox( "loadData", ywRecordStatusArr);
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	var xzbh = $("#xzbh").combotree("getValue");
	if(xzbh!="-1") {
		sParam.xzbh = xzbh;
	}
	sParam.recordstate = $("#recordstate").combotree("getValue");
//	sParam.countdateStart = $('#countdateStart').datebox("getValue");
//	sParam.countdateEnd = $('#countdateEnd').datebox("getValue");
	sParam.order = "order by a.createtime desc ";
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [/*{
    	text    :	'刷新',
        iconCls : 	'icon-refresh',
        handler : 	function(){location.reload(true);}
    },*/{
    	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:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywrecordcurrentstateAction!listYwrecordcurrentstateByPage.do',
		columns : [[
		    /*{
				field: 'A',
				checkbox:'false',
				width:30
			},*/
		    /*{field:'recordstateid',title:'recordstateid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},*/
			{field:'channelname',title:'设备名称',hidden:false,width:120,
				formatter:function showClick(value,record,num){
					if (record.channelname == null) {
						return record.channelname2;
					} else {
						return record.channelname;
					}
				}
			},
			{field:'sbip',title:'设备IP',hidden:false,width:120},
			{field:'cjmc',title:'厂家名称',hidden:false,width:120},
			{field:'videocatalog',title:'所属区域',hidden:true,width:120},
			{field:'countdate',title:'检测日期',hidden:false,width:120},
			{field:'recordstate',title:'录像状态',hidden:false,width:60,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'twentyfourstate',title:'0',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'onestate',title:'1',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'twostate',title:'2',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'threestate',title:'3',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fourstate',title:'4',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fivestate',title:'5',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sixstate',title:'6',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sevenstate',title:'7',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'eightstate',title:'8',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'ninestate',title:'9',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'tenstate',title:'10',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'elevenstate',title:'11',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twelvestate',title:'12',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'thirteenstate',title:'13',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fourteenstate',title:'14',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'fifteenstate',title:'15',hidden:false,width:25,				
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sixteenstate',title:'16',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'seventeenstate',title:'17',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'eighteenstate',title:'18',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'nineteenstate',title:'19',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentystate',title:'20',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentyonestate',title:'21',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentytwostate',title:'22',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentythreestate',title:'23',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			
			{field:'recordstateid',title:'操作',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					var videonum = record.videonum;			// 设备编号
					var channelnum = record.channelnum;		// 通道编号
					var channelname=record.channelname;
        			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="jumpPage(\''+value+'\',\''+videonum+'\',\''+channelnum+'\',\''+channelname+'\')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';

				/*	return "<a  href='javascript:void(0);' class='ahref' onclick='jumpPage(\""+value+"\",\""+videonum+"\",\""+channelnum+"\")'>"+"操作1  "+"</a>" +
				//			"<embsp/><embsp/><a  href='javascript:void(0);' class='ahref' onclick='playVideo("+videonum+","+channelnum+")'>"+" 播放录像"+"</a>" +
							"<embsp/><embsp/><a  href='javascript:void(0);' class='ahref' onclick='playRecordVideo("+num+")'>"+" 播放录像2"+"</a>";*/
				}
			},
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywrecordcurrentstateAction!delete_Ywrecordcurrentstate.do',
			data:{
				recordstateid	:	selRows.recordstateid
			},
			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].recordstateid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywrecordcurrentstateAction!batch_delete_Ywrecordcurrentstate.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,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywrecordcurrentstate_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywrecordcurrentstate_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.XGTJrecordstateid = jq('#recordstateid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywrecordcurrentstateAction!update_Ywrecordcurrentstate.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:'ywrecordcurrentstateAction!insert_Ywrecordcurrentstate.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/ywrecordcurrentstate_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywrecordcurrentstateAction!count_Ywrecordcurrentstate.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 = 'ywrecordcurrentstateAction!exp_Ywrecordcurrentstate.do?'+downParam;
	document.excelForm.submit(); 
}

function getBlock(flag){		// 根据 状态码 1-正常  0-异常  返回红色或绿色块
	if(flag == 1){
		return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}else if(flag == 0){
		return "<div style='height:13px;width:13px;background-color:red;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}else{
		return flag;
	}
}

function jumpPage(recordstateid, videonum, channelnum,channelname){			// 点击操作跳转页面
//console.log("pppppppppppppp=====",recordstateid, videonum, channelnum, channelname)
	windowId = top.$.createWin({
		title	: 	'详情',
		width	:  	790,
		height	: 	500,
		url	:	sysUrl+"/yw/recordalarm/ywrecordcurrentstate/ywrecordvideostateinfo.jsp?recordstateid="+recordstateid+"&videonum="+videonum+"&channelnum="+channelnum+"&channelname="+encodeURI(channelname)
	 });	
}

function playVideo(videonum, channelnum){
	windowId = top.$.createWin({
		title	: 	' ',
		width	:  	690,
		height	: 	400,  
		url	:	sysUrl+"/yw/recordalarm/ywrecordcurrentstate/recordvideo/playrecordvideo.jsp?videonum="+videonum+"&channelnum="+channelnum
	 });
}

function playRecordVideo(index){
	var selRows = $('#tableGrid').datagrid('getRows')[index];
//	console.log("选择：",selRows);
	windowId = top.$.createWin({
		title	: 	'录像',
		width	:  	700,
		height	: 	380, 
		data  :  selRows,
		url	:	sysUrl+"/yw/recordalarm/ywrecordcurrentstate/playvideo/playvideo.jsp",
		onClose : closeRecordVideo
	});
}

function closeRecordVideo(){
	var win		=	$.getTopWindowById(windowId);
	win.closeRecordvideo();
}
//初始化行政树
function initTreePanel(){
	$.ajax({
		 type:'post',
//		 async:false,
		 dataType : 'json',
		 url:'menuConfigAction!query_Ptxzs.do',
//		 url:'ptxzsAction!query_Ptxzs.do',
//		 data:{
//			 order : " order by jdpx asc "
//		 },
	 }).done(function(data){
		 $.each(data,function(i,o){
				o.state 	='closed';
				o.treeId 	= o.xzbh;
				o.name 		= o.xzmc;
				o.treeType 	= "xz";
				o.id		= o.xzbh;
				o.pid		= o.sjxzbh;
				o.start 	= 0; //iconCls
			}) 
			var xzfz = {};
		 	xzfz.treeType 	= "xz";
			xzfz.id		= "-1";
			xzfz.pid		= "xzTree";
			xzfz.start 	= 0; //iconCls
		 	xzfz.xzbh = "-1";
		 	xzfz.xzmc = "行政分组";
		 	xzfz.state = "closed";
		 	xzfz.treeId = "-1";
		 	xzfz.sjxzbh = "xzTree";
		 	xzfz.name = "行政分组";
			data.push(xzfz);
			$('#xzbh').combotree({
				checkbox:false,
				border:false,
				parentField:"pid",
				textFiled:"name",
				idFiled:"id",
				panelWidth:148,
				data:data,
				onLoadSuccess:function(node,data){
					if(data){
						
					}
				},
				onClick: function(node){
					
				},
				data   : data
			});
	});
}