/**
 * 案件信息
 */
function caseInfoClear(){
	var ids = ['modal-caseInfo-areaCity','modal-caseInfo-areaCounty'];
	for(var i in ids){
		$('#' + ids[i]).empty();
	}
	ids = ['mod-caseInfo-party-list','mod-caseInfo-applicant-list'];
	for(var i in ids){
		$("#viewCaseInfoForm").children().remove('.' + ids[i]);
	}
	ids = ['modal-caseInfo-areaProvince','mod-caseInfo-title','mod-caseInfo-occurDate','mod-caseInfo-info','mod-caseInfo-insurer',
	       'mod-caseInfo-policyType','mod-caseInfo-policyMoney','mod-caseInfo-policyTerm','mod-caseInfo-damage',
	       'mod-caseInfo-returnCycle','mod-caseInfo-returnRate','mod-caseInfo-rpnInfo','mod-caseInfo-moneyMin','mod-caseInfo-lotMin'];
	for(var i in ids){
		$('#' + ids[i]).val('');
	}
	$('#mod-caseInfo-rpn').val('1');
	$('#mod-caseInfo-div5').html('');
	picFileBxs = {};
}

function caseSubmit(){
	var countyId = $('#modal-caseInfo-areaCounty').val();
	if(!isNotBlank(countyId)){
		showMsg('请选择区/县');
		return;
	}
	if(isNotNaturalNumber($('#mod-caseInfo-returnCycle').val())){
		showMsg('预期回报周期只能为自然数');
		return;
	}
	var obj = {'mod-caseInfo-returnRate':'预期年收益率','mod-caseInfo-moneyMin':'起投金额','mod-caseInfo-lotMin':'投资最小份额'};
	for(var o in obj){
		if(isNotDouble($('#' + o).val())){
			showMsg(obj[o] + '只能为浮点数');
			return;
		}
	}
	var url = contextPath + '/ssrz/ssrzOrder/saveCase';
	var parameters = {};
	obj = {'mod-caseInfo-title':'title','modal-caseInfo-areaCounty':'countyId','mod-caseInfo-occurDate':'occurDate','mod-caseInfo-info':'info',
		'mod-caseInfo-insurer':'insurer','mod-caseInfo-policyType':'policyType','mod-caseInfo-policyMoney':'policyMoney',
		'mod-caseInfo-policyTerm':'policyTerm','mod-caseInfo-damage':'damage','mod-caseInfo-returnCycle':'returnCycle',
		'mod-caseInfo-returnRate':'returnRate','mod-caseInfo-rpnInfo':'rpnInfo','mod-caseInfo-moneyMin':'moneyMin',
		'mod-caseInfo-lotMin':'lotMin','mod-caseInfo-rpn':'rpn','mod-caseInfo-caseInfoId':'id','mod-caseInfo-orderId':'orderId'};
	for(var o in obj){
		parameters[obj[o]] = $('#' + o).val();
	}
	var addArray = [];
	var deleteArray = [];
	var a = null;
	for(var k in picFileBxs){
		a = picFileBxs[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['bxs'] = addArray;
	parameters['dels'] = deleteArray;
	postData(url, {'json':JSON.stringify(parameters)}, caseSubmitCallback, errorCallbackDefault);
}

function caseSubmitCallback(data){
	if(data.success){
		$('#modal-caseInfo').modal('toggle');
		showMsg('保存成功');
	}else{
		showMsg(data.message);
	}
}

//获取案件信息
function getCaseInfo(orderId){
	var url = contextPath + '/ssrz/ssrzOrder/getCaseInfo';
	var parameters = {'orderId':orderId};
	postData(url, parameters, getCaseInfoCallback, errorCallbackDefault);
}

function getCaseInfoCallback(data){
	if(data.success){
		caseInfoClear();
		var obj = {'mod-caseInfo-title':'title','mod-caseInfo-occurDate':'occurDate','mod-caseInfo-info':'info',
				'mod-caseInfo-insurer':'insurer','mod-caseInfo-policyType':'policyType','mod-caseInfo-policyMoney':'policyMoney',
				'mod-caseInfo-policyTerm':'policyTerm','mod-caseInfo-damage':'damage','mod-caseInfo-returnCycle':'returnCycle',
				'mod-caseInfo-returnRate':'returnRate','mod-caseInfo-rpnInfo':'rpnInfo','mod-caseInfo-moneyMin':'moneyMin',
				'mod-caseInfo-lotMin':'lotMin','mod-caseInfo-rpn':'rpn','mod-caseInfo-caseInfoId':'id','mod-caseInfo-orderId':'orderId'};
		for(var o in obj){
			$('#' + o).val(data[obj[o]]);
		}
		var a = data['applicants'];
		if(isNotBlank(a)){
			for(var i in a){
				applicantDivAdd(a[i]);
			}
		}
		a = data['partys'];
		if(isNotBlank(a)){
			for(var i in a){
				partyDivAdd(a[i]);
			}
		}
		a = data['countys'];
		if(isNotBlank(a)){
			setAreaOption('modal-caseInfo-areaCounty', a);
			$('#modal-caseInfo-areaCounty').val(data['countyId']);
		}
		a = data['citys'];
		if(isNotBlank(a)){
			setAreaOption('modal-caseInfo-areaCity', a);
			$('#modal-caseInfo-areaCity').val(data['cityId']);
		}
		if(isNotBlank(data['provinceId'])){
			$('#modal-caseInfo-areaProvince').val(data['provinceId']);
		}
		var attachs = data.bxs;
		if(isNotBlank(attachs)){
			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-caseInfo-div" + a.attachType).append(html);
			}
			setTimeout(function () {
				$("img.lazy").lazyload({
					threshold : 200
				});
			}, 200);
		}
		$('#modal-caseInfo').modal('toggle');
	}else{
		showMsg(data.message);
	}
}

function initAreaProvince(){
	$.getJSON(contextPath + "/sys/cdPrice/getProvince",function(result){
		$.each(result, function(i, v){
			$("#modal-caseInfo-areaProvince").append("<option value='"+v.code+"'>"+v.name+"</option>");
		});
	});	
}

function setChArea(obj){  
    var val = obj.value;  
    var sec = null;
    var areaId = $(obj).attr("id");
	if(areaId=="modal-caseInfo-areaProvince"){
		sec = document.getElementById('modal-caseInfo-areaCity');
	}else{
		sec = document.getElementById('modal-caseInfo-areaCounty');
	}
	sec.innerHTML = "<option selected value='' >全部</option>"; 
    if(val != ''){
    	//ajax
    	$.getJSON(contextPath + "/sys/cdPrice/getAreas?parentCode="+val,function(result){
    		$.each(result, function(i, v){
    			sec.innerHTML+="<option value='"+v.code+"'>"+v.name+"</option>";
    		});
  		});
    }     
}

function setAreaOption(id,data){
	var sec = document.getElementById(id);
	sec.innerHTML = "<option selected value='' >全部</option>";
	$.each(data, function(i, v){
		sec.innerHTML+="<option value='"+v.code+"'>"+v.name+"</option>";
	});
}
