<% item_label = format_label(@pagy.count, "location") %>
<% title = format_title(@searched, @scheme.service_name, current_user, item_label, @pagy.count, nil) %>
<% content_for :title, title %>

<% if current_user.support? %>
  <% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: {
    "Home" => root_path,
    "Schemes (#{@scheme.owning_organisation.name})" => schemes_organisation_path(@scheme.owning_organisation),
    content_for(:title) => scheme_path(@scheme),
  }) %>
<% else %>
  <% content_for :before_content do %>
    <%= govuk_back_link(href: scheme_path(@scheme)) %>
  <% end %>
<% end %>

<%= render partial: "organisations/headings", locals: { main: @scheme.service_name, sub: nil } %>
<div class="app-filter-layout" data-controller="filter-layout">
  <%= render SubNavigationComponent.new(items: scheme_items(request.path, @scheme.id)) %>
  <%= render partial: "locations/location_filters" %>
  <div class="app-filter-layout__content">
    <h2 class="govuk-visually-hidden">Locations</h2>

    <div class="app-filter-toggle"></div>
    <%= render SearchComponent.new(current_user:, search_label: "Search by location name or postcode", value: @searched) %>
    <%= govuk_section_break(visible: true, size: "m") %>

    <%= govuk_table do |table| %>
      <%= table.with_caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %>
        <%= render(SearchResultCaptionComponent.new(
          searched: @searched,
          count: @pagy.count,
          item_label:,
          total_count: @total_count,
          item: "location",
          filters_count: applied_filters_count(@filter_type),
        )) %>
      <% end %>
      <%= table.with_head do |head| %>
        <%= head.with_row do |row| %>
          <% row.with_cell(header: true, text: "Postcode", html_attributes: { scope: "col" }) %>
          <% row.with_cell(header: true, text: "Name", html_attributes: { scope: "col" }) %>
          <% row.with_cell(header: true, text: "Location code", html_attributes: { scope: "col" }) %>
          <% row.with_cell(header: true, text: "Status", html_attributes: { scope: "col" }) %>
        <% end %>
      <% end %>
      <% @locations.each do |location| %>
        <%= table.with_body do |body| %>
          <%= body.with_row do |row| %>
            <% row.with_cell(text: simple_format(location_cell_postcode(location, @scheme), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
            <% row.with_cell(text: location.name) %>
            <% row.with_cell(text: location.id) %>
            <% row.with_cell(text: status_tag_from_resource(location)) %>
          <% end %>
        <% end %>
      <% end %>
    <% end %>

    <% if LocationPolicy.new(current_user, @scheme.locations.new).create? && [:active, :merged].include?(@scheme.owning_organisation.status) %>
      <% if status_hint_message = scheme_status_hint(@scheme) %>
        <div class="govuk-hint">
          <%= status_hint_message %>
        </div>
        <br>
      <% end %>
      <%= govuk_button_to "Add a location", scheme_locations_path(@scheme), method: "post" %>
    <% end %>
    <br>
    <%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %>
  </div>
</div>