 var orgids=localStorage.getItem(getQueryVariable("JSONID")+"orgid");
$(function(){
	if(orgids.length==2){//市级登录
		$("#add").hide();
	}
	initTable();
	$("#add").click(function(){//新增
		dataId='';
		loadYears("year","");
		$("#hs").val('');
		$("#addModal").modal("show");
	});
	loadYears("selectYear","nckj_sfh");
	loadYears("year","");
});
var dataId="";
function save(){//保存或修改
	var hs=$("#hs").val();
	if(hs!=''&&!numcheck.test(hs)){
		showModal("请输入整数");
		return;
	}
	if(hs==''){
		hs=0;
	}
	$.ajax({
		type:"Post",
		url:"../nckjsfh/addOrUpdate.service",
		data:{
			year:$("#year").val(),
			hs:hs,
			id:dataId
		},
		dataType:"json",
		success:function(data){
			showModal(data.message);
			if(data.success=='true'){
				$("#addModal").modal("hide");
				reloadTable();
				loadYears("selectYear","nckj_sfh");
			}
		}
	});
}
function edit(rowId){//编辑
	dataId=rowId;
	var rowData = jQuery("#table_list_1").jqGrid("getRowData", rowId); //获取选中行的数据
	$("#hs").val(rowData.hs);
	$("#year").val(rowData.year);
	$("#addModal").modal("show");
}
function deleteInfo(rowId){//删除某条数据
	modalComfirm("确定要删除吗？",function(res){
		if(res){
			$.ajax({
       			type:"Post",
				url:"../nckjsfh/delete.service",
				data:{
					id:rowId
				},
				dataType:"json",
				success:function(data){
					showModal(data.message);
					if(data.success=='true'){
						reloadTable();
					}
				}
       		});
		}
	})
}
function exportData(){//导出
	$.ajax({
		type : "post",
		url : "../nckjsfh/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 : "../nckjsfh/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 : "../nckjsfh/getList.service",
		postData : {
			"year":$("#selectYear").val()
		},
		page : 1
	}).trigger("reloadGrid");
}
function initTable(){//表格初始化
	$.jgrid.defaults.styleUI = 'Bootstrap';
	 $("#table_list_1").jqGrid({
		 url:"../nckjsfh/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: 100
             },
             {
                 name: 'year',
                 index: 'year',
                 width: 80
             },
             {
                 name: 'hs',
                 index: 'hs',
                 width: 80,
                 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);
     }); 
}
