<?php
// Halaman kontak.php (Kontak Kami)
$pageTitle = 'Kontak Kami';
$activePage = 'kontak';
require_once '../includes/header.php';

// Ambil data profil yayasan
$db = dbConnect();
$db->query("SELECT * FROM profil_yayasan LIMIT 1");
$yayasan = $db->single();

// Proses form kontak jika disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = sanitize($_POST['nama']);
    $email = sanitize($_POST['email']);
    $subjek = sanitize($_POST['subjek']);
    $pesan = sanitize($_POST['pesan']);
    
    // Validasi input
    $errors = [];
    
    if (empty($nama)) {
        $errors[] = "Nama harus diisi";
    }
?>

<!-- Hero Section Kecil -->
<section class="py-5 bg-light">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h1 class="fw-bold mb-4">Kontak Kami</h1>
                <p class="lead">Hubungi kami untuk informasi lebih lanjut atau jika Anda memiliki pertanyaan</p>
            </div>
        </div>
    </div>
</section>

<!-- Contact Info & Form Section -->
<section class="py-5">
    <div class="container">
        <div class="row">
            <!-- Contact Info -->
            <div class="col-lg-5 mb-4 mb-lg-0">
                <div class="card h-100">
                    <div class="card-body p-4">
                        <h3 class="fw-bold mb-4">Informasi Kontak</h3>
                        
                        <div class="d-flex mb-4">
                            <div class="flex-shrink-0">
                                <div class="icon-box bg-light rounded-circle d-flex align-items-center justify-content-center">
                                    <i class="fas fa-map-marker-alt text-success"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="fw-bold">Alamat</h5>
                                <p class="text-muted mb-0">
                                    <?= isset($yayasan) ? $yayasan->alamat : 'Jl. Contoh No. 123, Kota Contoh' ?>
                                </p>
                            </div>
                        </div>
                        
                        <div class="d-flex mb-4">
                            <div class="flex-shrink-0">
                                <div class="icon-box bg-light rounded-circle d-flex align-items-center justify-content-center">
                                    <i class="fas fa-phone-alt text-success"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="fw-bold">Telepon</h5>
                                <p class="text-muted mb-0">
                                    <?= isset($yayasan) ? $yayasan->no_telp : '081234567890' ?>
                                </p>
                            </div>
                        </div>
                        
                        <div class="d-flex mb-4">
                            <div class="flex-shrink-0">
                                <div class="icon-box bg-light rounded-circle d-flex align-items-center justify-content-center">
                                    <i class="fas fa-envelope text-success"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="fw-bold">Email</h5>
                                <p class="text-muted mb-0">
                                    <?= isset($yayasan) ? $yayasan->email : 'info@pundiamalmuliadonasi.com' ?>
                                </p>
                            </div>
                        </div>
                        
                        <div class="d-flex">
                            <div class="flex-shrink-0">
                                <div class="icon-box bg-light rounded-circle d-flex align-items-center justify-content-center">
                                    <i class="fas fa-clock text-success"></i>
                                </div>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="fw-bold">Jam Operasional</h5>
                                <p class="text-muted mb-0">
                                    Senin - Jumat: 08:00 - 16:00<br>
                                    Sabtu: 09:00 - 13:00<br>
                                    Minggu: Tutup
                                </p>
                            </div>
                        </div>
                        
                        <div class="mt-4">
                            <h5 class="fw-bold mb-3">Ikuti Kami</h5>
                            <div class="social-links">
                                <a href="#" class="me-3"><i class="fab fa-facebook-f fa-lg text-success"></i></a>
                                <a href="#" class="me-3"><i class="fab fa-twitter fa-lg text-success"></i></a>
                                <a href="#" class="me-3"><i class="fab fa-instagram fa-lg text-success"></i></a>
                                <a href="#"><i class="fab fa-youtube fa-lg text-success"></i></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Contact Form -->
            <div class="col-lg-7">
                <div class="card">
                    <div class="card-body p-4">
                        <h3 class="fw-bold mb-4">Kirim Pesan</h3>
                        
                        <?php if (!empty($errors)): ?>
                            <div class="alert alert-danger">
                                <ul class="mb-0">
                                    <?php foreach ($errors as $error): ?>
                                        <li><?= $error ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php endif; ?>
                        
                        <form method="post" action="" class="contact-form">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="nama" class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="nama" name="nama" value="<?= isset($nama) ? $nama : '' ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="email" class="form-label">Email <span class="text-danger">*</span></label>
                                    <input type="email" class="form-control" id="email" name="email" value="<?= isset($email) ? $email : '' ?>" required>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="subjek" class="form-label">Subjek <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="subjek" name="subjek" value="<?= isset($subjek) ? $subjek : '' ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="pesan" class="form-label">Pesan <span class="text-danger">*</span></label>
                                <textarea class="form-control" id="pesan" name="pesan" rows="5" required><?= isset($pesan) ? $pesan : '' ?></textarea>
                            </div>
                            
                            <div class="d-grid">
                                <button type="submit" class="btn btn-success btn-lg">Kirim Pesan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Google Maps Section -->
<section class="py-5 bg-light">
    <div class="container">
        <div class="row">
            <div class="col-12">
                <div class="map-container">
                    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d126748.56347862248!2d106.6894333118296!3d-6.2269984005773025!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x2e69f3e945e34b9d%3A0x5371bf0fdad786a2!2sJakarta%2C%20Daerah%20Khusus%20Ibukota%20Jakarta!5e0!3m2!1sid!2sid!4v1683567273271!5m2!1sid!2sid" 
                            width="100%" height="450" style="border:0; border-radius: 12px;" allowfullscreen="" loading="lazy" 
                            referrerpolicy="no-referrer-when-downgrade"></iframe>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- CSS untuk Style Tambahan -->
<style>
    .icon-box {
        width: 50px;
        height: 50px;
        font-size: 1.25rem;
    }
    
    .map-container {
        overflow: hidden;
        border-radius: 12px;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
    }
</style>

<?php require_once '../includes/footer.php'; ?>
    
    if (empty($email)) {
        $errors[] = "Email harus diisi";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Format email tidak valid";
    }
    
    if (empty($subjek)) {
        $errors[] = "Subjek harus diisi";
    }
    
    if (empty($pesan)) {
        $errors[] = "Pesan harus diisi";
    }
    
    // Jika tidak ada error, simpan ke database
    if (empty($errors)) {
        // Cek apakah tabel kontak sudah ada
        $db->query("SHOW TABLES LIKE 'kontak'");
        $tableExists = $db->rowCount() > 0;
        
        // Jika tabel belum ada, buat dulu
        if (!$tableExists) {
            $db->query("CREATE TABLE kontak (
                id INT AUTO_INCREMENT PRIMARY KEY,
                nama VARCHAR(255) NOT NULL,
                email VARCHAR(255) NOT NULL,
                subjek VARCHAR(255) NOT NULL,
                pesan TEXT NOT NULL,
                is_read BOOLEAN DEFAULT FALSE,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )");
            $db->execute();
        }
        
        // Simpan pesan kontak
        $db->query("INSERT INTO kontak (nama, email, subjek, pesan) VALUES (:nama, :email, :subjek, :pesan)");
        $db->bind(':nama', $nama);
        $db->bind(':email', $email);
        $db->bind(':subjek', $subjek);
        $db->bind(':pesan', $pesan);
        
        if ($db->execute()) {
            // Kirim notifikasi ke admin/sekretaris
            // Cari ID sekretaris
            $db->query("SELECT id FROM users WHERE role = 'sekretaris' LIMIT 1");
            $sekretaris = $db->single();
            
            if ($sekretaris) {
                createNotification(
                    $db, 
                    $sekretaris->id, 
                    'Pesan Kontak Baru', 
                    "Ada pesan kontak baru dari $nama dengan subjek: $subjek", 
                    SITE_URL . '/sekretaris/kontak/index.php'
                );
            }
            
            setFlash('success', 'Pesan Anda berhasil dikirim. Kami akan menghubungi Anda segera.', 'success');
            redirect(SITE_URL . '/pages/kontak.php');
            exit;
        } else {
            $errors[] = "Terjadi kesalahan. Pesan gagal dikirim.";
        }
    }