{% extends "base.html" %}
{% import "partials/icons.html" as icons %}
{% import "partials/detail_cards.html" as detail_cards %}
{% block title %}Brewlog · Edit Bag{% endblock %}

{% block content %}
  <header class="flex flex-col gap-2">
    <h1 class="text-3xl font-semibold">Edit Bag</h1>
    <p class="max-w-2xl text-sm text-text-secondary">Update bag details.</p>
  </header>

  <section class="rounded-lg border bg-surface p-5">
    <form
      class="flex flex-col gap-4 pb-16 md:pb-0"
      data-signals:_submitting="false"
      data-signals:_submit-error="''"
      data-signals:_roast-date="'{{ roast_date }}'"
      data-signals:_amount="{{ amount }}"
      data-signals:_remaining="{{ remaining }}"
      data-on:submit="$_submitting = true; $_submitError = ''; @put('/api/v1/bags/{{ id }}', {contentType: 'form'})"
      data-on:datastar-fetch="if (!$_submitting) return;
        if (evt.detail.type === 'finished') { $_submitting = false; sessionStorage.setItem('toast', 'Bag updated') }
        else if (evt.detail.type === 'error') { $_submitting = false; $_submitError = 'Failed to save changes.' }"
    >
      <div class="flex flex-col gap-1 text-sm">
        <span
          class="text-xs font-semibold text-text-muted uppercase tracking-wide"
          >Roast*</span
        >
        <searchable-select
          name="roast_id"
          placeholder="Type to search roasts&hellip;"
          initial-value="{{ roast_id }}"
        >
          {% for roast in roast_options %}
            <button
              type="button"
              value="{{ roast.id }}"
              data-display="{{ roast.label }}"
              class="w-full px-3 py-2 text-left text-sm hover:bg-surface-alt transition"
            >
              <span class="font-medium text-text">{{ roast.name }}</span>
              <span class="ml-2 text-xs text-text-muted"
                >{{ roast.roaster_name }}</span
              >
            </button>
          {% endfor %}
        </searchable-select>
      </div>
      <div class="grid gap-4 sm:grid-cols-3">
        <label class="flex flex-col gap-1 text-sm">
          <span
            class="text-xs font-semibold text-text-muted uppercase tracking-wide"
            >Roast Date</span
          >
          <input
            type="date"
            name="roast_date"
            class="input-field"
            data-bind:_roast-date
          />
        </label>
        <label class="flex flex-col gap-1 text-sm">
          <span
            class="text-xs font-semibold text-text-muted uppercase tracking-wide"
            >Amount (g)*</span
          >
          <input
            type="number"
            name="amount"
            step="0.1"
            required
            aria-required="true"
            class="input-field"
            placeholder="250"
            data-bind:_amount
          />
        </label>
        <div class="flex flex-col gap-1 text-sm">
          <span
            class="text-xs font-semibold text-text-muted uppercase tracking-wide"
            >Remaining (g)</span
          >
          <div class="flex items-center gap-2">
            <button
              type="button"
              class="btn-adjust"
              data-on:click="$_remaining = Math.max(0, Number($_remaining) - 0.5)"
            >
              -
            </button>
            <input
              type="number"
              name="remaining"
              step="0.1"
              min="0"
              class="input-field flex-1 text-center"
              data-bind:_remaining
            />
            <button
              type="button"
              class="btn-adjust"
              data-on:click="$_remaining = Math.min(Number($_amount), Number($_remaining) + 0.5)"
            >
              +
            </button>
          </div>
        </div>
      </div>
      {{ detail_cards::edit_form_actions() }}
    </form>
  </section>
{% endblock %}
