<?php
require_once '../config/database.php';
require_once '../includes/functions.php';
$pageTitle = 'Manage Videos';
include 'header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $conn = getConnection();
    $title = sanitize($_POST['title']);
    $url = sanitize($_POST['video_url']);
    $duration = intval($_POST['duration']);
    $reward = floatval($_POST['reward']);
    
    $stmt = $conn->prepare("INSERT INTO videos (title, video_url, duration, reward) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssid", $title, $url, $duration, $reward);
    $stmt->execute();
    setFlash('success', 'Video added!');
    redirect('videos.php');
}

$conn = getConnection();
$videos = $conn->query("SELECT * FROM videos ORDER BY created_at DESC");
?>
<div class="container">
    <div class="card">
        <h3>🎥 Manage Videos</h3>
        <form method="POST" style="background:#f9f9f9;padding:20px;border-radius:8px;margin:20px 0">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Video Title</label>
                        <input type="text" name="title" class="form-control" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Video URL</label>
                        <input type="url" name="video_url" class="form-control" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Duration (seconds)</label>
                        <input type="number" name="duration" class="form-control" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Reward (KSH)</label>
                        <input type="number" step="0.01" name="reward" class="form-control" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-success" style="margin-top:28px">Add Video</button>
                </div>
            </div>
        </form>
        
        <table class="table">
            <tr><th>Title</th><th>Duration</th><th>Reward</th><th>Status</th><th>Actions</th></tr>
            <?php while ($video = $videos->fetch_assoc()): ?>
            <tr>
                <td><?php echo htmlspecialchars($video['title']); ?></td>
                <td><?php echo $video['duration']; ?>s</td>
                <td>Ksh <?php echo number_format($video['reward'], 2); ?></td>
                <td><?php echo $video['status']; ?></td>
                <td><a href="?delete=<?php echo $video['id']; ?>" class="btn btn-danger" style="padding:5px 10px">Delete</a></td>
            </tr>
            <?php endwhile; ?>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>
