<?php
declare(strict_types=1);
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/functions.php';

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$prefillSubject = trim((string)($_GET['subject'] ?? ''));
$prefillMessage = trim((string)($_GET['message'] ?? ''));

$sent = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!csrf_verify($_POST['csrf'] ?? null)) {
        $error = 'Invalid session. Please refresh the page and try again.';
    } else {
        $name = trim((string)($_POST['name'] ?? ''));
        $email = trim((string)($_POST['email'] ?? ''));
        $phone = trim((string)($_POST['phone'] ?? ''));
        $subject = trim((string)($_POST['subject'] ?? ''));
        $message = trim((string)($_POST['message'] ?? ''));
        if ($name === '' || $email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error = 'Please enter your name and a valid email address.';
        } else {
            try {
                $st = db()->prepare(
                    'INSERT INTO enquiries (name, email, phone, subject, message, status) VALUES (?,?,?,?,?,?)'
                );
                $st->execute([$name, $email, $phone ?: null, $subject ?: null, $message ?: null, 'new']);
                $sent = true;
            } catch (Throwable $e) {
                $error = 'Could not save your enquiry. Please try again later or call us directly.';
            }
        }
    }
}

$pageTitle = 'Contact Us — ' . setting('company_name', SITE_NAME);
$current = 'contact';
$breadcrumbTitle = 'Contact Us';
require __DIR__ . '/includes/head.php';
require __DIR__ . '/includes/preloader.php';
require __DIR__ . '/includes/offcanvas.php';
require __DIR__ . '/includes/header.php';
require __DIR__ . '/includes/breadcrumb.php';

$mail = setting('email', '');
$addr = setting('address', '');
$ph = setting('phone_display', setting('phone', ''));
$map = setting('map_embed', '');
?>
  <main>
    <style>
      .contact-pro-card {
        background: #fff;
        border: 1px solid #e7edf5;
        border-radius: 14px;
        padding: 22px;
        height: 100%;
        transition: all 0.25s ease;
      }

      .contact-pro-card:hover {
        transform: translateY(-3px);
        border-color: var(--ot-theme-color);
        box-shadow: 0 10px 24px rgba(4, 34, 62, 0.08);
      }

      .contact-pro-icon {
        width: 54px;
        height: 54px;
        border-radius: 50%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background: rgba(232, 136, 46, 0.14);
        color: var(--ot-title-color);
        font-size: 20px;
      }

      .contact-pro-form {
        background: #ffffff;
        border: 1px solid #e7edf5;
        border-radius: 14px;
        box-shadow: 0 12px 28px rgba(4, 34, 62, 0.08);
      }

      .contact-pro-form .contact-input input,
      .contact-pro-form .contact-input textarea {
        background: #f7f9fc;
        border-radius: 10px;
      }
    </style>
    <section class="ot-contact-area ot-space ot-faq-inner-style">
      <div class="container">
        <div class="ot-title-area text-center mb-50">
          <span class="ot-sec-subtitle">Contact Us</span>
          <h3 class="ot-sec-title ot-text-anime-3">
            Let Us Build Your Solar Solution
          </h3>
          <p class="ot-sec-text mx-auto" style="max-width:760px;">
            Get in touch with Bhatt Enterprises for consultation, installation, and support.
            Our team helps you choose the right solar setup for your home or business.
          </p>
        </div>

        <?php if ($sent): ?>
          <div class="alert alert-success">Thank you. We have received your enquiry and will contact you soon.</div>
        <?php endif; ?>
        <?php if ($error): ?>
          <div class="alert alert-danger"><?= h($error) ?></div>
        <?php endif; ?>

        <div class="row g-4 mb-40">
          <div class="col-lg-4 col-md-6">
            <div class="contact-pro-card text-center">
              <span class="contact-pro-icon mb-20"><i class="fa-solid fa-phone"></i></span>
              <h3 class="ot-contact-info-title">Call Us</h3>
              <a href="tel:<?= h(preg_replace('/\s+/', '', (string)setting('phone', ''))) ?>" class="ot-contact-info"><?= h($ph) ?></a>
            </div>
          </div>
          <div class="col-lg-4 col-md-6">
            <div class="contact-pro-card text-center">
              <span class="contact-pro-icon mb-20"><i class="fa-solid fa-envelope"></i></span>
              <h3 class="ot-contact-info-title">Email Us</h3>
              <a href="mailto:<?= h($mail) ?>" class="ot-contact-info"><?= h($mail) ?></a>
            </div>
          </div>
          <div class="col-lg-4 col-md-12">
            <div class="contact-pro-card text-center">
              <span class="contact-pro-icon mb-20"><i class="fa-solid fa-location-dot"></i></span>
              <h3 class="ot-contact-info-title">Visit Office</h3>
              <p class="ot-contact-info mb-0"><?= h($addr) ?></p>
            </div>
          </div>
        </div>

        <div class="row g-4">
          <div class="col-xl-5 col-lg-5 mb-4 mb-lg-0">
            <div class="ot-contact-content">
              <h3 class="ot-sec-title mb-20">Get in touch</h3>
              <p class="ot-sec-text mb-25"><?= nl2br(h(setting('contact_intro', 'We are here to support your solar journey with the right guidance and reliable service.'))) ?></p>
              <div class="ot-contact-info-wrapp">
                <ul class="list-unstyled mb-0">
                  <li class="mb-15"><i class="fa-solid fa-circle-check text-success me-2"></i>Free consultation for rooftop solar systems</li>
                  <li class="mb-15"><i class="fa-solid fa-circle-check text-success me-2"></i>Residential and commercial installation support</li>
                  <li class="mb-15"><i class="fa-solid fa-circle-check text-success me-2"></i>Guidance on inverters, batteries, and maintenance</li>
                  <li><i class="fa-solid fa-clock text-success me-2"></i><?= h(setting('hours', 'Generally opens around 10:00 AM')) ?></li>
                </ul>
              </div>
            </div>
          </div>
          <div class="col-xl-7 col-lg-7">
            <div class="ot-contact-formbox2 contact-pro-form rounded p-4 p-md-5">
              <h3 class="ot-contact-title text-center text-sm-start mb-30">Send a message</h3>
              <form method="post" action="contact.php" novalidate>
                <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
                <div class="row gy-3">
                  <div class="col-md-6">
                    <div class="contact-input">
                      <input type="text" name="name" placeholder="Your name" required value="<?= h((string)($_POST['name'] ?? '')) ?>">
                      <span class="form-input-icon"><i class="flaticon-user-1"></i></span>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="contact-input">
                      <input type="email" name="email" placeholder="Your email" required value="<?= h((string)($_POST['email'] ?? '')) ?>">
                      <span class="form-input-icon"><i class="flaticon-open-email"></i></span>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="contact-input">
                      <input type="text" name="phone" placeholder="Phone (optional)" value="<?= h((string)($_POST['phone'] ?? '')) ?>">
                      <span class="form-input-icon"><i class="fa-solid fa-phone"></i></span>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="contact-input">
                      <input type="text" name="subject" placeholder="Subject (optional)" value="<?= h((string)($_POST['subject'] ?? $prefillSubject)) ?>">
                      <span class="form-input-icon"><i class="fa-solid fa-pen-to-square"></i></span>
                    </div>
                  </div>
                  <div class="col-12">
                    <div class="contact-input form-msg">
                      <textarea name="message" placeholder="Your message" rows="4"><?= h((string)($_POST['message'] ?? $prefillMessage)) ?></textarea>
                      <span class="form-input-icon"><i class="flaticon-pen"></i></span>
                    </div>
                  </div>
                </div>
                <button type="submit" class="ot-btn-primary rounded mt-30 w-100">Submit enquiry</button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </section>
    <?php if ($map !== ''): ?>
    <div class="ot-contact-maps-area mt-0">
      <div class="container-fluid p-0">
        <div class="ot-contact-maps">
          <iframe src="<?= h($map) ?>" style="border:0;width:100%;height:420px;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
        </div>
      </div>
    </div>
    <?php endif; ?>
  </main>
<?php require __DIR__ . '/includes/footer.php'; ?>
