var windowId,sysUrl = top.sysUrl,toEdit;
var winId	= $.getUrlParam("winId");
$(function(){
	initGrid();
});

//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEzcmc = $('#LIKEzcmc').textbox("getValue");
	var bdfs = $('#sfbd').combobox('getValue');
	if(bdfs=="1"){
		sParam.bdfs="1";
	}else if(bdfs=="0"){
		sParam.bdfs="";
	}
	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 : 'nhywjjzcxxAction!listNhywjjzcxxByPage.do',
		columns : [[
			{
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'zlbm',title:'资产编码',hidden:false,width:120,
				formatter:function showClick(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
			},
			{field:'zlmc',title:'资产名称',hidden:false,width:120},
			{field:'zclb',title:'类别',hidden:false,width:120},
			{field:'zcxh',title:'型号',hidden:false,width:120},
			{field:'zcdw',title:'单位',hidden:false,width:120},
			{field:'bdfs',title:'变动方式',hidden:false,width:120},
			{field:'syqk',title:'使用情况',hidden:false,width:120},
			{field:'syrq',title:'使用日期',hidden:false,width:120},
			{field:'cfdd',title:'存放地点',hidden:false,width:120},
			{field:'zcsl',title:'数量',hidden:false,width:120},
			{field:'yzyb',title:'原值原币',hidden:false,width:120},
			{field:'syip',title:'使用ip',hidden:false,width:120},
			{field:'bz',title:'备注',hidden:false,width:120}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected');
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjjzcxxAction!delete_Nhywjjzcxx.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:'nhywjksbtzxxAction!batch_delete_Nhywjksbtzxx.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	: 	500,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjjzcxx/nhywjjzcxx_show.jsp"
	});
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	500,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywjjzcxx/nhywjjzcxx_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.XGTJxh = jq('#xh').val();
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywjjzcxxAction!update_Nhywjjzcxx.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:'nhywjjzcxxAction!insert_Nhywjjzcxx.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/zcxx.xlsx";
	window.location.href= downFile;
}
//导入
function doImport(){
	windowId = top.$.createWin({
		id		:	$.uniqueId(),
		title	: 	'导入',
		width	:  	500,
		height	: 	400,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		url	:	sysUrl+"/yw/nhyw/nhywjjzcxx/nhywjjzcxxemp.jsp"
	});
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'nhywjjzcxxAction!count_Nhywjjzcxx.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 LIKEzcmc =$('#LIKEzcmc').textbox("getValue");
	var bdfs = $('#sfbd').combobox('getValue');
	document.excelForm.action = 'nhywjjzcxxAction!exp_Nhywjjzcxx.do?LIKEzcmc='+LIKEzcmc+"&bdfs="+bdfs;
	document.excelForm.submit();
}
function isNull(str){
	if(str==null || str=='' || str==" " || str=="null" || str == undefined){
		return true;
	}else{
		return false;
	}
}