<?php
error_reporting(0);
include "includes/header.php";
$jsonFilePath = 'includes/ad_type.json';
$jsonData = json_decode(file_get_contents($jsonFilePath), true);
$currentAdType = $jsonData['adType'] ?? 'manual'; 
$db = new SQLite3("./api/.bet_tmdb.db");
$table_name = "api_key";
$db->exec("CREATE TABLE IF NOT EXISTS " . $table_name . " (id INTEGER PRIMARY KEY, key 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 . "(key) VALUES('')");
	
	
}



if ($_SERVER["REQUEST_METHOD"] === "POST") {
    if (isset($_POST["submit"])) {
        $newKey = $_POST["key"]; // No validation/sanitization here for demonstration purposes

        // Use prepared statements to prevent SQL injection
        $stmt = $db->prepare("UPDATE " . $table_name . " SET key = :newKey WHERE id = 1");
        $stmt->bindValue(':newKey', $newKey, SQLITE3_TEXT);
        $stmt->execute();

        header("Location: tmdb_api.php");
        exit();
    
    } elseif (isset($_POST["deleteCache"])) {
        // Specify the path to your JSON cache file
        $cacheFilePath = "./api/cache/combined_cache.json";

        // Check if the cache file exists and delete it
        if (file_exists($cacheFilePath)) {
            unlink($cacheFilePath); // Delete the cache file
        }

        // Redirect and exit

        header("Location: tmdb_api.php");
        exit();
    }
}

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


if (empty($rowU['key'])) {
    echo '<div class="alert alert-warning" role="alert">
        You must add your tMDB API key before using the app. Follow the instructions below to add your API key.
    </div>';
} 

?>
<link rel="stylesheet" href="css/slider.css">
        <div class="col-md-12 mx-auto">
            <div class="card-body">
                
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                                    <div class="card-header bg-primary py-3">
                                        
                                        <div id="slider-container" data-ad-type="<?php echo $currentAdType; ?>" style="display: flex; align-items: center; justify-content: center;">
    <span style="margin-right: 20px; font-size: 24px;">Manual Ads</span> <!-- Increased font size here -->
    <label class="switch">
        <input type="checkbox" id="pageSlider" onchange="toggleAdType()">
        <span class="slider round"></span>
    </label>
    <span style="margin-left: 20px; font-size: 24px;">TMDB Ads</span> <!-- And here -->
</div>
                            <center>
                            <h5> Advert Type Selector</h5>
                            </center>
                                    </div>
                                    <div class="card-body">
                                        <!-- Add instructions here -->
                                        <p style="font-size: 16px;">
                            
                                        </p>
                                        <ol>

                    <!-- ============================================================== -->
                    <!-- End Container fluid  -->
                    <!-- ============================================================== -->
                </div>
                <!-- ============================================================== -->
                <!-- End Page wrapper  -->
                <!-- ============================================================== -->
            </div>
        </div>
    </div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var sliderContainer = document.getElementById('slider-container');
    var currentAdType = sliderContainer.getAttribute('data-ad-type');
    var slider = document.getElementById('pageSlider');

    // Set the slider based on the ad type from JSON
    slider.checked = (currentAdType === 'tmdb');

    slider.addEventListener('change', function() {
        var adType = this.checked ? 'tmdb' : 'manual';
        updateAdType(adType);
    });
});

function updateAdType(adType, isChecked) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ad_type.php', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.onload = function() {
        // Redirect to the appropriate page based on the slider's position
        window.location.href = isChecked ? 'tmdb_api.php' : 'ads.php';
    };
    xhr.send('adType=' + adType);
}
</script>
<?php include "includes/functions.php"; ?>
