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

$teacher_id = $_SESSION['teacher_id'];
$id = $_GET['id'] ?? null;

if (!$id) { echo "<script>window.location.href='manage_classes.php';</script>"; exit; }

$msg = ""; $msgType = "";

// ১. ক্লাসের বর্তমান তথ্য সংগ্রহ করা
try {
    $stmt = $pdo->prepare("SELECT * FROM live_classes WHERE id = ? AND teacher_id = ?");
    $stmt->execute([$id, $teacher_id]);
    $class = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$class) { die("<div class='p-5 text-center fw-bold text-danger'>Class not found!</div>"); }

    // টিচারের এসাইন করা কোর্সগুলো নিয়ে আসা
    $course_stmt = $pdo->prepare("SELECT id, title FROM courses WHERE teacher_id = ? OR 1=1 AND status='active'");
    $course_stmt->execute([$teacher_id]);
    $courses = $course_stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) { die("Database Error: " . $e->getMessage()); }

// ২. আপডেট লজিক
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $course_id = $_POST['course_id'];
    $title = trim($_POST['class_title']);
    $topic = trim($_POST['topic']);
    $date = $_POST['class_date'];
    $time = $_POST['class_time'];
    $link = trim($_POST['meeting_link']);
    $status = $_POST['status'];

    try {
        $sql = "UPDATE live_classes SET course_id=?, class_title=?, topic=?, class_date=?, class_time=?, meeting_link=?, status=? 
                WHERE id=? AND teacher_id=?";
        $pdo->prepare($sql)->execute([$course_id, $title, $topic, $date, $time, $link, $status, $id, $teacher_id]);
        
        $msg = "Class schedule updated successfully!";
        $msgType = "success";
        
        // আপডেট হওয়া ডাটা পুনরায় লোড করা
        $stmt->execute([$id, $teacher_id]);
        $class = $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        $msg = "Error: " . $e->getMessage();
        $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; }
    .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); }
    .form-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); padding: 35px; border-top: 6px solid var(--theme-green); }
    
    .form-label { font-weight: 700; color: var(--theme-dark); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
    .form-control, .form-select { border-radius: 10px; padding: 12px 18px; border: 2px solid #f1f5f9; background: var(--bg-soft); transition: 0.3s; font-size: 14px; color: var(--theme-dark); font-weight: 500;}
    .form-control:focus, .form-select:focus { border-color: var(--theme-green); background: #fff; box-shadow: 0 0 0 4px rgba(6, 78, 59, 0.1); outline: none; }
    
    .btn-update { background: var(--theme-green); color: #fff; padding: 15px 45px; border-radius: 50px; font-weight: 800; font-size: 16px; border: none; transition: 0.3s; box-shadow: 0 8px 20px rgba(6, 78, 59, 0.2); }
    .btn-update:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-3px); }

    /* 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; }

    @media (max-width: 768px) {
        .main-content { padding: 15px !important; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .form-card { padding: 25px 20px !important; }
        .btn-update { width: 100%; }
    }
</style>

<div class="main-content p-4">
    <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-edit me-2" style="color: var(--theme-gold);"></i> Edit Class Session</h4>
        </div>
        <a href="manage_classes.php" class="btn btn-light border px-4 py-2 rounded-pill fw-bold shadow-sm text-dark text-decoration-none">
            <i class="fas fa-arrow-left me-2"></i> Back to List
        </a>
    </div>

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

            <div class="form-card fade-up">
                <form method="POST">
                    <div class="row g-4">
                        <div class="col-md-12">
                            <label class="form-label">Class Title *</label>
                            <input type="text" name="class_title" class="form-control" value="<?= htmlspecialchars($class['class_title']) ?>" required>
                        </div>
                        
                        <div class="col-md-6">
                            <label class="form-label">Assign to Course *</label>
                            <select name="course_id" class="form-select fw-bold" required>
                                <?php foreach($courses as $c): ?>
                                    <option value="<?= $c['id'] ?>" <?= ($class['course_id'] == $c['id']) ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($c['title']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label">Class Topic</label>
                            <input type="text" name="topic" class="form-control" value="<?= htmlspecialchars($class['topic'] ?? '') ?>">
                        </div>

                        <div class="col-md-4">
                            <label class="form-label">Class Date *</label>
                            <input type="date" name="class_date" class="form-control" value="<?= $class['class_date'] ?>" required>
                        </div>

                        <div class="col-md-4">
                            <label class="form-label">Class Time *</label>
                            <input type="time" name="class_time" class="form-control" value="<?= $class['class_time'] ?>" required>
                        </div>

                        <div class="col-md-4">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select fw-bold">
                                <option value="active" <?= ($class['status'] == 'active') ? 'selected' : '' ?>>Scheduled (Active)</option>
                                <option value="completed" <?= ($class['status'] == 'completed') ? 'selected' : '' ?>>Completed</option>
                                <option value="cancelled" <?= ($class['status'] == 'cancelled') ? 'selected' : '' ?>>Cancelled</option>
                            </select>
                        </div>

                        <div class="col-md-12">
                            <label class="form-label">Meeting Link (Zoom/Meet/Live) *</label>
                            <input type="url" name="meeting_link" class="form-control" value="<?= htmlspecialchars($class['meeting_link']) ?>" required>
                        </div>
                    </div>

                    <div class="text-center mt-5 pt-4 border-top">
                        <button type="submit" class="btn btn-update">
                            <i class="fas fa-save me-2"></i> Update Class Details
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

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