@extends('app.shell')

@section('body')
  @php
    /** @var \App\Models\Person $mePerson */
    /** @var \Illuminate\Support\Collection<int, \App\Models\Person> $people */
    $emojiPresets = ['📺','🧊','🍽️','🛋️','💻','🚗'];
  @endphp

  <div class="min-h-screen safe-bottom bg-background animate-sheet-up">
    <div class="app-page-wrap pt-6 md:pt-6 lg:pt-8 pb-10 md:pb-10 space-y-7">
      <x-sheet-handle />

      <x-page-header title="{{ __('New shared purchase') }}" back>
        <x-slot:right>
          <x-sheet-save-button form="create-shared-purchase-form" disabled />
        </x-slot:right>
      </x-page-header>

      <x-error-alert />

      <form id="create-shared-purchase-form" method="POST" action="{{ route('app.shared-purchases.store') }}" class="space-y-7">
        @csrf

        <div class="text-center py-2">
          <div id="purchaseEmojiPreview" class="w-20 h-20 rounded-[22px] bg-primary-muted flex items-center justify-center text-[40px] mx-auto mb-4">
            📺
          </div>
          <input
            type="text"
            name="name"
            placeholder="{{ __('Purchase name') }}"
            class="text-[24px] font-bold text-foreground bg-transparent outline-none text-center w-full placeholder:text-muted-foreground/25 tracking-tight"
            required
            autofocus
            data-name
          />
        </div>

        <input type="hidden" name="emoji" id="purchaseEmoji" value="📺" />

        <div>
          <p class="section-header mb-3 px-1">{{ __('Emoji') }}</p>
          <div class="flex flex-wrap gap-2 justify-center">
            @foreach ($emojiPresets as $e)
              <button
                type="button"
                class="w-12 h-12 rounded-2xl text-2xl flex items-center justify-center transition-all apple-card"
                data-emoji="{{ $e }}"
                onclick="window.__pickPurchaseEmoji?.(this.dataset.emoji)"
              >
                {{ $e }}
              </button>
            @endforeach
          </div>
        </div>

        <div>
          <p class="section-header mb-2.5 px-1">{{ __('Target (optional)') }}</p>
          <input
            type="number"
            step="0.01"
            min="0"
            name="target_amount"
            placeholder="{{ __('0.00') }}"
            class="w-full apple-card-inset px-4 py-3.5 text-[15px] text-foreground placeholder:text-muted-foreground/50 outline-none focus:ring-2 focus:ring-primary/15 rounded-2xl"
          />
        </div>

        <div>
          <p class="section-header mb-2.5 px-1">{{ __('Notes (optional)') }}</p>
          <textarea
            name="notes"
            rows="3"
            placeholder="{{ __('Add context, links, constraints...') }}"
            class="w-full apple-card-inset px-4 py-3.5 text-[15px] text-foreground placeholder:text-muted-foreground/50 outline-none focus:ring-2 focus:ring-primary/15 rounded-2xl resize-none"
          ></textarea>
        </div>

        <x-member-selector
          :me-person="$mePerson"
          :people="$people"
          title="{{ __('Members') }}"
          description="{{ __('Select who’s in this purchase.') }}"
        />

        <x-invite-email-panel />

        <button
          type="submit"
          class="w-full py-4 rounded-2xl bg-primary text-primary-foreground text-[17px] font-semibold disabled:opacity-30 active:scale-[0.98] transition-transform"
        >
          {{ __('Create purchase') }}
        </button>
      </form>
    </div>
  </div>

  <script>
    window.__pickPurchaseEmoji = (e) => {
      const v = e || '📺';
      const input = document.getElementById('purchaseEmoji');
      const preview = document.getElementById('purchaseEmojiPreview');
      if (input) input.value = v;
      if (preview) preview.textContent = v;
    };

    (function () {
      const saveBtn = document.querySelector('[data-save-btn]');
      const nameInput = document.querySelector('[data-name]');
      function syncSaveEnabled() {
        if (!saveBtn || !nameInput) return;
        saveBtn.disabled = String(nameInput.value || '').trim() === '';
      }
      nameInput?.addEventListener('input', syncSaveEnabled);

      window.WeShareMembers?.initRows?.(document);
      syncSaveEnabled();
    })();
  </script>
@endsection
