<script lang="ts">
  import { Trash2, ChevronUp, ChevronDown, Plus } from 'lucide-svelte';
  import type { DraftIng } from './recipe-editor-types';

  type Props = {
    ing: DraftIng;
    idx: number;
    total: number;
    onmove: (dir: -1 | 1) => void;
    onremove: () => void;
    onaddSection: () => void;
    onremoveSection: () => void;
  };

  let { ing, idx, total, onmove, onremove, onaddSection, onremoveSection }: Props = $props();
</script>

{#if ing.section_heading === null}
  <li class="section-insert">
    <button type="button" class="add-section" onclick={onaddSection}>
      <Plus size={12} strokeWidth={2.5} />
      <span>Abschnitt hinzufügen</span>
    </button>
  </li>
{:else}
  <li class="section-heading-row">
    <input
      class="section-heading"
      type="text"
      bind:value={ing.section_heading}
      placeholder='Sektion, z. B. „Für den Teig"'
      aria-label="Sektionsüberschrift"
    />
    <button
      type="button"
      class="section-remove"
      aria-label="Sektion entfernen"
      onclick={onremoveSection}
    >
      <Trash2 size={14} strokeWidth={2} />
    </button>
  </li>
{/if}
<li class="ing-row">
  <div class="move">
    <button
      class="move-btn"
      type="button"
      aria-label="Zutat nach oben"
      disabled={idx === 0}
      onclick={() => onmove(-1)}
    >
      <ChevronUp size={14} strokeWidth={2.5} />
    </button>
    <button
      class="move-btn"
      type="button"
      aria-label="Zutat nach unten"
      disabled={idx === total - 1}
      onclick={() => onmove(1)}
    >
      <ChevronDown size={14} strokeWidth={2.5} />
    </button>
  </div>
  <input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
  <input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
  <input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
  <input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
  <button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
    <Trash2 size={16} strokeWidth={2} />
  </button>
</li>

<style>
  .ing-row {
    display: grid;
    grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
    gap: 0.35rem;
    align-items: center;
  }
  .move {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .move-btn {
    width: 28px;
    height: 20px;
    border: 1px solid #cfd9d1;
    background: white;
    border-radius: 6px;
    cursor: pointer;
    color: #555;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
  }
  .move-btn:hover:not(:disabled) {
    background: #f4f8f5;
  }
  .move-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
  }
  .ing-row input {
    padding: 0.5rem 0.55rem;
    border: 1px solid #cfd9d1;
    border-radius: 8px;
    font-size: 0.9rem;
    min-height: 38px;
    font-family: inherit;
    min-width: 0;
  }
  .del {
    width: 40px;
    height: 40px;
    border: 1px solid #f1b4b4;
    background: white;
    color: #c53030;
    border-radius: 8px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .del:hover {
    background: #fdf3f3;
  }
  @media (max-width: 560px) {
    .ing-row {
      grid-template-columns: 28px 70px 1fr 40px;
      grid-template-areas:
        'move qty name del'
        'move unit unit del'
        'note note note note';
    }
    .ing-row .move {
      grid-area: move;
    }
    .ing-row .qty {
      grid-area: qty;
    }
    .ing-row .unit {
      grid-area: unit;
    }
    .ing-row .name {
      grid-area: name;
    }
    .ing-row .note {
      grid-area: note;
    }
    .ing-row .del {
      grid-area: del;
    }
  }
  .section-insert {
    display: flex;
    justify-content: center;
    list-style: none;
    margin: -0.2rem 0 0.1rem;
    opacity: 0;
    transition: opacity 0.15s;
  }
  /* Parent-UL liegt im RecipeEditor, daher :global(.ing-list). Ohne das
     scopt Svelte die Klasse und der Selector matcht zur Laufzeit nicht. */
  :global(.ing-list):hover .section-insert,
  .section-insert:focus-within {
    opacity: 1;
  }
  .add-section {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.2rem 0.55rem;
    border: 1px dashed #cfd9d1;
    background: white;
    color: #2b6a3d;
    border-radius: 999px;
    cursor: pointer;
    font-size: 0.75rem;
    font-family: inherit;
  }
  .add-section:hover {
    background: #f4f8f5;
  }
  .section-heading-row {
    display: grid;
    grid-template-columns: 1fr 32px;
    gap: 0.35rem;
    list-style: none;
    margin-top: 0.4rem;
  }
  .section-heading {
    padding: 0.45rem 0.7rem;
    border: 1px solid #cfd9d1;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 600;
    color: #2b6a3d;
    font-family: inherit;
    background: #f4f8f5;
  }
  .section-remove {
    width: 32px;
    height: 38px;
    border: 1px solid #cfd9d1;
    background: white;
    border-radius: 8px;
    color: #666;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .section-remove:hover {
    background: #fdf3f3;
    border-color: #f1b4b4;
    color: #c53030;
  }
</style>
