<?php
require_once 'config/db.php';
$settings = getAllSettings($pdo);
$activePage = 'contact';
$pageTitle = 'Contact - '.($settings['site_name']??'');

$success = '';
$error   = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_message'])) {
    $name    = trim($_POST['name']    ?? '');
    $email   = trim($_POST['email']   ?? '');
    $phone   = trim($_POST['phone']   ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if ($name && $email && $subject && $message) {
        $stmt = $pdo->prepare("INSERT INTO contact_messages (name,email,phone,subject,message,created_at) VALUES (?,?,?,?,?,NOW())");
        if ($stmt->execute([$name,$email,$phone,$subject,$message])) {
            $success = 'Pesan Anda berhasil dikirim. Kami akan segera menghubungi Anda.';
        } else {
            $error = 'Terjadi kesalahan, silakan coba lagi.';
        }
    } else {
        $error = 'Mohon lengkapi semua field yang wajib diisi.';
    }
}

include 'partials/header.php';
?>

<div class="page-hero">
    <div class="container">
        <nav aria-label="breadcrumb"><ol class="breadcrumb mb-2"><li class="breadcrumb-item"><a href="index.php">Home</a></li><li class="breadcrumb-item active" data-en="Contact" data-id="Kontak">Contact</li></ol></nav>
        <h1 class="fw-bold mb-1 fs-2" data-en="Contact Us" data-id="Hubungi Kami">Hubungi Kami</h1>
        <p class="mb-0" style="color:rgba(255,255,255,.6);" data-en="Get in touch with our team" data-id="Hubungi tim kami">Hubungi tim kami</p>
    </div>
</div>

<section class="py-5 py-lg-6">
    <div class="container">
        <div class="row g-5 align-items-start">
            <!-- Contact Info -->
            <div class="col-lg-4 fade-up">
                <div class="section-header mb-4">
                    <span class="section-label" data-en="Info" data-id="Informasi">Informasi</span>
                    <h3 class="section-title" data-en="Our Information" data-id="Informasi Kami">Informasi Kami</h3>
                </div>

                <div class="contact-info-item">
                    <div class="contact-icon"><i class="bi bi-geo-alt"></i></div>
                    <div>
                        <strong data-en="Address" data-id="Alamat">Alamat</strong>
                        <p class="mb-0 text-muted"><?= nl2br(htmlspecialchars($settings['site_address']??'Jl. Contoh No. 123, Jakarta Selatan, Indonesia')) ?></p>
                    </div>
                </div>
                <div class="contact-info-item">
                    <div class="contact-icon"><i class="bi bi-telephone"></i></div>
                    <div>
                        <strong data-en="Phone" data-id="Telepon">Telepon</strong>
                        <p class="mb-0 text-muted"><?= htmlspecialchars($settings['site_phone']??'-') ?></p>
                    </div>
                </div>
                <div class="contact-info-item">
                    <div class="contact-icon"><i class="bi bi-envelope"></i></div>
                    <div>
                        <strong>Email</strong>
                        <p class="mb-0 text-muted"><?= htmlspecialchars($settings['site_email']??'-') ?></p>
                    </div>
                </div>
                <div class="contact-info-item">
                    <div class="contact-icon"><i class="bi bi-clock"></i></div>
                    <div>
                        <strong data-en="Working Hours" data-id="Jam Kerja">Jam Kerja</strong>
                        <p class="mb-0 text-muted" data-en="Mon–Sat: 08.00–17.00 WIB" data-id="Senin–Sabtu: 08.00–17.00 WIB">Senin–Sabtu: 08.00–17.00 WIB</p>
                    </div>
                </div>

                <div class="social-links mt-4">
                    <?php if (!empty($settings['social_instagram'])): ?>
                        <a href="https://instagram.com/<?= htmlspecialchars($settings['social_instagram']) ?>" target="_blank" class="social-link"><i class="bi bi-instagram"></i></a>
                    <?php endif; ?>
                    <?php if (!empty($settings['social_facebook'])): ?>
                        <a href="https://facebook.com/<?= htmlspecialchars($settings['social_facebook']) ?>" target="_blank" class="social-link"><i class="bi bi-facebook"></i></a>
                    <?php endif; ?>
                    <?php if (!empty($settings['social_youtube'])): ?>
                        <a href="https://youtube.com/<?= htmlspecialchars($settings['social_youtube']) ?>" target="_blank" class="social-link"><i class="bi bi-youtube"></i></a>
                    <?php endif; ?>
                    <?php if (!empty($settings['social_whatsapp'] ?? $settings['site_whatsapp'])): ?>
                        <a href="https://wa.me/<?= htmlspecialchars($settings['social_whatsapp']??$settings['site_whatsapp']) ?>" target="_blank" class="social-link"><i class="bi bi-whatsapp"></i></a>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Contact Form -->
            <div class="col-lg-8 fade-up">
                <div class="contact-form-wrap">
                    <div class="section-header mb-4">
                        <span class="section-label" data-en="Message" data-id="Pesan">Pesan</span>
                        <h3 class="section-title" data-en="Send Us a Message" data-id="Kirim Pesan ke Kami">Kirim Pesan ke Kami</h3>
                    </div>

                    <?php if ($success): ?><div class="alert alert-success"><?= htmlspecialchars($success) ?></div><?php endif; ?>
                    <?php if ($error): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>

                    <form method="POST" class="contact-form">
                        <div class="row g-3">
                            <div class="col-sm-6">
                                <label class="form-label" data-en="Full Name *" data-id="Nama Lengkap *">Nama Lengkap *</label>
                                <input type="text" name="name" class="form-control" required placeholder="John Doe"
                                       value="<?= htmlspecialchars($_POST['name']??'') ?>">
                            </div>
                            <div class="col-sm-6">
                                <label class="form-label" data-en="Email Address *" data-id="Alamat Email *">Alamat Email *</label>
                                <input type="email" name="email" class="form-control" required placeholder="email@example.com"
                                       value="<?= htmlspecialchars($_POST['email']??'') ?>">
                            </div>
                            <div class="col-sm-6">
                                <label class="form-label" data-en="Phone Number" data-id="Nomor Telepon">Nomor Telepon</label>
                                <input type="text" name="phone" class="form-control" placeholder="+62 8xx xxxx xxxx"
                                       value="<?= htmlspecialchars($_POST['phone']??'') ?>">
                            </div>
                            <div class="col-sm-6">
                                <label class="form-label" data-en="Subject *" data-id="Subjek *">Subjek *</label>
                                <select name="subject" class="form-select" required>
                                    <option value="" disabled <?= empty($_POST['subject'])?'selected':'' ?>>-- Pilih Subjek --</option>
                                    <option value="Pertanyaan Produk" <?= ($_POST['subject']??'')==='Pertanyaan Produk'?'selected':'' ?>>Pertanyaan Produk</option>
                                    <option value="Permintaan Penawaran" <?= ($_POST['subject']??'')==='Permintaan Penawaran'?'selected':'' ?>>Permintaan Penawaran</option>
                                    <option value="Kemitraan" <?= ($_POST['subject']??'')==='Kemitraan'?'selected':'' ?>>Kemitraan</option>
                                    <option value="Lainnya" <?= ($_POST['subject']??'')==='Lainnya'?'selected':'' ?>>Lainnya</option>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label" data-en="Message *" data-id="Pesan *">Pesan *</label>
                                <textarea name="message" class="form-control" rows="5" required
                                          placeholder="Tulis pesan Anda di sini..."><?= htmlspecialchars($_POST['message']??'') ?></textarea>
                            </div>
                            <div class="col-12">
                                <button type="submit" name="send_message" class="btn-submit">
                                    <i class="bi bi-send me-2"></i>
                                    <span data-en="Send Message" data-id="Kirim Pesan">Kirim Pesan</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Maps Section -->
<?php if (!empty($settings['maps_embed'])): ?>
<section class="pb-5">
    <div class="container">
        <div class="maps-container fade-up">
            <iframe src="<?= htmlspecialchars($settings['maps_embed']) ?>" width="100%" height="400" style="border:0;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
        </div>
    </div>
</section>
<?php endif; ?>

<?php include 'partials/footer.php'; ?>
