@(currentPage: io.ebean.PagedList[Computer], currentSortBy: String, currentOrder: String, currentFilter: String)

@****************************************
* Helper generating navigation links    *
****************************************@
@link(newPage:Int, newSortBy:String) = @{

    var sortBy = currentSortBy
    var order = currentOrder

    if(newSortBy != null) {
        sortBy = newSortBy
        if(currentSortBy == newSortBy) {
            if(currentOrder == "asc") {
                order = "desc"
            } else {
                order = "asc"
            }
        } else {
            order = "asc"
        }
    }

    // Generate the link
    routes.HomeController.list(newPage, sortBy, order, currentFilter)

}

@**********************************
* Helper generating table headers *
***********************************@
@header(key:String, title:String) = {
    <th class="@key.replace(".","_") header @if(currentSortBy == key) { @{if(currentOrder == "asc") "headerSortDown" else "headerSortUp" } }">
        <a href="@link(0, key)">@title</a>
    </th>
}

@main {

    <h1 id="homeTitle">@Messages("computers.list.title", currentPage.getTotalCount)</h1>

    @if(flash.containsKey("success")) {
        <div class="alert-message warning">
            <strong>Done!</strong> @flash.get("success")
        </div>
    }

    <div id="actions">

        <form action="@link(0, "name")" method="GET">
            <input type="search" id="searchbox" name="f" value="@currentFilter" placeholder="Filter by computer name...">
            <input type="submit" id="searchsubmit" value="Filter by name" class="btn primary">
        </form>

        <a class="btn success" id="add" href="@routes.HomeController.create()">Add a new computer</a>

    </div>

    @if(currentPage.getTotalCount == 0) {

        <div class="well">
            <em>Nothing to display</em>
        </div>

    } else {

        <table class="computers zebra-striped">
            <thead>
                <tr>
                    @header("name", "Computer name")
                    @header("introduced", "Introduced")
                    @header("discontinued", "Discontinued")
                    @header("company.name", "Company")
                </tr>
            </thead>
            <tbody>

                @for(computer <- currentPage.getList.asScala) {
                    <tr>
                        <td><a href="@routes.HomeController.edit(computer.id)">@computer.name</a></td>
                        <td>
                            @if(computer.introduced == null) {
                                <em>-</em>
                            } else {
                                @computer.introduced.format("dd MMM yyyy")
                            }
                        </td>
                        <td>
                            @if(computer.discontinued == null) {
                                <em>-</em>
                            } else {
                                @computer.discontinued.format("dd MMM yyyy")
                            }
                        </td>
                        <td>
                            @if(computer.company == null) {
                                <em>-</em>
                            } else {
                                @computer.company.name
                            }
                        </td>
                    </tr>
                }

            </tbody>
        </table>

        <div id="pagination" class="pagination">
            <ul>
                @if(currentPage.hasPrev) {
                    <li class="prev">
                        <a href="@link(currentPage.getPageIndex - 1, null)">&larr; Previous</a>
                    </li>
                } else {
                    <li class="prev disabled">
                        <a>&larr; Previous</a>
                    </li>
                }
                <li class="current">
                    <a>Displaying @currentPage.getDisplayXtoYofZ(" to "," of ")</a>
                </li>
                @if(currentPage.hasNext) {
                    <li class="next">
                        <a href="@link(currentPage.getPageIndex + 1, null)">Next &rarr;</a>
                    </li>
                } else {
                    <li class="next disabled">
                        <a>Next &rarr;</a>
                    </li>
                }
            </ul>
        </div>

    }

}

