<?php
$page_title = "Hubungi Kami";
$page_desc = "Hubungi PT Namora Nexa Niaga untuk pemesanan batu alam, konsultasi proyek, atau permintaan sampel batu.";
require_once 'includes/header.php';

if (!isset($settings['menu_contact_visible']) || $settings['menu_contact_visible'] == 0) {
    header("Location: index.php");
    exit;
}

$alert_message = '';
$alert_type = '';

if (isset($_POST['send_message'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $subject = trim($_POST['subject']);
    $message_text = trim($_POST['message']);
    
    if (empty($name) || empty($email) || empty($message_text)) {
        $alert_message = "Nama, Email, dan Pesan wajib diisi.";
        $alert_type = "error";
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO messages (name, email, phone, subject, message) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$name, $email, $phone, $subject, $message_text]);
            
            $alert_message = "Pesan Anda telah berhasil dikirim! Tim sales kami akan segera menghubungi Anda.";
            $alert_type = "success";
            
            // Clear post values to empty form
            $_POST = array();
        } catch (Exception $e) {
            $alert_message = "Gagal mengirim pesan: " . $e->getMessage();
            $alert_type = "error";
        }
    }
}
?>

<!-- Page Header Banner -->
<section style="background: linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.8)), url('assets/images/hero_bg.jpg') no-repeat center center/cover; padding: 120px 0 60px; text-align: center; color: var(--text-light);">
    <div class="container">
        <h1 style="font-size: 42px; font-weight: 800; margin-bottom: 10px;">Hubungi Kami</h1>
        <p style="color: var(--primary); font-weight: 600; letter-spacing: 2px; text-transform: uppercase; font-size: 14px;">Hubungi Tim Kami</p>
    </div>
</section>

<!-- Contact Form & Info Section -->
<section class="bg-alt">
    <div class="container">
        
        <?php if (!empty($alert_message)): ?>
            <div class="alert alert-<?php echo $alert_type; ?>">
                <?php if ($alert_type == 'success'): ?>
                    <i class="fas fa-check-circle" style="font-size: 20px;"></i>
                <?php else: ?>
                    <i class="fas fa-exclamation-circle" style="font-size: 20px;"></i>
                <?php endif; ?>
                <div><?php echo $alert_message; ?></div>
            </div>
        <?php endif; ?>

        <div class="contact-grid">
            <!-- Contact Details Column -->
            <div class="contact-info">
                <div>
                    <span class="section-tag">INFORMASI KONTAK</span>
                    <h2 class="section-title">Mari Berdiskusi</h2>
                    <p style="color: var(--text-muted); margin-bottom: 10px;">Hubungi kami melalui nomor telepon, email, atau kunjungi langsung kantor kami untuk konsultasi sampel batu alam secara gratis.</p>
                </div>
                
                <div class="info-item">
                    <div class="info-icon"><i class="fas fa-map-marker-alt"></i></div>
                    <div class="info-details">
                        <h4>Alamat Kantor</h4>
                        <p><?php echo htmlspecialchars($settings['address']); ?></p>
                    </div>
                </div>
                
                <div class="info-item">
                    <div class="info-icon"><i class="fas fa-phone-alt"></i></div>
                    <div class="info-details">
                        <h4>Telepon & WhatsApp</h4>
                        <p><a href="tel:<?php echo str_replace(' ', '', $settings['phone']); ?>"><?php echo htmlspecialchars($settings['phone']); ?></a></p>
                        <p><a href="https://wa.me/<?php echo $settings['whatsapp_number']; ?>" target="_blank" rel="noopener noreferrer">Hubungi Sales (WA)</a></p>
                    </div>
                </div>
                
                <div class="info-item">
                    <div class="info-icon"><i class="fas fa-envelope"></i></div>
                    <div class="info-details">
                        <h4>Email</h4>
                        <p><a href="mailto:<?php echo $settings['email']; ?>"><?php echo htmlspecialchars($settings['email']); ?></a></p>
                    </div>
                </div>
            </div>
            
            <!-- Message Form Column -->
            <div class="contact-form">
                <h3 style="font-size: 22px; font-weight: 700; color: var(--accent); margin-bottom: 25px;">Kirim Pesan</h3>
                <form action="" method="POST">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="name">Nama Lengkap *</label>
                            <input type="text" id="name" name="name" class="form-control" placeholder="Nama Anda" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="email">Alamat Email *</label>
                            <input type="email" id="email" name="email" class="form-control" placeholder="nama@email.com" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label for="phone">Nomor HP / WA</label>
                            <input type="text" id="phone" name="phone" class="form-control" placeholder="Contoh: 0812..." value="<?php echo isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : ''; ?>">
                        </div>
                        <div class="form-group">
                            <label for="subject">Subjek</label>
                            <input type="text" id="subject" name="subject" class="form-control" placeholder="Kebutuhan Batu Alam" value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject']) : ''; ?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="message">Pesan Anda *</label>
                        <textarea id="message" name="message" class="form-control" placeholder="Tuliskan spesifikasi produk batu alam atau pertanyaan proyek Anda..." required><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''; ?></textarea>
                    </div>
                    <button type="submit" name="send_message" class="btn-primary" style="width: 100%; border: none; font-size: 16px; cursor: pointer;">Kirim Pesan Sekarang</button>
                </form>
            </div>
        </div>
        
        <!-- Google Map Section -->
        <?php if (!empty($settings['map_iframe'])): ?>
            <div class="map-container">
                <?php echo $settings['map_iframe']; ?>
            </div>
        <?php endif; ?>
        
    </div>
</section>

<?php require_once 'includes/footer.php'; ?>
