/**
 * 农村居民收入与支出js
 */
$(function(){
	var orgid = localStorage.getItem(getQueryVariable("JSONID")+"orgid");
	if(orgid.length>2){
		$("#addBtn").show();
		$("#xd").show();
	}else{  
		$("#sd").show();
	}
	loadYears("year","nyzb_njzl")
	loadYears("baseyear")
	initTable();
})
/**
 * 新增录入
 */
function addBtn(){
	$("#hiddenid").val("");
	$("#gzj").val("");
	$("#wgj").val("");
	$("#sb").val("");
	$("#jsggjx").val("");
	$("#jgfshtj").val("");
	$("#zbwrj").val("");
	$("#gwlhsgj").val("");
	$("#jdtlj").val("");
	$("#zyj").val("");
	$("#trj").val("");
	$("#baseModal").modal("show");
}
/**
 * 编辑修改
 */
function edit(id){
	var rowdata = jQuery("#table_list_1").jqGrid("getRowData", id);
	$("#hiddenid").val(id);
	$("#baseyear").val(rowdata.year)
	$("#gzj").val(rowdata.gzj);
	$("#wgj").val(rowdata.wgj);
	$("#sb").val(rowdata.sb);
	$("#jsggjx").val(rowdata.jsggjx);
	$("#jgfshtj").val(rowdata.jgfshtj);
	$("#zbwrj").val(rowdata.zbwrj);
	$("#gwlhsgj").val(rowdata.gwlhsgj);
	$("#jdtlj").val(rowdata.jdtlj);
	$("#zyj").val(rowdata.zyj);
	$("#trj").val(rowdata.trj);
	$("#baseModal").modal("show");
}

/**
 * 保存信息
 */
function saveBtn(){
	var id = $("#hiddenid").val();
	var year = $("#baseyear").val();
	var flag = true
	$("input[name=money]").each(function(i,item){
		var value = item.value;
		if(value!=""&&!numcheck.test(value)){
			flag= false
			showModal("请输入正确的"+item.getAttribute("data-name")+"整数")
			return;
		}
	})
	if(!flag){
		return;
	}
	var gzj = $("#gzj").val()==""?"0":$("#gzj").val();
	var wgj = $("#wgj").val()==""?"0":$("#wgj").val();
	var sb = $("#sb").val()==""?"0":$("#sb").val();
	var jsggjx = $("#jsggjx").val()==""?"0":$("#jsggjx").val();
	var jgfshtj = $("#jgfshtj").val()==""?"0":$("#jgfshtj").val();
	var zbwrj = $("#zbwrj").val()==""?"0":$("#zbwrj").val();
	var gwlhsgj = $("#gwlhsgj").val()==""?"0":$("#gwlhsgj").val();
	var jdtlj = $("#jdtlj").val()==""?"0":$("#jdtlj").val();
	var zyj = $("#zyj").val()==""?"0":$("#zyj").val();
	var trj = $("#trj").val()==""?"0":$("#trj").val();
	$.ajax({
		type:"Post",
		url:"../njzl/saveRecord.service",
		data:{ 
			id:id,
			year:year,
			gzj:gzj,
			wgj:wgj,
			sb:sb,
			jsggjx:jsggjx,
			jgfshtj:jgfshtj,
			zbwrj:zbwrj,
			gwlhsgj:gwlhsgj,
			jdtlj:jdtlj,
			zyj:zyj,
			trj:trj
		},
		dataType:"json",
		success:function(data){
			if(data.success=="true"){
				if(id==""){
					loadYears("year","nyzb_njzl")
				}
				$("#baseModal").modal("hide");
				showModal(data.message);
				reLoadTable();
			}else{
				showModal(data.message);
				return;
			}
		}
	});
}
/**
 * 删除信息
 */
function deleteInfo(id){
	modalComfirm("确定删除该条信息吗",function(res){
		if(res){
			$.ajax({
				type:"Post",
				url:"../njzl/deleteRecord.service",
				data:{ 
					id:id
				},
				dataType:"json",
				success:function(data){
					if(data.success=="true"){
						showModal(data.message);
						reLoadTable();
					}else{
						showModal(data.message);
						return;
					}
				}
			});
		}
	})
}

/* 重新加载表格 */
function reLoadTable() {
	$("#table_list_1").jqGrid('setGridParam', {
		url : "../njzl/getRecordList.service",
		postData : {
			'year' : $("#year").val()
		}, //发送数据 
		page : 1
	}).trigger("reloadGrid"); //重新载入  
}
	
/* 初始化表格 */
function initTable() {
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#table_list_1").jqGrid({
		url : "../njzl/getRecordList.service",
		loadBeforeSend: function(xhr){
			xhr.setRequestHeader("token",getQueryVariable("JSONID"));
        },
		datatype : "json",
		height : 500,
		autowidth : true,
		shrinkToFit : true,
		rowNum : 50,
		rowList : [ 50, 100, 150 ],
		jsonReader : {
			root : "list",
			page : "currPage",
			total : "totalpages", //   很重要 定义了 后台分页参数的名字。
			records : "count",
			repeatitems : false,
			id : "id"
		},
		colNames : [ '操作', '部门','年度','耕整机(套)','微耕机(套)','水泵(套)','节水灌溉机械(套)','秸秆粉碎还田机(台)','植保无人机(台)','谷物联合收割机(台)','机动脱粒机(台)','增氧机(台)','投饵机(台)','1'],
		colModel : [
			{  name : 'caozuo',width: 50,align:'center',formatter :cLink,sortable:false },
			{  name : 'orgname',index : 'orgname',width: 80,sortable:false},
			{  name : 'year',index : 'year',width: 80,sortable:false},
			{  name : 'gzj',index : 'gzj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'wgj',index : 'wgj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'sb',index : 'sb',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'jsggjx',index : 'jsggjx',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'jgfshtj',index : 'jgfshtj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'zbwrj',index : 'zbwrj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'gwlhsgj',index : 'gwlhsgj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'jdtlj',index : 'jdtlj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'zyj',index : 'zyj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'trj',index : 'trj',width: 80,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'orgid',index : 'orgid',width: 140,sortable:false,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);
	});
}

/* 表格操作事件 */
function cLink(cellvalue, options, rowObject){  
	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>";
}

/**
 * 导出
 */
function exportData(){
	$.ajax({
		type : "post",
		url : "../njzl/exprotRecord.service",
		data : {
			'year':$("#year").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 : "../njzl/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reLoadTable();
			}
		}
	});
}