const uploadCert=document.getElementById('uploadCert')
const uploadfile1=document.getElementById('uploadfile1')
uploadCert.onclick=function(){
    uploadfile1.click()
}
const uploadProvision=document.getElementById('uploadProvision')
const uploadfile2=document.getElementById('uploadfile2')
uploadProvision.onclick=function(){
    uploadfile2.click()
}
const uploadipa=document.getElementById('uploadipa')
const uploadfile3=document.getElementById('uploadfile3')
uploadipa.onclick=function(){
    uploadfile3.click()
}
const myuploadipa=document.getElementById('myuploadipa')
const uploadfile4=document.getElementById('uploadfile4')
myuploadipa.onclick=function(){
    uploadfile4.click()
}

uploadjs1= function (){
    var p=document.getElementById("uploadfile1").value;
    if(p.indexOf("p12") == -1){
         $('#uploadCerts').html('<font color=red>文件错误</font>');
    }else{
        $('#uploadCerts').html('<font color=#52ABF8>已选择</font>');
   }
    
   
}

uploadjs2= function (){
    var p=document.getElementById("uploadfile2").value;
   if(p.indexOf("mobileprovision") == -1){
        $('#uploadProvisions').html('<font color=red>文件错误</font>');
   }else{
        $('#uploadProvisions').html('<font color=#52ABF8>已选择</font>');
   }
    
   
}

uploadjs3= function (){
    var p=document.getElementById("uploadfile3").value;
    if(p.indexOf("ipa") == -1){
         $('#uploadipas').html('<font color=red>文件错误</font>');
        
        
    }else{
        $('#uploadipas').html('<font color=#52ABF8>已选择</font>');
   }
    
    
}

uploadjs4= function (){
        var p=document.getElementById("uploadfile4").value;
        if(p.indexOf("ipa") == -1){
         $('#myuploadipas').html('<font color=red>文件错误</font>');
        
    }else{
        $('#myuploadipas').html('<font color=#52ABF8>已选择</font>');
   }
      
}

	$(document).ready(function(){
		var progressbox=$("#progressbox");
		var progressbar=$("#progressbar");
		var progress=$("#progress");
		var progressboxs=$("#progressboxs");
		var progressbars=$("#progressbars");
		var progresss=$("#progresss");
		var completed="0%";
		var completeds="0%";

		var options={
			beforeSubmit:beforeSubmit,
			uploadProgress:OnProgress,
			success:afterSuccess,
			resetForm:true
		};
		
			var optionss={
			beforeSubmit:beforeSubmits,
			uploadProgress:OnProgresss,
			success:afterSuccesss,
			resetForm:true
		};

		$("#uploadform").submit(function(){
		     var uid=document.getElementById("uid").value;
               if(uid==""){
                   alert("请先获取UDID");
                     window.location.href="/"; 
                      return;
                        }
              var p=document.getElementById("uploadfile4").value;
                    if(p.indexOf("ipa") == -1){
                    alert('APP文件错误');
                    return;
                    
                }
			$(this).ajaxSubmit(options);
			return false;
		});
		
		$("#uploadforms").submit(function(){
		     var uid=document.getElementById("uid").value;
             var a=document.getElementById("uploadfile3").value;
             var pw=document.getElementById("password").value;
              var p=document.getElementById("uploadfile1").value;
               var m=document.getElementById("uploadfile2").value;
                if(a.indexOf("ipa") == -1){
                    alert('APP文件错误');
                    return false;
                    
                }
                 if(p.indexOf("p12") == -1){
                    alert('P12文件错误');
                    return false;
                    
                }
                 if(m.indexOf("mobileprovision") == -1){
                    alert('描述文件错误');
                    return false;
                    
                }
                 if(pw==''){
                    alert('P12密码不能为空');
                    return false;
                    
                }
			$(this).ajaxSubmit(optionss);
			return false;
		});


		function OnProgress(event,position,total,percentComplete ) {
			progressbar.width(percentComplete + "%");
			progress.html(percentComplete + "%");
		   $("#output").html("状态:正在上传");
		}

	function OnProgresss(event,position,total,percentComplete ) {
			progressbars.width(percentComplete + "%");
			progresss.html(percentComplete + "%");
		   $("#outputs").html("状态:正在上传");
		}
		
		
		function afterSuccess(data){
		  
		 $("#output").html("状态:正在签名");
		  
			 var apppath = data.data.apppath;
                 var udid = data.data.udid;
                    
                     $.ajax({
                    type: 'POST',
                    url: "/web/signn/sign",
                    data: {
                      type:1,
                      udid:udid,
                      apppath:apppath
                    },
                    dataType: "json",
                    success: function(result, textStatus, jqXHR) {
            
                      if (result.code==1) {
                       
                         var dataurl=result.data.url
                         
                         alert("签名成功\n开始安装");
                         
                       
                         window.parent.window.location.href="itms-services://?action=download-manifest&url=" + dataurl; 
            
                        return false;
                      } else {
                        
                        alert(result.msg);
                       
                        return false;
                      }
                    },
                    error: function(response) {
                    
                      alert('签名请求超时');
                    }});
 
		
				 $('#myuploadipas').html('<font color=#cdcdce>选择IPA文件</font>');
			$("#output").html("状态:空闲");
                         progressbar.width('0%');
		                 progress.html('0%');
			
		}

	function afterSuccesss(data){
		  
		 $("#outputs").html("状态:正在签名");
		  var pw=data.data.pw;
		  var apppath = data.data.apppath;
          var p12 = data.data.p12;
          var mp = data.data.mp;
                    
                     $.ajax({
                    type: 'POST',
                    url: "/web/signn/sign",
                    data: {
                      mp:mp,
                      p12:p12,
                      pw:pw,
                      apppath:apppath
                    },
                    dataType: "json",
                    success: function(result, textStatus, jqXHR) {
            
                      if (result.code==1) {
                   
				
		
                         var dataurl=result.data.url
                       
                         alert("签名成功\n开始安装");
                         
                         //alert('<a href="itms-services://?action=download-manifest&url=' + dataurl + '"><button style="width:70%" type="button" class="up1"><b class="item-title">签名完成-点我安装</b></button></a>');
                         window.parent.window.location.href="itms-services://?action=download-manifest&url=" + dataurl; 
            
                        return false;
                      } else {
                        $('#mysign1').text('立即签名');
                        alert(result.msg);
                       
                        return false;
                      }
                    },
                    error: function(response) {
                    
                      alert('签名请求超时');
                    }});
                 $('#uploadipas').html('<font color=#cdcdce>选择IPA文件</font>');
				 $('#uploadCerts').html('<font color=#cdcdce>选择P12文件</font>');
				 $('#uploadProvisions').html('<font color=#cdcdce>选择描述文件</font>');
				 
	}
	
		function beforeSubmit(){
		
			progressbar.width(completed);
			progress.html(completed);
		}
		
		
		
			function beforeSubmits(){
		
			progressbars.width(completeds);
			progresss.html(completeds);
		}
	});
	




