var orgids=localStorage.getItem(getQueryVariable("JSONID")+"orgid");
$(function(){
	if(orgids.length==2){//市级登录
		$("#add").hide();
	}
	initTable();
	$("#add").click(function(){//新增
		dataId='';
		loadYears("year","");
		$("#ls").val("");
		$("#dg").val("");
		$("#hs").val("");
		$("#dd").val("");
		$("#zt").val("");
		$("#sg").val("");
		$("#addModal").modal("show");
	});
	loadYears("selectYear","zzy_ncpzy");
	loadYears("year","");
});
var dataId="";
function save(){//保存或修改
	var ls=$("#ls").val();
	var dg=$("#dg").val();
	var hs=$("#hs").val();
	var dd=$("#dd").val();
	var zt=$("#zt").val();
	var sg=$("#sg").val();
	if(ls!=''&&!moneycheck.test(ls)){
		showModal("粮食人均占有量请输入数字");
		return;
	}
	if(dg!=''&&!moneycheck.test(dg)){
		showModal("稻谷人均占有量请输入数字");
		return;
	}
	if(hs!=''&&!moneycheck.test(hs)){
		showModal("花生人均占有量请输入数字");
		return;
	}
	if(dd!=''&&!moneycheck.test(dd)){
		showModal("大豆人均占有量请输入数字");
		return;
	}
	if(zt!=''&&!moneycheck.test(zt)){
		showModal("蔗糖人均占有量请输入数字");
		return;
	}
	if(sg!=''&&!moneycheck.test(sg)){
		showModal("水果人均占有量请输入数字");
		return;
	}
	$.ajax({
		type:"Post",
		url:"../zzyncpzy/addOrUpdate.service",
		data:{
			year:$("#year").val(),
			ls:ls,
			dg:dg,
			hs:hs,
			dd:dd,
			zt:zt,
			sg:sg,
			id:dataId
		},
		dataType:"json",
		success:function(data){
			showModal(data.message);
			if(data.success=='true'){
				$("#addModal").modal("hide");
				reloadTable();
				loadYears("selectYear","zzy_ncpzy");
			}
		}
	});
}
function edit(rowId){//编辑
	dataId=rowId;
	var rowData = jQuery("#table_list_1").jqGrid("getRowData", rowId); //获取选中行的数据
	$("#ls").val(rowData.ls);
	$("#dg").val(rowData.dg);
	$("#hs").val(rowData.hs);
	$("#dd").val(rowData.dd);
	$("#zt").val(rowData.zt);
	$("#sg").val(rowData.sg);
	$("#year").val(rowData.year);
	$("#addModal").modal("show");
}
function deleteInfo(rowId){//删除某条数据
	modalComfirm("确定要删除吗？",function(res){
		if(res){
			$.ajax({
       			type:"Post",
				url:"../zzyncpzy/delete.service",
				data:{
					id:rowId
				},
				dataType:"json",
				success:function(data){
					showModal(data.message);
					if(data.success=='true'){
						reloadTable();
					}
				}
       		});
		}
	})
}
function exportData(){//导出
	$.ajax({
		type : "post",
		url : "../zzyncpzy/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 : "../zzyncpzy/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 : "../zzyncpzy/getList.service",
		postData : {
			"year":$("#selectYear").val()
		},
		page : 1
	}).trigger("reloadGrid");
}
function initTable(){//表格初始化
	$.jgrid.defaults.styleUI = 'Bootstrap';
	 $("#table_list_1").jqGrid({
		 url:"../zzyncpzy/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: 'ls',
                 index: 'ls',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'dg',
                 index: 'dg',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'hs',
                 index: 'hs',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'dd',
                 index: 'dd',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'zt',
                 index: 'zt',
                 width: 80,
                 formatter:function(cellValue,options,rowData){
                	if(cellValue==0.0){
                		return "";
                	}
                	return cellValue;
                 }
             },
             {
                 name: 'sg',
                 index: 'sg',
                 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);
     }); 
}
