<?php 
$db = new SQLite3("./api/db/studiolivecode_sports.db");
$table_name = "sports";
$base_file = basename($_SERVER["SCRIPT_NAME"]);
$db->exec("CREATE TABLE IF NOT EXISTS " . $table_name . "(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, header_n TEXT, border_c TEXT, background_c TEXT, text_c TEXT, days TEXT, auto_s TEXT, url VARCHAR(100), type 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 . "(id, header_n, border_c, background_c, text_c, days, auto_s, url, type) VALUES('1', 'Event', '#000000', '#000000', '#ffffff', '3', '1', 'https://www.tvsportguide.com/widget/5cc316f797659?filter_mode=all&filter_value=&days=3&heading=Event&border_color=custom&autoscroll=1&prev_nonce=a7242d2019&custom_colors=000000,000000,ffffff', '0')");
}
$resU = @$db->query("SELECT * FROM " . $table_name . " WHERE id='1'");
$rowU = @$resU->fetchArray();
$new_url = str_replace(" ", "", $rowU["url"]);
if (isset($_POST["submit"])) {
    $new_url = str_replace(" ", "", $_POST["url"]);
    // Invert the value of type before saving
    $type = $_POST["type"] == "0" ? "1" : "0";
    $db->exec("UPDATE " . $table_name . " SET header_n='" . $_POST["header_n"] . "',\r\n\t\t\t\t\t\t\t\t\t\tborder_c='" . $_POST["border_c"] . "', \r\n\t\t\t\t\t\t\t\t\t\tbackground_c='" . $_POST["background_c"] . "', \r\n\t\t\t\t\t\t\t\t\t\ttext_c='" . $_POST["text_c"] . "',\r\n\t\t\t\t\t\t\t\t\t\tdays='" . $_POST["days"] . "',\r\n\t\t\t\t\t\t\t\t\t\tauto_s='" . $_POST["auto_s"] . "',\r\n\t\t\t\t\t\t\t\t\t\turl='" . $new_url . "',\r\n\t\t\t\t\t\t\t\t\t\ttype='" . $type . "'\r\n\t\t\t\t\t\t\t\t\tWHERE \r\n\t\t\t\t\t\t\t\t\t\tid='1'");
    $db->close();
    header("Location: sports.php?r=atualizado");
}
?>

<?php include './includes/header.php';?>
<div class="card radius-10">
    <div class="card-body">
        <h4 class="card-title">Guia de Esportes</h4>
        <form method="post">
            <div class="form-group mb-4">
                <label class="control-label"><strong>Select URL Type</strong></label>
                <select class="form-control type" id="type" name="type">
                    <!-- Invert the selected value to match the stored value -->
                    <option data-value="op0" value="0" <?php echo $rowU["type"] == "1" ? "selected" : ""; ?>>Enter External URL</option>
                    <option data-value="op1" value="1" <?php echo $rowU["type"] == "0" ? "selected" : ""; ?>>Default Dashboard URL</option>
                </select>
            </div>
            <div class="active2">
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Sports URL</strong></label>
                    <input type="text" class="form-control" name="url" value="<?php echo $new_url ?>" placeholder="Enter Sports URL">
                </div>
            </div>
            <div class="active1">
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Border</strong></label>
                    <input type="text" class="form-control" name="border_c" value="<?php echo $rowU["border_c"] ?>" type="color" />
                </div>
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Background Color</strong></label>
                    <input type="text" class="form-control" name="background_c" value="<?php echo $rowU["background_c"] ?>" type="color" />
                </div>
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Text Color</strong></label>
                    <input type="text" class="form-control" name="text_c" value="<?php echo $rowU["text_c"] ?>" type="color" />
                </div>
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Days</strong></label>
                    <select class="form-control" id="select" name="days">
                        <option value="1" <?php echo $rowU["days"] == "1" ? "selected" : ""; ?>>1</option>
                        <option value="3" <?php echo $rowU["days"] == "3" ? "selected" : ""; ?>>3</option>
                        <option value="7" <?php echo $rowU["days"] == "7" ? "selected" : ""; ?>>7</option>
                    </select>
                </div>
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Auto Scroll</strong></label>
                    <select class="form-control" id="select" name="auto_s">
                        <option value="0" <?php echo $rowU["auto_s"] == "0" ? "selected" : ""; ?>>No</option>
                        <option value="1" <?php echo $rowU["auto_s"] == "1" ? "selected" : ""; ?>>Yes</option>
                    </select>
                </div>
                <div class="form-group mb-4">
                    <label class="control-label"><strong>Header Name</strong></label>
                    <input type="text" class="form-control" name="header_n" value="<?php echo $rowU["header_n"] ?>" type="text" />
                </div>
            </div>
            <button type="submit" name="submit" class="btn btn-primary mr-2"><i class='bx bx-check mr-1'></i>Editar</button>
        </form>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    function toggleFields() {
        if($('.type').val() == "0") {
            $('.active2').show();
            $('.active1').hide();
        } else {
            $('.active1').show();
            $('.active2').hide();
        }
    }

    toggleFields();

    $('.type').change(function(){
        toggleFields();
    });
});
</script>

<?php 
if (isset($_GET["r"])) {
    $result = $_GET["r"];
    switch ($result) {
        case "atualizado":
            echo "<script>
            const Toast = Swal.mixin({
                toast: true,
                position: 'bottom',
                showConfirmButton: false,
                timer: 2000,
            })
            Toast.fire({
                icon: 'success',
                title: 'Aplicativo Atualizado com Sucesso!'
            })</script>";
    }
}
?>

<?php include './includes/footer.php';?>