var isLifeClaim = $("#isLifeClaim").val();
var html = $("#otherDesc table").html();
$(function(){
	var isHas = $("#auditConsultResources").html();
//	$("#otherDesc table").html('');
	if(isLifeClaim == "Y"&&isHas.indexOf("<tr")>0){
		$("#otherDesc table").html('');
	}
});


function skip2OtherPageByClaimNo(url,claimNo){
	var params = new Array();;
	params["claimNo"] = claimNo;
	SkipByPost(url, params);
}
/**补充资料描述新增**/
function changeotherDesc(){
	//单证名称
	var resourceName = $("#other_resourceName").val();
	//补充资料描述
	var resourceDesc = $("#other_resourceDesc").val();
	if(resourceName=="" || resourceDesc==""){
		alert("单证名称和补充资料描述都不能为空！");
		return;
	}
	
	$("#auditConsultResources").append("<tr onclick='chooseTr(this);' class=''>" +
			"<td width=\"22%\" id='resourceCodeDesc'></td>"+
			"<td width=\"21%\" id='resourceName'>"+resourceName+"</td>"+
			"<td width=\"41%\" id='resourceDesc'><input type=\"text\" class=\"input i_s_100\" value="+resourceDesc+"></td>"+
			"<td width=\"26%\" id='resourceState'><span id='au"+MathRand()+"' class='check_no' onclick='checksp(this.id)'></span></td>"+
			"</tr>");
	
	$("#spanotherDesc").removeClass("check_yes").addClass("check_no");
	$("#other_resourceName").val("");
	$("#other_resourceDesc").val("");
	$("#other_resourceName").attr("disabled",true);
	$("#other_resourceDesc").attr("disabled",true);
	
	var isHas = $("#auditConsultResources").html();
	if(isLifeClaim == "Y"&&isHas.indexOf("<tr")>0){
		$("#otherDesc table").html('');
	}
	
}
//其它资料选择
function otherDescCheckBox(){
	if($("#spanotherDesc").attr("class")=="check_no"){//选中
		$("#other_resourceName").attr("disabled",false);
		$("#other_resourceDesc").attr("disabled",false);
	}else{
		$("#other_resourceName").val("");
		$("#other_resourceDesc").val("");
		$("#other_resourceName").attr("disabled",true);
		$("#other_resourceDesc").attr("disabled",true);
	}
	//alert($("#spanotherDesc").attr("class"));
}
function change(type){
	if(type=="input"){
		var checkyes = $("#resourceConfigs").find(".check_yes");
		if(checkyes.length<1){
			alert("请选择索赔资料！");
			return ;
		}
		//将选中的索赔资料写到需补充资料中
		$("#resourceConfigs").find("tr").each(function(){
			var current=$(this);
			if(current.find(".check_yes").length>0){
				var resourceCode = $(this).find("td[id=resourceCode]").html();
				var resourceName = $(this).find("td[id=resourceName]").html();
				$("#auditConsultResources").append("<tr onclick='chooseTr(this);' class=''>" +
						"<td width=\"22%\" id='resourceCode'>"+resourceCode+"</td>"+
						"<td width=\"21%\" id='resourceName'>"+resourceName+"</td>"+
						"<td width=\"41%\" id='resourceDesc'><input type=\"text\" class=\"input i_s_100\"/></td>"+
						"<td width=\"26%\" id='resourceState'><span id='au"+resourceCode+"' class='check_no' onclick='checksp(this.id)'></span></td>"+
						"</tr>");
				current.remove();
			}
				
			
		});
		
	}else if(type=="output"){
		var checkyes = $("#auditConsultResources").find(".table_list_tr_cur");
		if(checkyes.length<1){
			alert("请选择需要移出的补充资料！");
			return ;
		}
		
		$("#auditConsultResources").find("tr").each(function(){
			var currentTr = $(this);
			//alert("currentTr):"+currentTr.is(".table_list_tr_cur"));
			if(currentTr.is(".table_list_tr_cur")){
				var resourceCode = currentTr.find("td[id=resourceCode]").html();
				var resourceName = currentTr.find("td[id=resourceName]").html();
				if(resourceCode!=null && resourceCode != ''){//补充资料的不需要插入清单中
					$("#resourceConfigs").prepend("<tr>" +
							"<td width=\"9%\"><span id='re"+resourceCode+"' class='check_no' onclick='checksp(this.id)'></span></td>" +
							"<td width=\"30%\" id='resourceCode'>"+resourceCode+"</td>"+
							"<td width=\"70%\" id='resourceName'>"+resourceName+"</td>"+
							"</tr>");
				}
				
				currentTr.remove();
			}
			var isHas = $("#auditConsultResources").html();
			if(isLifeClaim == "Y"&&isHas.indexOf("<tr")<0){
				$("#otherDesc table").append(html);
			}
			$(".check_yes,.check_no").unbind("click");
			$(".check_yes,.check_no").click(function(){ 
				if($(this).is(".check_yes")){
					$(this).removeClass("check_yes");
					$(this).addClass("check_no"); 
				}else{
					$(this).removeClass("check_no");
				    $(this).addClass("check_yes");
				}
			});
		});
	}
}

function checksp(id){
	if($("#"+id).is(".check_no")){
		$("#"+id).attr("class","check_yes");
	}else{
		$("#"+id).attr("class","check_no");
	}

}
//添加需补充资料
function addAuditConsultResources(){
	var auditConsultResourcesArray = new Array();
	var flag=true;
	$("#auditConsultResources").find("tr").each(function(){
		var resourceNo = $(this).find("td[id=resourceCode]").html();
		var resourceName = $(this).find("td[id=resourceName]").html();
		//alert("input:"+$(this).find("td[id=resourceDesc] input").val());
		var resourceDesc = $(this).find("td[id=resourceDesc] input").val();
		var resourceState = $(this).find("td[id=resourceState]").find("span").is(".check_yes");
		if(resourceState){
			if(resourceName==null||resourceName==''){
				alert("请输入其他补充的单证名称");
				flag=false;
			}
			resourceState="2";
		}else{
			resourceState="1";
		}
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),resourceNo:resourceNo,resourceName:resourceName,resourceDesc:resourceDesc,resourceState:resourceState});
	});
	if(!flag){
		return;
	}
	//补充资料判断
	var otherDesc = $("#otherDesc tr th span").is(".check_yes");
	if(otherDesc){
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),
			resourceNo:"",
			resourceName:$("#otherDesc").find("#other_resourceName").val(),
			resourceDesc:$("#otherDesc").find("#other_resourceDesc").val(),
			resourceState:"2"});
	}
	var auditConsultResources = JSON.stringify(auditConsultResourcesArray);
	$.ajax({
		url:"addAuditResource.do",
		data:{auditConsultResources:auditConsultResources,claimNo:$("#claimNo").val()},
		error: function(){
			alert("失败！");
		},
		success :function(data){
			if (null == data ||data=="") {
				alert("保存失败：需补充资料不能为空！");
			}else{
				alert("保存成功！");
			}
		}
	});
}
/**
 * 申请
 */
function applyAuditResource(caseSource,isBpo,Tpa){
	var auditConsultResourcesArray = new Array();
	$("#auditConsultResources").find("tr").each(function(){
		var resourceNo = $(this).find("td[id=resourceCode]").html();
		var resourceName = $(this).find("td[id=resourceName]").html();
		//alert("input:"+$(this).find("td[id=resourceDesc] input").val());
		var resourceDesc = $(this).find("td[id=resourceDesc] input").val();
		var resourceState = $(this).find("td[id=resourceState]").find("span").is(".check_yes");
		if(resourceState){
			resourceState="2";
		}else{
			resourceState="1";
		}
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),resourceNo:resourceNo,resourceName:resourceName,resourceDesc:resourceDesc,resourceState:resourceState});
	});
	//补充资料判断
	var otherDesc = $("#otherDesc tr th span").is(".check_yes");
	if(otherDesc){
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),
			resourceNo:"",
			resourceName:$("#otherDesc").find("#resourceName").val(),
			resourceDesc:$("#otherDesc").find("#resourceDesc").val(),
			resourceState:"2"});
	}
	var auditConsultResources = JSON.stringify(auditConsultResourcesArray);
	$.ajax({
		url:"applyAuditResource.do",
		data:{auditConsultResources:auditConsultResources,claimNo:$("#claimNo").val(),caseSource:caseSource},
		error: function(){
			alert("失败！");
		},
		success :function(data){
			if ('N' == data.successFlag ) {
				alert("申请失败：" + data.errorMsg);
			} else {
				alert("申请成功！");
				$(".bot_btn_03").css("color","#ccc");
				$(".bot_btn_13").css("color","#ccc");
				$(".bot_btn_03").removeAttr("onclick");
				$(".bot_btn_13").removeAttr("onclick");

				if ($("#caseSource").val() == "2" && $("#lifeFlag").val() == "1") {
					$("#moveIn").removeAttr("onclick");
					$("#moveOut").removeAttr("onclick");
					$(".la_cc_con_ic01").css("color","#ccc");
					$(".la_cc_con_ic02").css("color","#ccc");
					$(".bot_btn_02").css("color","#007aff");
					$(".bot_btn_02").attr("onclick","completeAuditConsultResources();");
				}

				if(caseSource != '2' || (caseSource =='2' && isBpo == 'N')|| (caseSource == '2' && Tpa== '1')){
					//打开短信面板
					showSmsContent($("#claimNo").val());
				}
				
			}
		}
	});
}
function completeAuditConsultResources() {
	$(".bot_btn_02").css("color","#ccc");
	$(".bot_btn_02").removeAttr("onclick");
    var auditConsultResourcesArray = new Array();
	$("#auditConsultResources").find("tr").each(function(){
		var resourceNo = $(this).find("td[id=resourceCode]").html();
		var resourceName = $(this).find("td[id=resourceName]").html();
		//alert("input:"+$(this).find("td[id=resourceDesc] input").val());
		var resourceDesc = $(this).find("td[id=resourceDesc] input").val();
		var resourceState = $(this).find("td[id=resourceState]").find("span").is(".check_yes");
		if(resourceState){
			resourceState="2";
		}else{
			resourceState="1";
		}
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),resourceNo:resourceNo,resourceName:resourceName,resourceDesc:resourceDesc,resourceState:resourceState});
	});
	//补充资料判断
	var otherDesc = $("#otherDesc tr th span").is(".check_yes");
	if(otherDesc){
		auditConsultResourcesArray.push({claimNo:$("#claimNo").val(),
			resourceNo:"",
			resourceName:$("#otherDesc").find("#resourceName").val(),
			resourceDesc:$("#otherDesc").find("#resourceDesc").val(),
			resourceState:"2"});
	}
	var auditConsultResources = JSON.stringify(auditConsultResourcesArray);
	$.ajax({
		url:"completeAuditResource.do",
		data:{auditConsultResources:auditConsultResources,claimNo:$("#claimNo").val()},
		error: function(){
			alert("保存失败！");
			$(".bot_btn_02").css("color","#007aff");
			$(".bot_btn_02").attr("onclick","completeAuditConsultResources();");
		},
		success :function(data){
			if ('N' == data.successFlag ) {
				alert("补充资料反馈失败：" + data.errorMsg);
				$(".bot_btn_02").css("color","#007aff");
				$(".bot_btn_02").attr("onclick","completeAuditConsultResources();");
			} else {
				alert("补充资料反馈成功！");
			}
		}
	});
}
function chooseTr(currentValue){
	if($(currentValue).is(".table_list_tr_cur")){
		$(currentValue).attr("class","");
	}else{
		$(currentValue).attr("class","table_list_tr_cur");
	}
}
/***发送*/
function sendSmsContent(claimNo){
	 var sms_clmPerson;
	   var sms_salesman;
	   if($('#clmPerson').attr('class')=="check_yes"){
		   sms_clmPerson = 1;

	   }else{
			sms_clmPerson = 0;
	   }
	   
	   if($('#salesman').attr('class')=="check_yes"){
		   sms_salesman = 1;
		  
	   }else{
			sms_salesman = 0;
	   }
	   var operator = $("#operator").val();
	   var yname = $("#yname").val();
	   var phone = $("#phone").val();
	   var phone1 = $("#phone1").val();
	
	$.ajax({
        cache: true,
        type: "POST",
        url:"showsmscontent.do",
        data:{"claimNo":claimNo,"sms_clmPerson":sms_clmPerson,"sms_salesman":sms_salesman,"operator":operator,"yname":yname,"phone":phone,"phone1":phone1},
        async: false,
        error: function(request) {
            alert("发送短信失败！");
        },
        success: function(data) {
        	if(sms_salesman=='1'){
   			 if(operator!=null && operator.length!=11){
         			   alert("请输入11位手机号码");
         			   return ;
         		   }
   		 }
        	
        	if(sms_clmPerson=='1' || sms_salesman=='1'){
				if(data){
					 closeDiv('openMse');
	        		 alert("短信发送成功！");
	        		 $('#clmPerson').attr('class',"check_no");
	        		 $('#salesman').attr('class',"check_no");
	        		 $("#operator").val('');
				}
        	}
        }
    });
}
/**短信内容展示*/
function showSmsContent(claimNo){
	 var sms_clmPerson;
	   var sms_salesman;
	   if($('#clmPerson').attr('class')=="check_yes"){
		   sms_clmPerson = 1;

	   }else{
			sms_clmPerson = 0;
	   }
	   
	   if($('#salesman').attr('class')=="check_yes"){
		   sms_salesman = 1;
		  
	   }else{
			sms_salesman = 0;
	   }
	   var operator = $("#operator").val();
	   
		
	$.ajax({
        cache: true,
        type: "POST",
        url:"showsmscontent.do",
        data:{"claimNo":claimNo,"sms_clmPerson":sms_clmPerson,"sms_salesman":sms_salesman,"operator":operator},
        async: false,
        error: function(request) {
            alert("展示短信失败！");
        },
        success: function(data) {
        	var ba_ms = data[0];
        	var sp_ms = data[1];
        	//alert(ba_ms);
        	$("#ba_ms").text(ba_ms);//申请人
        	$("#sp_ms").text(sp_ms);//业务员
        	var ba_ms_html = $("#ba_ms").html();
        	if (ba_ms_html != '') {
        		
        		ba_ms_html = ba_ms_html.replace("12312312312","<input type=\"text\" id=\"phone1\" name=\"phone1\" class=\"input input_bot_line i_s_100\" />");
        	}
        	
        	var sp_ms_html = $("#sp_ms").html();
        	if (sp_ms_html != '') {
        		sp_ms_html = sp_ms_html.replace("尊敬的","尊敬的<input type=\"text\" id=\"yname\" name=\"yname\" class=\"input input_bot_line i_s_100\" />");
        		sp_ms_html = sp_ms_html.replace("12312312312","<input type=\"text\" id=\"phone\" name=\"phone\" class=\"input input_bot_line i_s_100\" />");
        	}
        	$("#ba_ms").html(ba_ms_html);
        	$("#sp_ms").html(sp_ms_html);
        	
        	
        	$("#phoneno").html(data[2]);
        	showDiv('openMse');
        }
    });
	
	
}
//生成随机数据
function MathRand() { 
var Num=""; 
for(var i=0;i<6;i++) 
{ 
Num+=Math.floor(Math.random()*10); 
} 
	return Num;
} 

function forwardAdit(claimNo){
	SkipByPost("../auditinfo/showauditdetail.do",{claimNo:claimNo});
}