<p class="text-success text-center"><%= notice %></p>
<% content_for :title, "Students" %>

<style>
  .students-container {
    max-width: 900px;
    margin: 25px auto;
    padding: 10px;
    font-family: "Poppins", sans-serif;
  }

  .page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 18px;
  }

  .page-header h1 {
    font-size: 1.8rem;
    font-weight: 600;
    margin: 0;
  }

  .btn-primary {
    background: #2563eb;
    color: #fff;
    padding: 10px 14px;
    text-decoration: none;
    border-radius: 8px;
    box-shadow: 0 4px 10px rgba(37,99,235,0.25);
    font-weight: 600;
    transition: 0.2s ease;
  }
  .btn-primary:hover {
    background: #1e4fc4;
    transform: translateY(-2px);
  }

  .student-card {
    background: #fff;
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 4px 14px rgba(0,0,0,0.06);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .student-info h3 {
    font-size: 1.1rem;
    margin: 0;
    font-weight: 600;
    color: #111827;
  }

  .student-info p {
    margin: 3px 0 0;
    color: #6b7280;
    font-size: 0.9rem;
  }

  .btn-small {
    background: #10b981;
    color: #fff;
    padding: 8px 12px;
    text-decoration: none;
    border-radius: 6px;
    font-weight: 600;
    transition: 0.2s ease;
  }
  .btn-small:hover {
    background: #0a8d63;
  }
  .badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 0.8rem;
    color: #fff;
    text-decoration: none;
  }
  .badge-primary { background: #2563eb; }
  .badge-secondary { background: #6b7280; }
</style>

<div class="students-container">
  <div class="page-header">
    <h1>Students</h1>
    <% if @institution.present? %>
      <%= link_to "➕ New Student", new_institution_student_path(@institution), class: "btn-primary" %>
    <% else %>
      <%= link_to "➕ New Student", new_institution_path, class: "btn-primary" %>
    <% end %>
  </div>

  <div id="students-list">
    <% @students.each do |student| %>
      <div class="student-card">
        <div class="student-info">
          <h3><%= student.first_name %> <%= student.last_name %></h3>
          <p>Place: <%= student.place %></p>
          <p>
            Institution:
            <% if student.institution.present? %>
              <%= link_to student.institution.name, institution_path(student.institution), class: "badge badge-primary" %>
            <% else %>
              <span class="badge badge-secondary">No institution</span>
            <% end %>
          </p>
        </div>
        <%# view path: prefer nested if current page is nested, else use student's institution if present %>
        <% view_path = if @institution.present?
                         institution_student_path(@institution, student)
                       elsif student.institution.present?
                         institution_student_path(student.institution, student)
                       else
                         student_path(student)
                       end %>
        <%= link_to "View →", view_path, class: "btn-small" %>
      </div>
    <% end %>

    <% if @students.empty? %>
      <p class="text-center" style="color:#6b7280;">No students found.</p>
    <% end %>
  </div>
</div>
