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

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

// ১.assigned courses নিয়ে আসা
try {
    // এখানে চেক করুন আপনার ডাটাবেস টেবিলের কলামের নাম teacher_id কি না
   $stmt = $pdo->prepare("SELECT id, title FROM courses WHERE status='active'");
$stmt->execute();
    $courses = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // ডিবাগিং: যদি কোর্স না পাওয়া যায় তবে এরর মেসেজ সেট করা
    if (empty($courses)) {
        $msg = "Warning: No courses are assigned to you in the database. Please contact admin.";
        $msgType = "warning";
    }
} catch (PDOException $e) { 
    $msg = "Database Error: " . $e->getMessage(); 
    $msgType = "danger";
}

// ২. ফর্ম সাবমিট লজিক
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['course_id'])) {
    $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']);

    try {
        $sql = "INSERT INTO live_classes (course_id, teacher_id, class_title, topic, class_date, class_time, meeting_link, status) 
                VALUES (?, ?, ?, ?, ?, ?, ?, 'active')";
        $pdo->prepare($sql)->execute([$course_id, $teacher_id, $title, $topic, $date, $time, $link]);
        $msg = "Class scheduled successfully!";
        $msgType = "success";
    } catch (PDOException $e) {
        $msg = "Error: " . $e->getMessage();
        $msgType = "danger";
    }
}
?>

<style>
    .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: 12px; padding: 12px 18px; border: 2px solid #f1f5f9; background: var(--bg-soft); transition: 0.3s; font-size: 14px; font-weight: 500; }
    .form-control:focus { border-color: var(--theme-green); background: #fff; box-shadow: 0 0 0 4px rgba(6, 78, 59, 0.1); outline: none; }
    
    .btn-submit { background: var(--theme-green); color: #fff; padding: 14px 40px; 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-submit:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }

    #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; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .form-card { padding: 25px 20px !important; }
        .btn-submit { width: 100%; }
    }
</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">Schedule New Class</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" placeholder="e.g. Introduction to Anatomy" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Select Course *</label>
                            <select name="course_id" class="form-select fw-bold" required>
                                <option value="">Choose your course...</option>
                                <?php foreach($courses as $c): ?>
                                    <option value="<?= $c['id'] ?>"><?= 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" placeholder="Specific topic or lesson">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Class Date *</label>
                            <input type="date" name="class_date" class="form-control" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Class Time *</label>
                            <input type="time" name="class_time" class="form-control" required>
                        </div>
                        <div class="col-md-12">
                            <label class="form-label">Meeting Link (Zoom/Google Meet) *</label>
                            <input type="url" name="meeting_link" class="form-control" placeholder="https://zoom.us/j/..." required>
                        </div>
                    </div>

                    <div class="text-center mt-5 pt-4 border-top">
                        <button type="submit" class="btn-submit">
                            <i class="fas fa-calendar-check me-2"></i> Schedule Now
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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