<?php include('meta.php') ?>
<?php $bg_header = "bg_hc"; include('header.php') ?>
<div id="box_body"><!-- body center -->

     <br /><span class="big_title_h2">Informes</span> <a href="home_informes.php"><span class='glyphicon glyphicon-chevron-left'></span>Regresar</a><br />

    <?php 
    $date_start = $_GET['date_start'];
    $date_end = $_GET['date_end'];

    $sql = 'SELECT id FROM hc_control1 WHERE date_control BETWEEN "'.$date_start.' 00:00:00" AND "'.$date_end.' 23:59:59" GROUP BY id';

    // echo $sql.'<br />';
    $result = mysqli_query($sql);
    //$rows_user = mysqli_fetch_assoc($result);

    $cumplido = 0;
    $total = 0;
    $incumplidos = array();

    while ($row_user = mysqli_fetch_assoc($result)) {
      $c = getUser($row_user['id']);
      $total++;
      $cumplido += $c;
    /*  echo '<br />';
      echo '<br />'; */
      if ($c === 0) {
        $incumplidos[] = $row_user['id'];
      }
    }

    $p_cumplimiento = ($cumplido / $total) * 100;
    $p_incumplimiento = 100 - $p_cumplimiento;

    $p_cumplimiento = round($p_cumplimiento, 1);
    $p_incumplimiento = round($p_incumplimiento, 1);
    $num_con = 0;
    echo "<br /><span class='title'>Informe del porcentaje de adherencia para el <strong>Periodo:</strong><span style='color:black'> ".$date_start." al ".$date_end."</span></span><br /><br />"; 
    echo "Informe basado en controles<br />";

    echo "<span class='title2'>Total de pacientes: $total<br />";
    echo "Cumplieron: $cumplido<br />";
    /* echo 'Porcentaje de cumplimiento: '.$p_cumplimiento.'%<br />'; */
    echo "Porcentaje de incumplimiento: $p_incumplimiento % </span>";
    echo "<br /><br /><span class='title'>Paciente que no cumplieron:</span><br />";
    echo "<table  class='display table table-hover' style='width:80%'> \n"; 
    echo " <thead>
                <tr>
                <th>N°</th>
                <th>Nombre</th>
                <th>Cedula</th>
                <th>Teléfono</th>  
                <th></th>
                </tr>
              </thead>
              <tbody> \n"; 
    foreach($incumplidos as $id_incumplido) {
      //consultar el nombre del usuario
      $cons = mysqli_query($con,"SELECT * FROM hc_paciente WHERE id = '$id_incumplido'");
      $result_cons = mysqli_fetch_assoc($cons);
      $name = $result_cons['name'];  
      $cell = $result_cons['cell']; 
      $cc = $result_cons['cc']; 
      $num_con = $num_con+1;
      echo "<tr> 
                <td>$num_con</td>
                <td><strong>$name</strong></td>
                <td>$cc</td>
                <td>$cell</td>
                <td><a target='_blank' href='../result.php?id=$id_incumplido'>Ver mas info</a></td>";
      }
      echo " \n"; 
      echo " </tbody></table> \n";     


    function getUser($id_user) {
      $sql_date = mysqli_query($con,'SELECT date_control FROM hc_control1 WHERE id = '.$id_user);
      $date = array();
      $weeks = array();
      while ($row_date = mysqli_fetch_assoc($sql_date)) {
        $date[] = $row_date['date_control'];
        $weeks[] = date('W', strtotime($row_date['date_control']));
      }

      $consecutive = true;
      $nc = 1;

      if (count($weeks) >= 5) {
        foreach ($weeks as $i => $week) {
          $ni = $i+1;
          $nw = (int)$week+1;
          if ($nc === 5) {
            $consecutive = true;
            break;
          }
          if (!isset($weeks[$ni])) {
            break;
          }

          if ((int)$weeks[$ni] !== $nw) {
            $consecutive = false;
            break;
          } else {
            $nc++;
          }
        }
      } else {
        $consecutive = false;
      }
      /*
      echo '<pre>';
      echo 'Consecutive:'.($consecutive ? 'Si' : 'No').'::'.$nc;
      echo '<br />';
      print_r($date);
      print_r($weeks);
      echo '</pre>';
      */
      return (int)$consecutive;
    }


    ?>

  </div><!-- END body center -->
<?php include('../footer.php') ?>
