<?php
include("./includes/common.php");
$title = '新建文本文件 - '.$conf['title'];
include("./includes/header.php");
?>
<div class="container">
  <div class="panel panel-success">
    <div class="panel-heading">新建文本文件</div>
    <div class="panel-body">
      <div class="well" style="background:#f8f9fa;border:1px solid #e3e3e3;box-shadow:none;">
        <form id="form-text">
          <div class="form-group">
            <label for="filename">文件名：</label>
            <input type="text" class="form-control" id="filename" name="filename" required placeholder="请输入文件名(不需要输入.txt后缀)">
          </div>
          <div class="form-group">
            <label for="content">文件内容：</label>
            <textarea class="form-control" id="content" name="content" rows="10" required placeholder="请输入文件内容"></textarea>
          </div>
          <button type="submit" class="btn btn-success btn-lg btn-block waves-effect waves-light">
            <i class="fa fa-file-text"></i> 创建文件
          </button>
        </form>
      </div>
      <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-text").submit(function(e){
    e.preventDefault();
    var filename = $("#filename").val();
    var content = $("#content").val();
    if(filename == ''){
      alert("请输入文件名");
      return false;
    }
    if(content == ''){
      alert("请输入文件内容");
      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: "./create_txt.php",
      data: {filename:filename,content:content},
      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();
            $("#filename").val("");
            $("#content").val("");
          }else if(data.code == 1){
            $("#result").attr("class", "alert alert-info").html(data.msg).show();
            $("#filename").val("");
            $("#content").val("");
          }else{
            $("#result").attr("class", "alert alert-danger").html(data.msg).show();
          }
        }, 500);
      },
      error: function(jqXHR, textStatus, errorThrown){
        clearInterval(progressInterval);
        $(".progress").hide();
        // 显示服务器返回的原始错误信息
        $("#result").attr("class", "alert alert-danger").html("服务器错误: " + jqXHR.responseText).show();
      },
      complete: function(){
        btn.attr("disabled", false);
      }
    });
  });
});
</script>
<?php include("./includes/footer.php");?>