<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Contact us';
$success = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $message = trim($_POST['message'] ?? '');
    if (!$name || !$email || !$message) {
        $error = 'Please fill in Name, Email and Message.';
    } else {
        $pdo = getDB();
        $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, phone, message) VALUES (?, ?, ?, ?)");
        $stmt->execute([$name, $email, $phone, $message]);
        if (file_exists(__DIR__ . '/includes/smtp_mail.php')) {
            require_once __DIR__ . '/includes/smtp_mail.php';
            notifyEnquiryOrContact('contact', [
                'name' => $name, 'email' => $email, 'phone' => $phone, 'message' => $message
            ]);
        }
        $success = true;
    }
}

$assetBase = (BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';

// Your details (override with admin settings when set)
// Contact page content — editable in Admin → Settings (Contact page section)
$companyName = getSetting('contact_company_name', 'Crimson Uni Quepack Pvt. Ltd.');
$addressLine1 = getSetting('contact_address_line1', 'Plot No: 117, Sector 6A, SIDCUL');
$addressLine2 = getSetting('contact_address_line2', 'Haridwar- 249403 (UK)');
$emailSetting = getSetting('contact_email', 'crimsonhwr@gmail.com');
$phone1 = getSetting('contact_phone', '+91-9319059893');
$phone2 = getSetting('contact_phone2', '+91-7906230351');
$defaultMapEmbed = 'https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2731.9732705036477!2d78.0565395755506!3d29.96221667496511!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x390949c983bc87d7%3A0x18ca17005e56ebd0!2sCrimson%20Corrugated%20Boxes!5e1!3m2!1sen!2sin!4v1773329489628!5m2!1sen!2sin';
$defaultMapLink = 'https://maps.app.goo.gl/zDottMd3CiJUJ3e2A';
$mapEmbedUrl = getSetting('contact_map_embed_url', $defaultMapEmbed);
$mapLinkUrl = getSetting('contact_map_link_url', $defaultMapLink);
if ($mapEmbedUrl === '') $mapEmbedUrl = $defaultMapEmbed;
if ($mapLinkUrl === '') $mapLinkUrl = $defaultMapLink;

require_once __DIR__ . '/includes/header.php';
?>
<div class="contact-page-wrap">
    <header class="contact-page-header">
        <div class="container contact-page-header-inner">
            <span class="contact-page-eyebrow">Reach us</span>
            <h1 class="contact-page-title">Contact us</h1>
            <p class="contact-page-lead">Visit us at SIDCUL Haridwar, call, or write—we’re happy to help with packaging needs.</p>
        </div>
    </header>

    <div class="page-content contact-page">
        <div class="container contact-page-inner">
            <div class="contact-map-section">
                <h2 class="contact-section-title">Location</h2>
                <div class="contact-map-frame">
                    <iframe
                        title="Crimson Uni Quepack - Haridwar"
                        src="<?php echo htmlspecialchars($mapEmbedUrl); ?>"
                        width="100%"
                        height="380"
                        style="border:0;"
                        allowfullscreen=""
                        loading="lazy"
                        referrerpolicy="no-referrer-when-downgrade"></iframe>
                </div>
                <p class="contact-map-caption">
                    <a href="<?php echo htmlspecialchars($mapLinkUrl); ?>" target="_blank" rel="noopener">Open in Google Maps</a>
                </p>
            </div>

            <div class="contact-details-form-row">
                <section class="contact-details-block" aria-labelledby="contact-address-heading">
                    <h2 id="contact-address-heading" class="contact-address-heading">Address</h2>
                    <ol class="contact-timeline">
                        <li class="contact-timeline-item">
                            <span class="contact-timeline-icon" aria-hidden="true">
                                <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>
                            </span>
                            <div class="contact-timeline-body">
                                <h3 class="contact-timeline-title">Company name</h3>
                                <p class="contact-timeline-text contact-timeline-text--link"><?php echo htmlspecialchars($companyName); ?></p>
                            </div>
                        </li>
                        <li class="contact-timeline-item">
                            <span class="contact-timeline-icon" aria-hidden="true">
                                <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
                            </span>
                            <div class="contact-timeline-body">
                                <h3 class="contact-timeline-title">Address</h3>
                                <p class="contact-timeline-text"><?php echo htmlspecialchars($addressLine1); ?><br><?php echo htmlspecialchars($addressLine2); ?></p>
                            </div>
                        </li>
                        <li class="contact-timeline-item">
                            <span class="contact-timeline-icon" aria-hidden="true">
                                <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z"/></svg>
                            </span>
                            <div class="contact-timeline-body">
                                <h3 class="contact-timeline-title">E-mail ID</h3>
                                <p class="contact-timeline-text">
                                    <a href="mailto:<?php echo htmlspecialchars($emailSetting); ?>"><?php echo htmlspecialchars($emailSetting); ?></a>
                                </p>
                            </div>
                        </li>
                        <li class="contact-timeline-item">
                            <span class="contact-timeline-icon" aria-hidden="true">
                                <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/></svg>
                            </span>
                            <div class="contact-timeline-body">
                                <h3 class="contact-timeline-title">Mob. no.</h3>
                                <p class="contact-timeline-text">
                                    <?php if ($phone1): ?><a href="tel:<?php echo preg_replace('/[^0-9+]/', '', $phone1); ?>"><?php echo htmlspecialchars($phone1); ?></a><?php endif; ?>
                                    <?php if ($phone1 && $phone2): ?><br><?php endif; ?>
                                    <?php if ($phone2): ?><a href="tel:<?php echo preg_replace('/[^0-9+]/', '', $phone2); ?>"><?php echo htmlspecialchars($phone2); ?></a><?php endif; ?>
                                </p>
                            </div>
                        </li>
                    </ol>
                </section>

                <div class="contact-form-card contact-form-card--standalone">
                    <?php if ($success): ?>
                    <div class="contact-success">
                        <span class="contact-success-icon">✓</span>
                        <h3>Message sent</h3>
                        <p>Thank you. We’ll get back to you soon.</p>
                        <a href="<?php echo htmlspecialchars($assetBase); ?>index.php" class="btn-glow btn-primary-modern">Home</a>
                    </div>
                    <?php else: ?>
                    <h3 class="contact-form-card-title">Send a message</h3>
                    <?php if ($error): ?>
                    <div class="enquiry-alert enquiry-alert--error"><?php echo htmlspecialchars($error); ?></div>
                    <?php endif; ?>
                    <form method="post" class="enquiry-form-modern contact-form-modern">
                        <div class="enquiry-field">
                            <label for="c-name">Name <span class="req">*</span></label>
                            <input id="c-name" type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>" placeholder="Your name" autocomplete="name">
                        </div>
                        <div class="enquiry-field">
                            <label for="c-email">Email <span class="req">*</span></label>
                            <input id="c-email" type="email" name="email" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" placeholder="you@email.com" autocomplete="email">
                        </div>
                        <div class="enquiry-field enquiry-field--full">
                            <label for="c-phone">Phone</label>
                            <input id="c-phone" type="tel" name="phone" value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>" placeholder="Optional">
                        </div>
                        <div class="enquiry-field enquiry-field--full">
                            <label for="c-message">Message <span class="req">*</span></label>
                            <textarea id="c-message" name="message" rows="4" required placeholder="How can we help?"><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea>
                        </div>
                        <button type="submit" class="btn-glow btn-primary-modern enquiry-submit">Send message</button>
                    </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
