prepare($sql);
    $stmt->execute([
        "game_id" => $_GET["game"],
        "member_id" => $_SESSION["account"]->id,
    ]);
    $submission = $stmt->fetch(PDO::FETCH_OBJ);
    if (!$submission) {
        http_response_code(303);
        header("Location: /games/" . $_GET["game"]);
        die();
    }
    $sql = "SELECT id, name, status_id FROM games
    WHERE id = :id";
    $stmt = $db["data"]->prepare($sql);
    $stmt->execute([
        "id" => $_GET["game"],
    ]);
    $game = $stmt->fetch(PDO::FETCH_OBJ);
    define("GAME_IS_OPEN", $game->status_id === STATUS_ENROLLING);
    $title = "Update: {$game->name}";
    $description = "Update your submission for the " . $game->name . " vote.";
}
if ($_SERVER["REQUEST_METHOD"] === "POST"):
    define("NEW_MANUSCRIPT", $_POST["keep-manuscript"] === "0");
    define("EXISTING_MANUSCRIPT", $_POST["keep-manuscript"] === "1");
    define(
        "RULES_WERE_FOLLOWED",
        isset($_POST["agree-toc"]) &&
            $_POST["agree-toc"] === "1" &&
            (isset($_POST["agree-guidelines"]) &&
                $_POST["agree-guidelines"] === "1")
    );
    define("FILE_EMPTY", $_FILES["manuscript"]["size"] === 0);
    define("FILE_TOO_BIG", $_FILES["manuscript"]["size"] > UPLOAD_MAX_FILESIZE);
    $stmt = $db["data"]->prepare("SELECT id FROM games WHERE id = :id");
    $stmt->execute([
        "id" => $_GET["game"],
    ]);
    $errors = [];
    if (!$stmt->fetch(PDO::FETCH_COLUMN)) {
        $errors["game"] = "The chosen game doesn't exist.";
    }
    if (GAME_IS_OPEN && NEW_MANUSCRIPT && !RULES_WERE_FOLLOWED) {
        $errors["agreements"] =
            "Please accept the Terms & Conditions and the Submission Guidelines.";
    }
    if (GAME_IS_OPEN && NEW_MANUSCRIPT && FILE_EMPTY) {
        $errors["filesize"] = "A file upload is required.";
    } elseif (NEW_MANUSCRIPT && FILE_TOO_BIG) {
        $errors["filesize"] = "Your document is too large.";
    } elseif (GAME_IS_OPEN && NEW_MANUSCRIPT) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime_type = finfo_file($finfo, $_FILES["manuscript"]["tmp_name"]);
        finfo_close($finfo);
        $PROPER_MIMETYPE = $mime_type === "application/pdf";
    }
    if (isset($PROPER_MIMETYPE) && !$PROPER_MIMETYPE) {
        $errors["mimetype"] = "Only PDF submissions are allowed.";
    }
    if (GAME_IS_OPEN && (!isset($_POST["title"]) || !trim($_POST["title"]))) {
        $errors["title"] = "Please enter a title.";
    }
    if (GAME_IS_OPEN && !isset($_SESSION["account"])) {
        $errors["account"] =
            "We can't upload a document without knowing which account it belongs to.";
    }
    if (GAME_IS_OPEN && !isset($_POST["tx-id"])) {
        $errors["payment"] = "You must submit a payment.";
    }
    if (count($errors) > 0) {
        http_response_code(400);
        define(
            "HAS_FILE_ERRORS",
            isset($errors["filesize"]) || isset($errors["mimetype"])
        );
        if (NEW_MANUSCRIPT && !HAS_FILE_ERRORS) {
            $errors["upload"] =
                "Fix all other errors and choose your file again.";
        }
    } else {
        $params = [
            "submission_id" =>  $submission->id,
            "title" => $_POST["title"],
            "doc_is_public" => isset($_POST["public-doc"]) ? 1 : 0,
            "name_is_public" => isset($_POST["public-name"]) ? 1 : 0,
        ];
        if (GAME_IS_OPEN && NEW_MANUSCRIPT) {
            $basename =
                md5(microtime() . $game->id . $_SESSION["account"]->id) .
                ".pdf";
            $hash = md5(
                $_SESSION["account"]->id . $game->id . microtime() . "salt"
            );
            $file_destination = sprintf(
                "%s/%s/%s",
                DIRECTORY_DOCS,
                $game->id,
                $basename
            );
            try {
                $file_moved = move_uploaded_file(
                    $_FILES["manuscript"]["tmp_name"],
                    $file_destination
                );
                if ($file_moved) {
                    $stmt = $db["data"]->prepare(
                        "SELECT basename FROM submissions WHERE id = :submission_id"
                    );
                    $stmt->execute([
                        "submission_id" => $submission->id,
                    ]);
                    $old_manuscript = sprintf(
                        "%s/%s/%s",
                        DIRECTORY_DOCS,
                        $game->id,
                        $stmt->fetch(PDO::FETCH_COLUMN)
                    );
                    unlink($old_manuscript);
                    $stmt = $db["data"]
                        ->prepare("UPDATE submissions SET (title, basename, hash, doc_is_public, name_is_public, created_at)
                        = (:title, :basename, :hash, :doc_is_public, :name_is_public, :created_at) WHERE id = :submission_id");
                    $params["basename"] = $basename;
                    $params["hash"] = $hash;
                    $params["created_at"] = date("Y-m-d\TH:i:s\Z");
                    $stmt->execute($params);
                    http_response_code(303);
                    header("Location: /games/" . $_GET["game"]);
                }
            } catch (Exception $e) {
                var_dump($e);
                http_response_code(500);
                unlink($file_destination);
                $errors["upload"] =
                    "There was an error adding your submission to our database. Please try again.";
            }
        }
        if (GAME_IS_OPEN && EXISTING_MANUSCRIPT) {
            $stmt = $db["data"]
                ->prepare("UPDATE submissions SET (title, doc_is_public, name_is_public)
                = (:title, :doc_is_public, :name_is_public) WHERE id = :submission_id");
            $stmt->execute($params);
            http_response_code(303);
            header("Location: /games/" . $_GET["game"]);
        }
        if (!GAME_IS_OPEN) {
            $stmt = $db["data"]
                ->prepare("UPDATE submissions SET (doc_is_public, name_is_public)
                = (:doc_is_public, :name_is_public) WHERE id = :submission_id");
            $stmt->execute([
                "submission_id" => $submission->id,
                "doc_is_public" => isset($_POST["public-doc"]) ? 1 : 0,
                "name_is_public" => isset($_POST["public-name"]) ? 1 : 0,
            ]);
            http_response_code(303);
            header("Location: /games/" . $_GET["game"]);
        }
    }
endif;
include "partials/head.php";
?>
    
        
        
        
            
                    If you would like to withdraw your submission, please email us at sixfold@sixfold.org.
                
                    This game's submissions are now locked, and you may only edit your work's public visibility.
                
                
                
                    Submission Details
                    
                        
                            | Submission ID | 
                            Transaction ID | 
                            Account ID | 
                        
                    
                    
                        
                            | = $submission->id ?> | 
                            = $submission->transaction_id ?> | 
                            = $_SESSION["account"]->id ?> | 
                        
                    
                
                 
                
                     
                 
                doc_is_public;
                $name_is_public = isset($_POST["public-name"])
                    ? (bool) $_POST["public-name"]
                    : $submission->name_is_public;
                ?>