<?php $page = preg_split('/[\/]/', $_SERVER['REQUEST_URI']);
if ($page[count($page)-2] != 'view'): ?>
<h5>Filter Chansons</h5>
<button type="button" onclick="toggleFilterOptions()">Filter</button>

<div id="filter-options" class="filter-options" style="display:none;">
    <form method="get">
        <div id="filter-buttons" class="filter-buttons">
            <button type="button" onclick="toggleCheckboxes('genre')">Genres</button>
            <button type="button" onclick="toggleCheckboxes('artist')">Artists</button>
            <button type="button" onclick="toggleCheckboxes('year')">Years</button>
            <button type="button" onclick="toggleCheckboxes('album')">Albums</button>
        </div>

        <div id="genre-checkboxes" style="display:none;">
            <?php foreach ($genres as $genre){ ?>
                <label>
                    <input type="checkbox" name="genre[]" value="<?= $genre->genreName; ?>">
                    <?= $genre->genreName; ?>
                </label><br>
            <?php } ?>
        </div>

        <div id="artist-checkboxes" style="display:none;">
            <?php foreach ($artists as $artist){ ?>
                <label>
                    <input type="checkbox" name="artist[]" value="<?= $artist->artistName; ?>">
                    <?= $artist->artistName; ?>
                </label><br>
            <?php } ?>
        </div>

        <div id="year-checkboxes" style="display:none;">
            <?php foreach ($years as $year){ ?>
                <label>
                    <input type="checkbox" name="year[]" value="<?= $year->year; ?>">
                    <?= $year->year; ?>
                </label><br>
            <?php } ?>
        </div>

        <div id="album-checkboxes" style="display:none;">
            <?php foreach ($albums as $album){ ?>
                <label>
                    <input type="checkbox" name="albums[]" value="<?= $album->albumName; ?>">
                    <?= $album->albumName; ?>
                </label><br>
            <?php } ?>
        </div>

        <button type="submit">Apply Filters</button>
    </form>
</div>

<h5>Sort Chansons</h5>
<button type="button" onclick="toggleSortButtons()">Sort</button>
<div id="sort-buttons" class="sort-buttons" style="display:none;">
    <button type="button" onclick="sortAlbums('year', 'asc')">Sort by Year Asc</button>
    <button type="button" onclick="sortAlbums('year', 'desc')">Sort by Year Desc</button>
    <button type="button" onclick="sortAlbums('artistName', 'asc')">Sort by Artist Asc</button>
    <button type="button" onclick="sortAlbums('artistName', 'desc')">Sort by Artist Desc</button>
    <button type="button" onclick="sortAlbums('name', 'asc')">Sort by Chanson Name Asc</button>
    <button type="button" onclick="sortAlbums('name', 'desc')">Sort by Chanson Name Desc</button>
    <button type="button" onclick="sortAlbums('albumName', 'asc')">Sort by Album Name Asc</button>
    <button type="button" onclick="sortAlbums('albumName', 'desc')">Sort by Album Name Desc</button>
    <button type="button" onclick="sortAlbums('genreName', 'asc')">Sort by Genre Asc</button>
    <button type="button" onclick="sortAlbums('genreName', 'desc')">Sort by Genre Desc</button>
</div>
<?php endif; ?>

<h5>Chansons list</h5>
<section class="list">

<?php
// Pagination setup
$items_per_page = 51; // Number of items to display per page
$total_chansons = count($chansons); // Total number of chansons
$total_pages = ceil($total_chansons / $items_per_page); // Total number of pages

// Get current page from URL, default is page 1
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$current_page = max(1, min($total_pages, $current_page)); // Ensure the current page is within valid range

// Calculate the offset for the SQL query
$offset = ($current_page - 1) * $items_per_page;

// Fetch chansons for the current page
$chansons_for_current_page = array_slice($chansons, $offset, $items_per_page);

$page = preg_split('/[\/]/', $_SERVER['REQUEST_URI']);
$long_page = count($page) - 1;
$valid_segments = ['albums', 'chansons'];
$url = '';

for ($i = $long_page; $i >= 0; $i--) {
    if (in_array($page[$i], $valid_segments)) {
        $url = implode('/', array_slice($page, $i));
        break;
    }
}

foreach($chansons_for_current_page as $chanson): ?>
    <div>
        <article>
            <header class='short-text'>
                <h6> <?php echo $chanson->name; ?> </h6>
                <?php if($this->session->userdata('logged_in')): ?>
                    <?php if($this->model_music->SongInPlaylist($chanson->trackId)): ?>
                        <?= anchor("chansons/deleteSongtoPlaylist/{$chanson->trackId}?page=$url", "<i class='fa fa-trash'></i>"); ?>
                    <?php endif; ?>
                    <?= anchor("chansons/addSongtoPlaylist/{$chanson->trackId}?page=$url", "<i class='fa fa-plus'></i>"); ?>
                <?php endif; ?>
            </header>
            <nav class='short-text'>Nom album: <?= $chanson->albumName; ?></nav>
            <nav class='short-text'>Genre: <?= $chanson->genreName; ?></nav>
            <footer class='short-text'><?= $chanson->year; ?> - <?= $chanson->artistName; ?></footer>
        </article>
    </div>
<?php endforeach; ?>
</section>


<!-- Pagination Controls -->
<div class="pagination">
<?php if ($current_page > 1): ?>
        <a href="?page=<?= $current_page - 1 ?>">&laquo; Previous</a>
    <?php endif; ?>

    <?php
    // Determine the start and end page numbers
    $start_page = max(1, $current_page - 2);
    $end_page = min($total_pages, $current_page + 2);

    // Adjust the start and end page if at the boundaries
    if ($start_page === 1) {
        $end_page = min(5, $total_pages);
    }
    if ($end_page === $total_pages) {
        $start_page = max(1, $total_pages - 4);
    }

    if ( $total_pages > 1){
        for ($i = $start_page; $i <= $end_page; $i++): ?>
            <?php if ($i == $current_page): ?>
                <span><?= $i ?></span>
            <?php else: ?>
                <a href="?page=<?= $i ?>"><?= $i ?></a>
            <?php endif; ?>
        <?php endfor; ?>

        <?php if ($current_page < $total_pages): ?>
            <a href="?page=<?= $current_page + 1 ?>">Next &raquo;</a>
        <?php endif; ?>
    <?php } ?>
</div>