<?php

header('Content-Type: application/json');

try {
    // Conectar ao banco de dados
    $db = new SQLite3('./db/3studiolivecode_banner.db');
    // Consulta para informações
    $resb = $db->query("SELECT * FROM type");
    $rowb = $resb->fetchArray(SQLITE3_ASSOC);
    $db->close();

    $json_data = [];

    if ($rowb['type'] == 1) {
        $db = new SQLite3('./db/3studiolivecode_banner.db');
        $res = $db->query("SELECT * FROM banner");

        $json_response = [];

        while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
            $row_array['AdName'] = $row['title'];
            $row_array['AdUrl'] = $row['path'];
            array_push($json_response, $row_array);
        }

        $db->close();

        $db2 = new SQLite3("./db/3studiolivecode_banner.db");
        // $res2 = $db2->query("SELECT * FROM banner WHERE id IN ('1', '2', '3', '4', '5')");
        $res2 = $db2->query("SELECT * FROM banner");

        while ($row2 = $res2->fetchArray(SQLITE3_ASSOC)) {
            $status = $row2["status"];
            if ($status == 1) {
                $row_array['AdName'] = $row2['title'];
                $row_array['AdUrl'] = $row2['thumbpath'];
                array_push($json_response, $row_array);
            }
        }

        $db2->close();
    } else {
        $db = new SQLite3('./db/3studiolivecode_banner.db');
        $res = $db->query("SELECT * FROM banner");

        $json_response = [];

        while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
            $status = $row["status"];
                $row_array['AdName'] = $row['title'];
                $row_array['AdUrl'] = $row['path'];
                array_push($json_response, $row_array);
        }

        $db->close();
    }

    $json_data = $json_response;

    header('Content-type: application/json; charset=UTF-8');
    echo json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

} catch (Exception $e) {
    echo "Ocorreu um erro: " . $e->getMessage();
}
?>