/**
 * 申请人
 */
function applicantDivAdd(data){
	var html = '<div class="form-group mod-caseInfo-applicant-list" id="applicantDiv' + data.cpId + '">';
	html += '<label class="col-sm-2 control-label"></label>';
	html += '<label class="col-sm-6 text-left">' + data.name + '</label>';
	html += '<div class="col-sm-4 text-right">';
	html += '<button class="btn btn-primary btn-white checkbox-inline" type="button" onclick="applicantInfoUpdateShow(\'' + data.cpId + '\',\'' + $('#mod-caseInfo-orderId').val() + '\')">修改</button>';
	html += '<button class="btn btn-primary btn-white checkbox-inline" type="button" onclick="applicantInfoDelete(\'' + data.cpId + '\',\'' + $('#mod-caseInfo-orderId').val() + '\')">删除</button></div>';
	html += '</div>';
	$('#mod-caseInfo-applicant-list').after(html);
}

function applicantInfoClear(){
	var applicantIds = ['mod-applicant-caseInfoId','mod-applicant-casePersonId','mod-applicant-orderId','mod-applicant-name','mod-applicant-idNum','mod-applicant-contactInfo','mod-applicant-illness','mod-applicant-homeplace','mod-applicant-dependants'];
	for(var i in applicantIds){
		$('#' + applicantIds[i]).val('');
	}
	var divIds = ['mod-applicant-div50','mod-applicant-div51','mod-applicant-div52'];
	for(var i in divIds){
		$('#' + divIds[i]).html('');
	}
	picFileInfo = {};
}

function applicantInfoDelete(cpId, orderId){
	var url = contextPath + '/ssrz/ssrzOrder/deleteApplicant';
	var parameters = {'cpId':cpId,'orderId':orderId};
	$("#applicantDiv" + cpId).remove();
	postData(url, parameters, applicantInfoDeleteCallback, errorCallbackDefault);
}

function applicantInfoDeleteCallback(data){
	if(data.success){
		showMsg('删除成功');
	}else{
		showMsg(data.message);
	}
}

function applicantInfoUpdateShow(cpId,orderId){
	var url = contextPath + '/ssrz/ssrzOrder/getApplicantInfo';
	var parameters = {'cpId':cpId,'orderId':orderId};
	postData(url, parameters, applicantInfoUpdateShowCallback, errorCallbackDefault);
}

function applicantInfoUpdateShowCallback(data){
	if(data.success){
		applicantInfoClear();
		$('#mod-applicant-caseInfoId').val($('#mod-caseInfo-caseInfoId').val());
		$('#mod-applicant-casePersonId').val(data.applicant.casePersonId);
		$('#mod-applicant-orderId').val($('#mod-caseInfo-orderId').val());
		var obj = {'mod-applicant-name':'name','mod-applicant-idNum':'idNum','mod-applicant-contactInfo':'contactInfo','mod-applicant-illness':'illness','mod-applicant-homeplace':'homeplace','mod-applicant-dependants':'dependants'};
		for(var o in obj){
			$('#' + o).val(data.applicant[obj[o]]);
		}
		var attachs = null;
		if(data.applicant != 'null'){
			attachs = data.applicant.adds;
		}
		if(attachs != null){
			var html = '';
			var a = null;
			for(var o in attachs){
				a = attachs[o];
				html = '<div style="text-align:center;display:inline-block;vertical-align:top;" id="mod-pic-' + a.id + '">';
				html += '<a href="' + logoUrl + '/' + a.path + '" target="_blank">';
				html += '<img class="lazy" src="' + contextPath +'/static/img/loading.gif" data-original="' + logoThum  + '/' + a.path + logoSize + '" height="100" width="100" />';
				html += '</a><br/><a href="javascript:void(0)" onclick="deletePic(\'' + a.path + '\',\'' + a.id + '\',\'' + a.attachType + '\')">删除</a>';
				html += '</div>&nbsp;';
				$("#mod-applicant-div" + a.attachType).append(html);
			}
			setTimeout(function () {
				$("img.lazy").lazyload({
					threshold : 200
				});
			}, 200);
		}
		$('#modal-applicant').modal('toggle');
	}else{
		showMsg(data.message);
	}
}

function applicantSubmit(){
	var url = contextPath + '/ssrz/ssrzOrder/saveApplicant';
	var parameters = {'orderId':$('#mod-applicant-orderId').val(),'caseInfoId':$('#mod-applicant-caseInfoId').val(),'casePersonId':$('#mod-applicant-casePersonId').val()};
	var obj = {'mod-applicant-name':'name','mod-applicant-idNum':'idNum','mod-applicant-contactInfo':'contactInfo','mod-applicant-illness':'illness','mod-applicant-homeplace':'homeplace','mod-applicant-dependants':'dependants'};
	var count = 0;
	var s = '';
	for(var o in obj){
		s = $('#' + o).val();
		parameters[obj[o]] = s;
		if(s.length > 0){
			count ++;
		}
	}
	
	var addArray = [];
	var deleteArray = [];
	var a = null;
	for(var k in picFileInfo){
		a = picFileInfo[k];
		if('add' == a[0]){
			addArray.push({'attachType':a[1],'name':a[2],'path':a[3],'suffix':a[4]});
		}else{
			deleteArray.push(a[1]);
		}
	}
	parameters['adds'] = addArray;
	parameters['dels'] = deleteArray;
	count += addArray.length + deleteArray.length;
	if(count > 0){
		postData(url, {'json':JSON.stringify(parameters)}, applicantSubmitCallback, errorCallbackDefault);
	}else{
		showMsg('请输入信息');
	}
}

function applicantSubmitCallback(data){
	if(data.success){
		$('#modal-applicant').modal('toggle');
		$('#mod-caseInfo-caseInfoId').val(data.caseInfoId);
		$("#applicantDiv" + data.cpId).remove();
		applicantDivAdd(data);
		showMsg('保存成功');
	}else{
		showMsg(data.message);
	}
}
