<div class="card" style="max-width:820px">
  <div class="card-body">
    <form method="post" action="<?= $article ? url('/kb/' . $article['id']) : url('/kb') ?>">
      <?= csrf_field() ?>
      <div class="form-group">
        <label>Title <span class="req">*</span></label>
        <input type="text" name="title" required value="<?= e($article['title'] ?? '') ?>">
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>Category</label>
          <select name="category_id">
            <option value="">—</option>
            <?php foreach ($categories as $c) : ?><option value="<?= $c['id'] ?>" <?= ($article['category_id'] ?? null)==$c['id']?'selected':'' ?>><?= e($c['name']) ?></option><?php endforeach; ?>
          </select>
        </div>
        <div class="form-group">
          <label>Product / Module</label>
          <select name="product_id">
            <option value="">Any</option>
            <?php foreach ($products as $p) : ?><option value="<?= $p['id'] ?>" <?= ($article['product_id'] ?? null)==$p['id']?'selected':'' ?>><?= e($p['name']) ?> — <?= e($p['module']) ?></option><?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="form-group">
        <label>Visibility</label>
        <select name="visibility">
          <option value="internal" <?= ($article['visibility'] ?? '')==='internal'?'selected':'' ?>>Internal only</option>
          <option value="customer" <?= ($article['visibility'] ?? '')==='customer'?'selected':'' ?>>Customer-visible</option>
        </select>
      </div>
      <div class="form-group">
        <label>Keywords <span class="help-text">(comma separated, improves search matching)</span></label>
        <input type="text" name="keywords" value="<?= e($article['keywords'] ?? '') ?>">
      </div>
      <div class="form-group">
        <label>Body <span class="req">*</span></label>
        <textarea name="body" rows="12" required><?= e($article['body'] ?? '') ?></textarea>
      </div>
      <div class="flex justify-between">
        <a href="<?= url('/kb') ?>" class="btn">Cancel</a>
        <div class="flex gap-2">
          <?php if ($article && $article['status'] !== 'published') : ?>
            <button type="submit" formaction="<?= url('/kb/' . $article['id'] . '/publish') ?>" class="btn" style="background:var(--green-bg);border-color:#bbf7d0;color:#166534">Save &amp; publish</button>
          <?php endif; ?>
          <?php if ($article && $article['status'] === 'published') : ?>
            <button type="submit" formaction="<?= url('/kb/' . $article['id'] . '/retire') ?>" class="btn btn-danger">Retire</button>
          <?php endif; ?>
          <button type="submit" class="btn btn-primary"><?= $article ? 'Save changes' : 'Create draft' ?></button>
        </div>
      </div>
    </form>
  </div>
</div>
