<?php 
// ১. পারশিয়াল ফাইলগুলো ইনক্লুড করা
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

// ডাটাবেস কানেকশন
require_once '../db_connect.php'; 

$msg = "";
$id = isset($_GET['id']) ? $_GET['id'] : null;

// ২. ডাটা আপডেট লজিক
if (isset($_POST['save_changes'])) {
    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $course = trim($_POST['course']);
    $address = trim($_POST['address']);

    try {
        $stmt = $pdo->prepare("UPDATE online_admissions SET name=?, phone=?, course=?, address=? WHERE id=?");
        if ($stmt->execute([$name, $phone, $course, $address, $id])) {
            $msg = "<div class='alert alert-success border-0 border-start border-success border-4 shadow-sm mb-4'>
                        <i class='fas fa-check-circle me-2'></i> তথ্যটি সফলভাবে আপডেট করা হয়েছে।
                    </div>";
        }
    } catch (PDOException $e) { 
        $msg = "<div class='alert alert-danger border-0 border-start border-danger border-4 shadow-sm mb-4'>এরর: " . $e->getMessage() . "</div>"; 
    }
}

// ৩. বর্তমান ডাটা রিড
$stmt = $pdo->prepare("SELECT * FROM online_admissions WHERE id = ?");
$stmt->execute([$id]);
$lead = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$lead) { 
    echo "<div class='main-content'><div class='container py-5'><div class='alert alert-warning shadow-sm'><h4>লিড খুঁজে পাওয়া যায়নি!</h4><a href='fb_leads.php' class='btn btn-success mt-3 rounded-pill'>তালিকায় ফিরে যান</a></div></div></div>"; 
    include 'includes/footer.php'; exit; 
}

// ৪. লিডের ধরণ এবং সময় নির্ধারণ
$isBranch = (strpos(strtolower($lead['course']), 'branch') !== false);
$time_formatted = date('d M Y, h:i A', strtotime($lead['created_at']));
?>

<style>
    :root { --admin-green: #0a5f3e; --admin-yellow: #ffb800; }
    .main-content { background-color: #f4f7fa; min-height: 100vh; padding: 25px; }
    
    /* Header Section */
    .edit-header { background: #fff; padding: 20px 25px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 30px; border-left: 5px solid var(--admin-green); }

    /* Premium Form Card */
    .edit-card { background: #fff; border-radius: 24px; box-shadow: 0 15px 45px rgba(0,0,0,0.06); max-width: 750px; margin: auto; overflow: hidden; border: none; }
    .card-header-pro { background: linear-gradient(135deg, var(--admin-green), #128055); color: #fff; padding: 35px; border-bottom: 5px solid var(--admin-yellow); position: relative; }
    
    .type-badge { position: absolute; top: 35px; right: 35px; background: rgba(255,255,255,0.2); backdrop-filter: blur(10px); padding: 5px 15px; border-radius: 50px; font-size: 11px; font-weight: 800; text-transform: uppercase; border: 1px solid rgba(255,255,255,0.3); }

    .form-label { color: #4a5568; font-size: 14px; font-weight: 700; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
    .form-control { border-radius: 14px; padding: 14px 18px; border: 2px solid #eef2f7; background: #fcfdfe; font-weight: 500; transition: 0.3s; }
    .form-control:focus { border-color: var(--admin-green); background: #fff; box-shadow: 0 0 0 5px rgba(10, 95, 62, 0.05); }

    .btn-update-pro { background: var(--admin-green); color: #fff; border: none; padding: 15px 40px; border-radius: 50px; font-weight: 800; transition: 0.3s; box-shadow: 0 8px 20px rgba(10, 95, 62, 0.2); }
    .btn-update-pro:hover { background: #084d32; transform: translateY(-2px); box-shadow: 0 12px 25px rgba(10, 95, 62, 0.3); color: #fff; }

    .meta-info { background: #f8fafc; padding: 15px 25px; border-radius: 12px; font-size: 13px; color: #64748b; margin-bottom: 30px; display: flex; justify-content: space-between; align-items: center; }

    @media (max-width: 768px) {
        .edit-card { border-radius: 15px; }
        .card-header-pro { padding: 25px; }
        .type-badge { position: static; display: inline-block; margin-top: 10px; }
    }
</style>

<div class="main-content">
    
    <div class="edit-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light d-md-none me-3 shadow-sm" id="sidebarToggle">
                <i class="fas fa-bars text-success"></i>
            </button>
            <h4 class="mb-0 fw-bold" style="color: var(--admin-green);">আবেদন সংশোধন</h4>
        </div>
        <a href="fb_leads.php" class="btn btn-outline-secondary px-4 py-2 rounded-pill fw-bold btn-sm shadow-sm">
            <i class="fas fa-arrow-left me-2"></i> তালিকায় ফিরে যান
        </a>
    </div>

    <div class="edit-card fade-up">
        <div class="card-header-pro">
            <span class="type-badge">
                <?= $isBranch ? '<i class="fas fa-building me-1"></i> Branch App' : '<i class="fas fa-user-graduate me-1"></i> Course App' ?>
            </span>
            <h3 class="m-0 fw-bold"><i class="fas fa-user-edit me-2"></i> তথ্য সংশোধন করুন</h3>
            <p class="mb-0 mt-2 opacity-75 fw-medium small">লিডের তথ্য সতর্কতার সাথে আপডেট করুন</p>
        </div>
        
        <div class="card-body p-4 p-lg-5">
            <?= $msg; ?>
            
            <div class="meta-info">
                <span><i class="far fa-calendar-alt me-1"></i> আবেদন: <strong><?= $time_formatted ?></strong></span>
            </div>

            <form method="POST">
                <div class="row g-4">
                    <div class="col-md-6">
                        <label class="form-label"><i class="fas fa-user text-muted"></i> শিক্ষার্থীর নাম</label>
                        <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($lead['name']); ?>" required>
                    </div>
                    
                    <div class="col-md-6">
                        <label class="form-label"><i class="fas fa-phone-alt text-muted"></i> মোবাইল নম্বর</label>
                        <input type="tel" name="phone" class="form-control" value="<?= htmlspecialchars($lead['phone']); ?>" required>
                    </div>
                    
                    <div class="col-12">
                        <label class="form-label"><i class="fas fa-book-medical text-muted"></i> নির্বাচিত কোর্স / পদের নাম</label>
                        <input type="text" name="course" class="form-control" value="<?= htmlspecialchars($lead['course']); ?>" required>
                        <div class="form-text mt-2 small text-muted">দ্রষ্টব্য: এটি আপনার ল্যান্ডিং পেজের কোর্স বা 'Branch Application' হতে পারে।</div>
                    </div>
                    
                    <div class="col-12">
                        <label class="form-label"><i class="fas fa-map-marker-alt text-muted"></i> বর্তমান ঠিকানা</label>
                        <textarea name="address" class="form-control" rows="3"><?= htmlspecialchars($lead['address']); ?></textarea>
                    </div>
                </div>
                
                <hr class="my-5 opacity-50">
                
                <div class="d-flex flex-wrap gap-3">
                    <button type="submit" name="save_changes" class="btn btn-update-pro">
                        <i class="fas fa-save me-2 text-warning"></i> পরিবর্তন সেভ করুন
                    </button>
                    <a href="fb_leads.php" class="btn btn-update-pro">ফিরে যান</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener("DOMContentLoaded", function() {
    // মোবাইল মেনু টগল (আপনার এডিট পেজের লজিক অনুযায়ী)
    const toggleBtn = document.getElementById('sidebarToggle');
    const sidebar = document.querySelector('.sidebar') || document.getElementById('sidebarMenu');

    if (toggleBtn && sidebar) {
        toggleBtn.addEventListener('click', function(e) {
            e.stopPropagation();
            sidebar.classList.toggle('active');
        });

        document.addEventListener('click', function(event) {
            if (!sidebar.contains(event.target) && !toggleBtn.contains(event.target)) {
                sidebar.classList.remove('active');
            }
        });
    }
});
</script>

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