<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Page Title -->
  <title>Web Infra Reports IT</title>
  <link rel="shortcut icon" type="image/png" href="/include/favicon-32x32.png">

  <!-- JQuery -->
  <script src="/js/jquery-3.6.1.min.js"></script>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/bootstrap-icons/bootstrap-icons.css">
  <link rel="stylesheet" href="/css/preloader.css">
  <script src="/js/bootstrap.bundle.min.js"></script>
  <!-- Bootstrap-tables -->
  <link rel="stylesheet" href="/css/bootstrap-table.min.css">
  <script src="/js/bootstrap-table.min.js"></script>
  <script src="/js/bootstrap-table-fr-FR.min.js"></script>
  <script src="/js/tableExport.min.js"></script>
  <script src="/js/bootstrap-table-export.min.js"></script>
  <script src="/js/libs/js-xlsx/xlsx.core.min.js"></script>


</head>

<body class="bg-light text-dark">
<?php include $_SERVER['DOCUMENT_ROOT'] . "/include/all.php"; ?> <!-- Include All -->
<?php $answers = Invoke_Infra("select * from cmdb_vms where cluster like 'DMV-VMH-SYN%' and decomtime is null order by name"); ?>

<!-- HTML -->
<div class="container-fluid" id="content">
  <div class="row flex-nowrap">
    <!-- Left NAVBAR -->
    <div class="col-auto col-md-2 col-xl-2 px-sm-2 px-0 bg-dark vh-100 position-sticky top-0" style="-ms-flex: 0 0 230px;flex: 0 0 230px;">
      <?php include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html"; ?>
    </div>
    <!-- Display -->
    <div class="col py-3">
      <!-- Page Title -->
      <h2><span class="badge text-bg-secondary " style="width:100%;" >Synergy VM List - <?php echo count($answers); ?> VMs</span></h2>

      <!-- Main content -->
      <div class="container-fluid">
        <!-- MODAL WAIT -->
        <div class="modal fade bs-example-modal-sm" id="wait" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" style="padding-top: 15%;">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title mb-1 text-dark text-uppercase text-center">
                  <i class="bi bi-hourglass-split"></i><br> Work in progress ...
                </h4>
              </div>
              <div class="modal-body">
                <div class="progress">
                  <div class="progress-bar progress-bar-secondary progress-bar-striped progress-bar-animated" style="width: 100%"></div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- TABLE -->
        <div>
          <div id="result" class="text-center"></div>
          <table class='table table-bordered table-hover table-sm' id='t1' data-height="620" data-toggle="table" data-search="true" data-show-columns="true" data-export-types="['xlsx','csv','json']" data-show-export="true" data-sortable="true" data-sort-name="vm">
            <thead> <!-- Header -->
            <tr>
              <th data-field='vm' data-sortable='true'>VM</th>
              <th data-field='owner' data-sortable='true'>Owner</th>
              <th data-field='infos' data-sortable='true'>Infos</th>
              <th data-field='PreferedOwner' data-sortable='true'>Prefered Owner</th>
              <th data-field='gen-ver' data-sortable='true'>Gen & Version</th>
              <th data-field='LastInventory' data-sortable='true'>Last Seen</th>
            </tr>
            </thead>

            <tbody> <!-- Body -->
            <?php
              $wrongOwner = $unclustered = $v10 = 0 ; $msg ="" ; $CpuComp = 0; $DynVHD = 0;
              foreach ($answers as $row) {
                echo "<tr>";
                # Name
                $state = '<i class="bi bi-question-square-fill text-primary"></i> ';
                if($row['State'] == "Running"){$state = '<i class="bi bi-play-btn-fill text-success"></i> ';}
                if($row['State'] == "Off" || $row['State'] == "Stopping"){$state = '<i class="bi bi-stop-btn-fill text-danger"></i> ';}
                if($row['State'] == "Paused"){$state = '<i class="bi bi-pause-btn-fill text-warning"></i> ';}
                echo "<td>".$state." <b>".$row['Name']."</td>";
                # Owner
                echo "<td>".$row['Owner']."</td>";
                # Infos
                echo '<td><span class="badge rounded-pill bg-secondary text-light">'.$row['Memory'].'GB / '.$row['CPU'].' CPU</span> ';
                echo '<span class="badge rounded-pill bg-secondary text-light">VHDx(s) : '.$row['TotalDiskSize'].' GB</span> ';
                if($row['WWPNs'] != ""){
                  echo '<span class="badge rounded-pill bg-primary text-light">LUN(s)</span> ';
                }
                if($row['CPUcomp'] == "True"){
                  echo '<span class="badge rounded-pill bg-warning text-dark">CPU Comp.</span> '; $CpuComp++;
                }
                if($row['DynamicVHD'] == "Y"){
                  echo '<span class="badge rounded-pill bg-secondary text-warning">Dyn. VHDX</span> '; $DynVHD++;
                }
                echo "</td>";
                # Prefered Owner
                if($row['Owner'] == $row['PreferredOwner']){
                  echo '<td><span class="badge rounded-pill bg-success text-light">'.$row['Owner'].'</span></td> ';
                }else{
                  if($row['IsClustered'] == 'True'){
                    echo '<td><span class="badge rounded-pill bg-warning text-dark">'.$row['Owner'].'</span></td> ';
                    $wrongOwner++;
                  }else{
                    echo '<td><span class="badge rounded-pill bg-danger text-dark">NOT CLUSTERED</span></td> ';
                    $unclustered++;
                  }
                }
                # Gen & Ver
                echo "<td>";
                if($row['Generation'] == "2"){
                  echo '<span class="badge rounded-pill bg-success text-light">Gen '.$row['Generation'].'</span> ';
                }else{
                  echo '<span class="badge rounded-pill bg-warning text-dark">Gen '.$row['Generation'].'</span> ';
                }
                if($row['Version'] == "10.0"){
                  echo '<span class="badge rounded-pill bg-success text-light">Ver '.$row['Version'].'</span> ';
                }else{
                  echo '<span class="badge rounded-pill bg-warning text-dark">Ver '.$row['Version'].'</span> ';
                  $v10++;
                }
                echo "</td>";
                # TS
                echo "<td>".$row['LastInventory']."</td>";
                echo "</tr>";


              }
              if($wrongOwner > 0){$msg = "<span class='badge bg-warning text-dark'>$wrongOwner VM(s) on Wrong Owner</span>";}
              if($unclustered > 0){$msg .= "&nbsp;<span class='badge bg-danger'>$unclustered VM(s) Unclustered</span>";}
              if($v10 > 0){$msg .= "&nbsp;<span class='badge bg-warning text-dark'>$v10 VM(s) < Ver 10.0</span>";}
              if($CpuComp > 0){$msg .= "&nbsp;<span class='badge bg-warning text-dark'>$CpuComp VM(s) With 'CPU Compatibility' checked</span>";}
              if($DynVHD > 0){$msg .= "&nbsp;<span class='badge bg-secondary text-warning'>$DynVHD VM(s) With Dynamic VHDx</span>";}
            ?>
            </tbody>
          </table>
          <br>
        </div>
      </div>
      <!-- End of main content -->
    </div>
  </div>
</div>
</body>
<script src="/js/switch.js"></script>
</HTML>

<SCRIPT>
  $(function() {
    const table = $('#t1');
    // Exécution initiale pour définir la hauteur
    adjustTableHeight();

    // Événement de redimensionnement
    $(window).on('resize', function() {
      adjustTableHeight();
    });

    function adjustTableHeight() {
      const windowHeight = $(window).height();
      const tableTop = table.offset().top;
      const footerHeight = 50; // Hauteur estimée pour d'éventuels éléments en bas
      let availableHeight = windowHeight - tableTop - footerHeight;

      // Définir une hauteur minimale
      availableHeight = Math.max(availableHeight, 400);

      table.bootstrapTable('refreshOptions', {
        height: availableHeight
      });
    }
  });

  document.getElementById("result").innerHTML = "<?php echo "<h4>".$msg."</h4>" ; ?>";

</script>
