//初始化
var g;
var HistoricalPreData = {Rows:[],Total:0};
var isQueryNull = true;
var syncType = false;
$(function (){
	$("#maingrid4").ligerGrid({
		columns: [
		          {display: '主团单号', name: 'smgpolicyno', align: 'center' },
		          {display: '公司名称', name: 'corporateName', align: 'center' },
		          {display: '系统来源', name: 'appsource', align: 'center',render:function(n){
		        	  
						if(n.appsource == '1' ){
							return '产险';
						}else if(n.appsource == '2' ){
							return '寿险';
						}else if(n.appsource == '3' ){
							return '健康险';
						}
				    }, width: 120 },
		          {display: '外包商', name: 'compflag', align: 'center' },
		          {display: '生效日期', name: 'grqbrqStr', align: 'center' },
		          {display: '结束日期', name: 'grzbrqStr', align: 'center' }
		],
		data: '', 
		pageSize: 50, 
		sortName: 'smgpolicyno',
		sortorder : 'asc',
		width: '99.85%', 
		height: '500px', 
		isChecked: f_isChecked,
		onCheckRow: f_onCheckRow,
		dateFormat: "yyyy-MM-dd hh:mm:ss",
		checkbox: true
	});
	g = $("#maingrid4").ligerGrid();
});

function queryInfo(){
	var param = {};
	param.smgpolicyno = $("#smgpolicyno").val();
	param.compflagCode = $("#compflagCode").attr("code");
	param.syscode = $("#syscode").attr("code");
	$.ajax({
		type:'POST',
		url:'queryDirectPaymentCases.do',
		data : param,
		success:function(data){
			$(".l-bar-btnfirst").click();
			g.loadData(HistoricalPreData);
			if(data ==""){
				alert("查询不到相关内容!");
			}else{
				var datastr = '('+data+')';
				g.set("data",eval(datastr));
				g.reload();
			}
		},
		error:function(){
			alert("查询出错啦！");
		}
	});
}

function f_isChecked(){
//	if (g.CustomerID.indexOf('AN') == 0) {
//		return false;
//	}else {
//	}
}

function f_onCheckRow(checked, data){
//	data.isCheck = checked;
}

function getInfo(){
	
	var array = g.getSelectedRows();
	
	var sendOutsourcingProvider = $("#sendOutsourcingProvider").attr("code");
	
	var TblHistorPreservationVo = {};
	
	var tblHistorList = [];
	
	var isSelectedSmg = false;
	
	for ( var i = 0; i < array.length; i++) {
		if($("#smgpolicyno").val() != "" && $("#smgpolicyno").val() == array[i].smgpolicyno){
			isSelectedSmg = true;
		}
		
		var TblHistorPreservation = {};
		TblHistorPreservation.smgpolicyno = array[i].smgpolicyno;
		TblHistorPreservation.corporateName = array[i].corporateName;
		TblHistorPreservation.appsource = array[i].appsource;
		
		TblHistorPreservation.compflag = $("#sendOutsourcingProvider").html();
		TblHistorPreservation.compflagCode = sendOutsourcingProvider;
		tblHistorList.push(TblHistorPreservation);
	}
	
	if($("#smgpolicyno").val() != "" && !isSelectedSmg){
		var TblHistorPreservation = {};
		TblHistorPreservation.smgpolicyno = $("#smgpolicyno").val();
		TblHistorPreservation.appsource = $("#syscode").attr("code");
		
		TblHistorPreservation.compflag = $("#sendOutsourcingProvider").html();
		TblHistorPreservation.compflagCode = sendOutsourcingProvider;
		tblHistorList.push(TblHistorPreservation);
	}
	
	
	TblHistorPreservationVo.tblHistorList = tblHistorList;
	
	
	if(tblHistorList.length == 0){
		alert("请勾选数据,或输入团单号!");
		return;
	}
	
	
	if(""==sendOutsourcingProvider || null==sendOutsourcingProvider){
		alert("请选择外包商!");
		return;
	}
	
	$.ajax({
		type:'POST',
		url:'sendReport.do',
		data : JSON.stringify(TblHistorPreservationVo),
		contentType:"application/json; charset=UTF-8",
		success:function(data){
			if("success" == data){
				alert("发送成功！");
				closeDiv('divformOutsourcingProvider');
				queryInfo();
			}
		},
		error:function(){
			alert("查询出错啦！");
		}
	});
	
}