<!DOCTYPE html>
<html>
<head>
  <title>ESRU BPA</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <link type="text/css" rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"/>
  <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro"/>
  <link type="text/css" rel="stylesheet" href="../CSS/style.css"/>
  <script src="../JS/js_functions.js"></script>
  <script>
    function HandleBrowseClick(i){
      var fileinput = document.getElementById("browse" + i);
      fileinput.click();
    }
    function DescEntered() {
      var fileinput1 = document.getElementById("browse1");
      var fileinput2 = document.getElementById("browse2");
      var fileinput3 = document.getElementById("browse3");
      var fileinput4 = document.getElementById("browse4");
      var model_str = document.getElementById("model_desc");
      if(
        fileinput1.files.item(0).name !== '' &&
        //fileinput2.files.item(0).name !== '' &&
        //fileinput3.files.item(0).name !== '' &&
        //fileinput4.files.item(0).name !== '' &&
        model_str.value.trim() !== ''
      ){
        DisplaySubmitBtn();
      }else{
        document.getElementById("submitbutton").style.display = "none";
      }
    }
    function Handlechange(i){
      var fileinput = document.getElementById("browse" + i);
      var textinput = document.getElementById("filename" + i);
      textinput.value = fileinput.files.item(0).name;

      var model_str = document.getElementById("model_desc");
      if(model_str.value.trim() == ''){
        window.alert("Enter small phrase describing model");
      }else{
        DescEntered();
      }
    }
    function DisplaySubmitBtn(){
      var disp_butn = document.getElementById("submitbutton");
      disp_butn.style.display = "inline";
    }
    function postForm(){
      document.forms["add_model"].submit();
    }
  </script>
</head>
<body>
  <!-- Add model -->
  <?php
    $get_add = "";
	if(!empty($_GET["add"]))$get_add = $_GET["add"];
    if($get_add == "yes"){
      require "UserAdmin/super_secret_stuff.php";
      $con = mysqli_connect($website,$mysqli_username,$pw,$dbase);
      $model_name = $_POST["model_name"];

      $filename1 = $_FILES["file1"]["name"];
	  $path_parts1 = pathinfo($filename1);
	  $filename1_only = $path_parts1['basename'];
 
      $user_id = get_userid($con);
      $res = mysqli_query($con,"SELECT COUNT(calproj_id) FROM Calibro_projects WHERE shared_between LIKE '%,{$user_id},%'");
      $row = mysqli_fetch_array($res);
      $next_proj = (int)$row[0];
      $next_proj++;
      $target_file1 = $user_id . "_" . $next_proj . "_" . $filename1;
      $target_dir = $proj_file_path_deep;
      $target_file_n_path1 = $target_dir . $target_file1;
      $uploadOk = true;

      echo "<div class='w3-display-middle'>";

      if(file_exists($target_file_n_path1)){
    	  echo "<h3>Model by this name already exists as {$target_file_n_path1} change the name of your file and upload again </h3>";
    		$uploadOk = false;
    	}

      // Check if $uploadOk is set to false by an error
      if ($uploadOk == false) {
      	echo "<h3>Sorry, your file was not uploaded </h3></div>";
        exit;

      }else{ // if everything is ok, try to upload file
        $uploadOK = true;
        if(move_uploaded_file($_FILES["file1"]["tmp_name"], $target_file1)){
          $md5sum = -1;
          if(file_exists($target_file1))$md5sum = md5_file($target_file1);
          if(rename($target_file1,$target_file_n_path1)){}else{$uploadOK = false;}
        }else{
          $uploadOK = false;
        }

        if($uploadOK){
          mysqli_query($con,"INSERT INTO Calibro_projects(name,meas_perf,MD5tarball,shared_between)
          VALUES('{$model_name}','{$target_file1}','{$md5sum}',',{$user_id},')");
          mysqli_close($con);
          /*
          TODO
          1. User to be able to edit project shared_between
          */
          header("location: projects.php");
        }else{
      		echo "<h3>Sorry, there was an error uploading your file. Press browser back button to continue working</h3></div>";
      		exit;
      	}
      } // try to upload file
    } // if $_GET["add"] = "yes";
  ?>

  <!-- Display UI for adding model -->
    <div id="header_div_left" class="normal_text1">
		Add model
	</div>

    <div class="normal_text1">Add model</div>
    <form action="add_model.php?add=yes" id="add_model" method="post" enctype="multipart/form-data">
      <p class="normal_text4">
        <input id="model_desc" type="text" pattern="[a-zA-Z0-9.-\s]*" maxlength="80" size="40"
         title="Only alphanumerics and . allowed" name="model_name"
         placeholder="Enter small phrase describing model*" onblur="DescEntered()">
      </p>

      <p class="normal_text4">
        <input type="file" id="browse1" name="file1" style="display: none" onChange="Handlechange(1);"/>
        <label for="browse1" style='opacity:0.6;'>
          <!-- button type "button" ensures form is not submitted when this button is clicked -->
          <button type="button" title="Accepts *.csv file" onClick="HandleBrowseClick(1)">
            <i class='fa fa-folder-open-o'></i>Select compressed model</button>
          <p class="normal_text4"><input class="liketext" type="text" id="filename1" placeholder="Measured performance file" size="40" disabled="disabled"/></p>
        </label>
      </p>
      <p class="normal_text4" style='opacity:0.6;'>
	    <a href="https://bpa.miraheze.org/wiki/Main_Page" target="_blank"><u>see formatting guidelines</u></a>
		<a href="javascript: postForm()" id='submitbutton' name='edit_details' style='display:none;position:relative;left:190px;cursor:pointer;'><i class='fa fa-check'></i></a>
      </p>

    </form>
  
</body>
</html>



























<!--  -->
