<?php
$db = new SQLite3("./api/data/.bet_tmdb.db");
$db->exec("CREATE TABLE IF NOT EXISTS ads(id INTEGER PRIMARY KEY NOT NULL, images TEXT, title TEXT, description TEXT)");

$res = $db->query("SELECT * FROM ads");
$rows = $db->query("SELECT COUNT(*) as count FROM ads");
$row1 = $rows->fetchArray();
$HOSTa = $lurl = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off" ? "https" : "http") . "://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]) . "/ads/default_ad.png";

$numRows = $row1["count"];
if ($numRows == 0) {
    $db->exec("INSERT INTO ads(images, title, description) VALUES('https://www.dropbox.com/s/nrt1wpozl8a54sd/logo.png?raw=1', 'Bet3 Creations', 'https://t.me/bet3inc')");
}

if (isset($_GET["delete"])) {
    $db->exec("DELETE FROM ads WHERE id=" . $_GET["delete"]);
    $db->close();
    header("Location: ads.php");
    exit();
}

include "includes/header.php";
?>

<!-- [ Main Content ] start -->
<div class="pcoded-main-container">
    <div class="pcoded-wrapper">
        <div class="pcoded-content">
            <div class="pcoded-inner-content">
                <div class="main-body">
                    <div class="page-wrapper">
                        <!-- [ Main Content ] start -->
                        <div class="row">
                            <!-- Column -->
                            <div class="col-lg-12">
                                <div class="card mb-4">
                                    <div class="card-header bg-primary py-3">
                                        <h4 align="center" class="m-0 font-weight-bold text-white"><i class="fas fa-images" style="font-size:20px">&nbsp;&nbsp;</i>Movie AD Manager</h4>
                                    </div>
                                    <div class="card-body">
                                        <br>
                                        <div align="center" class="col-12">
                                            <a href="./add_ads.php"><button class="btn btn-danger btn-icon-split" name="submit" type="submit"><span class="icon text-white-50"><i class="fa fa-plus"></i></span>&nbsp;&nbsp;<span class="text"> Upload AD Image</span></button></a>
                                        </div>
                                        <br>
                                        <div class="table-responsive">
                                            <table class="table table-striped table-sm">
                                                <thead align="center">
                                                    <tr>
                                                        <th><strong>Ad Image</strong></th>
                                                        <th><strong>Title</strong></th>
                                                        <th><strong>Description</strong></th>
                                                        <th><strong>Edit</strong></th>
                                                        <th><strong>Delete</strong></th>
                                                    </tr>
                                                </thead>
                                                <tbody align="center">
                                                    <?php
                                                    while ($row = $res->fetchArray()) {
                                                        echo "<tr class=\"text-primary\">";
                                                        echo "<td><img src=\"" . $row["images"] . "\" alt=\"\" border=3 height=140 width=600></td>";
                                                        echo "<td>" . $row["title"] . "</td>";
                                                        echo "<td>" . $row["description"] . "</td>";
                                                        echo "<td><a href=\"./edit_ads.php?update=" . $row["id"] . "\"><i class=\"fas fa-edit\" style=\"font-size:20px;color:white\"></i></a></td>";
                                                        echo "<td><a href=\"./ads.php?delete=" . $row["id"] . "\"><i class=\"fas fa-trash\" style=\"font-size:20px;color:red\"></i></a></td>";
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- Column -->
                        </div>
                    </div>
                </div>
                <!-- ============================================================== -->
                <!-- End Container fluid  -->
                <!-- ============================================================== -->
            </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Page wrapper  -->
        <!-- ============================================================== -->
    </div>
</div>

<?php
include "includes/functions.php";
?>
