<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

$path = __DIR__ . '/planilhas';

$objetos = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

$arquivos = [];
foreach ($objetos as $i => $obj) {
    if ($obj->isFile()) {
        $arquivos[] = $obj->getFileName();
    }
}

//asort($arquivos);
$collator = new Collator('pt_BR');
$collator->sort($arquivos);
//var_dump($arquivos);
?>
<!doctype html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Planilhas Portal Infohidro</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap.min.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/dataTables.bootstrap.min.js"></script>
    <script>
        $(function () {
            $('#tbl_mun').DataTable({
                language: {
                    search: "Pesquisar",
                    lengthMenu: "Exibir _MENU_ registros por página",
                    zeroRecords: "Nenhum registro encontrado",
                    info: "Exibindo página _PAGE_ de _PAGES_",
                    infoEmpty: "Nenhum registro disponível",
                    infoFiltered: "(filtrado(s) de um total de _MAX_ registros)",
                    paginate: {
                        first: "Início",
                        last: "Fim",
                        next: "Próxima",
                        previous: "Anterior"
                    }
                }
            });
        });
    </script>
</head>
<body>
<div class="container">
    <br>
    <table id="tbl_mun" class="table table-striped table-bordered table-hover">
        <thead>
        <tr>
            <th>Município</th>
            <th>Planilha</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($arquivos as $arquivo) : ?>
            <tr>
                <td><?= explode('-', $arquivo)[0] ?></td>
                <td><a href="planilhas/<?= $arquivo ?>"><i class="glyphicon glyphicon-download-alt"></i> Download</a></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
</body>
</html>
