@page
@using Models;
@using System.Security.Claims;
@model WebApp.Pages.AnnouncementsModel
@{
    ViewData["Title"] = "Announcements";
    List<Announcement> announcements = ((List<Announcement>)ViewData["announcements"]).OrderByDescending(x => x.IsSticky).ToList();
    int currentPage = 0;
    if (ViewData["page"] != null)
    {
        currentPage = Convert.ToInt32(ViewData["page"]);
    }
}
<div id="functions">
    <a href="./CreateAnnouncement" class="btn btn-primary">Create new announcement</a>
    
    <form class="card" method="get">  
        <div class="card-body">
                <input type="hidden" name="handler" value="filter" />
                <input type="text" name="s" />  
            <div class="form-check form-check-inline">
                <label class="form-check-label" for="true">Sort by ascending order</label>
                <input class="form-check-input" type="radio" name="asc" value="true" />
            </div>
            <div class="form-check form-check-inline">
                <label class="form-check-label" for="false">Sort by descending order</label>
                <input class="form-check-input" type="radio" name="asc" value="false" />
            </div>
            <div class="form-check form-check-inline">
                <label class="form-check-label" for="imp">Only important</label>
                <input class="form-check-input" type="checkbox" name="imp" value="true" />
            </div>
                <input type="submit" asp-page-handler="Filter" value="Filter" class="btn btn-default" role="button" />
        </div>
    </form>  
</div>

<div class="mb-3 mt-3">
    @foreach (Announcement announcement in announcements)
    {
        <div class="card" style="display:inline-flex; width: 18rem;">
            <div class="card-body">
                <h5 class="card-title" @if (announcement.IsImportant)
                {
                    @: style="color: red;"
                }>@announcement.Title</h5>
                <h6 class="card-subtitle mb-2 text-muted">@announcement.Author.Name</h6>
                <p class="card-text">@announcement.Description.PadRight(100).Substring(0,100).Trim()</p>
                <a href="./Announcement?id=@announcement.ID" class="btn btn-primary">More details</a>
            </div>
        </div>
    }
</div>

@if (ViewData["page"] != null)
{
 <nav aria-label="Page navigation">
    <ul class="pagination justify-content-center">
        @if (currentPage <= 1)
        {
            @: <li class="page-item disabled">
            @: <a class="page-link" tabindex="-1">Previous</a>
            @: </li>
        }
        else  
        {
            @: <li class="page-item">
            @: <a class="page-link" href="./Announcements?p=@(currentPage - 1)" tabindex="-1">Previous</a>
            @: </li>
            @: <li class="page-item"><a class="page-link" href="./Announcements?p=@(currentPage - 1)">@(currentPage - 1)</a></li>
        }
        <li class="page-item"><a class="page-link">@currentPage</a>
        @if (announcements.Count == 0 || announcements.Count < Convert.ToInt32(ViewData["count"]))
        {
            @: <li class="page-item disabled">
            @: <a class="page-link">Next</a>
            @: </li>
        }
        else  
        {
            @: <li class="page-item"><a class="page-link" href="./Announcements?p=@(currentPage + 1)">@(currentPage + 1)</a></li>
            @: <li class="page-item">
            @: <a class="page-link" href="./Announcements?p=@(currentPage + 1)">Next</a>
            @: </li>
        }
    </ul>
</nav>   
}