<?php require_once '../includes/header.php';

$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title       = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $youtube_url = trim($_POST['youtube_url'] ?? '');
    $sort_order  = (int)($_POST['sort_order'] ?? 0);
    $is_active   = isset($_POST['is_active']) ? 1 : 0;

    if ($title === '' || $youtube_url === '') {
        $errors[] = 'Title and YouTube URL are required.';
    }

    // Thumbnail upload (optional)
    $thumbnail = null;
    if (!empty($_FILES['thumbnail']['name'])) {
        $thumbnail = uploadFile('thumbnail', ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], '../assets/uploads/');
        if ($thumbnail === false) {
            $errors[] = 'Thumbnail upload failed.';
        }
    }

    if (empty($errors)) {
        $stmt = $pdo->prepare("INSERT INTO tutorials (title, description, youtube_url, thumbnail, sort_order, is_active)
                              VALUES (:title, :desc, :url, :thumb, :sort, :active)");
        $stmt->execute([
            ':title'   => $title,
            ':desc'    => $description,
            ':url'     => $youtube_url,
            ':thumb'   => $thumbnail,
            ':sort'    => $sort_order,
            ':active'  => $is_active,
        ]);
        setFlash('success', 'Tutorial added.');
        header('Location: tutorials.php');
        exit;
    }
}
?>

<h1 class="h2 mb-4">Add Tutorial</h1>

<?php if ($errors): ?>
<div class="alert alert-danger"><ul class="mb-0"><?php foreach ($errors as $e) echo '<li>'.htmlspecialchars($e).'</li>'; ?></ul></div>
<?php endif; ?>

<div class="card shadow-sm">
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="mb-3">
                <label class="form-label">Title *</label>
                <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($_POST['title'] ?? '') ?>" required>
            </div>
            <div class="mb-3">
                <label class="form-label">Description</label>
                <textarea name="description" class="form-control" rows="3"><?= htmlspecialchars($_POST['description'] ?? '') ?></textarea>
            </div>
            <div class="mb-3">
                <label class="form-label">YouTube URL *</label>
                <input type="url" name="youtube_url" class="form-control" value="<?= htmlspecialchars($_POST['youtube_url'] ?? '') ?>" required>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Thumbnail (image)</label>
                    <input type="file" name="thumbnail" class="form-control" accept="image/*">
                </div>
                <div class="col-md-3 mb-3">
                    <label class="form-label">Sort Order</label>
                    <input type="number" name="sort_order" class="form-control" value="<?= htmlspecialchars($_POST['sort_order'] ?? '0') ?>">
                </div>
                <div class="col-md-3 mb-3 d-flex align-items-end">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="is_active" id="is_active" <?= isset($_POST['is_active']) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="is_active">Active</label>
                    </div>
                </div>
            </div>
            <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Save Tutorial</button>
            <a href="tutorials.php" class="btn btn-secondary">Cancel</a>
        </form>
    </div>
</div>

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