<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>

<script>

function handleButtonClick(value) { 
            alert('Button value: ' + value); 
        } 

function myFunction(val)
 {

    //alert(val);
  document.getElementById("demo").innerHTML = val;

  // https://codepen.io/chrisdpratt/pen/RKxJNo

  let xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function()
			{
					//  le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200)
					{
                        var response = xhr.responseText;
                        document.getElementById('demo').innerHTML = "Valide";
                        //alert(response);
                    }
                }

               // val = "zoulou";

            xhr.open("POST","version_donwload.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.send("champ="+val);	
            		
			//champ = document.getElementById(val).value;

}

</script>

<?php

    //require "analyse_version.002.php";

    require "debug_analyse_version02.php";

    //echo "<pre>";
	//print_r($_SERVER);
	//echo "</pre>";

    if (isset($_GET['p'])) 
    {
       

        $pieces = explode("*", $_GET['p'] );

        $chemin = $pieces[0];
        //echo "<br><br>";
        $initial = $pieces[1]; 
        //echo "<br><br>";
        
        $chemin = "/home/analogde/www/" .$chemin;

        //echo $chemin;

        $array_file_version = [];
        $array_file_version = analyse_version($chemin);  

        //echo "<pre>";
	    //print_r($array_file_version);
	    //echo "</pre>";

        for ($i = 0; $i <= count($array_file_version); $i++) 
        {
            if( $array_file_version[$i][0] == $initial  )
            {
                $version = $array_file_version[$i][1];
                break;
            }
        }

        //echo "<pre>";
	    //print_r($version);
	    //echo "</pre>";

        


?>

<h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>  

<div class="container">
        <table class="table">
        <thead>
        <tr>
          <th>Name</th>
          <th>Blabla</th>
        </tr>
        </thead>
<?php

        $z = "message";

        for ($i = 0; $i < count($version); $i++) 
        {
            $name = "titi"; //$version[$i];

            $file = $version[$i];
            $path = "/home/analogde/www/Fusion/File explorer";

?>
        <tr>
  	    <td>
<?php
        $indice = $i + 1;

        echo $indice . ":    " .$version[$i] ."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" .formatFileSize($version[$i]);
?>
        </td>

        <td>
        <!--
        <button class="editbtn" onclick="myFunction(<?php echo(json_encode($i)); ?>)">edit</button>
        -->
        <!--
         <button onclick="myFunction(<?php echo $z; ?>)">Click</button>
        -->
         <button onclick="myFunction(this.value)" value="<?php echo $version[$i] ?>" >Button 1</button> 

         <a title="Nombre de versions" href="version_donwload.php?file=<?php echo $file ?>&path=<?php echo $path ?>">Download</a>

        
        </td>
          </tr>


         
<?php
        }
?>        
       
  </table>
 
 <?php

      // retourver toutes les versions en lien avec le fichier initial

        
      $db_host = "analogdepat.mysql.db";
      $db_user = "analogdepat";
      $db_pass = "Un92pac007";
      $db_name = "analogdepat";

      $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);

      
        // Check connection
        if ($conn -> connect_errno) {
          echo "Failed to connect to MySQL: " . $conn -> connect_error;
          exit();
        }
        
        // Perform query
        if ($result = $conn -> query("SELECT * FROM files")) 
        {
          echo "Returned rows are: " . $result -> num_rows;
          // Free result set
          $result -> free_result();
        }
        

        echo "<br><br><br><br>";

        $champ = "code_bla.txt";

        $req = "SELECT * FROM files WHERE source ='$champ'";



        $result = $conn->query($req);

  if ($result->num_rows > 0) 
  {
    // output data of each row
    while($row = $result->fetch_assoc()) 
    {
      echo "id: " . $row["id"]. " ---  " . $row["source"]. " " . $row["revision"]. "<br>";
    }
  } 
  else 
  {
    echo "0 results";
  }

        $conn -> close();

    }
?>

</div>

<!--
<table>
  <tr>
    <th>Name</th>
  </tr>
  <tr>
  	<td>Anna Fitzgerald</td>
  </tr>
  <tr>
  	<td>John Smith</td>
  </tr>
  <tr>
  	<td>Zendaya Grace</td>
  </tr>
</table>

-->

<p id="demo"></p>

<?php

function formatFileSize($filename) 
{
    $size = filesize($filename);

    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    $formattedSize = $size;

    for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++) {
        $size /= 1024;
        $formattedSize = round($size, 2);
    }

    return $formattedSize . ' ' . $units[$i];
}

?>

<!--

https://phppot.com/jquery/dynamic-content-load-using-jquery-ajax/

https://www.educba.com/jquery-replace-div-contents/

https://crunchify.com/how-to-refresh-div-content-without-reloading-page-using-jquery-and-ajax/


-->