/**
 * 农产品进出口贸易js
 */
$(function(){
	var orgid = localStorage.getItem(getQueryVariable("JSONID")+"orgid");
	if(orgid.length>2){
		$("#addBtn").show();
		$("#xd").show();
	}else{
		$("#sd").show();
	}
	loadYears("year","zzy_jjzw")
	loadYears("baseyear")
	initTable();
})
/**
 * 新增录入
 */
function addBtn(){
	$("#hiddenid").val("");
	$("#ylbzmj").val("");
	$("#ylzcl").val("");
	$("#gzbzmj").val("");
	$("#gzzcl").val("");
	$("#cybzmj").val("");
	$("#cyzcl").val("");
	$("#baseModal").modal("show");
}
/**
 * 编辑修改
 */
function edit(id){
	var rowdata = jQuery("#table_list_1").jqGrid("getRowData", id);
	$("#hiddenid").val(id);
	$("#baseyear").val(rowdata.year)
	$("#ylbzmj").val(rowdata.ylbzmj);
	$("#ylzcl").val(rowdata.ylzcl);
	$("#gzbzmj").val(rowdata.gzbzmj);
	$("#gzzcl").val(rowdata.gzzcl);
	$("#cybzmj").val(rowdata.cybzmj);
	$("#cyzcl").val(rowdata.cyzcl);
	$("#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!=""&&!moneycheck.test(value)){
			flag = false
			showModal("请输入正确的"+item.getAttribute("data-name")+"数字")
			return;
		}
		
	})
	if(!flag){
		return;
	}
	var ylbzmj = $("#ylbzmj").val()==""?"0":$("#ylbzmj").val();
	var ylzcl = $("#ylzcl").val()==""?"0":$("#ylzcl").val();
	var gzbzmj = $("#gzbzmj").val()==""?"0":$("#gzbzmj").val();
	var gzzcl = $("#gzzcl").val()==""?"0":$("#gzzcl").val();
	var cybzmj = $("#cybzmj").val()==""?"0":$("#cybzmj").val();
	var cyzcl = $("#cyzcl").val()==""?"0":$("#cyzcl").val();
	var bzzmj = parseFloat(ylbzmj)+parseFloat(gzbzmj)+parseFloat(cybzmj);
	var zcl = parseFloat(ylzcl)+parseFloat(gzzcl)+parseFloat(cyzcl);  
	$.ajax({
		type:"Post",
		url:"../jjzw/saveRecord.service",
		data:{ 
			id:id,
			year:year,
			ylbzmj:ylbzmj,
			ylzcl:ylzcl,
			gzbzmj:gzbzmj,
			gzzcl:gzzcl,
			cybzmj:cybzmj,
			cyzcl:cyzcl,
			bzzmj:bzzmj,
			zcl:zcl
		},
		dataType:"json",
		success:function(data){
			if(data.success=="true"){
				if(id==""){
					loadYears("year","zzy_jjzw")
				}
				$("#baseModal").modal("hide");
				showModal(data.message);
				reLoadTable();
			}else{
				showModal(data.message);
				return;
			}
		}
	});
}
/**
 * 删除信息
 */
function deleteInfo(id){
	modalComfirm("确定删除该条信息吗",function(res){
		if(res){
			$.ajax({
				type:"Post",
				url:"../jjzw/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 : "../jjzw/getRecordList.service",
		postData : {
			'year' : $("#year").val()
		}, //发送数据 
		page : 1
	}).trigger("reloadGrid"); //重新载入  
}
	
/* 初始化表格 */
function initTable() {
	$.jgrid.defaults.styleUI = 'Bootstrap';  
	$("#table_list_1").jqGrid({
		url : "../jjzw/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: 80,align:'center',formatter :cLink,sortable:false },
			{  name : 'orgname',index : 'orgname',width: 140,sortable:false},
			{  name : 'year',index : 'year',width: 140,sortable:false},
			{  name : 'ylbzmj',index : 'ylbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'ylzcl',index : 'ylzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'gzbzmj',index : 'gzbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'gzzcl',index : 'gzzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'cybzmj',index : 'cybzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'cyzcl',index : 'cyzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'bzzmj',index : 'bzzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'zcl',index : 'zcl',width: 140,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
	}).jqGrid('setGroupHeaders', {
	    useColSpanStyle: true,
	    groupHeaders:[
	        {startColumnName:'ylbzmj', numberOfColumns:2, titleText: '油料'},
	        {startColumnName:'gzbzmj', numberOfColumns: 2, titleText: '甘蔗'},
	        {startColumnName:'cybzmj', numberOfColumns: 2, titleText: '茶叶'},
	    ]
	});;
	$(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 : "../jjzw/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 : "../jjzw/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reLoadTable();
			}
		}
	});
}