var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	
	$('#videofactory').combobox({  
		data : $.obj2ary(CommonData.videofactory),  
		textField:1,  
		valueField:0,
		panelWidth:148,
		value:"" 
	});
	$('#cameratype').combobox({  
		data : $.obj2ary(CommonData.cameratype),  
		textField:1,  
		valueField:0,
		panelWidth:148,
		value:"" 
	});
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.videofactory = $('#videofactory').combobox("getValue");
	sParam.cameratype = $('#cameratype').combobox("getValue")
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	sParam.LIKEvideoinstalladdress = $('#LIKEvideoinstalladdress').textbox("getValue");
	sParam.order = "order by 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_batch
    },{
        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 : 'ywarchivevideoinfoAction!listYwarchivevideoinfoByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},		    
			{field:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'videofactory',title:'设备厂商',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.videofactory[value];
				}
			},
			{field:'cameratype',title:'摄像机类型',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.cameratype[value];
				}
			},
			{field:'videoresolution',title:'摄像机分辨率',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.videoresolution[value];
				}
			},
			{field:'videoinstalladdress',title:'安装地址',hidden:false,width:120},
			{field:'videopointname',title:'点位俗称',hidden:false,width:120},
			{field:'installpositiontype',title:'室外、内',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.installpositiontype[value];
				}
			},
			{field:'builddepartment',title:'相机建设部门',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.builddepartment[value];
				}			
			},
			{field:'archivevideoid',title:'详情',hidden:false,width:100, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='getDetail("+num+")'>查看</a>";
				}
 			},
			
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivevideoinfoAction!delete_Ywarchivevideoinfo.do',
			data:{
				archivevideoid	:	selRows.archivevideoid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || selRows.length<1){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].archivevideoid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywarchivevideoinfoAction!batch_delete_Ywarchivevideoinfo.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag=="success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	1000,
		height	: 	600,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/archive/ywarchivevideoinfo/ywarchivevideoinfo_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	1000,
		height	: 	600,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/archive/ywarchivevideoinfo/ywarchivevideoinfo_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;
	}
	model.remarks = jq("#remarks").val();
	
	var useattributes = jq('#useattribute').combobox('getValues');
	var useattribute = useattributes.join('/')
	
	var cameraresolutions = jq('#cameraresolution').combobox('getValues');
	var cameraresolution = cameraresolutions.join(',')
	
	model.useattribute = useattribute;
	model.cameraresolution = cameraresolution;
	
	if(toEdit){//修改
		model.XGTJarchivevideoid = jq('#archivevideoid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivevideoinfoAction!update_Ywarchivevideoinfo.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:'ywarchivevideoinfoAction!insert_Ywarchivevideoinfo.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'))
	
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(selRows.length > 1){
		$.showMsg('请先选择一行! ');
		 return;
	}
	
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var fileName = "一机一档数据标准V1.0.xls";
	$.ajax({
		type : 'post',
		dataType : 'json',
		url : 'ywarchivevideoinfoAction!isDownload.do',
		data : {"fileName" : fileName},
		success : function(res){
			if(res=="success"){
				var downFile = baseUrl+"/download?path="+fileName+"&privatePath=downPath";
				//var downFile = baseUrl+"/download?path="+encodeURI(fileName)+"&privatePath=downPath";
				window.location.href= downFile;
			}else {
				$.showMsg("文件不存在，下载失败！");
			}
		}
	})
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	660,
		 height	: 	500,
		 onClose: doSearch,
		 buttons:  [{
		     text:$.fn.textbox.defaults.ok,
		     handler:uploadFile
	 	 }],
		 url	:	sysUrl+"/yw/archive/ywarchivevideoinfo/ywarchivevideoinfo_imp.jsp"
//		 sysUrl+"/yw/archive/ywarchivevideoinfo/ywarchivevideoinfo_show.jsp"
	 });
}

function uploadFile(){
//	console.log("excel导入===============");
	var frame   =   top.$("#"+windowId).children("iframe")[0];
	var win     =   frame.contentWindow||frame.window;
	var file 	= 	win.$("#myfile").prop('files');
	var checked = win.$('input[name="sbbh"]:checked');
//	console.log("checked: ",checked);
	if (!file.length) {
    	$.showMsg('请选择要上传的文件！');
    	return;
    }
	win.$("#form1").submit();
	$(this).hide();

}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywarchivevideoinfoAction!count_Ywarchivevideoinfo.do',
		success : function(value) {
			if(value>0){
				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 = "";
	
	var videofactory = model.videofactory;
	var cameratype = model.cameratype;
	var LIKEchannelname = model.LIKEchannelname; 
	var LIKEvideoinstalladdress = model.LIKEvideoinstalladdress; 
	var order = model.order;
	
	downParam = "videofactory="+videofactory+"&cameratype="+cameratype+"&LIKEchannelname="+LIKEchannelname+
				"&order="+order+"&LIKEvideoinstalladdress="+LIKEvideoinstalladdress; 
			
	document.excelForm.action = 'ywarchivevideoinfoAction!exp_Ywarchivevideoinfo.do?'+downParam;
	document.excelForm.submit(); 
}

// 查看详情
function getDetail(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	1000,
		height	: 	600,
		data   	:   selRows,
		url	:	sysUrl+"/yw/archive/ywarchivevideoinfo/ywarchivevideoinfo_detail.jsp"
	});
}