/**
 * 农产品进出口贸易js
 */
$(function(){
	var orgid = localStorage.getItem(getQueryVariable("JSONID")+"orgid");
	if(orgid.length>2){
		$("#addBtn").show();
		$("#xd").show();
	}else{
		$("#sd").show();
	}
	loadYears("year","zzy_lszw")
	loadYears("baseyear")
	initTable();
})
/**
 * 新增录入
 */
function addBtn(){
	$("#hiddenid").val("");
	$("#lsbzmj").val("");
	$("#lsmc").val("");
	$("#dgbzmj").val("");
	$("#dgmc").val("");
	$("#hsbzmj").val("");
	$("#hsmc").val("");
	$("#ddmc").val("");
	$("#ddbzmj").val("");
	$("#ztbzmj").val("");
	$("#ztmc").val("");
	$("#slbzmj").val("");
	$("#slmc").val("");
	$("#baseModal").modal("show");
}
/**
 * 编辑修改
 */
function edit(id){
	var rowdata = jQuery("#table_list_1").jqGrid("getRowData", id);
	$("#hiddenid").val(id);
	$("#baseyear").val(rowdata.year)
	$("#lsbzmj").val(rowdata.lsbzmj);
	$("#lsmc").val(rowdata.lsmc);
	$("#dgbzmj").val(rowdata.dgbzmj);
	$("#dgmc").val(rowdata.dgmc);
	$("#hsbzmj").val(rowdata.hsbzmj);
	$("#hsmc").val(rowdata.hsmc);
	$("#ddmc").val(rowdata.ddmc);
	$("#ddbzmj").val(rowdata.ddbzmj);
	$("#ztbzmj").val(rowdata.ztbzmj);
	$("#ztmc").val(rowdata.ztmc);
	$("#slbzmj").val(rowdata.slbzmj);
	$("#slmc").val(rowdata.slmc);
	$("#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 lsbzmj = $("#lsbzmj").val()==""?"0":$("#lsbzmj").val();
	var lsmc = $("#lsmc").val()==""?"0":$("#lsmc").val();
	var lszcl = (parseFloat(lsbzmj)*parseFloat(lsmc)/1000).toFixed(2);
	var dgbzmj = $("#dgbzmj").val()==""?"0":$("#dgbzmj").val();
	var dgmc = $("#dgmc").val()==""?"0":$("#dgmc").val();
	var dgzcl = (parseFloat(dgbzmj)*parseFloat(dgmc)/1000).toFixed(2);
	var hsbzmj = $("#hsbzmj").val()==""?"0":$("#hsbzmj").val();
	var hsmc = $("#hsmc").val()==""?"0":$("#hsmc").val();
	var hszcl = (parseFloat(hsbzmj)*parseFloat(hsmc)/1000).toFixed(2);
	var ddbzmj = $("#ddbzmj").val()==""?"0":$("#ddbzmj").val();
	var ddmc = $("#ddmc").val()==""?"0":$("#ddmc").val();
	var ddzcl = (parseFloat(ddbzmj)*parseFloat(ddmc)/1000).toFixed(2);
	var ztbzmj = $("#ztbzmj").val()==""?"0":$("#ztbzmj").val();
	var ztmc = $("#ztmc").val()==""?"0":$("#ztmc").val();
	var ztzcl = (parseFloat(ztbzmj)*parseFloat(ztmc)/1000).toFixed(2);
	var slbzmj = $("#slbzmj").val()==""?"0":$("#slbzmj").val();
	var slmc = $("#slmc").val()==""?"0":$("#slmc").val();
	var slzcl = (parseFloat(slbzmj)*parseFloat(slmc)/1000).toFixed(2);
	var bzzmj = parseFloat(lsbzmj)+parseFloat(dgbzmj)+parseFloat(hsbzmj)+parseFloat(ddbzmj)+parseFloat(ztbzmj)+parseFloat(slbzmj);
	var zmc = parseFloat(lsmc)+parseFloat(dgmc)+parseFloat(hsmc)+parseFloat(ddmc)+parseFloat(ztmc)+parseFloat(slmc);
	var zcl = parseFloat(lszcl)+parseFloat(dgzcl)+parseFloat(hszcl)+parseFloat(ddzcl)+parseFloat(ztzcl)+parseFloat(slzcl);  
	
	$.ajax({
		type:"Post",
		url:"../lszw/saveRecord.service",
		data:{ 
			id:id,
			year:year,
			lsbzmj:lsbzmj,
			lsmc:lsmc,
			lszcl:lszcl,
			dgbzmj:dgbzmj,
			dgmc:dgmc,
			dgzcl:dgzcl,
			hsbzmj:hsbzmj,
			hsmc:hsmc,
			hszcl:hszcl,
			ddbzmj:ddbzmj,
			ddmc:ddmc,
			ddzcl:ddzcl,
			ztbzmj:ztbzmj,
			ztmc:ztmc,
			ztzcl:ztzcl,
			slbzmj:slbzmj,
			slmc:slmc,
			slzcl:slzcl,
			bzzmj:bzzmj,
			zmc:zmc,
			zcl:zcl
		},
		dataType:"json",
		success:function(data){
			if(data.success=="true"){
				if(id==""){
					loadYears("year","zzy_lszw")
				}
				$("#baseModal").modal("hide");
				showModal(data.message);
				reLoadTable();
			}else{
				showModal(data.message);
				return;
			}
		}
	});
}
/**
 * 删除信息
 */
function deleteInfo(id){
	modalComfirm("确定删除该条信息吗",function(res){
		if(res){
			$.ajax({
				type:"Post",
				url:"../lszw/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 : "../lszw/getRecordList.service",
		postData : {
			'year' : $("#year").val()
		}, //发送数据 
		page : 1
	}).trigger("reloadGrid"); //重新载入  
}
	
/* 初始化表格 */
function initTable() {
	$.jgrid.defaults.styleUI = 'Bootstrap';  
	$("#table_list_1").jqGrid({
		url : "../lszw/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: 200,align:'center',formatter :cLink,sortable:false },
			{  name : 'orgname',index : 'orgname',width: 140,sortable:false},
			{  name : 'year',index : 'year',width: 140,sortable:false},
			{  name : 'lsbzmj',index : 'lsbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'lsmc',index : 'lsmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'lszcl',index : 'lszcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			
			{  name : 'dgbzmj',index : 'dgbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'dgmc',index : 'dgmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'dgzcl',index : 'dgzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			
			{  name : 'hsbzmj',index : 'hsbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'hsmc',index : 'hsmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'hszcl',index : 'hszcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			
			{  name : 'ddbzmj',index : 'ddbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'ddmc',index : 'ddmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'ddzcl',index : 'ddzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			
			{  name : 'ztbzmj',index : 'ztbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'ztmc',index : 'ztmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'ztzcl',index : 'ztzcl',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			
			{  name : 'slbzmj',index : 'slbzmj',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'slmc',index : 'slmc',width: 140,sortable:false,formatter:function(c,p,o){if(c==0){return ""}else{return c}}},
			{  name : 'slzcl',index : 'slzcl',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 : 'zmc',index : 'zmc',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:'lsbzmj', numberOfColumns:3, titleText: '粮食'},
	        {startColumnName:'dgbzmj', numberOfColumns: 3, titleText: '稻谷'},
	        {startColumnName:'hsbzmj', numberOfColumns: 3, titleText: '花生'},
	        {startColumnName:'ddbzmj', numberOfColumns: 3, titleText: '大豆'},
	        {startColumnName:'ztbzmj', numberOfColumns: 3, titleText: '蔗糖'},
	        {startColumnName:'slbzmj', 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 : "../lszw/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 : "../lszw/importData.service",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			showModal(data.message);
			if (data.success == "true") {
				reLoadTable();
			}
		}
	});
}