<div class="flex flex-col justify-center items-center space-y-8">
  <h1 class="text-2xl title title-primary-500">
    {gettext("Range day")}
  </h1>

  <%= if @containers |> Enum.empty?() do %>
    <h1 class="text-xl title text-primary-600">
      {gettext("No containers staged")}
      {display_emoji("😔")}
    </h1>

    <.link navigate={~p"/containers"} class="btn btn-primary">
      {dgettext("actions", "Why not get some ready to shoot?")}
    </.link>
  <% else %>
    <.link navigate={~p"/containers"} class="btn btn-primary">
      {dgettext("actions", "Stage containers")}
    </.link>

    <div class="flex flex-row flex-wrap justify-center items-stretch w-full">
      <.container_card
        :for={{container_id, container} <- @containers}
        container={container}
        current_user={@current_user}
      >
        <div class="flex flex-wrap justify-center items-center px-4 py-2 h-full min-w-20">
          <button
            type="button"
            class="mx-2 my-1 text-sm btn btn-primary"
            phx-click="toggle_staged"
            phx-value-container_id={container_id}
          >
            {if container.staged,
              do: dgettext("actions", "Unstage"),
              else: dgettext("actions", "Stage")}
          </button>
        </div>
      </.container_card>
    </div>

    <hr class="hr" />

    <div class="flex flex-row flex-wrap justify-center items-stretch w-full">
      <.pack_card
        :for={%{id: pack_id, container_id: container_id} = pack <- @packs}
        pack={pack}
        original_count={Map.fetch!(@original_counts, pack_id)}
        cpr={Map.get(@cprs, pack_id)}
        last_used_date={Map.get(@last_used_dates, pack_id)}
        current_user={@current_user}
        container={Map.fetch!(@containers, container_id)}
      >
        <.link patch={~p"/range/add_shot_record/#{pack}"} class="btn btn-primary">
          {dgettext("actions", "Record shots")}
        </.link>
      </.pack_card>
    </div>
  <% end %>

  <hr class="hr" />

  <%= if @shot_record_count == 0 do %>
    <h1 class="text-xl title text-primary-600">
      {gettext("No shots recorded")}
      {display_emoji("😔")}
    </h1>
  <% else %>
    <h1 class="text-2xl title text-primary-600">
      {gettext("Shot log")}
    </h1>

    <canvas
      id="shot-log-chart"
      phx-hook="ShotLogChart"
      phx-update="ignore"
      class="max-h-72"
      data-chart-data={Jason.encode!(@chart_data)}
      data-label={gettext("Rounds shot")}
      data-color={random_color()}
      aria-label={gettext("Rounds shot chart")}
      role="img"
    >
      {dgettext("errors", "Your browser does not support the canvas element.")}
    </canvas>

    <div class="flex flex-col flex-wrap justify-center items-center space-y-4 w-full sm:flex-row sm:space-y-0 sm:space-x-4">
      <.form
        :let={f}
        for={%{}}
        as={:type}
        phx-change="change_class"
        phx-submit="change_class"
        class="flex items-center"
      >
        {label(f, :class, gettext("Class"), class: "title text-primary-600 text-lg text-center")}

        {select(
          f,
          :class,
          [
            {gettext("All"), :all},
            {gettext("Rifle"), :rifle},
            {gettext("Shotgun"), :shotgun},
            {gettext("Pistol"), :pistol}
          ],
          class: "mx-2 my-1 min-w-20 input input-primary",
          value: @class
        )}
      </.form>

      <.form
        :let={f}
        for={%{}}
        as={:search}
        phx-change="search"
        phx-submit="search"
        class="flex items-center grow"
      >
        {text_input(f, :search_term,
          class: "grow input input-primary",
          phx_debounce: 300,
          placeholder: gettext("Search shot records"),
          role: "search",
          value: @search
        )}
      </.form>

      <.form
        :let={f}
        for={%{}}
        as={:shot_records}
        phx-change="change_dates"
        phx-submit="change_dates"
        class="flex items-center"
      >
        {label(f, :dates_start, gettext("Dates"),
          class: "title text-primary-600 text-lg text-center"
        )}

        <.date_range name="dates" />
      </.form>
    </div>

    <%= if @shot_records |> Enum.empty?() do %>
      <h1 class="text-xl title text-primary-600">
        {gettext("No shots recorded")}
        {display_emoji("😔")}
      </h1>
    <% else %>
      <.live_component
        module={CanneryWeb.Components.ShotRecordTableComponent}
        id="shot-records-index-table"
        shot_records={@shot_records}
        current_user={@current_user}
      >
        <:actions :let={shot_record}>
          <div class="flex justify-center items-center px-4 py-2 space-x-4">
            <.link
              patch={~p"/range/edit/#{shot_record}"}
              class="text-primary-600 link"
              aria-label={
                dgettext("actions", "Edit shot record of %{shot_record_count} shots",
                  shot_record_count: shot_record.count
                )
              }
            >
              <i class="fa-fw fa-lg fas fa-edit"></i>
            </.link>

            <.link
              href="#"
              class="text-primary-600 link"
              phx-click="delete"
              phx-value-id={shot_record.id}
              data-confirm={
                dgettext("prompts", "Are you sure you want to delete this shot record?")
              }
              aria-label={
                dgettext("actions", "Delete shot record of %{shot_record_count} shots",
                  shot_record_count: shot_record.count
                )
              }
            >
              <i class="fa-fw fa-lg fas fa-trash"></i>
            </.link>
          </div>
        </:actions>
      </.live_component>
    <% end %>
  <% end %>
</div>

<%= case @live_action do %>
  <% :edit -> %>
    <.modal return_to={~p"/range"}>
      <.live_component
        module={CanneryWeb.RangeLive.FormComponent}
        id={@shot_record.id}
        title={@page_title}
        action={@live_action}
        shot_record={@shot_record}
        return_to={~p"/range"}
        current_user={@current_user}
      />
    </.modal>
  <% :add_shot_record -> %>
    <.modal return_to={~p"/range"}>
      <.live_component
        module={CanneryWeb.Components.AddShotRecordComponent}
        id={:new}
        title={@page_title}
        action={@live_action}
        pack={@pack}
        return_to={~p"/range"}
        current_user={@current_user}
      />
    </.modal>
  <% _ -> %>
<% end %>