
// 上报按钮(授权管理)
function reportOperate(source,id,cid,couid){
	creditReportQuery.listPage = $('#customerList').getGridParam('page');
	$('.reportDialog').find('input').prop('checked',false);
	$('.reportDialog').find('input').prop('disabled',false);
	$('.reportDialog').find('.buyLabel span:first').css('color','#333');
	$('.reportDialog').find('.buyLabel span:last').text('');
	$('.reportDialog').find('.repostResultBox').addClass('hide');
	$('.reportDialog').attr('opid',id);
	if( source == 'sell' ){
		// 只有卖方基本信息, 卖方为授信主体
		$('.reportDialog').find('.buyLabel').addClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.sellLabel').removeClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.sellLabel').attr('subjectId',cid);
	};
	if( source == 'buy' ){
		// 只有买方基本信息, 买方为交易对手
		$('.reportDialog').find('.sellLabel').addClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.buyLabel').removeClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.buyLabel').attr('subjectId',cid);
		// 正向保理无追时-暂不支持上报买方业务
		$('.reportDialog').find('.buyLabel input').prop('disabled',true);
		$('.reportDialog').find('.buyLabel span:first').css('color','#ccc');
		$('.reportDialog').find('.buyLabel span:last').text('(暂不支持该业务)');
	};
	if( source == 'all' ){
		// 买方基本信息和卖方基本信息都有, 买方为授信主体，卖方为交易对手
		$('.reportDialog').find('.sellLabel').removeClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.buyLabel').removeClass('hide').removeAttr('subjectId');
		$('.reportDialog').find('.sellLabel').attr('subjectId',couid);
		$('.reportDialog').find('.buyLabel').attr('subjectId',cid);
	};
	
	$('.reportDialog').modal('show');
};

// 修改按钮
function updateReportOperate(source,id,cid,couid){
	creditReportQuery.listPage = $('#customerList').getGridParam('page');
	creditReportQuery.source = source;
	creditReportQuery.creditReportingId = id;
	if( creditReportQuery.source == 'sell' ){
		// 只有卖方基本信息, 卖方为授信主体
		creditReportQuery.creditSubjectId = cid;
		$(".yk_rightcontent").load('/creditReportingBasicInfo/sellerView');
	};
	if( creditReportQuery.source == 'buy' ){
		// 只有买方基本信息, 买方为交易对手
		creditReportQuery.counterpartyId = cid;
		$(".yk_rightcontent").load('/creditReportingBasicInfo/buyerView');
	};
	if( creditReportQuery.source == 'all' ){
		// 买方基本信息和卖方基本信息都有, 买方为授信主体，卖方为交易对手
		creditReportQuery.counterpartyId = cid;// 买方
		creditReportQuery.creditSubjectId = couid;// 卖方
		$(".yk_rightcontent").load('/creditReportingBasicInfo/buyerView');
	};
};

$(function(){
	$("#fastSearch").val(creditReportQuery.fastSearch);
	
	// 上报授权弹框里的选择
	$('.authorizeResultBox input').change(function(){
		if( $('.authorizeResultBox input:checked').parents('label').hasClass('authorizeLabel') ){// 授权
			$('.repostResultBox').removeClass('hide');	
			if( $('label.buyLabel').hasClass('hide') && !$('label.sellLabel').hasClass('hide') ){// 只有卖方
				$('label.sellLabel input').prop('checked',true);
			};
		};
		if( $('.authorizeResultBox input:checked').parents('label').hasClass('notAuthorizeLabel') ){// 未授权
			$('.repostResultBox').addClass('hide');
		};
	})
	
	// 确认上报
	$('.reportDialog .chooseSure').click(function(){
		if( $('[name=authorizeResult]:checked').length<=0 ){
			new $.zui.Messager('请选择授权情况', {
			    type: 'danger'
			}).show();
			return;
		};
		if( !$('.repostResultBox').hasClass('hide') && $('[name=reportResult]:checked').length<=0 ){
			new $.zui.Messager('请选择上报方', {
			    type: 'danger'
			}).show();
			return;
		};
		if( !$('.repostResultBox').hasClass('hide') && $('.reportDialog').find('.buyLabel span:last').text()!='' && $('[name=reportResult]:checked').parents('label').hasClass('buyLabel') ){
			new $.zui.Messager('暂不支持该业务,请重新选择', {
			    type: 'danger'
			}).show();
			return;
		};
		
		var doReportData = {};
		if( $('.authorizeResultBox input:checked').parents('label').hasClass('authorizeLabel') ){// 授权
			doReportData.reportingFlag = true;
			doReportData.subjectId = $('.reportDialog').find('.repostResultBox input:checked').parents('label').attr('subjectid')
		};
		if( $('.authorizeResultBox input:checked').parents('label').hasClass('notAuthorizeLabel') ){// 未授权
			doReportData.reportingFlag = false;
		};
		doReportData.id = $('.reportDialog').attr('opid');
		
		$.ajax({
			url:'/creditreporting/doReportSubject',
			type:'post',
			data:doReportData,
			success:function(data){
				if( data.success ){
					new $.zui.Messager('上报授权成功', {
					    type: 'success'
					}).show();
					$('#customerList').setGridParam({
						mtype:'GET',
						url : '/creditreporting/doList',
						datatype : 'json',
						postData: {
							'fastSearch':creditReportQuery.fastSearch
						},
						page : creditReportQuery.listPage
					});
					$('#customerList').trigger('reloadGrid');
				}else{
					new $.zui.Messager('上报授权失败:'+data.msg, {
					    type: 'danger'
					}).show();
				};
				$('.reportDialog').modal('hide');
			},
			error:function(status){
				new $.zui.Messager('上报授权时出错:'+status, {
				    type: 'danger'
				}).show();
				$('.reportDialog').modal('hide');
			}
		})
	})
	
	function operateUpRecord(cellValue,options,rowObject){
		var btnstr = '';
		if( rowObject.positiveReverseFactoring == 'positiveFactoring' ){// 正向保理
			if( rowObject.recourseRights == 'recourse' ){// 有追索权
				// 只有卖方基本信息, 卖方为授信主体
				if( $('#creditReporting').length>0 ){
					btnstr += '<p><span>授权管理</span><i class="operateIconfont operateIcon-shouxinjilu" onclick="reportOperate(\'sell\','+rowObject.id+','+rowObject.creditSubjectId+')"></i></p>';
				};
				if( $('#creditReportingDetail').length>0 ){
					btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateReportOperate(\'sell\','+rowObject.id+','+rowObject.creditSubjectId+')"></i></p>';
				};
			}else{// 无追索权
				// 只有买方基本信息, 买方为交易对手
				if( $('#creditReporting').length>0 ){
					btnstr += '<p><span>授权管理</span><i class="operateIconfont operateIcon-shouxinjilu" onclick="reportOperate(\'buy\','+rowObject.id+','+rowObject.counterpartyId+')"></i></p>';
				};
				if( $('#creditReportingDetail').length>0 ){
					btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateReportOperate(\'buy\','+rowObject.id+','+rowObject.counterpartyId+')"></i></p>';
				};
			}
		}else{// 反向保理
			// 买方基本信息和卖方基本信息都有, 买方为授信主体，卖方为交易对手
			if( $('#creditReporting').length>0 ){
				btnstr += '<p><span>授权管理</span><i class="operateIconfont operateIcon-shouxinjilu" onclick="reportOperate(\'all\','+rowObject.id+','+rowObject.creditSubjectId+','+rowObject.counterpartyId+')"></i></p>';
			};
			if( $('#creditReportingDetail').length>0 ){
				btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateReportOperate(\'all\','+rowObject.id+','+rowObject.creditSubjectId+','+rowObject.counterpartyId+')"></i></p>';
			};
		};
		return '<div class="operateIcon noHover">'+btnstr+'</div>';
	};
	
	function showReportFlag(cellValue,options,rowObject){
		if( rowObject.reportingFlag!=undefined ){
			if( rowObject.reportingFlag ){
				return '已授权'
			}else{
				return '未授权'
			}
		}else{
			return ''
		}	
	};
	
	sxUtil.jqGrid('#customerList',{
		colNames:['ID','序号','业务编号','授信主体','保理申请人','保理合同编号','额度支用批次','授权情况','操作'],
		colModel:[
			{name: 'id', label: 'ID', hidedlg:true, hidden:true,sortable:false,title:false},
			{name: 'orderNumber', label: '序号',sortable:false,width:50,align:'center',title:false},
			{name: 'businessNo', label: '业务编号',sortable:false,width:150,align:'center',title:false},
            {name: 'creditSubjectName', label: '授信主体',sortable:false,align:'left',width:160,title:false},
            {name: 'applicantName', label: '保理申请人',sortable:false,align:'center',width:160,title:false},
            {name: 'contractNo', label: '保理合同编号',sortable:false,align:'center',width:160,title:false},
            {name: 'quotaDisburseBatch', label: '额度支用批次',sortable:false,align:'center',width:100,title:false},
            {name: 'reportingFlag', label: '授权情况',sortable:false,align:'center',width:100,title:false,formatter:showReportFlag},
            {name: 'remark', label: '操作',sortable:false,align:'left',width:140,title:false,formatter:operateUpRecord}
		], 
		height:'auto',
		mtype:'GET',
		url : '/creditreporting/doList',
		pager : "#customerListPage",
		multiselect: false,
		rownumbers:false,
		postData : {
			'fastSearch':creditReportQuery.fastSearch
		},
		page:creditReportQuery.listPage,
		loadError:function(){
			new $.zui.Messager('上报管理列表加载失败', {
			    type: 'warning'
			}).show();
		}
	},true);
	
	//查询按钮点击	
	$("#query").click(function(){
		creditReportQuery.fastSearch = $("#fastSearch").val();
		$('#customerList').setGridParam({
			mtype:'GET',
			url : '/creditreporting/doList',
			datatype : 'json',
			postData: {
				'fastSearch':creditReportQuery.fastSearch
			},
			page : 1
		});
		$('#customerList').trigger('reloadGrid');
	});
	
	//刷新
	$('#refresh').click(function(){
		$("#fastSearch").val('');
		creditReportQuery.fastSearch = '';
		$('#customerList').setGridParam({
			mtype:'GET',
			url : '/creditreporting/doList',
			datatype : 'json',
			postData: {
				'fastSearch':''
			},
			page : 1
		});
		$('#customerList').trigger('reloadGrid');
	});
	
})