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