<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$teacher_id = $_SESSION['teacher_id'];
$msg = ""; $msgType = "";

// ১. টিচারের বর্তমান তথ্য সংগ্রহ করা
try {
    $stmt = $pdo->prepare("SELECT * FROM teachers WHERE id = ?");
    $stmt->execute([$teacher_id]);
    $teacher = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { die("Database Error: " . $e->getMessage()); }

// ২. প্রোফাইল আপডেট লজিক
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_profile'])) {
    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $email = trim($_POST['email']);

    try {
        $sql = "UPDATE teachers SET name = ?, phone = ?, email = ? WHERE id = ?";
        $pdo->prepare($sql)->execute([$name, $phone, $email, $teacher_id]);
        
        $_SESSION['teacher_name'] = $name; // সেশন আপডেট
        $msg = "Profile updated successfully!";
        $msgType = "success";
        
        // তথ্য রিফ্রেশ করা
        $stmt->execute([$teacher_id]);
        $teacher = $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        $msg = "Update failed! Email might be in use.";
        $msgType = "danger";
    }
}

// ৩. পাসওয়ার্ড পরিবর্তন লজিক
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['change_password'])) {
    $old_pass = $_POST['old_password'];
    $new_pass = $_POST['new_password'];
    $conf_pass = $_POST['confirm_password'];

    if (password_verify($old_pass, $teacher['password'])) {
        if ($new_pass === $conf_pass) {
            $hashed_pass = password_hash($new_pass, PASSWORD_DEFAULT);
            $pdo->prepare("UPDATE teachers SET password = ? WHERE id = ?")->execute([$hashed_pass, $teacher_id]);
            $msg = "Password changed successfully!";
            $msgType = "success";
        } else {
            $msg = "New passwords do not match!";
            $msgType = "danger";
        }
    } else {
        $msg = "Current password is incorrect!";
        $msgType = "danger";
    }
}
?>

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    
    .main-content { background-color: var(--bg-soft); min-height: 100vh; padding: 30px; transition: 0.4s; }
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    
    /* Profile Card */
    .profile-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); overflow: hidden; height: 100%; }
    .profile-cover { height: 100px; background: linear-gradient(45deg, var(--theme-dark), var(--theme-green)); }
    .profile-img-wrapper { margin-top: -50px; margin-bottom: 15px; }
    .profile-img { width: 100px; height: 100px; border-radius: 50%; border: 5px solid #fff; background: #fff; object-fit: cover; box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
    
    .form-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; padding: 30px; border-top: 5px solid var(--theme-green); }
    .form-label { font-weight: 700; color: #64748b; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
    .form-control { border-radius: 12px; padding: 12px 18px; border: 2px solid #f1f5f9; background: var(--bg-soft); font-weight: 500; }
    .form-control:focus { border-color: var(--theme-green); background: #fff; box-shadow: none; }
    
    .btn-save { background: var(--theme-green); color: #fff; padding: 12px 30px; border-radius: 50px; font-weight: 700; border: none; transition: 0.3s; }
    .btn-save:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }

    /* Square Menu Button Fix */
    #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; box-shadow: 0 2px 5px rgba(0,0,0,0.05); }

    @media (max-width: 991px) {
        .main-content { padding: 15px !important; margin-left: 0; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
    }
</style>

<div class="main-content">
    
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-lg-none me-3 shadow-sm" id="sidebarToggle"><i class="fas fa-bars text-dark"></i></button>
            <h4 class="mb-0 fw-bold text-dark"><i class="fas fa-user-circle me-2" style="color: var(--theme-green);"></i> My Profile</h4>
        </div>
    </div>

    <?php if($msg): ?>
        <div class="alert alert-<?= $msgType ?> border-0 border-start border-4 border-<?= $msgType ?> shadow-sm mb-4 bg-white fw-bold p-3 fade-up">
            <i class="fas <?= $msgType == 'success' ? 'fa-check-circle' : 'fa-exclamation-circle' ?> me-2"></i> <?= $msg ?>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <div class="col-lg-4 fade-up" style="animation-delay: 0.1s;">
            <div class="profile-card text-center pb-4">
                <div class="profile-cover"></div>
                <div class="profile-img-wrapper">
                    <?php if(!empty($teacher['profile_photo'])): ?>
                        <img src="../<?= $teacher['profile_photo'] ?>" class="profile-img">
                    <?php else: ?>
                        <div class="profile-img d-inline-flex align-items-center justify-content-center bg-light text-dark fw-bold" style="font-size: 30px;">
                            <?= strtoupper(substr($teacher['name'], 0, 1)) ?>
                        </div>
                    <?php endif; ?>
                </div>
                <h5 class="fw-bold mb-1"><?= htmlspecialchars($teacher['name']) ?></h5>
                <p class="text-muted small fw-bold mb-3 text-uppercase" style="letter-spacing: 1px;">Academy Instructor</p>
                <div class="px-4 text-start">
                    <hr>
                    <div class="mb-2"><small class="text-muted fw-bold text-uppercase" style="font-size: 10px;">Email Address</small><br><span class="fw-bold small"><?= htmlspecialchars($teacher['email']) ?></span></div>
                    <div class="mb-2"><small class="text-muted fw-bold text-uppercase" style="font-size: 10px;">Phone Number</small><br><span class="fw-bold small"><?= htmlspecialchars($teacher['phone'] ?? 'Not Set') ?></span></div>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="form-card mb-4 fade-up" style="animation-delay: 0.2s;">
                <h6 class="fw-bold mb-4 text-dark"><i class="fas fa-id-card me-2 text-gold"></i> Basic Information</h6>
                <form method="POST">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Full Name</label>
                            <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($teacher['name']) ?>" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Contact Phone</label>
                            <input type="text" name="phone" class="form-control" value="<?= htmlspecialchars($teacher['phone'] ?? '') ?>">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Email Address</label>
                            <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($teacher['email']) ?>" required>
                        </div>
                    </div>
                    <div class="mt-4 text-end">
                        <button type="submit" name="update_profile" class="btn-save shadow-sm">Save Changes</button>
                    </div>
                </form>
            </div>

            <div class="form-card fade-up" style="animation-delay: 0.3s; border-top-color: var(--theme-gold);">
                <h6 class="fw-bold mb-4 text-dark"><i class="fas fa-shield-alt me-2 text-gold"></i> Security & Password</h6>
                <form method="POST">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Current Password</label>
                            <input type="password" name="old_password" class="form-control" placeholder="••••••••" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">New Password</label>
                            <input type="password" name="new_password" class="form-control" placeholder="••••••••" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Confirm New Password</label>
                            <input type="password" name="confirm_password" class="form-control" placeholder="••••••••" required>
                        </div>
                    </div>
                    <div class="mt-4 text-end">
                        <button type="submit" name="change_password" class="btn btn-dark rounded-pill px-4 fw-bold shadow-sm" style="border:none;">Change Password</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        const sidebar = document.getElementById('sidebarMenu');
        
        if (toggleBtn && sidebar) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                sidebar.classList.toggle('mobile-show');
            });
        }
    });
</script>

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