{% extends "admin/base_admin.html" %}
{% set admin_page = "articles" %}

{% block title %}{% if editing %}Edit{% else %}New{% endif %} Article — Admin — {{ config.APP_NAME }}{% endblock %}

{% block head %}{{ super() }}
<style>
  /* Override admin-main so the split editor fills the column */
  .admin-main {
    padding: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  /* ── Editor shell ──────────────────────────────────────────── */
  .ae-shell {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }

  /* ── Toolbar ────────────────────────────────────────────────── */
  .ae-toolbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 1.25rem;
    background: #fff;
    border-bottom: 1px solid #E2E8F0;
    flex-shrink: 0;
  }
  .ae-toolbar__back {
    font-size: 0.8125rem;
    color: #64748B;
    text-decoration: none;
    flex-shrink: 0;
    transition: color 0.1s;
  }
  .ae-toolbar__back:hover { color: #0F172A; }
  .ae-toolbar__sep {
    width: 1px; height: 1.25rem;
    background: #E2E8F0;
    flex-shrink: 0;
  }
  .ae-toolbar__title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #0F172A;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .ae-toolbar__status {
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 0.2rem 0.55rem;
    border-radius: 9999px;
    flex-shrink: 0;
  }
  .ae-toolbar__status--draft {
    background: #F1F5F9;
    color: #64748B;
  }
  .ae-toolbar__status--published {
    background: #DCFCE7;
    color: #16A34A;
  }

  /* ── Metadata strip ─────────────────────────────────────────── */
  #ae-form {
    display: contents; /* form participates in flex layout as transparent wrapper */
  }
  .ae-meta {
    padding: 0.75rem 1.25rem;
    background: #F8FAFC;
    border-bottom: 1px solid #E2E8F0;
    flex-shrink: 0;
  }
  .ae-meta__row {
    display: flex;
    gap: 0.625rem;
    flex-wrap: wrap;
    align-items: end;
  }
  .ae-meta__row + .ae-meta__row { margin-top: 0.5rem; }

  .ae-field {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    min-width: 0;
  }
  .ae-field--flex1  { flex: 1; min-width: 120px; }
  .ae-field--flex2  { flex: 2; min-width: 180px; }
  .ae-field--flex3  { flex: 3; min-width: 220px; }
  .ae-field--fixed80  { flex: 0 0 80px; }
  .ae-field--fixed120 { flex: 0 0 120px; }
  .ae-field--fixed160 { flex: 0 0 160px; }

  .ae-field label {
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: #94A3B8;
    white-space: nowrap;
  }
  .ae-field input,
  .ae-field select {
    width: 100%;
    padding: 0.3rem 0.5rem;
    border: 1px solid #E2E8F0;
    border-radius: 4px;
    font-size: 0.8125rem;
    font-family: var(--font-sans);
    color: #0F172A;
    background: #fff;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
    min-width: 0;
  }
  .ae-field input:focus,
  .ae-field select:focus {
    border-color: #1D4ED8;
    box-shadow: 0 0 0 2px rgba(29,78,216,0.1);
  }
  .ae-field input[readonly] {
    background: #F1F5F9;
    color: #94A3B8;
  }

  /* ── Split pane ─────────────────────────────────────────────── */
  .ae-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  .ae-pane {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }
  .ae-pane--editor { border-right: 1px solid #E2E8F0; }

  .ae-pane__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.375rem 0.875rem;
    background: #F8FAFC;
    border-bottom: 1px solid #E2E8F0;
    flex-shrink: 0;
  }
  .ae-pane--preview .ae-pane__header {
    background: #F8FAFC;
    border-bottom: 1px solid #E2E8F0;
  }
  .ae-pane__label {
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.09em;
    color: #94A3B8;
  }
  .ae-pane__hint {
    font-size: 0.625rem;
    font-family: var(--font-mono);
    color: #94A3B8;
  }

  /* The markdown textarea */
  .ae-editor {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    padding: 1.5rem 2rem;
    font-family: var(--font-mono);
    font-size: 0.875rem;
    line-height: 1.8;
    background: #FEFDFB;
    color: #1E293B;
    caret-color: #1D4ED8;
    tab-size: 2;
  }
  .ae-editor::placeholder { color: #CBD5E1; }
  .ae-editor:focus { outline: none; }

  /* Preview pane — iframe fills the content area */
  #ae-preview-content {
    flex: 1;
    display: flex;
    min-height: 0;
  }

  .preview-placeholder {
    font-size: 0.875rem;
    color: #94A3B8;
    font-style: italic;
    margin: 1.5rem 2rem;
  }

  /* Collapsible metadata */
  .ae-meta--collapsed { display: none; }

  .ae-toolbar__toggle {
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748B;
    background: none;
    border: 1px solid #E2E8F0;
    border-radius: 4px;
    padding: 0.25rem 0.6rem;
    cursor: pointer;
    flex-shrink: 0;
  }
  .ae-toolbar__toggle:hover { color: #0F172A; border-color: #94A3B8; }

  /* Word count footer */
  .ae-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0.25rem 0.875rem;
    background: #F8FAFC;
    border-top: 1px solid #E2E8F0;
    flex-shrink: 0;
  }
  .ae-wordcount {
    font-size: 0.625rem;
    font-family: var(--font-mono);
    color: #94A3B8;
  }

  /* HTMX loading indicator — htmx toggles .htmx-request on the element */
  .ae-loading {
    font-size: 0.625rem;
    color: #94A3B8;
    font-family: var(--font-mono);
    opacity: 0;
    transition: opacity 0.2s;
  }
  .ae-loading.htmx-request { opacity: 1; }

  /* Responsive: stack on narrow screens */
  @media (max-width: 900px) {
    .ae-split { grid-template-columns: 1fr; }
    .ae-pane--preview { display: none; }
  }
</style>
{% endblock %}

{% block admin_content %}
<div class="ae-shell">

  <!-- Toolbar -->
  <div class="ae-toolbar">
    <a href="{{ url_for('admin.articles') }}" class="ae-toolbar__back">← Articles</a>
    <div class="ae-toolbar__sep"></div>
    <span class="ae-toolbar__title">
      {% if editing %}{{ data.get('title', 'Edit Article') }}{% else %}New Article{% endif %}
    </span>
    {% if editing %}
    <span class="ae-toolbar__status ae-toolbar__status--{{ data.get('status', 'draft') }}">
      {{ data.get('status', 'draft') }}
    </span>
    {% endif %}
    <button type="button" class="ae-toolbar__toggle"
      onclick="document.querySelector('.ae-meta').classList.toggle('ae-meta--collapsed')">Meta ▾</button>
    <button form="ae-form" type="submit" class="btn btn-sm">
      {% if editing %}Save Changes{% else %}Create Article{% endif %}
    </button>
  </div>

  <!-- Form wraps everything below the toolbar -->
  <form id="ae-form" method="post" style="display:contents;">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

    <!-- Metadata strip -->
    <div class="ae-meta">
      <div class="ae-meta__row">
        <div class="ae-field ae-field--flex3">
          <label for="title">Title</label>
          <input type="text" id="title" name="title" value="{{ data.get('title', '') }}"
                 required placeholder="Article title…">
        </div>
        <div class="ae-field ae-field--flex2">
          <label for="slug">Slug</label>
          <input type="text" id="slug" name="slug" value="{{ data.get('slug', '') }}"
                 placeholder="auto-generated" {% if editing %}readonly{% endif %}>
        </div>
        <div class="ae-field ae-field--flex2">
          <label for="url_path">URL Path</label>
          <input type="text" id="url_path" name="url_path" value="{{ data.get('url_path', '') }}"
                 placeholder="/slug">
        </div>
        <div class="ae-field ae-field--fixed80">
          <label for="language">Language</label>
          <select id="language" name="language">
            <option value="en" {% if data.get('language', 'en') == 'en' %}selected{% endif %}>EN</option>
            <option value="de" {% if data.get('language') == 'de' %}selected{% endif %}>DE</option>
          </select>
        </div>
        <div class="ae-field ae-field--fixed120">
          <label for="article_type">Type</label>
          <select id="article_type" name="article_type">
            <option value="editorial" {% if data.get('article_type', 'editorial') == 'editorial' %}selected{% endif %}>Editorial</option>
            <option value="cornerstone" {% if data.get('article_type') == 'cornerstone' %}selected{% endif %}>Cornerstone</option>
          </select>
        </div>
        <div class="ae-field ae-field--fixed120">
          <label for="status">Status</label>
          <select id="status" name="status">
            <option value="draft" {% if data.get('status', 'draft') == 'draft' %}selected{% endif %}>Draft</option>
            <option value="published" {% if data.get('status') == 'published' %}selected{% endif %}>Published</option>
          </select>
        </div>
      </div>
      <div class="ae-meta__row">
        <div class="ae-field ae-field--flex3">
          <label for="meta_description">Meta Description</label>
          <input type="text" id="meta_description" name="meta_description"
                 value="{{ data.get('meta_description', '') }}" maxlength="160"
                 placeholder="160 chars max…">
        </div>
        <div class="ae-field ae-field--flex1">
          <label for="country">Country</label>
          <input type="text" id="country" name="country" value="{{ data.get('country', '') }}"
                 placeholder="e.g. US">
        </div>
        <div class="ae-field ae-field--flex1">
          <label for="region">Region</label>
          <input type="text" id="region" name="region" value="{{ data.get('region', '') }}"
                 placeholder="e.g. North America">
        </div>
        <div class="ae-field ae-field--flex2">
          <label for="og_image_url">OG Image URL</label>
          <input type="text" id="og_image_url" name="og_image_url"
                 value="{{ data.get('og_image_url', '') }}">
        </div>
        <div class="ae-field ae-field--fixed160">
          <label for="published_at">Publish Date</label>
          <input type="datetime-local" id="published_at" name="published_at"
                 value="{{ data.get('published_at', '')[:16] if data.get('published_at') else '' }}">
        </div>
      </div>
    </div>

    <!-- Split: editor | preview -->
    <div class="ae-split">

      <!-- Left — Markdown editor -->
      <div class="ae-pane ae-pane--editor">
        <div class="ae-pane__header">
          <span class="ae-pane__label">Markdown</span>
          <span class="ae-pane__hint">[scenario:slug] · [product:slug]</span>
        </div>
        <textarea
          id="body" name="body"
          class="ae-editor"
          {% if not editing %}required{% endif %}
          placeholder="Start writing in Markdown…"
          hx-post="{{ url_for('admin.article_preview') }}"
          hx-trigger="input delay:500ms"
          hx-target="#ae-preview-content"
          hx-include="[name=csrf_token]"
          hx-indicator="#ae-loading"
        >{{ data.get('body', '') }}</textarea>
        <div class="ae-footer">
          <span id="ae-wordcount" class="ae-wordcount">0 words</span>
        </div>
      </div>

      <!-- Right — Rendered preview -->
      <div class="ae-pane ae-pane--preview">
        <div class="ae-pane__header">
          <span class="ae-pane__label">Preview</span>
          <span id="ae-loading" class="ae-loading">Rendering…</span>
        </div>
        <div id="ae-preview-content" style="flex:1;display:flex;min-height:0;">
          {% if preview_doc %}
          <iframe
            srcdoc="{{ preview_doc | e }}"
            style="flex:1;width:100%;border:none;display:block;"
            sandbox="allow-same-origin allow-scripts"
            title="Article preview"
          ></iframe>
          {% else %}
          <p class="preview-placeholder">Start writing to see a preview.</p>
          {% endif %}
        </div>
      </div>

    </div>
  </form>

</div>
<script>
(function () {
  var textarea = document.getElementById('body');
  var counter = document.getElementById('ae-wordcount');
  function updateCount() {
    var text = textarea.value.trim();
    var count = text ? text.split(/\s+/).length : 0;
    counter.textContent = count + (count === 1 ? ' word' : ' words');
  }
  textarea.addEventListener('input', updateCount);
  updateCount();
}());
</script>
{% endblock %}
