<?php require_once(dirname(__DIR__, 1) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';

if (!isset($_SESSION['user_droits'])) {
    include('include/noaccess.php');
    echo '<br><a href="../index.php">' . ACCUEIL . '</a></p>';
    exit;
}

if ($_SESSION['user_droits'][1] !== "2") {
    echo '<p class="noaccess">'
        . NOMODULE .
        '<br><a href="./index.php">' . ACCUEIL . '</a></p>';

    exit;
}
?><style type="text/css">
	#med3 { background-color: #fff; }
	#med3 td { border: 3px #DEBB22 solid; }
	#med2 td { border: 0; }
</style><?php

$stmt = $pdo->prepare(
    "SELECT COUNT(seq_id)
     FROM {$prefix}_sequences
     WHERE seq_actif = 1"
);

$stmt->execute();

$nb_seq = (int)$stmt->fetchColumn();

$stmt = $pdo->prepare(
    "SELECT COUNT(seq_id)
     FROM {$prefix}_seqdocs"
);

$stmt->execute();

$nb_docs = (int)$stmt->fetchColumn();

if ($nb_seq === 0) {
    echo '<h1>' . EDITC_DOC1 . '</h1>
    <h2>' . EDITC_DOC2 . '
    <a href="edit.php?page=creation">' . SEQCREA . '</a>.
    </h2>';

    exit;
}

if ($nb_docs === 0) {
    echo '<h1>' . EDITC_DOC1 . '</h1>
    <h2>' . EDITC_DOC2 . '
    <a href="edit.php?page=consult">' . EDITNAV1 . '</a>.
    </h2>';

    exit;
}

$glob_taille = 0;

$stmt = $pdo->prepare(
    "SELECT docurl, docname
     FROM {$prefix}_seqdocs
     ORDER BY docname ASC"
);

$stmt->execute();

$docus = $stmt->fetchAll();

foreach ($docus as $docu) {
    $docurl = basename($docu['docurl']);
    $filename = 'seqfichiers/' . $prefix . '/' . $docurl;
    if (is_file($filename)) {
        $taille = number_format(filesize($filename) / 1048, 2);
        $glob_taille += (float)$taille;
    }
}

echo '<h1>'
    . EDITC_DOC1
    . ' (' . $nb_docs . ' : '
    . $glob_taille . ' kB)</h1>';

include('db/proc/freq_stats_nivcolors.php');

for ($i = 0; $i < count($tabDebniveau); $i++) {

    $stmt = $pdo->prepare(
        "SELECT
            seq.seq_id,
            seq.seq_nom,
            seq.niveau,
            seq.date_crea,
            seq.date_maj,
            seq.seq_complete,
            seq.seq_actif,
            seq.progression

         FROM {$prefix}_sequences seq

         INNER JOIN {$prefix}_seqdocs sd
            ON sd.seq_id = seq.seq_id

         WHERE seq.niveau = :niveau
         AND seq.seq_actif = 1"
    );

    $stmt->execute([
        ':niveau' => $tabDebniveau[$i]
    ]);

    $seqs = $stmt->fetchAll();

    $nb_seqs = count($seqs);

    $docs = 1;

    if ($nb_seqs > 0) {

        echo '<h2>' . htmlspecialchars($tabNiveau[$i]) . '</h2>';

        $stmt = $pdo->prepare(
            "SELECT
                seq.seq_id,
                seq.seq_nom,
                seq.niveau,
                seq.date_crea,
                seq.date_maj,
                seq.seq_complete,
                seq.seq_actif,
                seq.progression

             FROM {$prefix}_sequences seq

             INNER JOIN {$prefix}_seqdocs sd
                ON sd.seq_id = seq.seq_id

             WHERE seq.niveau = :niveau
             AND seq.seq_actif = 1

             ORDER BY progression, seq_nom"
        );

        $stmt->execute([
            ':niveau' => $tabDebniveau[$i]
        ]);

        $seqs = $stmt->fetchAll();

        include('./db/proc/edit_consult_listage.php');
    }
}

?>

<script>
window.addEventListener('mousedown', function(e) {

    var el = e.target;

    if (
        el.tagName.toLowerCase() === 'option' &&
        el.parentNode.hasAttribute('multiple')
    ) {

        e.preventDefault();

        if (el.hasAttribute('selected')) {
            el.removeAttribute('selected');
        } else {
            el.setAttribute('selected', '');
        }

        var select = el.parentNode.cloneNode(true);

        el.parentNode.parentNode.replaceChild(
            select,
            el.parentNode
        );
    }
});
</script>

<?php

echo ZIPDL;

if (!extension_loaded('zip')) {

    echo ZIPNOEXT2;

} else {

    $stmt = $pdo->prepare(
        "SELECT docurl, docname
         FROM {$prefix}_seqdocs
         ORDER BY docname ASC"
    );

    $stmt->execute();

    $docus = $stmt->fetchAll();

    echo '<form method="POST" action="" name="zip">';

    echo '<select
            name="files[]"
            class="inptxt"
            id="savedocs"
            multiple
            size="' . (count($docus) + 1) . '">';

    foreach ($docus as $docu) {

        $docurl = basename($docu['docurl']);

        $filename = 'seqfichiers/' . $prefix . '/' . $docurl;

        if (is_file($filename)) {

            $taille = number_format(filesize($filename) / 1048, 2);

            echo '<option
                    value="' . htmlspecialchars($docurl, ENT_QUOTES, 'UTF-8') . '"
                    title="' . htmlspecialchars($docu['docname'], ENT_QUOTES, 'UTF-8') . '"
                    selected>';

            echo !empty($docu['docname'])
                ? htmlspecialchars($docu['docname'], ENT_QUOTES, 'UTF-8')
                : htmlspecialchars($docurl, ENT_QUOTES, 'UTF-8');

            echo ' - ' . $taille . ' kB';

            echo '</option>';
        }
    }

    echo '</select><br>';

    echo '<input
            type="submit"
            value="' . DOWNLOAD . '"
            name="zipval"
            class="bouton boutseq">';

    echo '</form>';
}