var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	
	
	$('#LIKEbuilddepartment').combobox({  
		 data : $.obj2ary(CommonData.builddepartment),  
		 textField:1,  
		 valueField:0,
		 panelWidth:148,
		 value:" " 
	});
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	var str = $('#LIKEbuilddepartment').textbox("getValue");
	if(str == " "){
		str = "";
	}
	sParam.builddepartment = str
	sParam.LIKEareacode = $('#LIKEareacode').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
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywarchivedepartmentAction!listYwarchivedepartmentByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'departmentid',title:'departmentid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'builddepartment',title:'摄像机建设部门',hidden:false,width:140,
 				formatter:function showClick(value,record,num){ 
					return CommonData.builddepartment[value];
				}					
			},
			{field:'areacode',title:'所属行政区域编码',hidden:false,width:120},
			{field:'secreyattribute',title:'保密属性',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.secreyattribute[value];
				}	
			},
			{field:'videofunctiontype',title:'摄像机功能类型',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.videofunctiontype[value];
				}	
			},
			{field:'areapolicecode',title:'所属辖区公安机关编码',hidden:false,width:140},
			{field:'areapolicename',title:'所属辖区公安机关名称',hidden:false,width:140},
			{field:'videopolicetype',title:'摄像机所属警种',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return CommonData.videopolicetype[value];
				}
			},
			{field:'videopointtype',title:'监控点类型',hidden:true,width:120},
			{field:'manageunitcode',title:'管理单位编码',hidden:true,width:120},
			{field:'manageunitname',title:'管理单位名称',hidden:true,width:120},
			{field:'manageunitphone',title:'管理单位联系电话',hidden:true,width:120},
			{field:'manageunitaccount',title:'管理单位联系人',hidden:true,width:120},
			{field:'useattribute',title:'使用属性',hidden:true,width:120},
			{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:'ywarchivedepartmentAction!delete_Ywarchivedepartment.do',
			data:{
				departmentid	:	selRows.departmentid

			},
			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].departmentid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywarchivedepartmentAction!batch_delete_Ywarchivedepartment.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	:  	650,
		height	: 	480,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/archive/videoarchive/ywarchivedepartment_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	480,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/archive/videoarchive/ywarchivedepartment_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();
	if(toEdit){//修改
		model.XGTJdepartmentid = jq('#departmentid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivedepartmentAction!update_Ywarchivedepartment.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+"' ";
		var useattribute = '';
		var chosed = jq('input[name="useattribute"]:checked');
		$.each(chosed,function(){			
			useattribute += $(this).val() + '/';			
		})
//		console.log("选中：",useattribute);
		model.useattribute = useattribute;		
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywarchivedepartmentAction!insert_Ywarchivedepartment.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/ywarchivedepartment_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywarchivedepartmentAction!count_Ywarchivedepartment.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 = 'ywarchivedepartmentAction!exp_Ywarchivedepartment.do?'+downParam;
	document.excelForm.submit(); 
}
