<?php
session_start();

if (!isset($_SESSION["user"]) || empty($_SESSION["user"]["is_admin"])) {
    header("Location: ../Index.php");
    exit;
}

require "../Auth/db.php";

$id = $_GET["id"] ?? null;

if (!$id) {
    die("Missing PPV ID.");
}

$stmt = $pdo->prepare("
    SELECT *
    FROM media_ppv
    WHERE id = ?
");

$stmt->execute([$id]);

$ppv = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$ppv) {
    die("PPV event not found.");
}

if (isset($_POST["update_ppv"])) {

    $title = trim($_POST["title"] ?? "");
    $startTime = trim($_POST["start_time"] ?? "");
    $playerSlug = trim($_POST["player_slug"] ?? "");
    $description = trim($_POST["description"] ?? "");
    $category = trim($_POST["category"] ?? "");
    $bannerPath = $ppv["banner"] ?? null;

    /* BANNER */
    if (!empty($_FILES["banner"]["name"])) {

        $uploadDir = "../Uploads/PPVBanners/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["banner"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["banner"]["tmp_name"], $targetPath)) {
            $bannerPath = "Uploads/PPVBanners/" . $fileName;
        }
    }

    $streamUrl = "";

    if (!empty($playerSlug)) {
        $streamUrl = "https://pooembed.eu/embed/" . $playerSlug;
    }

    $stmt = $pdo->prepare("
        UPDATE media_ppv
        SET title = ?,
            start_time_bst = ?,
            player_slug = ?,
            description = ?,
            banner = ?,
            category = ?,
            stream_url = ?
        WHERE id = ?
    ");

    $stmt->execute([
        $title,
        $startTime,
        $playerSlug,
        $description,
        $bannerPath,
        $category,
        $streamUrl,
        $id
    ]);

    header("Location: Dashboard.php?tab=db_ppv");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit PPV - Rewire Admin</title>
<link rel="icon" type="image/png" href="../Assets/Logo.png">

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
}

body {
    background: #050505;
    color: white;
}

.edit-wrap {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    padding: 55px 25px;
}

.edit-card {
    width: 100%;
    max-width: 900px;
    background: #071014;
    border: 1px solid rgba(0,194,255,0.15);
    border-radius: 18px;
    padding: 30px;
}

.back-link {
    color: #00c2ff;
    text-decoration: none;
    font-weight: 800;
    display: inline-block;
    margin-bottom: 25px;
}

h1 {
    margin-bottom: 25px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 18px;
}

.form-group.full {
    grid-column: span 2;
}

label {
    display: block;
    margin-bottom: 9px;
    color: #8f9aa5;
    font-size: 0.8rem;
    font-weight: 800;
    letter-spacing: 1px;
}

input,
textarea,
select {
    width: 100%;
    background: #050505;
    border: 1px solid rgba(0,194,255,0.18);
    border-radius: 12px;
    color: white;
    padding: 14px 16px;
    outline: none;
}

textarea {
    min-height: 140px;
    resize: vertical;
}

input[type="file"]::file-selector-button {
    background: linear-gradient(135deg,#00c2ff,#0090c7);
    border: none;
    color: white;
    padding: 10px 16px;
    border-radius: 10px;
    margin-right: 14px;
    font-weight: 800;
    cursor: pointer;
}

select {
    appearance: none;
    background:
        #050505
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='white' viewBox='0 0 24 24'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E")
        no-repeat right 16px center;
}

select option {
    background: #050505;
    color: white;
}

.save-btn {
    margin-top: 24px;
    height: 52px;
    padding: 0 24px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg,#00c2ff,#0090c7);
    color: white;
    font-weight: 900;
    cursor: pointer;
}

.preview-image {
    width: 100%;
    max-height: 180px;
    object-fit: cover;
    border-radius: 12px;
    margin-top: 12px;
    border: 1px solid rgba(255,255,255,0.08);
}
</style>
</head>

<body>

<div class="edit-wrap">
    <div class="edit-card">

        <a href="Dashboard.php?tab=db_ppv" class="back-link">
            ← Back To PPV DB
        </a>

        <h1>Edit PPV Event</h1>

        <form method="POST" enctype="multipart/form-data">

            <div class="form-grid">

                <div class="form-group">
                    <label>Title</label>

                    <input
                        type="text"
                        name="title"
                        value="<?php echo htmlspecialchars($ppv["title"]); ?>"
                        required
                    >
                </div>

                <div class="form-group">
                    <label>Time Start In BST</label>

                    <input
                        type="datetime-local"
                        name="start_time"
                        value="<?php echo !empty($ppv["start_time_bst"]) ? date('Y-m-d\TH:i', strtotime($ppv["start_time_bst"])) : ''; ?>"
                        required
                    >
                </div>

                <div class="form-group">
                    <label>Player Slug</label>

                    <input
                        type="text"
                        name="player_slug"
                        value="<?php echo htmlspecialchars($ppv["player_slug"] ?? ""); ?>"
                        required
                    >
                </div>

                <div class="form-group">
                    <label>Category</label>

                    <select name="category" required>

                        <?php
                        $categories = [
                            "All Sports",
                            "Football",
                            "Basketball",
                            "American Football",
                            "Hockey",
                            "Combat Sports",
                            "Motorsports",
                            "24/7"
                        ];
                        ?>

                        <?php foreach ($categories as $cat): ?>

                            <option
                                value="<?php echo $cat; ?>"
                                <?php echo ($ppv["category"] ?? "") === $cat ? "selected" : ""; ?>
                            >
                                <?php echo $cat; ?>
                            </option>

                        <?php endforeach; ?>

                    </select>
                </div>

                <div class="form-group full">
                    <label>Description</label>

                    <textarea name="description"><?php echo htmlspecialchars($ppv["description"] ?? ""); ?></textarea>
                </div>

                <div class="form-group">
                    <label>Banner</label>

                    <input
                        type="file"
                        name="banner"
                        accept="image/*"
                    >

                    <?php if (!empty($ppv["banner"])): ?>
                        <img
                            src="../<?php echo htmlspecialchars($ppv["banner"]); ?>"
                            class="preview-image"
                        >
                    <?php endif; ?>
                </div>

            </div>

            <button
                type="submit"
                name="update_ppv"
                class="save-btn"
            >
                Save Changes
            </button>

        </form>

    </div>
</div>

</body>
</html>