<?php
// Pastikan sesi dimulai untuk mengakses user_id
session_start();

// Redirect jika belum login
if (!isset($_SESSION['user_id'])) {
    header('Location: ../auth/login.php'); // Sesuaikan path ke halaman login Anda
    exit();
}

require_once '../api/db_connect.php'; // Sesuaikan path ke file db_connect.php Anda

$userProfile = null;

try {
    $userId = $_SESSION['user_id'];

    // Query untuk mengambil data profil berdasarkan user_id
    $stmt = $pdo->prepare("SELECT full_name, phone_number, nik, profile_picture_url FROM users WHERE id = :id");
    $stmt->execute([':id' => $userId]);
    $userProfile = $stmt->fetch(PDO::FETCH_ASSOC);

    // Jika userProfile kosong, kemungkinan user_id di sesi tidak valid atau data tidak ditemukan
    if (!$userProfile) {
        // Hapus sesi dan redirect ke login
        session_unset();
        session_destroy();
        header('Location: ../auth/login.php?error=profil_not_found');
        exit();
    }

} catch (PDOException $e) {
    error_log("Error fetching profile data: " . $e->getMessage());
    // Untuk keamanan, jangan tampilkan error database langsung ke user
    // Anda bisa redirect ke halaman error atau tampilkan pesan generik
    echo "Terjadi kesalahan saat memuat data profil. Silakan coba lagi nanti.";
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil Pengguna</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    
    <link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">

    <link rel="stylesheet" href="../css/style.css"> 
    <style>
        /* CSS untuk menyembunyikan placeholder icon saat gambar profil ada */
        .profile-pic-preview {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 50%;
        }
        .profile-main-icon.hidden {
            display: none;
        }
        .profile-pic-preview.hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="page-header">
        <h1>Profil</h1>
    </div>

    <div class="main-content-area profile-page-content">
        <div class="profile-main-card">
            <div class="profile-icon-wrapper">
                <img id="profilePicMain" src="" alt="Foto Profil" class="profile-pic-preview <?php echo $userProfile['profile_picture_url'] ? '' : 'hidden'; ?>">
                <i class='bx bx-user-circle profile-main-icon <?php echo $userProfile['profile_picture_url'] ? 'hidden' : ''; ?>'></i>
                <span class="badge-icon"><i class='bx bxs-check-shield'></i></span>
            </div>
            <div class="user-details">
                <p class="user-name" id="displayFullName"><?php echo htmlspecialchars($userProfile['full_name'] ?? 'Nama Pengguna'); ?></p>
                <p class="user-contact"><i class='bx bx-phone'></i> <span id="displayPhoneNumber"><?php echo htmlspecialchars($userProfile['phone_number'] ?? 'N/A'); ?></span></p>
                <p class="user-id"><i class='bx bx-id-card'></i> <span id="displayNIK"><?php echo htmlspecialchars($userProfile['nik'] ?? 'N/A'); ?></span></p>
            </div>
            <a href="editprofile/" class="edit-profile-btn">
                <i class='bx bx-edit-alt'></i>
            </a>
        </div>

        <a href="#" class="profile-option-item">
            <div class="option-left">
                <i class='bx bx-qr-scan option-icon'></i>
                <span>Kode Identitas</span>
            </div>
            <i class='bx bx-chevron-right'></i>
        </a>

        <div class="vaccine-card">
            <div class="vaccine-info">
                <p class="vaccine-title">Kartu Status Vaksin dan Hasil Tes Covid-19</p>
                <p class="vaccine-name" id="vaccineFullName"><?php echo htmlspecialchars($userProfile['full_name'] ?? 'Nama Pengguna'); ?></p>
            </div>
            <a href="#" class="vaccine-arrow">
                <i class='bx bx-plus-medical'></i>
            </a>
        </div>

        <h3 class="section-title">Informasi Umum</h3>
        <div class="info-list">
            <div class="profile-option-container">
                <a href="#profileLinkedCollapse" class="profile-option-item" data-bs-toggle="collapse" role="button" aria-expanded="false" aria-controls="profileLinkedCollapse">
                    <div class="option-left">
                        <i class='bx bx-link-alt profile-icon-green'></i> <span>Profil Tertaut</span>
                    </div>
                    <i class='bx bx-chevron-down toggle-icon'></i>
                </a>
                <div class="collapse help-center-description" id="profileLinkedCollapse">
                    <div class="description-text">
                        Anda dapat menautkan profil ini ke akun lain untuk sinkronisasi data kesehatan.
                    </div>
                </div>
            </div>
            <div class="profile-option-container">
                <a href="#helpCenterCollapse" class="profile-option-item" data-bs-toggle="collapse" role="button" aria-expanded="false" aria-controls="helpCenterCollapse">
                    <div class="option-left">
                        <i class='bx bx-help-circle profile-icon'></i> <span>Pusat Bantuan</span>
                    </div>
                    <i class='bx bx-chevron-down toggle-icon'></i>
                </a>
                <div class="collapse help-center-description" id="helpCenterCollapse">
                    <div class="description-text">
                        Jika Anda membutuhkan bantuan lebih lanjut, silakan hubungi kami melalui WhatsApp.
                        <a href="https://wa.me/6281234567890" target="_blank" class="whatsapp-link">
                            <i class='bx bxl-whatsapp'></i> Hubungi Kami
                        </a>
                    </div>
                </div>
            </div>
            <div class="profile-option-container">
                <a href="#emergencyNumberCollapse" class="profile-option-item" data-bs-toggle="collapse" role="button" aria-expanded="false" aria-controls="emergencyNumberCollapse">
                    <div class="option-left">
                        <i class='bx bx-phone-call profile-icon-orange'></i> <span>Nomor Gawat Darurat</span>
                    </div>
                    <i class='bx bx-chevron-down toggle-icon'></i>
                </a>
                <div class="collapse help-center-description" id="emergencyNumberCollapse">
                    <div class="description-text">
                        <p>Daftar nomor darurat yang bisa Anda hubungi:</p>
                        <ul>
                            <li>Ambulans: 118</li>
                            <li>Polisi: 110</li>
                            <li>Pemadam Kebakaran: 113</li>
                            <li>BNPB: 117</li>
                        </ul>
                        <p>Gunakan hanya dalam keadaan darurat!</p>
                    </div>
                </div>
            </div>
            <div class="profile-option-container">
                <a href="#aboutCollapse" class="profile-option-item" data-bs-toggle="collapse" role="button" aria-expanded="false" aria-controls="aboutCollapse">
                    <div class="option-left">
                        <i class='bx bx-info-circle profile-icon-purple'></i> <span>Tentang</span>
                    </div>
                    <i class='bx bx-chevron-down toggle-icon'></i>
                </a>
                <div class="collapse help-center-description" id="aboutCollapse">
                    <div class="description-text">
                        <p>Aplikasi ini dikembangkan untuk memudahkan pengguna dalam mengakses layanan kesehatan dan informasi terkait profil kesehatan.</p>
                        <p>Versi Aplikasi: 1.0.0</p>
                        <p>Hak Cipta &copy; 2025 Lapor Kesehatan.</p>
                    </div>
                </div>
            </div>
            <div class="profile-option-container">
                <a href="../logout.php" class="profile-option-item logout-option-item">
                    <div class="option-left">
                        <i class='bx bx-log-out option-icon-red'></i> <span>Logout</span>
                    </div>
                    <i class='bx bx-chevron-right'></i>
                </a>
            </div>
        </div>
    </div>

    <nav class="nav1">
    <ul class="nav-content1">
        <li class="nav-list1">
            <a href="../" class="link-item1"> <i class='bx bx-home-alt link-icon1'></i> <span class="link-text1">Beranda</span>
            </a>
        </li>
        <li class="nav-list1">
            <a href="../dataperkembangan/" class="link-item1">
                <i class='bx bx-category link-icon1'></i> <span class="link-text1">Fitur</span>
            </a>
        </li>
        <li class="nav-list1">
            <a href="../jadwalposyandu/" class="link-item1">
                <i class='bx bx-chart link-icon1'></i> <span class="link-text1">Jadwal Posyandu</span>
            </a>
        </li>
        <li class="nav-list1">
            <a href="../laporkehamilan/" class="link-item1">
                <i class='bx bx-chat link-icon1'></i> <span class="link-text1">Lapor Kehamilan</span>
            </a>
        </li>
         <li class="nav-list1">
            <a href="../profil/" class="link-item1 active1">
                <i class='bx bx-user-circle link-icon1'></i> <span class="link-text1">Profil</span>
            </a>
        </li>
    </ul>
</nav>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
    <script src="../js/script.js"></script>
</body>
</html>