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