<?php
require_once __DIR__ . '/auth_guard.php';
require_once __DIR__ . '/../backend/config/database.php';
require_once __DIR__ . '/../backend/includes/site_settings.php';

$db = getDB();
ensureSiteSettingsTable($db);
$adminUserName = $_SESSION['user_name'];
$adminPage = 'settings';
$adminPageTitle = 'Site Settings';

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_settings'])) {
    $phone = trim($_POST['contact_phone'] ?? '');
    $phone2 = trim($_POST['contact_phone_2'] ?? '');
    $email = trim($_POST['contact_email'] ?? '');
    $location = trim($_POST['contact_location'] ?? '');
    $hours = trim($_POST['contact_hours'] ?? '');

    if (!$phone || !$phone2 || !$email || !$location || !$hours) {
        $error = 'All fields are required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        $stmt = $db->prepare('INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)');
        $stmt->execute(['contact_phone', $phone]);
        $stmt->execute(['contact_phone_2', $phone2]);
        $stmt->execute(['contact_email', $email]);
        $stmt->execute(['contact_location', $location]);
        $stmt->execute(['contact_hours', $hours]);
        $message = 'Settings updated successfully.';
    }
}

$settings = getSiteSettings();
?>
<?php require_once __DIR__ . '/layout_top.php'; ?>

<div style="max-width:920px;display:grid;grid-template-columns:1fr;gap:1.25rem">
  <div style="background:#fff;border:1px solid #e2e8f0;border-radius:18px;padding:1.25rem 1.25rem .95rem">
    <h2 style="margin:0 0 .45rem;font-size:1.05rem;font-weight:800;color:#0f172a">Public Contact Details</h2>
    <p style="margin:0;color:#64748b;font-size:.86rem;line-height:1.6">
      Values here are used in all page footers and the home page contact CTA.
    </p>
  </div>

  <?php if ($message): ?>
    <div style="background:#f0fdf4;border:1px solid #bbf7d0;color:#166534;padding:.75rem 1rem;border-radius:12px;font-size:.86rem;font-weight:700"><?= htmlspecialchars($message) ?></div>
  <?php endif; ?>

  <?php if ($error): ?>
    <div style="background:#fef2f2;border:1px solid #fecaca;color:#b91c1c;padding:.75rem 1rem;border-radius:12px;font-size:.86rem;font-weight:700"><?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <form method="POST" style="background:#fff;border:1px solid #e2e8f0;border-radius:18px;padding:1.25rem">
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:1rem" class="settings-grid">
      <div>
        <label style="display:block;color:#475569;font-weight:700;font-size:.76rem;letter-spacing:.05em;text-transform:uppercase;margin-bottom:.45rem">Contact Number 1</label>
        <input type="text" name="contact_phone" value="<?= htmlspecialchars($_POST['contact_phone'] ?? $settings['contact_phone']) ?>" required style="width:100%;padding:.68rem .85rem;border:2px solid #e2e8f0;border-radius:12px;font:inherit;outline:none">
      </div>
      <div>
        <label style="display:block;color:#475569;font-weight:700;font-size:.76rem;letter-spacing:.05em;text-transform:uppercase;margin-bottom:.45rem">Contact Number 2</label>
        <input type="text" name="contact_phone_2" value="<?= htmlspecialchars($_POST['contact_phone_2'] ?? $settings['contact_phone_2']) ?>" required style="width:100%;padding:.68rem .85rem;border:2px solid #e2e8f0;border-radius:12px;font:inherit;outline:none">
      </div>
    </div>

    <div style="margin-top:1rem">
      <label style="display:block;color:#475569;font-weight:700;font-size:.76rem;letter-spacing:.05em;text-transform:uppercase;margin-bottom:.45rem">Contact Email</label>
      <input type="email" name="contact_email" value="<?= htmlspecialchars($_POST['contact_email'] ?? $settings['contact_email']) ?>" required style="width:100%;padding:.68rem .85rem;border:2px solid #e2e8f0;border-radius:12px;font:inherit;outline:none">
    </div>

    <div style="margin-top:1rem">
      <label style="display:block;color:#475569;font-weight:700;font-size:.76rem;letter-spacing:.05em;text-transform:uppercase;margin-bottom:.45rem">Location</label>
      <input type="text" name="contact_location" value="<?= htmlspecialchars($_POST['contact_location'] ?? $settings['contact_location']) ?>" required style="width:100%;padding:.68rem .85rem;border:2px solid #e2e8f0;border-radius:12px;font:inherit;outline:none">
    </div>

    <div style="margin-top:1rem">
      <label style="display:block;color:#475569;font-weight:700;font-size:.76rem;letter-spacing:.05em;text-transform:uppercase;margin-bottom:.45rem">Business Hours</label>
      <input type="text" name="contact_hours" value="<?= htmlspecialchars($_POST['contact_hours'] ?? $settings['contact_hours']) ?>" required style="width:100%;padding:.68rem .85rem;border:2px solid #e2e8f0;border-radius:12px;font:inherit;outline:none">
    </div>

    <div style="display:flex;justify-content:flex-end;margin-top:1.2rem">
      <button type="submit" name="save_settings" value="1" style="background:linear-gradient(135deg,#f97316,#ea580c);color:#fff;border:none;border-radius:12px;padding:.72rem 1.15rem;font-weight:800;cursor:pointer">Save Changes</button>
    </div>
  </form>
</div>

<style>
@media(max-width:760px){ .settings-grid{ grid-template-columns:1fr !important; } }
</style>

<?php require_once __DIR__ . '/layout_bottom.php'; ?>
