<% content_for :title, "Letter Batch ##{@batch.id} - #{@batch.addresses.count} letters" %>
<div class="container">
  <div class="page-header">
    <h1 class="page-title">
      Letter Batch #<%= @batch.id %>
      <small>(<%= pluralize(@batch.letters.count, 'letter') %>)</small>
    </h1>
    <div class="actions">
      <%= secondary_link_to "Back to batches", letter_batches_path do %>
        <%= back_icon %>Back to batches
      <% end %>
      <%= secondary_link_to "Edit this batch", edit_letter_batch_path(@batch) do %>
        <%= edit_icon %>Edit this batch
      <% end %>
      <% unless @batch.processed? %>
        <%= button_to "Delete Batch", letter_batch_path(@batch), method: :delete, class: "btn danger" %>
      <% end %>
    </div>
    <div class="grid">
      <div class="grid md:grid-cols-12 gap-4">
        <div class="md:col-span-8">
          <article class="mb-4">
            <header>
              <h2>Details</h2>
            </header>
            <div class="p-3">
              <%= render partial: "batch", locals: { batch: @batch } %>
            </div>
          </article>
          <article class="mb-4">
            <header>
              <h2>Specifications</h2>
            </header>
            <div class="p-3">
              <div class="grid md:grid-cols-2 gap-4">
                <div>
                  <%= render 'shared/letter_attributes', record: @batch %>
                </div>
                <div>
                  <h3>Batch Information</h3>
                  <div class="p-2 border rounded">
                    <div><span class="text-gray-500">Weight:</span> <%= @batch.letter_weight %> oz</div>
                    <div><span class="text-gray-500">Mailing Date:</span> <%= @batch.letter_mailing_date %></div>
                  </div>
                </div>
              </div>
            </div>
          </article>
          <% if @batch.processed? %>
            <% if @batch.letters.any? %>
              <article class="mb-4">
                <header>
                  <h2>Letters</h2>
                </header>
                <div class="p-3">
                  <div class="overflow-x-auto">
                    <table class="table">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>Address</th>
                          <th>Status</th>
                          <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
                        <% @batch.letters.each do |letter| %>
                          <tr>
                            <td><%= letter.public_id %></td>
                            <td><%= letter.address.snailify %></td>
                            <td><%= letter.aasm.current_state %></td>
                            <td>
                              <%= link_to "View", letter_path(letter), class: "btn btn-sm btn-secondary" %>
                            </td>
                          </tr>
                        <% end %>
                      </tbody>
                    </table>
                  </div>
                </div>
              </article>
            <% end %>
          <% end %>
          <article class="mb-4">
            <header>
              <h2>Admin Information</h2>
            </header>
            <div class="p-3">
              <%= render partial: "admin_inspector", locals: { record: @batch } %>
            </div>
          </article>
        </div>
        <div class="md:col-span-4">
          <% if @batch.processed? && @batch.pdf_label.attached? %>
            <article class="sticky top-4">
              <header>
                <h2>Generated Labels</h2>
              </header>
              <div class="p-3">
                <div class="flex items-center justify-between mb-4">
                  <div>
                    <p class="text-sm font-medium">labels:</p>
                    <div class="flex gap-2 mt-2">
                      <%= link_to regenerate_labels_letter_batch_path(@batch), class: "btn btn-small warning" do %>
                        ↻ Regenerate Labels
                      <% end %>
                    </div>
                    <div class="border rounded overflow-hidden">
                      <iframe src="<%= rails_blob_path(@batch.pdf_label, disposition: 'inline') %>" class="w-full h-full border-0"></iframe>
                    </div>
                    <p class="text-xs text-gray-500">Generated
                      on <%= @batch.pdf_label.created_at.strftime("%B %d, %Y at %I:%M %p") %></p>
                  </div>
                  <div class="flex gap-2">
                    <%= link_to rails_blob_path(@batch.pdf_label, disposition: "attachment"), class: "btn btn-sm btn-primary" do %>
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                      </svg>
                      Download PDF
                    <% end %>
                  </div>
                </div>
              </div>
            </article>
            <%= render 'shared/instant_print_window', url: rails_blob_path(@batch.pdf_label, disposition: 'inline') unless @batch.letters.all?(&:mailed?) %>
            <% if @batch.processed? %>
              <div class="mt-4">
                <% if @batch.letters.any? && @batch.letters.all?(&:pending?) %>
                  <%= button_to mark_printed_letter_batch_path(@batch), method: :post, class: "btn btn-block success mb-2", id: 'mark_printed' do %>
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                    </svg>
                    Mark All Printed
                  <% end %>
                <% end %>
                <% if @batch.letters.any? && @batch.letters.any?(&:printed?) %>
                  <%= button_to mark_mailed_letter_batch_path(@batch), method: :post, class: "btn btn-block btn-info", data: { confirm: "Are you sure you want to mark all letters as mailed?" } do %>
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
                    </svg>
                    Mark All Mailed
                  <% end %>
                <% end %>
              </div>
            <% end %>
          </div>
        </article>
      <% end %>
    </div>
  </div>
</div>
</div>