<?php
session_start();
$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $pageSequence;
$tblName = "tblReport" . $pageSequence;
$btAdd = "btnAdd" . $pageSequence;
$btDel = "btnDel" . $pageSequence;
$btDelObj = "btnDelObj" . $pageSequence;
$dataSection = "dataSection" . $pageSequence;
$frmName = "form" . $pageSequence;
$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir'] . "core.php");

$svaTool = new svaTool();


$errorCode = 0;

$password = NULL;
$confPasswd = NULL;

$startDateTime = addslashes($_REQUEST['startDateTime']);
$endDateTime = addslashes($_REQUEST['endDateTime']);
$userId = addslashes($_REQUEST['userId']);
$status = addslashes($_REQUEST['statut']);
$granularity = $_REQUEST['granularity'];
$teamId = addslashes($_REQUEST['teamId']);

//print_r($_REQUEST);
/*
  $startDateTime = "2019-04-01 00:00:00";
  $endDateTime =  "2019-05-01 00:00:00";
  $userId = 66;
  $status =-1;
 */

$res = $dba->getReclamationTime($startDateTime, $endDateTime,$status, $granularity, $_SESSION['cfg']['max.sql.results.elements']);
$data = "";

$cmpt = 1;
$total = count($res);

//CONVERTIR S EN MOIS JOUR HEUR MIN S
function convertSecondsToMJDHMS($value) {
    $months = floor($value / (30 * 24 * 3600)); 
    $restantAPresMonths = $value % (30 * 24 * 3600);
    
    $days = floor($restantAPresMonths / (24 * 3600));
    $RestantApresDays = $restantAPresMonths % (24 * 3600);
    
    $hours = floor($RestantApresDays / 3600);
    $RestantApresHours = $RestantApresDays % 3600;
    
    $minutes = floor($RestantApresHours / 60);
    $seconds = $RestantApresHours % 60;
    
    $result = '';
    if ($months > 0) {
        $result .= $months . 'Mois ';
    }
    if ($days > 0) {
        $result .= $days . 'J ';
    }
    if ($hours > 0) {
        $result .= $hours . 'H ';
    }
    if ($minutes > 0) {
        $result .= $minutes . 'Min ';
    }
    if ($seconds > 0 || ($months == 0 && $days == 0 && $hours == 0 && $minutes == 0)) {
        $result .= $seconds . 'S';
    }
    
    return trim($result);
}


?>

<div class="btn-group">

    <button class="btn  btn-sm dropdown-toggle" data-toggle="dropdown"><i class="fa fa-bars"></i> Exporter</button>
    <ul class="dropdown-menu exp-drop" role="menu">
        <li>
            <a href="#" onclick="$('#<?php echo $tblName; ?>').tableExport({type: 'excel', escape: 'false'});" > ,
                <img src="assets/dist/img/xls.png" width="24" alt="logo"> XLS</a>
        </li>
        <li>
            <a href="#" onclick="$('#<?php echo $tblName; ?>').tableExport({type: 'csv', escape: 'false'});"> 
                <img src="assets/dist/img/csv.png" width="24" alt="logo"> CSV</a>
        </li>
        <li>
            <a href="#" onclick="$('#<?php echo $tblName; ?>').tableExport({type: 'txt', escape: 'false'});"> 
                <img src="assets/dist/img/txt.png" width="24" alt="logo"> TXT</a>
        </li>

        <li class="divider"></li> 
    </ul>

</div>

<!-- ./Plugin content:powerpoint,txt,pdf,png,word,xl -->
<div class="table-responsive">

    <table id="<?php echo $tblName; ?>"  width="100%" height="100%" class="table table-striped table-bordered" >
        <thead>
            <tr>
                <th>Date création</th>
                <th>Date clôture</th>
                <th>Numero ticket</th>
                <th>Nom</th>
                <th>Durée de traitement</th>
                <th>Status</th>
            </tr>
        </thead>
        
        
         <tbody>
            <?php for ($i = 0; $i < $total; $i++) { ?>
              
                
                <tr>
                    <td><?php echo $res[$i]['eventDate']; ?></td>
                    <td><?php echo $res[$i]['closure_date_d']; ?></td>
                    <td><?php echo $res[$i]['ticket_number_v']; ?></td>
                    <td><?php echo $res[$i]['last_name_v']; ?></td>
                    <td>
                        <?php 
                            echo ($res[$i]['traitement_duration'] == 0) 
                                ? "Pas encore déterminé" 
                                : convertSecondsToMJDHMS($res[$i]['traitement_duration']); 
                        ?>
                    </td>
                    <td><?php echo $res[$i]['status']; ?></td>
                </tr>

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



</div>

<script type="text/javascript" >
    $(document).ready(function () {





        var dataUrl = "<?php echo $currentBaseDir; ?>/return_json_default.php?json=[]";
        table = $('#<?php echo $tblName; ?>').DataTable();



    });
</script>