<!DOCTYPE html>
<html lang="fr">
<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</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">
   <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>
   <!-- Chart -->
   <script src="/js/chart.min.js"></script>
   
</head>

<body>
  <?php include $_SERVER['DOCUMENT_ROOT']."/include/db_connect.php" ; ?>  <!-- Database connexion -->
  <?php //include $_SERVER['DOCUMENT_ROOT']."/include/functions.inc.php" ; ?>  <!-- Functions -->
  <div class="container-fluid">
    <div class="row flex-nowrap">
      <!-- Left NAVBAR -->
      <div class="col-auto col-md-2 col-xl-2 px-sm-2 px-0 bg-dark" style="-ms-flex: 0 0 230px;flex: 0 0 230px;">
        <?php include $_SERVER['DOCUMENT_ROOT']."/navbar.html" ; ?>  <!-- Left Navbar -->
      </div>
      <!-- Display -->
      <div class="col py-3" style="zoom: 80%;">
      <?php 
         $state = $_GET['state'];
         switch ($state) {
            case 1: $title = "Tickets non attribués"; break;
            case 2: $title = "Tickets en cours"; break;
            case 3: $title = "Tickets planifies"; break;
            case 4: $title = "Tickets en attente"; break;
            case 5: $title = "Tickets resolus"; break;
            default:
          }
      ?>
        <!-- Page Title -->
        <h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;"><?php echo strtoupper($title); ?></span></h1>
        
        <!-- Main content -->
        <?php 
          DB_GLPI();
            $conn->query("SET NAMES 'utf8'");
            $sql = $conn->query("SELECT * FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = $state"); $r = mysqli_fetch_array($sql); 
            if($state > 1){
              $n0 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = $state and DATEDIFF(now(),date_creation) <= 7")))['total'];
              $n7 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = $state and DATEDIFF(now(),date_creation) <= 30 and DATEDIFF(now(),date_creation) > 7")))['total'];
              $n30 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = $state and DATEDIFF(now(),date_creation) > 30")))['total'];
            }else{
              $n0 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and id not in (select tickets_id from glpi_tickets_users where type = 2) and status < 6 and DATEDIFF(now(),date_creation) <= 7")))['total'];
              $n7 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and id not in (select tickets_id from glpi_tickets_users where type = 2) and status < 6 and DATEDIFF(now(),date_creation) <= 30 and DATEDIFF(now(),date_creation) > 7")))['total'];
              $n30 = (mysqli_fetch_array($conn->query("SELECT count(*) as total FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and id not in (select tickets_id from glpi_tickets_users where type = 2) and status < 6 and DATEDIFF(now(),date_creation) > 30")))['total'];
            }
            $sql = $conn->query(" SELECT count(*) as total, realname, firstname FROM glpi_tickets
                                  left join glpi_tickets_users on glpi_tickets.id = glpi_tickets_users.tickets_id and glpi_tickets_users.type = 2
                                  left join glpi_users on glpi_tickets_users.users_id = glpi_users.id                                  
                                  where glpi_tickets.id in (select tickets_id from glpi_groups_tickets where groups_id = 13)
                                  and status = $state
                                  and glpi_tickets_users.users_id in (SELECT glpi_users.id FROM glpi_groups_users left join glpi_users on users_id = glpi_users.id where glpi_groups_users.groups_id = 13)
                                  group by glpi_tickets_users.users_id, realname
                                  order by total desc");
            $repName='';$repTot='';
            while($row = mysqli_fetch_array($sql)){
              $repName .= $row['firstname'].' '.$row['realname'].',';
              $repTot .= $row['total'].',';
            }
            $repName = substr($repName, 0, -1);
            $repTot = substr($repTot, 0, -1);
            $sql = $conn->query(" SELECT count(*) as total FROM glpi_tickets
                                  where glpi_tickets.id in (select tickets_id from glpi_groups_tickets where groups_id = 13)
                                  and status = $state");
            $r = mysqli_fetch_array($sql); $ec = $r['total'];                                  
        ?>
        <div class="row flex-nowrap text-center">
          <div class="col">
            <div class="card border-success mb-3">
              <div class="card-header"><h3>Age : 0-7 Jours</h3></div>
              <div class="card-body">
               <h1><span class="badge bg-success"><?php echo $n0; ?></span></h1>
              </div>
            </div>
          </div>
          <div class="col">
            <div class="card border-warning mb-3">
              <div class="card-header"><h3>Age 7-30 Jours</h3></div>
              <div class="card-body">
              <h1><span class="badge bg-warning"><?php echo $n7; ?></span></h1>
              </div>
            </div>
          </div>
          <div class="col">
            <div class="card border-danger mb-3">
              <div class="card-header"><h3>Age > 30 Jours</h3></div>
              <div class="card-body">
               <h1><span class="badge bg-danger"><?php echo $n30; ?></span></h1>
              </div>
            </div>
          </div>
        </div>
        <br>
        <h2><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Répartition acteurs <small><small> (en cours + planifié + en attente)</small></small></span></h2>
        <div class="row flex-nowrap text-center">
          <?php
            if($repName <> ''){
               $nb = explode(',',$repTot);$i=0;
               foreach (explode(',',$repName)  as $name) {
               echo '<div class="col">';
                  echo '<div class="card mb-3">';
                     echo '<div class="card-header"><h6>'.(explode(' ',$name))[0].'</h6></div>';
                     echo '<div class="card-body">';
                     echo '<h3><span class="badge bg-primary">'.$nb[$i].'</span></h3>';
                     echo '</div>';
                  echo '</div>';
               echo '</div>';
               $ec -= $nb[$i];
               $i++;
               }
               echo '<div class="col">';
               echo '<div class="card mb-3">';
                  echo '<div class="card-header"><h6>Autre</h6></div>';
                  echo '<div class="card-body">';
                     echo '<h3><span class="badge bg-secondary">'.$ec.'</span></h3>';
                  echo '</div>';
               echo '</div>';
               echo '</div>';
            }
          ?>
        </div>
         <hr>

        <!-- Liste des tickets --> 
        
        <?php
          if($state > 1){ 
            $sql = $conn->query(" SELECT glpi_tickets.date_creation, glpi_tickets.id, glpi_tickets.name, glpi_tickets.date_mod, users_id_lastupdater, realname, firstname FROM glpi_tickets
                                  left join glpi_users on glpi_users.id = glpi_tickets.users_id_lastupdater
                                  where glpi_tickets.id in (select tickets_id from glpi_groups_tickets where groups_id = 13)
                                  and glpi_tickets.status = $state
                                  order by date_creation desc");
          }else{
            $sql = $conn->query(" SELECT glpi_tickets.date_creation, glpi_tickets.id, glpi_tickets.name, glpi_tickets.date_mod, users_id_lastupdater, realname, firstname FROM glpi_tickets
                                  left join glpi_users on glpi_users.id = glpi_tickets.users_id_lastupdater
                                  where glpi_tickets.id in (select tickets_id from glpi_groups_tickets where groups_id = 13)
                                  and glpi_tickets.id not in (select tickets_id from glpi_tickets_users where type = 2)
                                  and glpi_tickets.status < 6
                                  order by date_creation desc");

          }
         ?>
         <h2><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Liste des tickets</span></h2>

            <table  class='table table-bordered table-hover table-sm' id='t1'
               data-toggle="table" data-search="true" data-show-columns="true" data-pagination="true"  data-export-types="['xlsx','csv','json']" data-loading-template="loadingTemplate"
               data-page-list="[20, 50, 100, All]"  data-page-size="Tout" data-height="620" data-show-export="true" data-sortable="true"  style="width:99%;"
            > 
               <thead>
                  <th data-field="Ticket" data-sortable="true">Ticket</th>
                  <th data-field="Date" data-sortable="true">Date Création</th>
                  <th data-field="Resume" data-sortable="true">Résumé</th>
                  <th data-field="Last" data-sortable="true">Dernière modification</th>
               </thead>

               <tbody>
                  <?php
                     while($row = mysqli_fetch_array($sql)){
                        echo "<tr>";
                           echo "<td><b><a href='https://web-glpi-aim.process.dkm/glpi/front/ticket.form.php?id=".$row['id']."' target='_blank'>".$row['id']."</a></b></td>";
                           echo "<td>".explode(" ",$row['date_creation'])[0]."</td>";
                           echo "<td>".$row['name']."</td>";
                           echo "<td>".explode(" ",$row['date_mod'])[0]." par ".$row['firstname']." ".$row['realname']."</td>";
                        echo "</tr>";
                     }
                  ?>
               </tbody>
            </table>

      </div>  
        <!-- End of main content -->
      </div>
    </div>
  </div>
</body>

