<?php
require_once __DIR__ . '/../includes/auth.php';
require_login();
$pageTitle = 'About Page & Chairman Message';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify()) {
    $stmt = db()->prepare('SELECT * FROM about_content WHERE id = 1');
    $stmt->execute();
    $existing = $stmt->fetch() ?: [];

    $data = [
        'banner_title' => trim($_POST['banner_title'] ?? ''),
        'banner_subtitle' => trim($_POST['banner_subtitle'] ?? ''),
        'company_profile_title' => trim($_POST['company_profile_title'] ?? ''),
        'company_profile_body' => trim($_POST['company_profile_body'] ?? ''),
        'mission' => trim($_POST['mission'] ?? ''),
        'vision' => trim($_POST['vision'] ?? ''),
        'chairman_name' => trim($_POST['chairman_name'] ?? ''),
        'chairman_title' => trim($_POST['chairman_title'] ?? ''),
        'chairman_message' => trim($_POST['chairman_message'] ?? ''),
        'seo_title' => trim($_POST['seo_title'] ?? ''),
        'seo_description' => trim($_POST['seo_description'] ?? ''),
    ];
    $data['company_profile_image'] = handle_image_upload('company_profile_image', 'general', $existing['company_profile_image'] ?? null);
    $data['chairman_photo'] = handle_image_upload('chairman_photo', 'general', $existing['chairman_photo'] ?? null);

    $set = implode(', ', array_map(fn($k) => "$k = :$k", array_keys($data)));
    $stmt = db()->prepare("UPDATE about_content SET $set WHERE id = 1");
    $stmt->execute($data);

    flash_set('success', 'About page content updated.');
    redirect('about.php');
}

$stmt = db()->prepare('SELECT * FROM about_content WHERE id = 1');
$stmt->execute();
$about = $stmt->fetch() ?: [];

require __DIR__ . '/partials/header.php';
require __DIR__ . '/partials/sidebar.php';
require __DIR__ . '/partials/topbar.php';
?>

<form method="post" enctype="multipart/form-data" class="space-y-6">
  <?= csrf_field() ?>

  <div class="bg-white rounded-2xl shadow-sm p-6">
    <h2 class="font-heading font-bold text-lg mb-5"><i class="fa-solid fa-flag text-brand-red mr-2"></i>Page Banner</h2>
    <div class="grid gap-5">
      <div><label class="block text-sm font-medium mb-1.5">Banner Title</label>
        <input type="text" name="banner_title" value="<?= e($about['banner_title'] ?? '') ?>" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"></div>
      <div><label class="block text-sm font-medium mb-1.5">Banner Subtitle</label>
        <textarea name="banner_subtitle" rows="2" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['banner_subtitle'] ?? '') ?></textarea></div>
    </div>
  </div>

  <div class="bg-white rounded-2xl shadow-sm p-6">
    <h2 class="font-heading font-bold text-lg mb-5"><i class="fa-solid fa-building text-brand-green mr-2"></i>Company Profile</h2>
    <div class="grid gap-5">
      <div><label class="block text-sm font-medium mb-1.5">Section Title</label>
        <input type="text" name="company_profile_title" value="<?= e($about['company_profile_title'] ?? '') ?>" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"></div>
      <div><label class="block text-sm font-medium mb-1.5">Company Profile (use blank lines to separate paragraphs)</label>
        <textarea name="company_profile_body" rows="6" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['company_profile_body'] ?? '') ?></textarea></div>
      <div>
        <label class="block text-sm font-medium mb-1.5">Profile Image</label>
        <?php if (!empty($about['company_profile_image'])): ?><img src="<?= e(resolve_image($about['company_profile_image'])) ?>" class="h-24 rounded-lg mb-2 border"><?php endif; ?>
        <input type="file" name="company_profile_image" accept="image/*" class="w-full text-sm">
      </div>
    </div>
  </div>

  <div class="bg-white rounded-2xl shadow-sm p-6">
    <h2 class="font-heading font-bold text-lg mb-5"><i class="fa-solid fa-bullseye text-brand-gold mr-2"></i>Mission &amp; Vision</h2>
    <div class="grid md:grid-cols-2 gap-5">
      <div><label class="block text-sm font-medium mb-1.5">Mission</label>
        <textarea name="mission" rows="3" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['mission'] ?? '') ?></textarea></div>
      <div><label class="block text-sm font-medium mb-1.5">Vision</label>
        <textarea name="vision" rows="3" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['vision'] ?? '') ?></textarea></div>
    </div>
  </div>

  <div class="bg-white rounded-2xl shadow-sm p-6">
    <h2 class="font-heading font-bold text-lg mb-5"><i class="fa-solid fa-user-tie text-brand-red mr-2"></i>Chairman's Message</h2>
    <div class="grid gap-5">
      <div class="grid md:grid-cols-2 gap-5">
        <div><label class="block text-sm font-medium mb-1.5">Chairman Name</label>
          <input type="text" name="chairman_name" value="<?= e($about['chairman_name'] ?? '') ?>" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"></div>
        <div><label class="block text-sm font-medium mb-1.5">Title</label>
          <input type="text" name="chairman_title" value="<?= e($about['chairman_title'] ?? '') ?>" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"></div>
      </div>
      <div>
        <label class="block text-sm font-medium mb-1.5">Chairman Photo</label>
        <?php if (!empty($about['chairman_photo'])): ?><img src="<?= e(resolve_image($about['chairman_photo'])) ?>" class="h-24 rounded-lg mb-2 border"><?php endif; ?>
        <input type="file" name="chairman_photo" accept="image/*" class="w-full text-sm">
      </div>
      <div><label class="block text-sm font-medium mb-1.5">Message (use blank lines to separate paragraphs)</label>
        <textarea name="chairman_message" rows="10" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['chairman_message'] ?? '') ?></textarea></div>
    </div>
  </div>

  <div class="bg-white rounded-2xl shadow-sm p-6">
    <h2 class="font-heading font-bold text-lg mb-5"><i class="fa-solid fa-magnifying-glass text-brand-green mr-2"></i>Page SEO</h2>
    <div class="grid gap-5">
      <div><label class="block text-sm font-medium mb-1.5">SEO Title</label>
        <input type="text" name="seo_title" value="<?= e($about['seo_title'] ?? '') ?>" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"></div>
      <div><label class="block text-sm font-medium mb-1.5">SEO Description</label>
        <textarea name="seo_description" rows="2" class="w-full rounded-xl border border-gray-300 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-green"><?= e($about['seo_description'] ?? '') ?></textarea></div>
    </div>
  </div>

  <button type="submit" class="bg-gradient-to-r from-brand-red to-brand-green text-white font-semibold px-8 py-3 rounded-xl hover:brightness-110 transition">
    <i class="fa-solid fa-floppy-disk mr-2"></i>Save About Page
  </button>
</form>

<?php require __DIR__ . '/partials/footer.php'; ?>
