<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="wap-font-scale" content="no">
<title>保函在线</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="${contextPath}/static/weixin/ssdb/css/common.css" rel="stylesheet">
<link href="${contextPath}/static/weixin/ssdb/css/main.css" rel="stylesheet">
<link href="${contextPath}/static/weixin/ssdb/css/ssdb.css" rel="stylesheet">
<link href="${contextPath}/static/weixin/ssdb/css/style.css" rel="stylesheet">
<script src="${contextPath}/static/js/flexible.min.js"></script>
<style type="text/css">
	.filediv{
			display : inline-block;
		position : relative;
	}
	.filediv .delete{
		position : absolute;
		top : 0px;
		right : 0px;
		width : 20px;
		height : 20px;
	}
</style>
</head>
<body class="bg-grey">
	<div class="pad-lr-30 ub bc53a2 fs-12 service-progress">
		<div class="ub-f1 service-progress-item active">
			<div class="service-progress-bar"></div>
			<div class="service-progress-dotted "></div>
			<p>申请</p>
		</div>
		<div class="ub-f1 service-progress-item">
			<div class="service-progress-bar"></div>
			<div class="service-progress-dotted"></div>
			<p>审核</p>
		</div>
		<div class="ub-f1 service-progress-item">
			<div class="service-progress-bar"></div>
			<div class="service-progress-dotted"></div>
			<p>付款</p>
		</div>
		<div class="ub-f1 service-progress-item">
			<div class="service-progress-bar"></div>
			<div class="service-progress-dotted"></div>
			<p>出函</p>
		</div>
	</div>
	<section>
		<p class="retinabt"></p>
		<div id="searchDetailID" class="pad-lr-20 bg-white fs-14 fc-333 ub ub-pj ub-ac ub-pj guarantee-top" style="margin-top: 0.1rem;">
			<p>筛选基本信息</p>
			<span class="icon icon-list-arrow"></span>
		</div>
		<p class="retinabt"></p>
			<p class="retinabt mar-t-15"></p>
		<div class="pad-lr-20 bg-white fs-14 fc-333 ub ub-pj ub-ac ub-pj guarantee-top" style="margin-top: 0.1rem;">
			<p>诉讼保全责任险购买说明</p>
			<span class="icon icon-list-arrow"></span>
		</div>
		<div>
		<p class="retinabt"></p>
		<p class="pad-l-20 fs-14 fc-555 pad-tb-5">申请所需资料</p>
		<p class="retinabt"></p>
		<div class="pad-lr-20 pad-t-10">
			<ul id='fileListID'>
			</ul>
		</div>	
	</section>
	<a href="#" id="submitId" class="btn btn-blue-height btn-lar fs-16 online-letter-filter-btn" >提交</a>
	<script src="${contextPath}/static/js/jquery-1.12.0.min.js"></script>
	<script type="text/javascript" src="${contextPath}/static/js/uuid.js"></script>
	<script type="text/javascript" src="${contextPath}/static/js/layer/layer.js"></script>
	<script type="text/javascript" src="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.3.0.min.js"></script>
   <script type="text/javascript" src="${contextPath}/static/js/requestutil.js"></script>
   <script type="text/javascript" src="${contextPath}/static/js/jstorage.js"></script>
   <script type="text/javascript">
 	$(document).ready(function() {
 		baseURL='<%=com.fadu.app.util.AppConfig.url_duowenapp_b%>';
  		//动态显示
  		token=$.jStorage.get("token");
  		var company_id="${company_id}";
  		var searchId="${searchId}";
  		var orderId="${orderId}";
  		if(company_id){
  			if(token){
  				 var jsonReq={};
		  		jsonReq.companyId=company_id;
		  		jsonReq.token=token;
		  		jsonReq.searchId=searchId;
		  		jsonReq.actionCode="B518";
		  		var url=baseURL+"/B/android/";
		  		console.log(jsonReq);
		  		requestData(url,jsonReq,success,fail);
		  		//筛选基本信息
		  		$("#searchDetailID").click(function(inx,val){
		  			var url="${contextPath}/web/ssdb/toSearchInfo?companyId="+company_id+"&searchId="+searchId;
		  			location.href=url;
		  		});
  			}else{
  				 layer.msg('没有登录,请登录!!');
  				 var url="${contextPath}/web/ssdb/toLogin";
				 location.href=url;  		
  			}
  		}else{
  			if(orderId){
				getOrderDetail(orderId);//订单详情  				
  			}else{
  				layer.msg('加载失败,稍后重试!!');
  			}
  		}
  		function success(json){
  				console.log(json);
  				 if(json){
	  					if(json.success==true){
	  						//筛选基本信息
							var attachs=json.attachs;
							var html="";
							$.each(attachs,function(index,value){
								var start='';
								if(value.need==1){
									start='*';
								}
								var index_=index+1;
								var id=value.id;
								if(!id){
									id="otherId";
								}
								window[id]=[];
								html=html+'<li attr-id="'+id+'" attr-name="'+value.name+'" attr-need="'+value.need+'"><div><p ><span class="colorff9900 pad-t-5">'+start+'</span><span class="fs-16 fc-333 pad-t-5">'+index_+'.'+value.name+'</span></p></div>';
								html=html+'	<p class="retinabt"></p><div>';
								html=html+'<input type="file" id="'+id+1+'" name="social1" onchange="readURL(this,\''+id+'\',\''+index+'\')"   accept="image/*"  style="visibility: hidden; position: absolute;">';
								html=html+'<label for="'+id+1+'" class="s1" >'
								html=html+'<img id="imgFile" style="width:1rem;padding:0.2rem 0 0 ;"   src="${contextPath}/static/weixin/ssdb/images/icon-updata-img.png" /></label></div>';
								html=html+'<div id="ls'+id+'" ></div></li>';
							});
							$("#fileListID").append(html);
							//json外层信息
							bdCode=json.bdCode;
							bf=json.bf;
							companyId=json.companyId;
							courtId=json.courtId;
							sqbd=json.sqbd;
							stage=json.stage;
							points=json.points;
							bdCode1=json.bdCode1;
							bdCode2=json.bdCode2;
							//文件添加操作
							var liLength=attachs.length;//需要上传的数组有多少个
							arrayFile=new Array();
							for(var i=0;i<liLength;i++){
								arrayFile[i] = new Array();
							}
	  					}else{
	  						layer.msg(json.message);	
	  					}
	  				}else{
	  					layer.msg("获取信息失败!!");
	  				}	
  			}
  			//提交操作
  			$("#submitId").click(function(){
  				var ul=$("#fileListID li").length;
				for(var i=0;i<ul;i++){
				var isNeed=$($("#fileListID li").get(i)).attr("attr-need");
				var fileID=$($("#fileListID li").get(i)).attr("attr-id");
				var fileName=$($("#fileListID li").get(i)).attr("attr-name");
	  				if(isNeed==1){//这个附件是必须传入的
	  				  var length=$("#ls"+fileID).children("div").length;
	  				  if(length<=0){
						layer.msg("必须上传"+fileName)
	  				  return ;
	  				  }
	  				}
				}	  			
  				layer.load();
  				$("#submitId").text('数据提交中..');
  				/* var fileTypeLength=$("#fileListID li").length;
  				for(var i=0;i<fileTypeLength;i++){
  				var fileID=$($("#fileListID li").get(i)).attr("attr-id");
  				}
  				return; */
  				
  				if(typeof arrayFile != 'undefined'&&arrayFile){
	  				console.log(arrayFile);
	  				var sizeFile=arrayFile.length;
	  				var rows = 0;
					for ( var lvl = 0; lvl < arrayFile.length; lvl++ )
					{
					    rows += arrayFile[ lvl ].length;
					}
	  				for(var i=0;i<sizeFile;i++){
	  					var fileID=$($("#fileListID li").get(i)).attr("attr-id");
	  					var outerSize=arrayFile[i].length;
	  					for(var j=0;j<outerSize;j++){
	  						if(arrayFile[i][j]){
	  							ossUp(arrayFile[i][j], fileID,rows);	
	  						}else{
	  							console.log(fileID+"文件不存在!!");
	  						}
	  					}
	  				}
  				}else{
  					layer.msg("没有上传文件!!");
  				}
  			});
  			function fail(resp){
  				console.log(resp);
  				layer.msg("加载失败!!稍后重试!!");
  			}
  			
  			//订单详情
  			function getOrderDetail(orderId){
  				if(!orderId){
  					return;
  				}
  				
  				var jsonReq={};
		  		jsonReq.token=token;
		  		jsonReq.orderId=orderId;
		  		jsonReq.step=1;
		  		jsonReq.actionCode="B511";
		  		var url=baseURL+"/B/android/";
		  		console.log(jsonReq);
		  		requestData(url,jsonReq,success511,fail511);
  			}
  			//请求成功
  			function success511(res){
  				console.log(res);
  				if(res){
  					if(res.success==true){
  						
  					
  					}else{
  						layer.msg(res.message);
  					}
  				}else{
  					layer.msg("请求出错,请稍后重试!!");
  				}
  			}
  			//请求失败
  			function fail511(res){
  				console.log(res);
  				layer.msg("加载失败!!稍后重试!!");
  			}
  			
   });
  </script>
  <script type="text/javascript">
  function readURL(input,id,index) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
        	var img=new Image();
        	var imgDel=new Image();
        	var fileDiv=$("#ls"+id);
        	var divEle=$("<div></div>");
        	divEle.addClass("filediv");
        	imgDel.src ="${contextPath}/static/weixin/ssdb/images/delete.png"; //创建一个object URL，并不是你的本地路径
        	imgDel.className="delete";
        	imgDel.onclick= function(e){
        	$(this).remove();
        	$(img).remove();
			//loadImageDelete(input.files[0],img.name);
			}
        	img.src=e.target.result;
        	img.width =60;
			img.height =60;
			var outFile=input.files[0];
			var fileSize=$(fileDiv).children().size();
			arrayFile[index][fileSize]=outFile;
			console.log(arrayFile);
        	divEle.append(img);
			divEle.append(imgDel);
			fileDiv.append(divEle);
        }
        reader.readAsDataURL(input.files[0]);
    }
}
</script>
<script type="text/javascript">
	 //appServer = 'http://b.duowenlvshi.com/oss/token?name=ssdb';
	 appServer = 'http://test.duowenlvshi.com/duowenapp_b/oss/token?name=ssdb';
	 bucket = '${oss_bucket_ssdb}';
	 region = 'oss-cn-shenzhen';
	 Buffer = OSS.Buffer;
	 OSS = OSS.Wrapper;
	 STS = OSS.STS;
function ossUp(value,id,rows){
       attach=[];
       otherAttach=[];
	   var url = appServer;
        $.ajax({
            type: "POST",
            url: url,
            dataType: "jsonp",
           	async:false,
            success: function(e) {
                var client = new OSS({
                    region: region,
                    accessKeyId: e.AccessKeyId,
                    accessKeySecret: e.AccessKeySecret,
                    stsToken: e.SecurityToken,
                    bucket: bucket
                });
                var fileName = UUIDjs.create().hex;
    	 		var fileType = getFileType(value.name);
         		var key = "ssdb/order/"+companyId+"/"+new Date().getFullYear()+"/"+new Date().getMonth()+"/"+fileName+"."+fileType;
                client.multipartUpload(key, value,{
            	progress: progress
        		}).then(function(res) {
        			var  attachJson={};
        			var otherAttachJson={};
        			console.log(id+""+fileName);
        			if(id=="otherId"){
	        			otherAttachJson.id="";
	        			otherAttachJson.name=fileName;
	        			otherAttachJson.path=key;
	        			otherAttachJson.suffix=fileType;
	        			otherAttach.push(otherAttachJson);
        			}else{
	        			attachJson.id=id;
	        			attachJson.name=fileName;
	        			attachJson.path=key;
	        			attachJson.suffix=fileType;
	        			attach.push(attachJson);
        			}
        			console.log(attach);
        			console.log(otherAttach);
        			//暂时先这样  要 考虑更好的方案！！！
        			if(attach.length+otherAttach.length>=rows){
        				var cParameters={"attachOthers":otherAttach,"courtId":courtId,"bdCode":bdCode,"attachs":attach,"bf":bf,"companyId":companyId,"sqbd":sqbd,"stage":stage,"points":points,"bdCode1":bdCode1,"bdCode2":bdCode2,"actionCode":"B512","encryptCode":"","time":0,"token":token}
        				var url=baseURL+"/B/android/";
        				console.log(cParameters);
        				requestData(url,cParameters,successFile,failFile);
        			}else{
        				console.log("没有到指定位置!!");
        			}
        		});
            },
            error: function(data) {
                console.log(data);
            }
        	});
	}
	function successFile(json){
				layer.closeAll('loading');
  				console.log(json);
  				 if(json){
	  					if(json.success==true){
	  						if(json.orderId){
	  							var orderId=json.orderId;
	  							var url="${contextPath}/web/ssdb/toConfirm?orderId="+orderId;
	  							location.href=url;
	  						}else{
	  							layer.msg("创建订单失败!!");	
	  						}
	  					}else{
	  						layer.msg(json.message);	
	  					}
	  				}else{
	  					layer.msg("获取信息失败!!");
	  				}	
  			}
	function failFile(resp){
		layer.closeAll('loading');
		console.log(resp);
		layer.msg("提交失败!!稍后重试!!");
	}
	//根据文件名 获取后缀
	function getFileType(fileName){
    	var ldot = fileName.lastIndexOf('.')
    	if(ldot>=0)
    		return fileName.substring(ldot + 1);
    	else
    		return "";
    }
	 var progress = function(p) {//上传成功的进度条
    	return function (done) {
  	 		 done();
  		};
    }
    ;
</script>
</body>
</html>
