<!DOCTYPE html>
<!-- <html> -->
<html style="background-color: white;">
<head>
  <title>ESRU Calibro</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- <meta http-equiv="refresh" content="5"> -->
  <script type="text/javascript" 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 type="text/javascript">
  var iconst = document.getElementsByClassName("ajax_model_anchor");
  var icons0 = document.getElementsByClassName("ajax_anchor");
  var icons1 = document.getElementsByClassName("ajax_icon1");
  /* Result = Interpretation (icon shown on GUI)
      null  = Not simulated yet
          0 = Submitted (not written to MySQL DB but only to job_list file)
          1 = Calibration Running (spin)
          2 = Calibration failed (times)
          3 = PAM Compliant/Satisfactory (check) (file-pdf-o)
          4 = Critical performance issue identified (exclamation-triangle in red background) (file-pdf-o)
          5 = Potential/mild performance issue identified (exclamation-triangle in yellow background) (file-pdf-o)
          6 = Opportunity for performance improvement (gear) (file-pdf-o)
          7 = Cancel (times)
          8 = Cancelled (times)
          9 = Error (times)  */
  function update_results() { // function to update SQL results table from job_list file
    //var job_ids = [];
    //for (var i = 0, n = icons0.length; i < n; ++i)job_ids.push(icons0[i].id); // push currently displayed ids to array
    $.ajax({
     type: "POST",
     //data: "job_ids=" + job_ids,
     cache: false,
     url: "update_results.php",
     success: function(msg){
       var results = msg.trim();
       results = results.replace('[','');
       results = results.replace(']','');
       results = results.replace(/"/g,'');
       //alert("results="+results);
       var tagdata = results.split(",");
       var this_icon = ""; var id = ""; var this_result="";
       var title = "Click to start calibration";
       for (var i = 0, n = icons0.length; i < n; ++i){
         this_pair = tagdata[i].split("<<>>");
         id     = this_pair[0];
         this_result = this_pair[1];

         for (var j = 0; j < n; ++j){
           if(icons0[j].id === id)break;
         }
         //alert("id=" + id + "\nresult=" + this_result + "\nicon id=" + icons0[j].id + "\nj=" + j);
         if(this_result == "0"){ // Submitted
           iconst[j].title      = "Click to terminate calibration";
           icons1[j].title      = "Preparing to calibrate";
           icons1[j].className  = "ajax_icon1 fa fa-pulse fa-spinner";
           icons1[j].style.color= "grey";
         }else if(this_result == "1"){ // Running
           iconst[j].title      = "Click to terminate calibration";
           icons1[j].title      = "Calibration commenced";
           icons1[j].className  = "ajax_icon1 fa fa-pulse fa-spinner";
           icons1[j].style.color= "grey";
         }else if(this_result == "2"){ // Failed
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Calibration failed";
           icons1[j].className  = "ajax_icon1 fa fa-file-pdf-o";
           icons1[j].style.color= "Red";
         }else if(this_result == "3"){ // Compliant
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Results compliant/satisfactory";
           icons1[j].className  = "ajax_icon1 fa fa-file-pdf-o";
           icons1[j].style.color= "green";
         }else if(this_result == "4"){ // Critical performance issue
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Critical performance issue identified";
           icons1[j].className  = "ajax_icon1 fa fa-file-pdf-o";
           icons1[j].style.color= "red";
         }else if(this_result == "5"){ // Potential/mild performance issue
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Potential performance issue identified";
           icons1[j].className  = "ajax_icon1 fa fa-file-pdf-o";
           icons1[j].style.color= "orange";
         }else if(this_result == "6"){ // Opportunity for performance improvement
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Opportunity for performance improvement identified";
           icons1[j].className  = "ajax_icon1 fa fa-file-pdf-o";
           icons1[j].style.color= "ye";
         }else if(this_result == "7"){ // Cancel
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Cancelling";
           icons1[j].className  = "ajax_icon1";
         }else if(this_result == "8"){ // Cancelled
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Cancelling";
           icons1[j].className  = "ajax_icon1";
         }else if(this_result == "9"){ // Error
           iconst[j].title      = "Click to start calibration";
           icons1[j].title      = "Calibration error check your model";
           icons1[j].className  = "ajax_icon1 fa fa-times";
           icons1[j].style.color= "red";
         }else{
           icons0[j].title      = "Click to start calibration";
           icons1[j].title      = " ";
           icons1[j].className  = "ajax_icon1";
         }
       }
     }
    });
    return false;
  }

    // Upon page load update results and update SQL table
    window.onload = function() {
      update_results();
    };
    setInterval(update_results, 10*1000); // run every 10 * 1000 milliseconds
    //setInterval(update_model_results, 30*1000); // run every 10 * 1000 milliseconds
  </script>
</head>
<body>
  <?php
    require "UserAdmin/super_secret_stuff.php";
	$get_id = "";
	if(!empty($_GET["id"]))$get_id = $_GET["id"];
    $selected_id = (int)$get_id;
    $con = mysqli_connect($website,$mysqli_username,$pw,$dbase);
    $user_id = get_userid($con);

    // Get number of jobs submitted by this user
    $res = mysqli_query($con, "SELECT COUNT(calproj_id) FROM Calibro_results WHERE submitted_by = {$user_id} AND result = 1");
    while($row = mysqli_fetch_array($res))$jobs_submitted = $row[0];

    // if user clicked on a submitted/running job then terminate this job
    if(is_numeric($selected_id) && $selected_id != 0){

      $previously_sent_request = false;
      $res = mysqli_query($con,"SELECT COUNT(result) FROM Calibro_results WHERE calproj_id = {$selected_id}");
      $row = mysqli_fetch_array($res);
      if($row[0] > 0)$previously_sent_request = true;

      if($previously_sent_request){
        $res = mysqli_query($con,"SELECT result FROM Calibro_results WHERE calproj_id = {$selected_id}");
        $row = mysqli_fetch_array($res);
        $result = $row[0];
        if($result == "0" || $result == "1"){ // submitted or running
          mysqli_query($con,"UPDATE Calibro_results SET result = 7,submitted_by = {$user_id} WHERE calproj_id = {$selected_id}");
        }else{
          mysqli_query($con,"UPDATE Calibro_results SET result = 0,submitted_by = {$user_id} WHERE calproj_id = {$selected_id}");
        }
      }else{
        mysqli_query($con,"INSERT INTO Calibro_results(calproj_id,result,submitted_by) VALUES({$selected_id},0,{$user_id})");
      }
    }
  ?>
  <!--  ######################################  -->
  <!--  DISPLAY CALIBRATION PROJECTS AVAILABLE  -->
  <!--  ######################################  -->
  <div id="header_div_left" class="normal_text1">
      <a href="#" title="Calibration models available for you">Calibrations</a>
      <a href="edit_model.php" style="color:#2E75B6; style:float-right;"><i class="fa fa-edit" title="Edit model name"></i></a>
      <a href="add_model.php" style="color:#2E75B6; style:float-right;"><i class="fa fa-plus" title="Add new calibration model"></i></a>
  </div>

  <div id="header_div_right"></div>

  <div id="body_div_left" class='normal_text2'>
    <?php
      // New edit commencing
      $res = mysqli_query($con,"SELECT calproj_id,name FROM Calibro_projects WHERE shared_between like '%,{$user_id},%'");
      while($row = mysqli_fetch_array($res)){
        $id = $row[0];
        $name = $row[1];
        $link = "projects.php?id={$id}";
        echo "  <a title='Click to start calibration' href='{$link}' class='ajax_model_anchor'>{$name}</a>";
        $res1 = mysqli_query($con,"SELECT result FROM Calibro_results WHERE calproj_id = ${id}");
        $row1 = mysqli_fetch_array($res1);
        $result_string = get_result_string($row1[0]);
        $icon1 = "<i class='ajax_icon1' id='{$id}' style='font-size:1.0em;'></i>";
        echo "
              <a class='ajax_anchor' id='{$id}' title='Calibration result'></a>
              <a href='show_report.php?id={$id}' target='blank_'>{$icon1}&nbsp</a>
              <br/>"; // non breaking space writes a blank line ensuring calibration models and their results are on the same line
      } // end while
      mysqli_close($con);
    ?>
  </div>


</body>
</html>





























<!-- -->
