/**
 * 农产品进出口贸易js
 */
$(function(){
	var orgid = localStorage.getItem(getQueryVariable("JSONID")+"orgid");
	if(orgid.length>2){
		$("#addBtn").show();
		$("#xd").show();
	}else{ 
		$("#sd").show();
	}
	loadYears("year","zzy_scl")
	loadYears("baseyear")
	initTable();
})
/**
 * 新增录入
 */
function addBtn(){
	$("#hiddenid").val("");
	$("#scbzmj").val("");
	$("#sczcl").val("");
	$("#glbzmj").val("");
	$("#glzcl").val("");
	$("#baseModal").modal("show");
}
/**
 * 编辑修改
 */
function edit(id){
	var rowdata = jQuery("#table_list_1").jqGrid("getRowData", id);
	$("#hiddenid").val(id);
	$("#baseyear").val(rowdata.year)
	$("#scbzmj").val(rowdata.scbzmj);
	$("#sczcl").val(rowdata.sczcl);
	$("#glbzmj").val(rowdata.glbzmj);
	$("#glzcl").val(rowdata.glzcl);
	$("#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 scbzmj = $("#scbzmj").val()==""?"0":$("#scbzmj").val();
	var sczcl = $("#sczcl").val()==""?"0":$("#sczcl").val();
	var glbzmj = $("#glbzmj").val()==""?"0":$("#glbzmj").val();
	var glzcl = $("#glzcl").val()==""?"0":$("#glzcl").val();
	var bzzmj = parseFloat(scbzmj)+parseFloat(glbzmj);
	var zcl = parseFloat(sczcl)+parseFloat(glzcl);  
	$.ajax({
		type:"Post",
		url:"../scl/saveRecord.service",
		data:{ 
			id:id,
			year:year,
			scbzmj:scbzmj,
			sczcl:sczcl,
			glbzmj:glbzmj,
			glzcl:glzcl,
			bzzmj:bzzmj,
			zcl:zcl
		},
		dataType:"json",
		success:function(data){
			if(data.success=="true"){
				if(id==""){
					loadYears("year","zzy_scl")
				}
				$("#baseModal").modal("hide");
				showModal(data.message);
				reLoadTable();
			}else{
				showModal(data.message);
				return;
			}
		}
	});
}
/**
 * 删除信息
 */
function deleteInfo(id){
	modalComfirm("确定删除该条信息吗",function(res){
		if(res){
			$.ajax({
				type:"Post",
				url:"../scl/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 : "../scl/getRecordList.service",
		postData : {
			'year' : $("#year").val()
		}, //发送数据 
		page : 1
	}).trigger("reloadGrid"); //重新载入  
}
	
/* 初始化表格 */
function initTable() {
	$.jgrid.defaults.styleUI = 'Bootstrap';  
	$("#table_list_1").jqGrid({
		url : "../scl/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: 60,align:'center',formatter :cLink,sortable:false },
			{  name : 'orgname',index : 'orgname',width: 140,sortable:false},
			{  name : 'year',index : 'year',width: 140,sortable:false},
			{  name : 'scbzmj',index : 'scbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'sczcl',index : 'sczcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'glbzmj',index : 'glbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'glzcl',index : 'glzcl',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:'scbzmj', numberOfColumns:2, titleText: '蔬菜'},
	        {startColumnName:'glbzmj', 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 : "../scl/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 : "../scl/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reLoadTable();
			}
		}
	});
}