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

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

// কোর্স লিস্ট সংগ্রহ
$stmt_c = $pdo->prepare("SELECT id, title FROM courses WHERE teacher_id = ? OR 1=1 AND status='active'"); // 1=1 is fallback
$stmt_c->execute([$teacher_id]);
$courses = $stmt_c->fetchAll(PDO::FETCH_ASSOC);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $course_id = $_POST['course_id'];
    $title = trim($_POST['title']);
    $desc = trim($_POST['description']);
    
    if (isset($_FILES['material_file']) && $_FILES['material_file']['error'] == 0) {
        $upload_dir = '../uploads/materials/';
        if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);
        
        $file_ext = pathinfo($_FILES['material_file']['name'], PATHINFO_EXTENSION);
        $file_name = "doc_" . time() . "_" . rand(1000, 9999) . "." . $file_ext;
        $dest = $upload_dir . $file_name;
        
        if (move_uploaded_file($_FILES['material_file']['tmp_name'], $dest)) {
            $db_path = 'uploads/materials/' . $file_name;
            try {
                $sql = "INSERT INTO course_materials (course_id, teacher_id, title, description, file_path, file_type) VALUES (?, ?, ?, ?, ?, ?)";
                $pdo->prepare($sql)->execute([$course_id, $teacher_id, $title, $desc, $db_path, $file_ext]);
                header("Location: manage_materials.php?msg=success");
                exit;
            } catch (PDOException $e) { $msg = "DB Error: " . $e->getMessage(); $msgType = "danger"; }
        } else { $msg = "Failed to move uploaded file."; $msgType = "danger"; }
    } else { $msg = "Please select a valid file."; $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); font-size: 14px; }
    .btn-submit { background: var(--theme-green); color: #fff; padding: 14px 40px; border-radius: 50px; font-weight: 800; border: none; transition: 0.3s; box-shadow: 0 8px 20px rgba(6, 78, 59, 0.2); }
    
    #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; }
</style>

<div class="main-content p-4">
    <div class="dashboard-header d-flex justify-content-between align-items-center">
        <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">Upload Material</h4>
        </div>
        <a href="manage_materials.php" class="btn btn-light border px-4 py-2 rounded-pill fw-bold text-dark text-decoration-none">Cancel</a>
    </div>

    <div class="row justify-content-center">
        <div class="col-xl-8">
            <?php if($msg): ?> <div class="alert alert-<?= $msgType ?> fw-bold"><?= $msg ?></div> <?php endif; ?>
            <div class="form-card">
                <form method="POST" enctype="multipart/form-data">
                    <div class="row g-4">
                        <div class="col-md-12">
                            <label class="form-label">Material Title *</label>
                            <input type="text" name="title" class="form-control" placeholder="e.g. Chapter 1 PDF Lecture" 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 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">Select File (PDF/Doc/Image) *</label>
                            <input type="file" name="material_file" class="form-control" required>
                        </div>
                        <div class="col-md-12">
                            <label class="form-label">Brief Description</label>
                            <textarea name="description" class="form-control" rows="3" placeholder="Optional notes about this file..."></textarea>
                        </div>
                    </div>
                    <div class="text-center mt-5">
                        <button type="submit" class="btn-submit">Upload & Publish</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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