<?php 
// Debugging (Remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();

// 1. Database Connection
if (!file_exists('db_connect.php')) {
    die("Error: db_connect.php file is missing.");
}
require_once 'db_connect.php';

// Security Check
if (!isset($_SESSION['student_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$student_id = $_SESSION['student_id'];
$msg = "";

// 2. Password Update Handling
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_password'])) {
    $current_pass = $_POST['current_password'];
    $new_pass = $_POST['new_password'];
    $confirm_pass = $_POST['confirm_password'];

    $stmt = $pdo->prepare("SELECT password FROM students WHERE id = ?");
    $stmt->execute([$student_id]);
    $user = $stmt->fetch();

    if ($user && password_verify($current_pass, $user['password'])) {
        if ($new_pass === $confirm_pass) {
            $hashed_pass = password_hash($new_pass, PASSWORD_DEFAULT);
            $update = $pdo->prepare("UPDATE students SET password = ? WHERE id = ?");
            if ($update->execute([$hashed_pass, $student_id])) {
                $msg = '<div class="alert alert-success border-0 shadow-sm rounded-3 fw-bold"><i class="fas fa-check-circle me-2"></i> Password has been changed successfully!</div>';
            }
        } else {
            $msg = '<div class="alert alert-danger border-0 shadow-sm rounded-3 fw-bold"><i class="fas fa-exclamation-circle me-2"></i> The new passwords do not match.</div>';
        }
    } else {
        $msg = '<div class="alert alert-danger border-0 shadow-sm rounded-3 fw-bold"><i class="fas fa-times-circle me-2"></i> Your current password is incorrect.</div>';
    }
}

// 3. Fetch all student info
try {
    $stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$student) { die("Student information could not be found."); }

    // Fallback data
    $student_name = $student['name'] ?? 'Unknown Student';
    $profile_img = !empty($student['profile_photo']) ? '../' . $student['profile_photo'] : 'https://ui-avatars.com/api/?name='.urlencode($student_name).'&size=128&background=064e3b&color=fff&bold=true';
    
} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

// Include Header & Sidebar
if(file_exists('includes/user_header.php')) { include 'includes/user_header.php'; } 
elseif(file_exists('header.php')) { include 'header.php'; }

if(file_exists('includes/sidebar.php')) { include 'includes/sidebar.php'; } 
elseif(file_exists('sidebar.php')) { include 'sidebar.php'; }
?>

<style>
    :root { 
        --primary: #064e3b; 
        --primary-dark: #022c22;
        --accent: #d4af37; /* Golden */
    }
    
    .fade-up { animation: fadeInUp 0.5s ease-out forwards; }
    @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
    
    .profile-card { border-top: 6px solid var(--accent); position: sticky; top: 20px; }
    
    .info-label { 
        font-weight: 700; 
        color: #64748b; 
        font-size: 11px; 
        text-transform: uppercase; 
        letter-spacing: 0.5px; 
        margin-bottom: 3px; 
        display: block; 
    }
    .info-value { font-weight: 600; color: var(--primary-dark); font-size: 15px; display: block; margin-bottom: 15px; }
    
    .section-title { 
        font-weight: 800; 
        color: var(--primary); 
        font-size: 17px; 
        margin-bottom: 20px; 
        border-bottom: 2px dashed #e2e8f0; 
        padding-bottom: 10px; 
    }
    
    .form-control:focus { 
        box-shadow: 0 0 0 4px rgba(212, 175, 55, 0.15); 
        border-color: var(--accent) !important; 
    }
</style>

<div class="main-content p-4" style="background: #f8fafc; min-height: 100vh;">
    
    <div class="d-flex align-items-center mb-4 fade-up">
        <h4 class="fw-bold mb-0" style="color: var(--primary);"><i class="fas fa-user-shield me-2" style="color: var(--accent);"></i> My Profile</h4>
    </div>

    <?= $msg; ?>

    <div class="row g-4">
        <div class="col-lg-4 fade-up" style="animation-delay: 0.1s;">
            <div class="card profile-card border-0 shadow-sm rounded-4 text-center p-4">
                <div class="position-relative d-inline-block mx-auto mb-3">
                    <img src="<?= $profile_img ?>" class="rounded-circle shadow-sm" width="130" height="130" style="object-fit: cover; border: 4px solid var(--accent);">
                    <span class="position-absolute bottom-0 end-0 bg-success p-2 rounded-circle border border-3 border-white" title="Active Account"></span>
                </div>
                
                <h5 class="fw-bold mb-1 text-dark"><?= htmlspecialchars($student_name); ?></h5>
                <p class="text-muted small fw-bold mb-0">ID: #<?= str_pad($student['id'], 5, '0', STR_PAD_LEFT); ?></p>
                <span class="badge bg-opacity-10 mt-2 px-3 py-2 rounded-pill fw-bold border" style="background-color: rgba(6, 78, 59, 0.1); color: var(--primary); border-color: rgba(6, 78, 59, 0.2);">
                    <i class="fas fa-graduation-cap me-1"></i> <?= htmlspecialchars($student['course_name'] ?? 'N/A'); ?>
                </span>
                
                <hr class="my-4 opacity-10">
                
                <div class="text-start">
                    <span class="info-label"><i class="fas fa-envelope me-2"></i> Email Address</span>
                    <span class="info-value"><?= htmlspecialchars($student['email'] ?? 'N/A'); ?></span>
                    
                    <span class="info-label"><i class="fas fa-phone-alt me-2"></i> Mobile Number</span>
                    <span class="info-value"><?= htmlspecialchars($student['phone'] ?? 'N/A'); ?></span>

                    <span class="info-label"><i class="fas fa-university me-2"></i> Admission Type</span>
                    <span class="badge bg-warning bg-opacity-25 text-dark fw-bold px-3 py-1"><?= htmlspecialchars($student['admission_type'] ?? 'Online'); ?></span>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            
            <div class="card border-0 shadow-sm rounded-4 p-4 mb-4 fade-up" style="animation-delay: 0.2s;">
                <h5 class="section-title"><i class="fas fa-id-card-alt me-2"></i> Detailed Information</h5>
                <div class="row">
                    <div class="col-md-6">
                        <span class="info-label">Father / Husband's Name</span>
                        <span class="info-value"><?= htmlspecialchars($student['father_husband_name'] ?? 'N/A'); ?></span>
                    </div>
                    <div class="col-md-6">
                        <span class="info-label">Mother's Name</span>
                        <span class="info-value"><?= htmlspecialchars($student['mother_name'] ?? 'N/A'); ?></span>
                    </div>
                    <div class="col-md-6">
                        <span class="info-label">Guardian's Phone</span>
                        <span class="info-value"><?= htmlspecialchars($student['guardian_phone'] ?? 'N/A'); ?></span>
                    </div>
                    <div class="col-md-6">
                        <span class="info-label">SSC / Equivalent Info</span>
                        <span class="info-value"><?= htmlspecialchars($student['edu_ssc'] ?? 'N/A'); ?></span>
                    </div>
                    <div class="col-md-6">
                        <span class="info-label">HSC / Equivalent Info</span>
                        <span class="info-value"><?= htmlspecialchars($student['edu_hsc'] ?? 'N/A'); ?></span>
                    </div>
                    <div class="col-12">
                        <span class="info-label">Present Address</span>
                        <span class="info-value text-muted fw-medium" style="font-size: 14px;"><?= nl2br(htmlspecialchars($student['present_address'] ?? 'N/A')); ?></span>
                    </div>
                    <div class="col-12">
                        <span class="info-label">Permanent Address</span>
                        <span class="info-value text-muted fw-medium" style="font-size: 14px;"><?= nl2br(htmlspecialchars($student['permanent_address'] ?? 'N/A')); ?></span>
                    </div>
                </div>
            </div>

            <div class="card border-0 shadow-sm rounded-4 p-4 fade-up" style="animation-delay: 0.3s; border-top: 4px solid var(--primary);">
                <h5 class="section-title" style="color: var(--primary); border-color: rgba(6, 78, 59, 0.2);"><i class="fas fa-lock me-2"></i> Change Password</h5>
                <form action="" method="POST">
                    <div class="mb-4">
                        <label class="form-label fw-bold text-muted small text-uppercase">Current Password</label>
                        <input type="password" name="current_password" class="form-control border-light bg-light py-2" placeholder="Enter current password" required>
                    </div>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label fw-bold text-muted small text-uppercase">New Password</label>
                            <input type="password" name="new_password" class="form-control border-light bg-light py-2" placeholder="Enter new password" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-bold text-muted small text-uppercase">Confirm Password</label>
                            <input type="password" name="confirm_password" class="form-control border-light bg-light py-2" placeholder="Type again" required>
                        </div>
                    </div>
                    <div class="mt-4">
                        <button type="submit" name="update_password" class="btn px-4 py-2 rounded-pill fw-bold shadow-sm" style="background: var(--primary); color: white; border: none; transition: 0.3s;" onmouseover="this.style.backgroundColor='var(--accent)'; this.style.color='var(--primary-dark)';" onmouseout="this.style.backgroundColor='var(--primary)'; this.style.color='white';">
                            <i class="fas fa-sync-alt me-2"></i> Update Password
                        </button>
                    </div>
                </form>
            </div>
            
            <div class="alert bg-white border-0 shadow-sm rounded-4 p-4 mt-4 fade-up" style="animation-delay: 0.4s; border-left: 5px solid var(--accent) !important;">
                <div class="d-flex align-items-center">
                    <div class="h1 me-3 mb-0" style="color: var(--accent);"><i class="fas fa-info-circle"></i></div>
                    <div>
                        <h6 class="fw-bold mb-1" style="color: var(--primary-dark);">Need to correct your information?</h6>
                        <p class="mb-0 small text-muted">If there is any mistake in your name, mobile number, or educational details, please contact the academy administration directly to request an update.</p>
                    </div>
                </div>
            </div>
            
        </div>
    </div>
</div>

<?php 
// Include Footer 
if(file_exists('includes/user_footer.php')) { include 'includes/user_footer.php'; } 
elseif(file_exists('footer.php')) { include 'footer.php'; }
?>