/**
 * 农产品进出口贸易js
 */
$(function(){
	var orgid = localStorage.getItem(getQueryVariable("JSONID")+"orgid");
	if(orgid.length>2){
		$("#addBtn").show();
		$("#xd").show();
	}else{
		$("#sd").show();
	}
	
	loadYears("year","ncjj_jck")
	loadYears("baseyear")
	initTable();
})
/**
 * 新增录入
 */
function addBtn(){
	$("#hiddenid").val("");
	$("#dwcp_in").val("");
	$("#dwcp_out").val("");
	$("#zwcp_in").val("");
	$("#zwcp_out").val("");
	$("#yzjl_in").val("");
	$("#yzjl_out").val("");
	$("#yczp_int").val("");
	$("#yczp_int").val("");
	$("#qt_in").val("");
	$("#qt_out").val("");
	$("#baseModal").modal("show");
}
/**
 * 编辑修改
 */
function edit(id){
	var rowdata = jQuery("#table_list_1").jqGrid("getRowData", id);
	$("#hiddenid").val(id);
	$("#baseyear").val(rowdata.year)
	$("#dwcp_in").val(rowdata.dwcp_in);
	$("#dwcp_out").val(rowdata.dwcp_out);
	$("#zwcp_in").val(rowdata.zwcp_in);
	$("#zwcp_out").val(rowdata.zwcp_out);
	$("#yzjl_in").val(rowdata.yzjl_in);
	$("#yzjl_out").val(rowdata.yzjl_out);
	$("#yczp_int").val(rowdata.yczp_int);
	$("#yczp_out").val(rowdata.yczp_out);
	$("#qt_in").val(rowdata.qt_in);
	$("#qt_out").val(rowdata.qt_out);
	$("#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 dwcp_in = $("#dwcp_in").val()==""?"0":$("#dwcp_in").val();
	var dwcp_out = $("#dwcp_out").val()==""?"0":$("#dwcp_out").val();
	var zwcp_in = $("#zwcp_in").val()==""?"0":$("#zwcp_in").val();
	var zwcp_out = $("#zwcp_out").val()==""?"0":$("#zwcp_out").val();
	var yzjl_in = $("#yzjl_in").val()==""?"0":$("#yzjl_in").val();
	var yzjl_out = $("#yzjl_out").val()==""?"0":$("#yzjl_out").val();
	var yczp_int = $("#yczp_int").val()==""?"0":$("#yczp_int").val();
	var yczp_out = $("#yczp_out").val()==""?"0":$("#yczp_out").val();
	var qt_in = $("#qt_in").val()==""?"0":$("#qt_in").val();
	var qt_out = $("#qt_out").val()==""?"0":$("#qt_out").val();
	var inzj = parseFloat(dwcp_in)+parseFloat(zwcp_in)+parseFloat(yzjl_in)+parseFloat(yczp_int)+parseFloat(qt_in);
	var outzj = parseFloat(dwcp_out)+parseFloat(zwcp_out)+parseFloat(yzjl_out)+parseFloat(yczp_out)+parseFloat(qt_out);
	var inoutzj = parseFloat(inzj)+parseFloat(outzj);  
	
	$.ajax({
		type:"Post",
		url:"../jck/saveJinChuKouInfo.service",
		data:{ 
			id:id,
			year:year,
			dwcp_in:dwcp_in,
			dwcp_out:dwcp_out,
			zwcp_in:zwcp_in,
			zwcp_out:zwcp_out,
			yzjl_in:yzjl_in,
			yzjl_out:yzjl_out,
			yczp_int:yczp_int,
			yczp_out:yczp_out,
			qt_in:qt_in,
			qt_out:qt_out,
			inzj:inzj,
			outzj:outzj,
			inoutzj:inoutzj
		},
		dataType:"json",
		success:function(data){
			if(data.success=="true"){
				if(id==""){
					loadYears("year","ncjj_jck")
				}
				$("#baseModal").modal("hide");
				showModal(data.message);
				reLoadTable();
			}else{
				showModal(data.message);
				return;
			}
		}
	});
}
/**
 * 删除信息
 */
function deleteInfo(id){
	modalComfirm("确定删除该条信息吗",function(res){
		if(res){
			$.ajax({
				type:"Post",
				url:"../jck/deleteJinChuKouInfo.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 : "../jck/getJinChuKouRecord.service",
		postData : {
			'year' : $("#year").val()
		}, //发送数据 
		page : 1
	}).trigger("reloadGrid"); //重新载入  
}
	
/* 初始化表格 */
function initTable() {
	$.jgrid.defaults.styleUI = 'Bootstrap';  
	$("#table_list_1").jqGrid({
		url : "../jck/getJinChuKouRecord.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 : ['操作','部门','年度','进口(万元)','出口(万元)',
								  '进口(万元)','出口(万元)',
								  '进口(万元)','出口(万元)',
								  '进口(万元)','出口(万元)',
								  '进口(万元)','出口(万元)',
				'进出口<br>贸易总额','进口<br>总额','出口<br>总额','1'],
		colModel : [  
			{  name : 'caozuo',width: 140,align:'center',formatter :cLink,sortable:false },
			{  name : 'orgname',index : 'orgname',width: 140,sortable:false},
			{  name : 'year',index : 'year',width: 140,sortable:false},
			{  name : 'dwcp_in',index : 'dwcp_in',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'dwcp_out',index : 'dwcp_out',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'zwcp_in',index : 'zwcp_in',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'zwcp_out',index : 'zwcp_out',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'yzjl_in',index : 'yzjl_in',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'yzjl_out',index : 'yzjl_out',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'yczp_int',index : 'yczp_int',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'yczp_out',index : 'yczp_out',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'qt_in',index : 'qt_in',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'qt_out',index : 'qt_out',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'inzj',index : 'inzj',width: 180,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'outzj',index : 'outzj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'inoutzj',index : 'inoutzj',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:'dwcp_in', numberOfColumns:2, titleText: '动物产品'},
	        {startColumnName:'zwcp_in', numberOfColumns: 2, titleText: '植物产品'},
	        {startColumnName:'yzjl_in', numberOfColumns: 2, titleText: '动植物油脂及蜡'},
	        {startColumnName:'yczp_int', numberOfColumns: 2, titleText: '食品烟草及制品'},
	        {startColumnName:'qt_in', 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 : "../jck/exprotJinChuKouRecord.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 : "../jck/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reLoadTable();
			}
		}
	});
}