<?php
include '../config.php';

if (!isSiswa()) {
    redirect('../login.php');
}

$user_id = $_SESSION['user_id'];

// Ambil data profil siswa
$sql_siswa = "SELECT * FROM lms_users WHERE id = ?";
$stmt_siswa = $conn->prepare($sql_siswa);
$stmt_siswa->bind_param('i', $user_id);
$stmt_siswa->execute();
$result_siswa = $stmt_siswa->get_result();
$siswa = $result_siswa->fetch_assoc();

// Hitung statistik siswa
$sql_stats = "SELECT 
    COUNT(*) as total_lms_courses,
    AVG(progress) as avg_progress,
    SUM(CASE WHEN progress = 100 THEN 1 ELSE 0 END) as completed_lms_courses,
    (SELECT COUNT(*) FROM lms_quiz_attempts WHERE id_siswa = ?) as total_lms_quizzes
    FROM lms_enrollments 
    WHERE id_siswa = ? AND status = 'active'";
$stmt_stats = $conn->prepare($sql_stats);
$stmt_stats->bind_param('ii', $user_id, $user_id);
$stmt_stats->execute();
$stats = $stmt_stats->get_result()->fetch_assoc();

// Proses update profil
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_profile'])) {
    $nama_lengkap = sanitize($_POST['nama_lengkap']);
    $email = sanitize($_POST['email']);
    
    // Handle upload foto profil
    $foto_profil = $siswa['foto_profil'];
    if (isset($_FILES['foto_profil']) && $_FILES['foto_profil']['error'] === 0) {
        $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
        $file_type = $_FILES['foto_profil']['type'];
        
        if (in_array($file_type, $allowed_types)) {
            $upload_dir = '../uploads/profiles/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }
            
            $file_extension = pathinfo($_FILES['foto_profil']['name'], PATHINFO_EXTENSION);
            $new_filename = 'profile_' . $user_id . '_' . time() . '.' . $file_extension;
            $upload_path = $upload_dir . $new_filename;
            
            if (move_uploaded_file($_FILES['foto_profil']['tmp_name'], $upload_path)) {
                // Hapus foto lama jika ada
                if ($foto_profil && file_exists('../' . $foto_profil)) {
                    unlink('../' . $foto_profil);
                }
                $foto_profil = 'uploads/profiles/' . $new_filename;
            }
        }
    }
    
    // Update data profil
    $sql_update = "UPDATE lms_users SET nama_lengkap = ?, email = ?, foto_profil = ? WHERE id = ?";
    $stmt_update = $conn->prepare($sql_update);
    $stmt_update->bind_param('sssi', $nama_lengkap, $email, $foto_profil, $user_id);
    
    if ($stmt_update->execute()) {
        $_SESSION['success'] = "Profil berhasil diperbarui!";
        $_SESSION['nama_lengkap'] = $nama_lengkap;
        redirect('profile.php');
    } else {
        $_SESSION['error'] = "Gagal memperbarui profil!";
    }
}

// Proses update password
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_password'])) {
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    
    // Validasi
    if ($new_password !== $confirm_password) {
        $_SESSION['error'] = "Password baru dan konfirmasi password tidak cocok!";
    } elseif (strlen($new_password) < 6) {
        $_SESSION['error'] = "Password minimal 6 karakter!";
    } else {
        // Verifikasi password saat ini
        if (password_verify($current_password, $siswa['password'])) {
            $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
            $sql_update_pass = "UPDATE lms_users SET password = ? WHERE id = ?";
            $stmt_update_pass = $conn->prepare($sql_update_pass);
            $stmt_update_pass->bind_param('si', $hashed_password, $user_id);
            
            if ($stmt_update_pass->execute()) {
                $_SESSION['success'] = "Password berhasil diperbarui!";
            } else {
                $_SESSION['error'] = "Gagal memperbarui password!";
            }
        } else {
            $_SESSION['error'] = "Password saat ini salah!";
        }
    }
    redirect('profile.php');
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil Saya - SMANSAB LMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #3f51b5;
            --secondary-color: #f5f5f5;
        }
        
        .sidebar {
            background: var(--primary-color);
            color: white;
            min-height: 100vh;
            padding: 0;
        }
        
        .sidebar-sticky {
            position: sticky;
            top: 0;
            height: 100vh;
            padding-top: 20px;
            overflow-y: auto;
        }
        
        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.8);
            padding: 12px 20px;
            margin: 2px 0;
            border-radius: 0;
            transition: all 0.3s;
        }
        
        .sidebar .nav-link:hover {
            color: white;
            background: rgba(255, 255, 255, 0.1);
        }
        
        .sidebar .nav-link.active {
            color: white;
            background: rgba(255, 255, 255, 0.2);
            border-left: 4px solid #fff;
        }
        
        .sidebar .nav-link i {
            width: 20px;
            margin-right: 10px;
        }
        
        .main-content {
            background: #f8f9fa;
            min-height: 100vh;
        }
        
        .navbar-custom {
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .profile-header {
            background: linear-gradient(135deg, var(--primary-color), #7986cb);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }
        
        .profile-picture {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 5px solid white;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        }
        
        .stat-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            margin-bottom: 20px;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .nav-tabs .nav-link {
            color: #495057;
            border: none;
            padding: 1rem 2rem;
            border-radius: 0;
        }
        
        .nav-tabs .nav-link.active {
            background: var(--primary-color);
            color: white;
            border: none;
        }
        
        .tab-content {
            background: white;
            border-radius: 0 0 15px 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(63, 81, 181, 0.25);
        }
        
        .btn-primary {
            background: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-primary:hover {
            background: #303f9f;
            border-color: #303f9f;
        }
        
        .achievement-badge {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
            font-size: 2rem;
        }
        
        #fotoPreview {
            max-width: 200px;
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 sidebar">
                <div class="sidebar-sticky">
                    <div class="text-center mb-4">
                        <h4 class="fw-bold">
                            <i class="bi bi-book-half"></i><br>
                            SMANSAB LMS
                        </h4>
                        <small class="text-white-50">Siswa Dashboard</small>
                    </div>
                    
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="dashboard.php">
                                <i class="bi bi-speedometer2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="my_courses.php">
                                <i class="bi bi-journal-bookmark"></i> Kursus Saya
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="progress.php">
                                <i class="bi bi-graph-up"></i> Progress Belajar
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="quizzes.php">
                                <i class="bi bi-pencil-square"></i> Kuis & Ujian
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="certificates.php">
                                <i class="bi bi-award"></i> Sertifikat
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="profile.php">
                                <i class="bi bi-person"></i> Profil Saya
                            </a>
                        </li>
                        <li class="nav-item mt-4">
                            <a class="nav-link" href="../logout.php">
                                <i class="bi bi-box-arrow-right"></i> Logout
                            </a>
                        </li>
                    </ul>
                    
                    <div class="mt-5 px-3">
                        <small class="text-white-50">
                            <i class="bi bi-info-circle"></i> Butuh bantuan?
                            <a href="#" class="text-white">Hubungi Admin</a>
                        </small>
                    </div>
                </div>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 col-lg-10 main-content">
                <!-- Top Navbar -->
                <nav class="navbar navbar-custom">
                    <div class="container-fluid">
                        <div class="d-flex align-items-center">
                            <button class="btn btn-outline-primary me-3 d-md-none" type="button">
                                <i class="bi bi-list"></i>
                            </button>
                            <h5 class="mb-0">Profil Saya</h5>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="dropdown">
                                <button class="btn btn-outline-primary dropdown-toggle" type="button" 
                                        data-bs-toggle="dropdown">
                                    <i class="bi bi-person-circle me-2"></i>
                                    <?php echo $_SESSION['nama_lengkap']; ?>
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a class="dropdown-item" href="profile.php">
                                        <i class="bi bi-person me-2"></i>Profil
                                    </a></li>
                                    <li><a class="dropdown-item" href="../courses.php">
                                        <i class="bi bi-search me-2"></i>Jelajah Kursus
                                    </a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><a class="dropdown-item" href="../logout.php">
                                        <i class="bi bi-box-arrow-right me-2"></i>Logout
                                    </a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </nav>

                <div class="container-fluid mt-4">
                    <!-- Alert Messages -->
                    <?php if (isset($_SESSION['success'])): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($_SESSION['error'])): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <?php echo $_SESSION['error']; unset($_SESSION['error']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <!-- Profile Header -->
                    <div class="profile-header">
                        <div class="row align-items-center">
                            <div class="col-md-2 text-center">
                                <img src="../<?php echo $siswa['foto_profil'] ?: 'assets/images/default-avatar.png'; ?>" 
                                     alt="Foto Profil" class="profile-picture" id="profilePicture">
                            </div>
                            <div class="col-md-8">
                                <h2 class="fw-bold mb-2"><?php echo htmlspecialchars($siswa['nama_lengkap']); ?></h2>
                                <p class="mb-1">
                                    <i class="bi bi-person-badge me-2"></i>Siswa - SMANSAB LMS
                                </p>
                                <p class="mb-1">
                                    <i class="bi bi-envelope me-2"></i><?php echo htmlspecialchars($siswa['email']); ?>
                                </p>
                                <p class="mb-0">
                                    <i class="bi bi-calendar me-2"></i>
                                    Bergabung sejak <?php echo date('d F Y', strtotime($siswa['tanggal_daftar'])); ?>
                                </p>
                            </div>
                            <div class="col-md-2 text-end">
                                <span class="badge bg-success fs-6 p-2">
                                    <i class="bi bi-check-circle me-1"></i>Aktif
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0">
                                <div class="card-body text-center">
                                    <div class="achievement-badge bg-primary">
                                        <i class="bi bi-journal-bookmark text-white"></i>
                                    </div>
                                    <h3 class="fw-bold text-primary"><?php echo $stats['total_lms_courses']; ?></h3>
                                    <p class="text-muted mb-0">Total Kursus</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0">
                                <div class="card-body text-center">
                                    <div class="achievement-badge bg-success">
                                        <i class="bi bi-check-circle text-white"></i>
                                    </div>
                                    <h3 class="fw-bold text-success"><?php echo $stats['completed_lms_courses']; ?></h3>
                                    <p class="text-muted mb-0">Kursus Selesai</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0">
                                <div class="card-body text-center">
                                    <div class="achievement-badge bg-warning">
                                        <i class="bi bi-graph-up text-white"></i>
                                    </div>
                                    <h3 class="fw-bold text-warning"><?php echo number_format($stats['avg_progress'] ?? 0, 1); ?>%</h3>
                                    <p class="text-muted mb-0">Progress Rata-rata</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0">
                                <div class="card-body text-center">
                                    <div class="achievement-badge bg-info">
                                        <i class="bi bi-pencil-square text-white"></i>
                                    </div>
                                    <h3 class="fw-bold text-info"><?php echo $stats['total_lms_quizzes']; ?></h3>
                                    <p class="text-muted mb-0">Total Kuis</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Tab Navigation -->
                    <ul class="nav nav-tabs" id="profileTab" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="edit-tab" data-bs-toggle="tab" 
                                    data-bs-target="#edit" type="button" role="tab">
                                <i class="bi bi-pencil me-2"></i>Edit Profil
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="password-tab" data-bs-toggle="tab" 
                                    data-bs-target="#password" type="button" role="tab">
                                <i class="bi bi-lock me-2"></i>Ubah Password
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="achievements-tab" data-bs-toggle="tab" 
                                    data-bs-target="#achievements" type="button" role="tab">
                                <i class="bi bi-trophy me-2"></i>Pencapaian
                            </button>
                        </li>
                    </ul>

                    <!-- Tab Content -->
                    <div class="tab-content p-4" id="profileTabContent">
                        <!-- Edit Profile Tab -->
                        <div class="tab-pane fade show active" id="edit" role="tabpanel">
                            <form method="POST" enctype="multipart/form-data">
                                <input type="hidden" name="update_profile" value="1">
                                
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="username" class="form-label">Username</label>
                                            <input type="text" class="form-control" id="username" 
                                                   value="<?php echo htmlspecialchars($siswa['username']); ?>" readonly>
                                            <div class="form-text">Username tidak dapat diubah</div>
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label for="nama_lengkap" class="form-label">Nama Lengkap *</label>
                                            <input type="text" class="form-control" id="nama_lengkap" name="nama_lengkap"
                                                   value="<?php echo htmlspecialchars($siswa['nama_lengkap']); ?>" required>
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label for="email" class="form-label">Email *</label>
                                            <input type="email" class="form-control" id="email" name="email"
                                                   value="<?php echo htmlspecialchars($siswa['email']); ?>" required>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <div class="mb-3">
                                            <label for="foto_profil" class="form-label">Foto Profil</label>
                                            <input type="file" class="form-control" id="foto_profil" name="foto_profil"
                                                   accept="image/jpeg,image/png,image/gif">
                                            <div class="form-text">
                                                Format: JPG, PNG, GIF. Maksimal 2MB.
                                            </div>
                                        </div>
                                        
                                        <div class="text-center">
                                            <img src="../<?php echo $siswa['foto_profil'] ?: 'assets/images/default-avatar.png'; ?>" 
                                                 alt="Preview Foto" class="img-thumbnail mb-3" 
                                                 id="fotoPreview" style="max-width: 200px;">
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="d-flex justify-content-between">
                                    <a href="dashboard.php" class="btn btn-secondary">
                                        <i class="bi bi-arrow-left me-2"></i>Kembali ke Dashboard
                                    </a>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="bi bi-check-circle me-2"></i>Simpan Perubahan
                                    </button>
                                </div>
                            </form>
                        </div>

                        <!-- Change Password Tab -->
                        <div class="tab-pane fade" id="password" role="tabpanel">
                            <form method="POST">
                                <input type="hidden" name="update_password" value="1">
                                
                                <div class="row">
                                    <div class="col-md-8">
                                        <div class="mb-3">
                                            <label for="current_password" class="form-label">Password Saat Ini *</label>
                                            <input type="password" class="form-control" id="current_password" 
                                                   name="current_password" required>
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label for="new_password" class="form-label">Password Baru *</label>
                                            <input type="password" class="form-control" id="new_password" 
                                                   name="new_password" required>
                                            <div class="form-text">Minimal 6 karakter</div>
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label for="confirm_password" class="form-label">Konfirmasi Password Baru *</label>
                                            <input type="password" class="form-control" id="confirm_password" 
                                                   name="confirm_password" required>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="alert alert-info">
                                            <h6><i class="bi bi-info-circle me-2"></i>Tips Password</h6>
                                            <ul class="small mb-0">
                                                <li>Gunakan kombinasi huruf dan angka</li>
                                                <li>Minimal 6 karakter</li>
                                                <li>Hindari password yang mudah ditebak</li>
                                                <li>Jangan gunakan password yang sama dengan akun lain</li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="d-flex justify-content-between">
                                    <a href="dashboard.php" class="btn btn-secondary">
                                        <i class="bi bi-arrow-left me-2"></i>Kembali ke Dashboard
                                    </a>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="bi bi-key me-2"></i>Ubah Password
                                    </button>
                                </div>
                            </form>
                        </div>

                        <!-- Achievements Tab -->
                        <div class="tab-pane fade" id="achievements" role="tabpanel">
                            <div class="row">
                                <div class="col-md-6">
                                    <h5 class="mb-4">Pencapaian Belajar</h5>
                                    
                                    <div class="d-flex align-items-center mb-4">
                                        <div class="flex-shrink-0">
                                            <div class="bg-primary rounded-circle p-3">
                                                <i class="bi bi-award text-white"></i>
                                            </div>
                                        </div>
                                        <div class="flex-grow-1 ms-3">
                                            <h6 class="mb-1">Pembelajar Aktif</h6>
                                            <p class="text-muted mb-0">Menyelesaikan <?php echo $stats['completed_lms_courses']; ?> kursus</p>
                                            <div class="progress mt-2" style="height: 6px;">
                                                <div class="progress-bar" style="width: <?php echo min(100, ($stats['completed_lms_courses'] / 5) * 100); ?>%"></div>
                                            </div>
                                            <small class="text-muted"><?php echo $stats['completed_lms_courses']; ?> dari 5 kursus</small>
                                        </div>
                                    </div>
                                    
                                    <div class="d-flex align-items-center mb-4">
                                        <div class="flex-shrink-0">
                                            <div class="bg-success rounded-circle p-3">
                                                <i class="bi bi-clock text-white"></i>
                                            </div>
                                        </div>
                                        <div class="flex-grow-1 ms-3">
                                            <h6 class="mb-1">Konsistensi Belajar</h6>
                                            <p class="text-muted mb-0">Progress rata-rata <?php echo number_format($stats['avg_progress'] ?? 0, 1); ?>%</p>
                                            <div class="progress mt-2" style="height: 6px;">
                                                <div class="progress-bar bg-success" style="width: <?php echo $stats['avg_progress'] ?? 0; ?>%"></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="col-md-6">
                                    <h5 class="mb-4">Level Prestasi</h5>
                                    
                                    <div class="card border-0 bg-light mb-3">
                                        <div class="card-body text-center">
                                            <i class="bi bi-person text-primary" style="font-size: 3rem;"></i>
                                            <h5 class="mt-2">Pemula</h5>
                                            <p class="text-muted">Terus belajar untuk naik level!</p>
                                        </div>
                                    </div>
                                    
                                    <div class="row text-center">
                                        <div class="col-4">
                                            <div class="border rounded p-2">
                                                <i class="bi bi-journal text-warning d-block" style="font-size: 1.5rem;"></i>
                                                <small>Kursus</small>
                                                <h6 class="mb-0"><?php echo $stats['total_lms_courses']; ?></h6>
                                            </div>
                                        </div>
                                        <div class="col-4">
                                            <div class="border rounded p-2">
                                                <i class="bi bi-check-circle text-success d-block" style="font-size: 1.5rem;"></i>
                                                <small>Selesai</small>
                                                <h6 class="mb-0"><?php echo $stats['completed_lms_courses']; ?></h6>
                                            </div>
                                        </div>
                                        <div class="col-4">
                                            <div class="border rounded p-2">
                                                <i class="bi bi-pencil text-info d-block" style="font-size: 1.5rem;"></i>
                                                <small>Kuis</small>
                                                <h6 class="mb-0"><?php echo $stats['total_lms_quizzes']; ?></h6>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Preview foto profil sebelum upload
        document.getElementById('foto_profil').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('fotoPreview').src = e.target.result;
                    document.getElementById('profilePicture').src = e.target.result;
                }
                reader.readAsDataURL(file);
            }
        });

        // Validasi form password
        document.addEventListener('DOMContentLoaded', function() {
            const passwordForm = document.querySelector('form[action="profile.php"]');
            if (passwordForm) {
                passwordForm.addEventListener('submit', function(e) {
                    const newPassword = document.getElementById('new_password').value;
                    const confirmPassword = document.getElementById('confirm_password').value;
                    
                    if (newPassword !== confirmPassword) {
                        e.preventDefault();
                        alert('Password baru dan konfirmasi password tidak cocok!');
                        return false;
                    }
                    
                    if (newPassword.length < 6) {
                        e.preventDefault();
                        alert('Password minimal 6 karakter!');
                        return false;
                    }
                });
            }
        });

        // Tab persistence
        document.addEventListener('DOMContentLoaded', function() {
            // Simpan tab aktif di localStorage
            const triggerTabList = [].slice.call(document.querySelectorAll('#profileTab button'));
            triggerTabList.forEach(function (triggerEl) {
                triggerEl.addEventListener('click', function (event) {
                    const tabId = triggerEl.getAttribute('data-bs-target');
                    localStorage.setItem('activeProfileTab', tabId);
                });
            });

            // Restore tab aktif dari localStorage
            const activeTab = localStorage.getItem('activeProfileTab');
            if (activeTab) {
                const triggerEl = document.querySelector(`#profileTab button[data-bs-target="${activeTab}"]`);
                if (triggerEl) {
                    bootstrap.Tab.getInstance(triggerEl).show();
                }
            }
        });
    </script>
</body>
</html>