<?php
use JasonGrimes\Paginator;

require '../../../inc/confNetisse.php';
require '../vendor/autoload.php';

$helper = new Core\Helper();
$statistique = new Statistiques();
$agence = new Agences();

$codeAgence = isset($_GET['agency']) ? $_GET['agency'] : null;
$daterange = isset($_GET['daterange']) ? $_GET['daterange'] : date("d/m/Y") . " - " . date("d/m/Y"); //."07/05/2018 - 07/05/2018";

$total = $statistique->numRows($codeAgence, $daterange);
$statRdv = $statistique->statRdv($codeAgence, $daterange);

$totalItems = $total->num_row;
$show_all = isset($_GET['export']) ? true : false;
$itemsPerPage = 25;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;

$clients = $statistique->listByAgence($codeAgence, $daterange, $show_all, $currentPage, $itemsPerPage);
$listAgences = $agence->listAgences();

/* * * pagination ** */

$query_url = $helper->getFullUrl();
$url_parts = parse_url($query_url);

if (isset($url_parts['query'])) {
    parse_str($url_parts['query'], $params);
}

$params['page'] = '_num';
$url_parts['query'] = http_build_query($params);
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'] . '?' . $url_parts['query'];
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . '/statistiques/dcc/' . '?' . $url_parts['query'];
$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $url);
?>
<div id="tableContainer">
    <div style="width: 100%; height: 600px; overflow: scroll; max-height: none; min-height: 0px; max-width: none; min-width: 0px;">
        <table id="fixTable" cellpadding="0" cellspacing="0" border="0" class="responsive dynamicTable display table table-bordered" width="100%" id="visitDetails">
            <thead>
            <th><?php echo $helper->addSort(array('field' => 'civ', 'name' => 'Civilité')) ?> </a></th>
            <th><?php echo $helper->addSort(array('field' => 'nom', 'name' => 'Nom')) ?></th>
            <?php
            if ($GLOBALS["BO_CONF"]["CLIENT"]["prenom"]) {
                echo "<th>" . $helper->addSort(array('field' => 'prenom', 'name' => 'Prénom')) . "</th>";
            }
            ?>
            <th><?php echo $helper->addSort(array('field' => 'date_arrivee', 'name' => 'Date')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'jour', 'name' => 'Jour')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'heure_arrivee', 'name' => 'Arrivée')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'heure_priseencharge', 'name' => 'Prise en charge')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'heure_cloture', 'name' => 'Fin de prise en charge')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'duree_attente', 'name' => 'Temps d\'attente')) ?></th>
            <th><?php echo $helper->addSort(array('field' => 'duree_rdv', 'name' => 'Durée de l\'entretien')) ?></th>
            <th class="green"><?php echo $helper->addSort(array('field' => 'conseiller', 'name' => 'Conseiller')) ?></th>
            <th class="blue"><?php echo $helper->addSort(array('field' => 'rdv', 'name' => 'Avec RDV')) ?></th>
            <th class="red"><?php echo $helper->addSort(array('field' => 'rdv', 'name' => 'Sans RDV')) ?></th>
            <?php 
            if($GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"] && $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["CHOICES"]){
                foreach ($GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["CHOICES"] as $choice) {
                    if($choice == 'shipping'){
            ?>
            <th class="red"><?php echo $helper->addSort(array('field' => 'rdv', 'name' => 'Livraison')) ?></th>
            <?php
                    }
                }
            }
            ?>
            </thead>
            <tfoot class="top-table">
                <tr>
                    <td colspan="<?php echo $GLOBALS["BO_CONF"]["CLIENT"]["prenom"] ? 10 : 9; ?>" class="gray"></td>
                    <td class="with-border">Total</td>
                    <td class="with-border"><?php echo $statRdv->ardv ?></td>
                    <td class="with-border last"><?php echo $statRdv->srdv ?></td>
                </tr>
            </tfoot>
            <tbody>
                <?php
                foreach ($clients as $client):
                    ?>
                    <tr>
                        <td><?php echo $client->civ ?></td>
                        <td><?php echo $client->nom ?></td>
                        <?php
                        if ($GLOBALS["BO_CONF"]["CLIENT"]["prenom"]) {
                            echo "<td>" . $client->prenom . "</td>";
                        }
                        ?>
                        <td><?php echo date("d/m/Y", strtotime($client->date_arrivee)) ?></td>
                        <td><?php echo $jours[date("N", strtotime($client->date_arrivee))] ?></td>
                        <td><?php echo $client->heure_arrivee ? $client->heure_arrivee : '-' ?></td>
                        <td><?php echo $client->heure_priseencharge ? $client->heure_priseencharge : '-' ?></td>
                        <td><?php echo $client->heure_cloture ? $client->heure_cloture : '-' ?></td>
                        <td><?php echo $client->duree_attente ? $client->duree_attente : '-' ?></td>
                        <td><?php echo $client->duree_rdv ? $client->duree_rdv : '-' ?></td>
                        <td><?php echo $client->conseiller ?></td>

                        <?php
                        if($GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"] && $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["CHOICES"] && in_array('shipping', $GLOBALS["BO_CONF"]["AGENCE"]["SIEGE"]["CHOICES"])){
                            if($client->rdv == 1){
                                echo "<td>1</td><td>0</td><td>0</td>";
                            }elseif ($client->rdv == 0) {
                                echo "<td>0</td><td>1</td><td>0</td>";
                            }elseif ($client->rdv == 5) {
                                echo "<td>0</td><td>0</td><td>1</td>";
                            }

                        }else{
                           if ($client->rdv) {
                                echo "<td>1</td><td>0</td>";
                            } else {
                                echo "<td>0</td><td>1</td>";
                            } 
                        }
                        
                        ?>
                    </tr>

                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php
echo $paginator;
?>
</div>