<?php
require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../lib/auth.php";
require_once __DIR__ . "/../lib/helpers.php";
require_once __DIR__ . "/../lib/upload.php";
require_once __DIR__ . "/../admin/admin_menu.php";

require_admin();

$uploadDirAbs = __DIR__ . "/../uploads/letters"; // səndə v2/uploads varsa bu düz olacaq

$success = $error = '';
$editId = isset($_GET['edit']) ? (int)$_GET['edit'] : 0;

$item = ['title'=>'','slug'=>'','cover'=>'','body_html'=>'','is_active'=>1];

if ($editId > 0) {
  $st = $pdo->prepare("SELECT * FROM letters WHERE id=? LIMIT 1");
  $st->execute([$editId]);
  $item = $st->fetch() ?: $item;
  if (empty($item['title'])) die("Məktub tapılmadı");
}

if ($_SERVER['REQUEST_METHOD']==='POST') {
  $title = trim((string)($_POST['title'] ?? ''));
  $slug  = trim((string)($_POST['slug'] ?? ''));
  $body  = (string)($_POST['body_html'] ?? '');
  $active= isset($_POST['is_active']) ? 1 : 0;

  if ($title==='') $error = "Başlıq boş ola bilməz.";
  if ($error==='') {
    if ($slug==='') $slug = slugify($title);

    $cover = upload_image('cover', $uploadDirAbs, $item['cover'] ?? null);

    if ($editId > 0) {
      $up = $pdo->prepare("UPDATE letters SET title=?, slug=?, cover=?, body_html=?, is_active=? WHERE id=?");
      $up->execute([$title,$slug,$cover,$body,$active,$editId]);
      $success = "Yadda saxlanıldı.";
      $item = array_merge($item, ['title'=>$title,'slug'=>$slug,'cover'=>$cover,'body_html'=>$body,'is_active'=>$active]);
    } else {
      $in = $pdo->prepare("INSERT INTO letters(title,slug,cover,body_html,is_active) VALUES(?,?,?,?,?)");
      $in->execute([$title,$slug,$cover,$body,$active]);
      header("Location: letter_form.php?edit=".(int)$pdo->lastInsertId());
      exit;
    }
  }
}
?>
<!doctype html>
<html lang="az">
<head>
  <meta charset="utf-8">
  <title><?= $editId>0 ? 'Məktub redaktə' : 'Yeni məktub' ?></title>
  <style>
    body{font-family:system-ui;margin:20px}
    label{display:block;margin:12px 0 6px}
    input,textarea{width:100%;padding:10px;border:1px solid #ccc;border-radius:10px}
    .btn{display:inline-block;padding:10px 14px;border:1px solid #111;text-decoration:none;border-radius:10px;background:#fff;cursor:pointer}
    .msg-ok{background:#e9fff0;border:1px solid #8be0a2;padding:10px;border-radius:10px;margin:10px 0}
    .msg-er{background:#fff1f1;border:1px solid #f3a4a4;padding:10px;border-radius:10px;margin:10px 0}
    .thumb{max-width:320px;border-radius:14px;border:1px solid #eee;margin-top:10px}
  </style>
</head>
<body>

<p><a class="btn" href="letters.php">← Siyahı</a></p>
<h2><?= $editId>0 ? 'Məktubu redaktə et' : 'Yeni məktub' ?></h2>

<?php if($success): ?><div class="msg-ok"><?= e($success) ?></div><?php endif; ?>
<?php if($error): ?><div class="msg-er"><?= e($error) ?></div><?php endif; ?>

<form method="post" enctype="multipart/form-data">
  <label>Başlıq</label>
  <input name="title" value="<?= e($item['title'] ?? '') ?>" required>

  <label>Slug (boş burax — özü düzəlsin)</label>
  <input name="slug" value="<?= e($item['slug'] ?? '') ?>">

  <label>Cover şəkil (jpg/png/webp)</label>
  <input type="file" name="cover" accept=".jpg,.jpeg,.png,.webp">
  <?php if(!empty($item['cover'])): ?>
    <img class="thumb" src="/v2/uploads/letters/<?= e($item['cover']) ?>" alt="">
  <?php endif; ?>

  <label>Mətn (HTML)</label>
  <textarea name="body_html" rows="10"><?= e($item['body_html'] ?? '') ?></textarea>

  <label style="display:flex;gap:10px;align-items:center;margin-top:12px">
    <input type="checkbox" name="is_active" value="1" <?= ((int)($item['is_active'] ?? 1)===1) ? 'checked' : '' ?> style="width:auto">
    Aktiv
  </label>

  <p style="margin-top:14px">
    <button class="btn" type="submit">Yadda saxla</button>
  </p>
</form>

</body>
</html>