var orgids=localStorage.getItem(getQueryVariable("JSONID")+"orgid");
$(function(){
	if(orgids.length==2){//市级登录
		$("#add").hide();
	}
	initTable();
	$("#add").click(function(){//新增
		dataId='';
		loadYears("year","");
		$("#ncmj").val("");
		$("#nmmj").val("");
		$("#stmj").val("");
		$("#sjtmj").val("");
		$("#hdmj").val("");
		$("#zjmj").val("");
		$("#jsmj").val("");
		$("#addModal").modal("show");
	});
	loadYears("selectYear","zzy_gdmj");
	loadYears("year","");
});
var dataId="";
function save(){//保存或修改 
	var ncmj=$("#ncmj").val();
	var nmmj=$("#nmmj").val();
	var stmj=$("#stmj").val();
	var sjtmj=$("#sjtmj").val();
	var hdmj=$("#hdmj").val();
	var zjmj=$("#zjmj").val();
	var jsmj=$("#jsmj").val();
	if(ncmj!=''&&!moneycheck.test(ncmj)){
		showModal("年初实有耕地面积请输入数字");
		return;
	}
	if(nmmj!=''&&!moneycheck.test(nmmj)){
		showModal("年末耕地保有量面积请输入数字");
		return;
	}
	if(stmj!=''&&!moneycheck.test(stmj)){
		showModal("水田实有耕地面积请输入数字");
		return;
	}
	if(sjtmj!=''&&!moneycheck.test(sjtmj)){
		showModal("水浇田实有耕地面积请输入数字");
		return;
	}
	if(hdmj!=''&&!moneycheck.test(hdmj)){
		showModal("旱地实有耕地面积请输入数字");
		return;
	}
	if(zjmj!=''&&!moneycheck.test(zjmj)){
		showModal("年内增加耕地面积请输入数字");
		return;
	}
	if(jsmj!=''&&!moneycheck.test(jsmj)){
		showModal("年内减少耕地面积请输入数字");
		return;
	}
	$.ajax({
		type:"Post",
		url:"../zzygdmj/addOrUpdate.service",
		data:{
			year:$("#year").val(),
			ncmj:ncmj,
			nmmj:nmmj,
			stmj:stmj,
			sjtmj:sjtmj,
			hdmj:hdmj,
			zjmj:zjmj,
			jsmj:jsmj,
			id:dataId
		},
		dataType:"json",
		success:function(data){
			showModal(data.message);
			if(data.success=='true'){
				$("#addModal").modal("hide");
				reloadTable();
				loadYears("selectYear","zzy_gdmj");
			}
		}
	});
}
function edit(rowId){//编辑
	dataId=rowId;
	var rowData = jQuery("#table_list_1").jqGrid("getRowData", rowId); //获取选中行的数据
	$("#ncmj").val(rowData.ncmj);
	$("#nmmj").val(rowData.nmmj);
	$("#stmj").val(rowData.stmj);
	$("#sjtmj").val(rowData.sjtmj);
	$("#hdmj").val(rowData.hdmj);
	$("#zjmj").val(rowData.zjmj);
	$("#jsmj").val(rowData.jsmj);
	$("#year").val(rowData.year);
	$("#addModal").modal("show");
}
function deleteInfo(rowId){//删除某条数据
	modalComfirm("确定要删除吗？",function(res){
		if(res){
			$.ajax({
       			type:"Post",
				url:"../zzygdmj/delete.service",
				data:{
					id:rowId
				},
				dataType:"json",
				success:function(data){
					showModal(data.message);
					if(data.success=='true'){
						reloadTable();
					}
				}
       		});
		}
	})
}
function exportData(){//导出
	$.ajax({
		type : "post",
		url : "../zzygdmj/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 : "../zzygdmj/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 : "../zzygdmj/getList.service",
		postData : {
			"year":$("#selectYear").val()
		},
		page : 1
	}).trigger("reloadGrid");
}
function initTable(){//表格初始化
	$.jgrid.defaults.styleUI = 'Bootstrap';
	 $("#table_list_1").jqGrid({
		 url:"../zzygdmj/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: 'ncmj',
                 index: 'ncmj',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'nmmj',
                 index: 'nmmj',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'stmj',
                 index: 'stmj',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'sjtmj',
                 index: 'sjtmj',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'hdmj',
                 index: 'hdmj',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'zjmj',
                 index: 'zjmj',
                 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);
     }); 
}
