<?php 
session_start();
require_once '../db_connect.php';

if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login");
    exit;
}

$success = ""; $error = "";

// Fetch active courses and teachers for the dropdowns
$courses = $pdo->query("SELECT id, title FROM courses WHERE status = 'active'")->fetchAll(PDO::FETCH_ASSOC);
$teachers = $pdo->query("SELECT id, name FROM teachers WHERE status = 'active'")->fetchAll(PDO::FETCH_ASSOC);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $course_id = $_POST['course_id'];
    $teacher_id = $_POST['teacher_id'];
    $topic = trim($_POST['topic']);
    $class_date = $_POST['class_date'];
    $class_time = $_POST['class_time'];
    $meeting_link = trim($_POST['meeting_link']);
    $status = $_POST['status'];

    if (!empty($topic) && !empty($meeting_link)) {
        try {
            $sql = "INSERT INTO live_classes (course_id, teacher_id, topic, class_date, class_time, meeting_link, status) VALUES (?, ?, ?, ?, ?, ?, ?)";
            $pdo->prepare($sql)->execute([$course_id, $teacher_id, $topic, $class_date, $class_time, $meeting_link, $status]);
            $success = "Live class has been scheduled successfully!";
        } catch (PDOException $e) {
            $error = "Failed to schedule class. Error: " . $e->getMessage();
        }
    } else {
        $error = "Please fill in all required fields.";
    }
}

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

<style>
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 35px; }
    .form-card { background: #fff; border: none; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.04); padding: 40px; border-top: 5px solid #059669; }
    .form-label { font-weight: 600; color: #4a5568; margin-bottom: 8px; font-size: 15px; }
    .form-control, .form-select { border-radius: 10px; padding: 12px 18px; border: 1px solid #e2e8f0; background-color: #f8fafc; transition: 0.3s; }
    .btn-save { background: #059669; color: #fff; padding: 15px; border-radius: 12px; font-weight: 700; border: none; transition: 0.3s; box-shadow: 0 5px 15px rgba(5,150,105,0.2); width: 100%; }
    .btn-save:hover { background: #047857; transform: translateY(-3px); color: #fff; }
    .fade-up { animation: fadeInUp 0.6s ease-out forwards; }
    @keyframes fadeInUp { 0% { opacity: 0; transform: translateY(20px); } 100% { opacity: 1; transform: translateY(0); } }
</style>

<div class="main-content p-3 p-md-4">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <h4 class="mb-0 fw-bold" style="color: #059669;"><i class="fas fa-video me-2 text-warning"></i> Schedule Live Class</h4>
        <a href="manage_classes.php" class="btn btn-outline-secondary px-4 py-2 rounded-pill fw-bold shadow-sm">View Schedule</a>
    </div>

    <div class="row justify-content-center fade-up">
        <div class="col-xl-8">
            <div class="form-card">
                <?php if($success): ?> <div class="alert alert-success fw-bold"><i class="fas fa-check-circle me-2"></i><?= $success ?></div> <?php endif; ?>
                <?php if($error): ?> <div class="alert alert-danger fw-bold"><i class="fas fa-exclamation-triangle me-2"></i><?= $error ?></div> <?php endif; ?>

                <form method="POST">
                    <div class="mb-4">
                        <label class="form-label">Class Topic / Title <span class="text-danger">*</span></label>
                        <input type="text" name="topic" class="form-control" placeholder="e.g. Basic Grammar Rules Lesson 1" required>
                    </div>

                    <div class="row mb-4">
                        <div class="col-md-6">
                            <label class="form-label">Select Course <span class="text-danger">*</span></label>
                            <select name="course_id" class="form-select" required>
                                <option value="">-- Choose 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">Assign Teacher <span class="text-danger">*</span></label>
                            <select name="teacher_id" class="form-select" required>
                                <option value="">-- Choose Teacher --</option>
                                <?php foreach($teachers as $t): ?>
                                    <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['name']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-4">
                        <div class="col-md-6">
                            <label class="form-label">Class Date <span class="text-danger">*</span></label>
                            <input type="date" name="class_date" class="form-control" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Class Time <span class="text-danger">*</span></label>
                            <input type="time" name="class_time" class="form-control" required>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Meeting Link (Zoom / Google Meet) <span class="text-danger">*</span></label>
                        <input type="url" name="meeting_link" class="form-control" placeholder="https://meet.google.com/..." required>
                    </div>

                    <div class="mb-5">
                        <label class="form-label">Class Status</label>
                        <select name="status" class="form-select fw-bold">
                            <option value="upcoming" class="text-primary">Upcoming</option>
                            <option value="live" class="text-danger">Live Now</option>
                            <option value="completed" class="text-secondary">Completed</option>
                        </select>
                    </div>

                    <button type="submit" class="btn btn-save shadow"><i class="fas fa-calendar-check me-2"></i> Save Class Schedule</button>
                </form>
            </div>
        </div>
    </div>
</div>

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