<!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</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>
   <!-- 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%;">
        <!-- Page Title -->
        <h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">TICKETS GLPI <small><small> Infra_Système_DK</small></small></span></h1>
        
        <!-- Main content -->
        <?php 
          DB_GLPI();
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini 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"); $r = mysqli_fetch_array($sql); $Nouveau = $r['total'];$d1=$r['mini'];$f1=$r['maxi'];
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = 2"); $r = mysqli_fetch_array($sql); $EnCours = $r['total'];$d2=$r['mini'];$f2=$r['maxi'];
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = 3"); $r = mysqli_fetch_array($sql); $Planifie = $r['total'];$d3=$r['mini'];$f3=$r['maxi'];
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = 4"); $r = mysqli_fetch_array($sql); $EnAttente = $r['total'];$d4=$r['mini'];$f4=$r['maxi'];
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = 5"); $r = mysqli_fetch_array($sql); $Resolu = $r['total'];$d5=$r['mini'];$f5=$r['maxi'];
            $sql = $conn->query("SELECT count(*) as total, max(date_mod) as maxi, min(date_mod) as mini FROM glpi_tickets where id in (select tickets_id from glpi_groups_tickets where groups_id = 13) and status = 6"); $r = mysqli_fetch_array($sql); $Clos = $r['total'];$d6=$r['mini'];$f6=$r['maxi'];
            $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'];
            $ec0 = (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 = 2 and DATEDIFF(now(),date_creation) <= 7")))['total'];
            $ec7 = (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 = 2 and DATEDIFF(now(),date_creation) <= 30 and DATEDIFF(now(),date_creation) > 7")))['total'];
            $ec30 = (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 = 2 and DATEDIFF(now(),date_creation) > 30")))['total'];
            $p0 = (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 = 3 and DATEDIFF(now(),date_creation) <= 7")))['total'];
            $p7 = (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 = 3 and DATEDIFF(now(),date_creation) <= 30 and DATEDIFF(now(),date_creation) > 7")))['total'];
            $p30 = (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 = 3 and DATEDIFF(now(),date_creation) > 30")))['total'];
            $ea0 = (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 = 4 and DATEDIFF(now(),date_creation) <= 7")))['total'];
            $ea7 = (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 = 4 and DATEDIFF(now(),date_creation) <= 30 and DATEDIFF(now(),date_creation) > 7")))['total'];
            $ea30 = (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 = 4 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 in (2,3,4)
                                  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 in(2,3,4)");
            $r = mysqli_fetch_array($sql); $ec = $r['total'];                                  
        ?>
        <span id="dataC0" class="invisible"><?php echo "$n0,$ec0,$p0,$ea0" ?></span>
        <span id="dataC7" class="invisible"><?php echo "$n7,$ec7,$p7,$ea7" ?></span>        
        <span id="dataC30" class="invisible"><?php echo "$n30,$ec30,$p30,$ea30" ?></span> 
        <span id="repName" class="invisible"><?php echo "$repName" ?></span> 
        <span id="repTot" class="invisible"><?php echo "$repTot" ?></span> 
        <div class="row flex-nowrap text-center">
          <div class="col"> <!-- CARTE Nouveaux -->
            <div class="card border-danger mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-person-x-fill text-danger"></i> Non Attribué</h5>
              </div>
              <a href="\tickets\list.php?state=1" class="text-decoration-none" target="_blank">
                <div class="card-body">
                  <h1><span class="badge bg-danger"><?php echo $Nouveau; ?></span></h1>
                  <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d1)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f1)) ?></h6>
                </div>
              </a>
            </div>  
          </div>  

          <div class="col"> <!-- CARTE En Cours -->
            <div class="card border-info mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-caret-right text-info"></i> En Cours</h5>
              </div>
              <a href="\tickets\list.php?state=2" class="text-decoration-none" target="_blank">
                <div class="card-body">
                  <h1><span class="badge bg-info"><?php echo $EnCours; ?></span></h1>
                  <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d2)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f2)) ?></h6>
                </div>
              </a>
            </div>  
          </div>  

          <div class="col"> <!-- CARTE Planifié -->
            <div class="card border-primary mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-calendar-check text-primary"></i> Planifié</h5>
              </div>
              <a href="\tickets\list.php?state=3" class="text-decoration-none" target="_blank">
                <div class="card-body">
                  <h1><span class="badge bg-primary"><?php echo $Planifie; ?></span></h1>
                  <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d3)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f3)) ?></h6>
                </div>
              </a>
            </div>  
          </div>  

          <div class="col"> <!-- CARTE En Attente -->
            <div class="card border-warning mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-hourglass-split text-warning"></i> En Attente</h5>
              </div>
              <a href="\tickets\list.php?state=4" class="text-decoration-none" target="_blank">
                <div class="card-body">
                  <h1><span class="badge bg-warning"><?php echo $EnAttente; ?></span></h1>
                  <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d4)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f4)) ?></h6>
                </div>
              </a>
            </div>  
          </div>  

          <div class="col"> <!-- CARTE Résolu -->
            <div class="card border-success mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-check-circle text-success"></i> Résolu</h5>
              </div>
              <a href="\tickets\list.php?state=5" class="text-decoration-none" target="_blank">
                <div class="card-body">
                  <h1><span class="badge bg-success"><?php echo $Resolu; ?></span></h1>
                  <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d5)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f5)) ?></h6>
                </div>
              </a>
            </div>  
          </div>  

          <div class="col"> <!-- CARTE Clos -->
            <div class="card border-secondary mb-3">
              <div class="card-header">
                <h5><i class="fs-4 bi-check-all text-secondary"></i> Clos</h5>
              </div>
              <div class="card-body">
                <h1><span class="badge bg-secondary"><?php echo $Clos; ?></span></h1>
                <h6 class="text-decoration-none text-black-50"><?php echo date("d/m/y", strtotime($d6)) ?> <i class="bi-arrow-right-short "></i> <?php echo date("d/m/y", strtotime($f6)) ?></h6>
              </div>
            </div>  
          </div>  
        </div>


        <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" style="height: 300px">
                <canvas id="C0" ></canvas>
              </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" style="height: 300px">
                <canvas id="C7"></canvas>
              </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" style="height: 300px">
                <canvas id="C30"></canvas>
              </div>
            </div>
          </div>
        </div>
        <br>
        <h2><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Repartition acteurs <small><small> (en cours + planifié + en attente)</small></small></span></h2>
        <div class="row flex-nowrap text-center">
          <?php
            $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>

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

<script>
Chart.defaults.global.legend.display = false;
var lab = ['Non Attribué','En Cours','Planifié','En Attente'];
var cols = ['rgba(220, 53, 69, 0.3)','rgba(13, 202, 240, 0.3)','rgba(13, 110, 253, 0.3)','rgba(255, 193, 7, 0.3)']
var bcols = ['rgba(220, 53, 69)','rgba(13, 202, 240)','rgba(13, 110, 253)','rgba(255, 193, 7)']

var dataC0 = document.getElementById("dataC0").innerHTML.split(",");
new Chart("C0", {
  type: 'bar',
  label: '',
  data: {
   labels: lab,
   datasets: [{
      backgroundColor: cols,
      borderColor: bcols,
      borderWidth: 1,
      data: dataC0
   }]
  },
  options: {  
    responsive: true,
    maintainAspectRatio: false
  }
});

var dataC7 = document.getElementById("dataC7").innerHTML.split(",");
new Chart("C7", {
  type: 'bar',
  label: '',
  data: {
   labels: lab,
   datasets: [{
      backgroundColor: cols,
      borderColor: bcols,
      borderWidth: 1,
      data: dataC7
   }]
  },
  options: {  
    responsive: true,
    maintainAspectRatio: false
  }
});

var dataC30 = document.getElementById("dataC30").innerHTML.split(",");
new Chart("C30", {
  type: 'bar',
  label: '',
  data: {
   labels: lab,
   datasets: [{
      backgroundColor: cols,
      borderColor: bcols,
      borderWidth: 1,
      data: dataC30
   }]
  },
  options: {  
    responsive: true,
    maintainAspectRatio: false
  }
});

</script>

