<?php
include("./includes/common.php");
$title = '远程下载 - '.$conf['title'];
include("./includes/header.php");
?>
<div class="container">
  <div class="panel panel-primary">
    <div class="panel-heading">远程下载</div>
    <div class="panel-body">
      <form id="form-download">
        <div class="form-group">
          <label for="url">URL地址：</label>
          <input type="url" class="form-control" id="url" name="url" required placeholder="请输入文件的URL地址">
        </div>
        <button type="submit" class="btn btn-primary btn-lg btn-block waves-effect waves-light">
          <i class="fa fa-cloud-download"></i> 开始下载
        </button>
      </form>
      <div class="progress" style="margin-top:10px;display:none;">
        <div class="progress-bar progress-bar-striped active" role="progressbar" style="width: 0%">
          <span>0%</span>
        </div>
      </div>
      <div id="result" style="margin-top:10px;display:none;" class="alert alert-info"></div>
    </div>
  </div>
</div>
<script>
$(document).ready(function(){
  $("#form-download").submit(function(e){
    e.preventDefault();
    var url = $("#url").val();
    if(url == ''){
      alert("请输入URL地址");
      return false;
    }
    var btn = $(this).find("button");
    btn.attr("disabled", true);
    $(".progress").show();
    $("#result").hide();
    
    var progress = 0;
    var progressInterval = setInterval(function(){
      progress += Math.random() * 30;
      if(progress > 90) progress = 90;
      $(".progress-bar").css("width", progress + "%");
      $(".progress-bar span").text(Math.round(progress) + "%");
    }, 500);
    
    $.ajax({  
      type: "POST",
      url: "./yc_submit.php",
      data: {url:url},
      dataType: "json",
      success: function(data){
        clearInterval(progressInterval);
        $(".progress-bar").css("width", "100%");
        $(".progress-bar span").text("100%");
        setTimeout(function(){
          $(".progress").hide();
          if(data.code == 0){
            $("#result").attr("class", "alert alert-success").html(data.msg).show();
            $("#url").val("");
          }else if(data.code == 1){
            $("#result").attr("class", "alert alert-info").html(data.msg).show();
            $("#url").val("");
          }else{
            $("#result").attr("class", "alert alert-danger").html(data.msg).show();
          }
        }, 500);
      },
      error: function(){
        clearInterval(progressInterval);
        $(".progress").hide();
        $("#result").attr("class", "alert alert-danger").html("服务器错误").show();
      },
      complete: function(){
        btn.attr("disabled", false);
      }
    });
  });
});
</script>
<?php include("./includes/footer.php");?>