var orgids=localStorage.getItem(getQueryVariable("JSONID")+"orgid");
$(function(){
	if(orgids.length==2){//市级登录
		$("#add").hide();
	}
	initTable();
	$("#add").click(function(){//新增
		dataId='';
		loadYears("year","");
		$("#jgs").val("");
		$("#rs").val("");
		$("#hjgs").val("");
		$("#hrs").val("");
		$("#nmrs").val("");
		$("#addModal").modal("show");
	});
	loadYears("selectYear","nyzb_user");
	loadYears("year","");
});
var dataId="";
function save(){//保存或修改
	var jgs=$("#jgs").val();
	var rs=$("#rs").val();
	var hjgs=$("#hjgs").val();
	var hrs=$("#hrs").val();
	var nmrs=$("#nmrs").val();
	if(jgs!=''&&!numcheck.test(jgs)){
		showModal("农机组织机构数请输入整数");
		return;
	}
	if(jgs==''){
		jgs=0;
	}
	if(rs!=''&&!numcheck.test(rs)){
		showModal("农机服务组织人数请输入整数");
		return;
	}
	if(rs==''){
		rs=0;
	}
	if(hjgs!=''&&!numcheck.test(hjgs)){
		showModal("农机户机构数请输入整数");
		return;
	}
	if(hjgs==''){
		hjgs=0;
	}
	if(hrs!=''&&!numcheck.test(hrs)){
		showModal("农机户人数请输入整数");
		return;
	}
	if(hrs==''){
		hrs=0;
	}
	if(nmrs!=''&&!numcheck.test(nmrs)){
		showModal("乡村农机从业人员年末人数请输入整数");
		return;
	}
	if(nmrs==''){
		nmrs=0;
	}
	$.ajax({
		type:"Post",
		url:"../nyzbuser/addOrUpdate.service",
		data:{
			year:$("#year").val(),
			jgs:jgs,
			rs:rs,
			hjgs:hjgs,
			hrs:hrs,
			nmrs:nmrs,
			id:dataId
		},
		dataType:"json",
		success:function(data){
			showModal(data.message);
			if(data.success=='true'){
				$("#addModal").modal("hide");
				reloadTable();
				loadYears("selectYear","nyzb_user");
			}
		}
	});
}
function edit(rowId){//编辑
	dataId=rowId;
	var rowData = jQuery("#table_list_1").jqGrid("getRowData", rowId); //获取选中行的数据
	$("#jgs").val(rowData.jgs);
	$("#rs").val(rowData.rs);
	$("#hjgs").val(rowData.hjgs);
	$("#hrs").val(rowData.hrs);
	$("#nmrs").val(rowData.nmrs);
	$("#year").val(rowData.year);
	$("#addModal").modal("show");
}
function deleteInfo(rowId){//删除某条数据
	modalComfirm("确定要删除吗？",function(res){
		if(res){
			$.ajax({
       			type:"Post",
				url:"../nyzbuser/delete.service",
				data:{
					id:rowId
				},
				dataType:"json",
				success:function(data){
					showModal(data.message);
					if(data.success=='true'){
						reloadTable();
					}
				}
       		});
		}
	})
}
function exportData(){//导出
	$.ajax({
		type : "post",
		url : "../nyzbuser/exportData.service",
		data : {
			'year':$("#selectYear").val()
		},
		dataType : "json",
		success : function(data) {
			if (data.success == "true") {
				showModal("导出成功:<a href='../" + data.data + "'>点击下载</a>");
			} else {
				showModal(data.message);
			}
		}
	});
}
function showImport(){//显示导入窗口
	$("#fileElement").val('');
	$('#modalImport').modal("show");
}
function importData() {
	var form = new FormData(document.getElementById("fom")); //获取from表单
	var fileElement = $("#fileElement").val();
	if (fileElement == "") {
		showModal("请选择导入文件"); //提示框
		return;
	}
	$('#modalImport').modal("hide");
	$.ajax({
		type : "Post",
		url : "../nyzbuser/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reloadTable();
			}
		}
	});
}
function reloadTable(){//重新加载表格
	$("#table_list_1").jqGrid('setGridParam', {
		url : "../nyzbuser/getList.service",
		postData : {
			"year":$("#selectYear").val()
		},
		page : 1
	}).trigger("reloadGrid");
}
function initTable(){//表格初始化
	$.jgrid.defaults.styleUI = 'Bootstrap';
	 $("#table_list_1").jqGrid({
		 url:"../nyzbuser/getList.service", 
         datatype: "json",
         loadBeforeSend: function(xhr){
         	xhr.setRequestHeader("token",getQueryVariable("JSONID"));
         },
         height: 500,
         autowidth: true,
         shrinkToFit: true,
         rowNum: 30,
         rowList: [30, 60, 90],
         jsonReader: {  
         root:"list", page:"currPage", total:"totalpages",          //   很重要 定义了 后台分页参数的名字。
		 records:"count", repeatitems:false, id : "id"
    	 },
         colNames: ['操作','部门','年度',"农机组织机构数(个)","农机服务组织人数(人)","农机户机构数(个)","农机户人数(人)","乡村农机从业人员年末人数(人)",'orgid','id'],
         colModel: [
             {
                 name: 'type',
                 index: 'type',
                 width: 50,
                 formatter: function(cellValue,options,rowData){
                		 return "<a title='编辑' onclick='edit(" + options.rowId + ")'>" +
             	 		"<span class='glyphicon glyphicon-pencil'></span></a>&nbsp&nbsp<a title='删除' onclick='deleteInfo("+options.rowId+")'>" +
             	 		"<span style='color:red' class='glyphicon glyphicon-remove'></span></a>";
                 }
             },
             {
                 name: 'orgname',
                 index: 'orgname',
                 width: 70
             },
             {
                 name: 'year',
                 index: 'year',
                 width: 40
             },
             {
                 name: 'jgs',
                 index: 'jgs',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'rs',
                 index: 'rs',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'hjgs',
                 index: 'hjgs',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'hrs',
                 index: 'hrs',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'nmrs',
                 index: 'nmrs',
                 width: 120,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'orgid',
                 index: 'orgid',
                 hidden:true
             },
             {
                 name: 'id',
                 index: 'id',
                 hidden:true
             }
         ],
         pager: "#pager_list_1",
         viewrecords: true,
         multiselect: false,
         caption: "",
         hidegrid: false
     });
     $(window).bind('resize', function () {
         var width = $('.jqGrid_wrapper').width();
         $('#table_list_1').setGridWidth(width);
     }); 
}
