<?php
error_reporting(0);
include "includes/header.php";
$db = new SQLite3("./api/data/.bet_tmdb.db");
$table_name = "banner";

// Modified query to include new fields
$db->exec("CREATE TABLE IF NOT EXISTS " . $table_name . "(id INTEGER PRIMARY KEY, title TEXT, description TEXT)");

$rows = $db->query("SELECT COUNT(*) as count FROM " . $table_name);
$row = $rows->fetchArray();
$numRows = $row["count"];
if ($numRows == 0) {
    $db->exec("INSERT INTO " . $table_name . "(title, description) VALUES('', '')");
}

$res = $db->query("SELECT * FROM " . $table_name . " WHERE id='1'");
$rowU = $res->fetchArray();

if (isset($_POST["submit"])) {
    // Use prepared statements to prevent SQL injection
    $stmt = $db->prepare("UPDATE " . $table_name . " SET title = :title, description = :description WHERE id = 1");
    $stmt->bindValue(':title', $_POST["title"], SQLITE3_TEXT);
    $stmt->bindValue(':description', $_POST["description"], SQLITE3_TEXT);
    $stmt->execute();
    
    // Redirect after updating
    header("Location: banner.php");
    exit();
}

// Output HTML
echo "<!-- [ Main Content ] start -->\r
    <div class=\"pcoded-main-container\">\r
        <div class=\"pcoded-wrapper\">\r
            <div class=\"pcoded-content\">\r
                <div class=\"pcoded-inner-content\">\r
                    <div class=\"main-body\">\r
                        <div class=\"page-wrapper\">\r
                            <!-- [ Main Content ] start -->\r
                            <div class=\"row\">\r
                                <!-- Column -->\r
                                <div class=\"col-lg-12\">\r
                                    <div class=\"card mb-4\">\r
                                        <div class=\"card-header bg-primary py-3\">\r
                                            <h4 align=\"center\" class=\"m-0 font-weight-bold text-white\"><i class=\"fas fa-comment\" style=\"font-size:20px\"></i>  Banner Message</h4>\r
                                        </div>\r
                                        <div class=\"card-body\">\r
                                            <form method=\"post\">\r
                                                <div class=\"form-group mb-3\">\r
                                                    <label class=\"control-label text-primary\" for=\"title\">\r
                                                        <strong>Title: </strong>\r
                                                    </label>\r
                                                    <div class=\"input-group\">\r
                                                        <input class=\"form-control\" id=\"title\" name=\"title\" value=\"" . substr($rowU["title"], 0, 20) . "\" type=\"text\" maxlength=\"20\"/>\r
                                                    </div>\r
                                                </div>\r
                                                <div class=\"form-group mb-3\">\r
                                                    <label class=\"control-label text-primary\" for=\"description\">\r
                                                        <strong>Description: </strong>\r
                                                    </label>\r
                                                    <div class=\"input-group\">\r
                                                        <input class=\"form-control\" id=\"description\" name=\"description\" value=\"" . substr($rowU["description"], 0, 75) . "\" type=\"text\" maxlength=\"75\"/>\r
                                                    </div>\r
                                                </div>\r
                                                <div align=\"center\" class=\"form-group mb-3\">\r
                                                    <br>\r
                                                    <div>\r
                                                        <button class=\"btn btn-danger\" name=\"submit\" type=\"submit\"><span class=\"icon text-white-50\"><i class=\"fas fa-save\"></i>&nbsp;&nbsp;</span><span class=\"text\"> Save Settings</span></button>\r
                                                    </div>\r
                                                </div>\r
                                            </form>\r
                                        </div>\r
                                    </div>\r
                                </div>\r
                                <!-- Column -->\r
                            </div>\r
                        </div>\r
                    </div>\r
                    <!-- ============================================================== -->\r
                    <!-- End Container fluid  -->\r
                    <!-- ============================================================== -->\r
                </div>\r
                <!-- ============================================================== -->\r
                <!-- End Page wrapper  -->\r
                <!-- ============================================================== -->\r
            </div>\r
        </div>\r
    </div>\r";

include "includes/functions.php";
?>
