<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Fishes</title>

    <link
      rel="canonical"
      href="https://getbootstrap.com/docs/4.5/examples/album/"
    />

    <!-- Bootstrap core CSS -->
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
      integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
      crossorigin="anonymous"
    />

    <script>
      function onLimitChange(limit) {
          location.replace(`/fishes?limit=${limit}`);
          return limit;
      }
  </script>


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
  </head>
  <body>
    <%- include('common/header') %>

    <div class="container">
      <% if (message) { %>
        <div class = "row">
        <div class="alert alert-success" role="alert">
        <%= message %>
        </div>
      <% } %>
      <div class="row col-md-12 col-md-offset-2 custyle mx-auto">
        <h1 class="mx-auto">üêü Fishes üêü</h1>
        

      </div>
      <table class="table table-striped custab">
        <thead>
          <tr>
            <th>Name</th>
            <th>#</th>
            <th>Selling price</th>
            <th>Spawn rate</th>
            <th>Shadow size</th>
            <th class="text-center">  <a href="/create-fish/"><button type="button" class="btn btn-primary ">Create a new fish</button></a> </th>
          </tr>
          

          <% fishes.forEach(fish => { %>

          <tr>
            
            <td><%= fish.Name %></td>
            <td><%= fish.Number %></td>
            <td><%= fish.Sell %></td>
            <td><%= fish.SpawnRates %></td>
            <td><%= fish.Shadow %></td>

            <td class="text-center">
              <a href="/fishes/update/<%= fish.id %>" class="btn btn-info btn-xs"
                ><span class="glyphicon glyphicon-edit"></span> Edit</a
              >
              <a href="/fishes/delete/<%= fish.id %>" class="btn btn-danger btn-xs"
                ><span class="glyphicon glyphicon-remove"></span> Del</a
              >
            </td>
          </tr>

          <% }); %>
        </thead>
      </table>
    </div>
    <nav aria-label="Page navigation example">
      <div class="row justify-content-md-center">

        <%
        if (isNaN(page)){
          page=1;
        };
        if (isNaN(limit)){
          limit=10;
        };
        
        if (page-1>0){
        %>
          <div class="page-item"><a class="page-link" href="/fishes?page=<%= page-1%>&limit=<%= limit%>">Previous</a></div>
          <%};%>
        
        <%
        
        
        for (let i = 1; i <= totalFishes/limit; i++) { %>
          
        <div class="page-item"><a class="page-link" href="/fishes?page=<%= i %>&limit=<%= limit%>"><%= i %></a></div>
        
        <% }; 
        
        if ( (page+1<=(totalFishes/limit)) || (isNaN(page)) ){
          %>
            <div class="page-item"><a class="page-link" href="/fishes?page=<%= page+1%>&limit=<%= limit%>">Next</a></div>
            <%};%>
        
        
        
        
        </div>
      <div class="row justify-content-md-center">
        <div class="col-">
          <form method="POST">
            <div class="form-group">
              <label for="cars">List size:</label>

              
                <select onchange="onLimitChange(this.value)" id="limit" value=<%= limit%> name="limit">
                  <option value=""></option>
                  <option value="5">5</option>
                  <option value="10">10</option>
                  <option value="15">15</option>
                  <option value="20">20</option>
                </select>
          </form>
        </div>
      </div>
    </nav>
    <%- include('common/footer') -%>
  </body>
</html>
