<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$keys = [
    'company_name' => 'Company name',
    'company_tagline' => 'Tagline',
    'phone' => 'Phone (tel: link — digits preferred)',
    'phone_display' => 'Phone (display text)',
    'whatsapp_pricing_phone' => 'WhatsApp — home “Choose packages” (digits only, e.g. 919771249046)',
    'email' => 'Email address',
    'address' => 'Full address',
    'hours' => 'Business hours',
    'contact_intro' => 'Intro paragraph on contact page',
    'map_embed' => 'Google Maps embed URL (iframe src only)',
];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify($_POST['csrf'] ?? null)) {
    foreach ($keys as $k => $_) {
        $v = (string)($_POST[$k] ?? '');
        db()->prepare('INSERT INTO site_settings (setting_key, setting_value) VALUES (?,?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)')->execute([$k, $v]);
    }
    admin_flash_set('success', 'Contact settings saved.');
    header('Location: contact-settings.php');
    exit;
}
$page = 'contact';
$pageTitle = 'Contact & map';
$pageSubtitle = 'Details shown in the header, footer, offcanvas, and contact page. Form submissions go to Enquiries.';
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'Contact & map', 'href' => null]];
$headerActions = '<a href="../contact.php" class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right me-1"></i>Preview contact</a>'
    . '<a href="enquiries.php" class="btn btn-outline-secondary btn-sm"><i class="bi bi-inbox me-1"></i>Enquiries</a>';
require __DIR__ . '/includes/layout-header.php';
$vals = [];
foreach (array_keys($keys) as $k) {
    $def = $k === 'whatsapp_pricing_phone' ? '919771249046' : '';
    $vals[$k] = setting($k, $def);
}
?>
<form method="post">
  <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
  <div class="row g-4">
    <div class="col-lg-6">
      <div class="admin-card rounded mb-4">
        <div class="card-header">Brand</div>
        <div class="card-body">
          <?php foreach (['company_name', 'company_tagline'] as $k): ?>
            <div class="mb-3">
              <label class="form-label fw-semibold"><?= h($keys[$k]) ?></label>
              <input type="text" name="<?= h($k) ?>" class="form-control" value="<?= h($vals[$k]) ?>">
            </div>
          <?php endforeach; ?>
        </div>
      </div>
      <div class="admin-card rounded mb-4">
        <div class="card-header">Reachability</div>
        <div class="card-body">
          <?php foreach (['phone', 'phone_display', 'whatsapp_pricing_phone', 'email', 'hours'] as $k): ?>
            <div class="mb-3">
              <label class="form-label fw-semibold"><?= h($keys[$k]) ?></label>
              <?php if ($k === 'hours'): ?>
                <input type="text" name="hours" class="form-control" value="<?= h($vals['hours']) ?>">
              <?php elseif ($k === 'email'): ?>
                <input type="email" name="email" class="form-control" value="<?= h($vals['email']) ?>">
              <?php elseif ($k === 'whatsapp_pricing_phone'): ?>
                <input type="text" name="whatsapp_pricing_phone" class="form-control" value="<?= h($vals['whatsapp_pricing_phone'] ?? '') ?>" placeholder="919771249046">
                <div class="form-text">Pre-filled message includes the selected plan name, price, and feature list. Country code, no + sign.</div>
              <?php else: ?>
                <input type="text" name="<?= h($k) ?>" class="form-control" value="<?= h($vals[$k]) ?>">
              <?php endif; ?>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
    <div class="col-lg-6">
      <div class="admin-card rounded mb-4">
        <div class="card-header">Location & map</div>
        <div class="card-body">
          <div class="mb-3">
            <label class="form-label fw-semibold"><?= h($keys['address']) ?></label>
            <textarea name="address" class="form-control" rows="4"><?= h($vals['address']) ?></textarea>
          </div>
          <div class="mb-0">
            <label class="form-label fw-semibold"><?= h($keys['map_embed']) ?></label>
            <textarea name="map_embed" class="form-control font-monospace small" rows="3" placeholder="https://www.google.com/maps/embed?..."><?= h($vals['map_embed']) ?></textarea>
          </div>
        </div>
      </div>
      <div class="admin-card rounded mb-4">
        <div class="card-header">Contact page copy</div>
        <div class="card-body">
          <label class="form-label fw-semibold"><?= h($keys['contact_intro']) ?></label>
          <textarea name="contact_intro" class="form-control" rows="5"><?= h($vals['contact_intro']) ?></textarea>
        </div>
      </div>
      <button type="submit" class="btn btn-primary btn-lg w-100"><i class="bi bi-check-lg me-1"></i>Save contact settings</button>
    </div>
  </div>
</form>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
