<?php
session_start(); 

$db = new SQLite3('./api/.boxbrdb.db');
$db->exec('CREATE TABLE IF NOT EXISTS dns(id INTEGER PRIMARY KEY NOT NULL, dns_title VARCHAR(25), url VARCHAR(25))');
$res = $db->query('SELECT * FROM dns');

// Deletar registro
if (isset($_GET['delete'])) {
    $db->exec('DELETE FROM dns WHERE id=' . intval($_GET['delete']));
    $db->close();
    header('Location: dns.php');
    exit;
}

// Atualizar ID
if (isset($_POST['submit_id_change'])) {
    $new_id = intval($_POST['new_id']);
    $current_id = intval($_POST['current_id']);

    if ($new_id != $current_id) {
        $stmt = $db->prepare("SELECT COUNT(*) FROM dns WHERE id = :id");
        $stmt->bindValue(':id', $new_id, SQLITE3_INTEGER);
        $count = $stmt->execute()->fetchArray()[0];

        if ($count == 0) {
            $stmt = $db->prepare("UPDATE dns SET id = :new_id WHERE id = :current_id");
            $stmt->bindValue(':new_id', $new_id, SQLITE3_INTEGER);
            $stmt->bindValue(':current_id', $current_id, SQLITE3_INTEGER);
            $stmt->execute();
            header('Location: dns.php');
            exit;
        } else {
            echo "<script>alert('ID já existe!');</script>";
        }
    } else {
        echo "<script>alert('Novo ID não pode ser igual ao atual!');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adicionar DNS</title>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
    body, html {
        height: 100%;
        margin: 0;
        font-family: sans-serif;
        background-color: #000;
    }
    #particles-js {
        position: fixed;
        top: 0; left: 0;
        width: 100%; height: 100%;
        z-index: 0;
    }
    .dashboard-container {
        position: relative;
        z-index: 1;
        background: rgba(50,50,50,0.85);
        color: white;
        padding: 20px;
        border-radius: 15px;
        margin: 30px auto;
        max-width: 1000px;
        width: 95%;
    }
    h2 {
        color: #1cc88a;
        margin-bottom: 20px;
    }
    .action-bar {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 10px;
        margin-bottom: 20px;
    }
    .search-input {
        display: flex;
        align-items: center;
        background-color: #444;
        border-radius: 8px;
        padding: 6px 10px;
        flex: 1 1 250px;
        max-width: 400px;
    }
    .search-input input {
        flex: 1;
        background: none;
        border: none;
        outline: none;
        color: white;
        padding: 6px;
    }
    .search-input input::placeholder {
        color: #ccc;
    }
    .dns-table-wrapper {
        width: 100%;
        overflow-x: auto; /* rolagem horizontal no celular */
    }
    .dns-table {
        width: 100%;
        border-collapse: collapse;
        min-width: 500px; /* garante estrutura no desktop */
    }
    .dns-table th, .dns-table td {
        padding: 12px 10px;
        text-align: left;
        border-bottom: 1px solid #555;
    }
    .dns-table th {
        background-color: #333;
    }
    .dns-table tr:hover { background-color: #444; }
    .btn-primary, .delete-btn, .btn-warning {
        border: none; border-radius: 5px;
        padding: 6px 12px;
        transition: transform 0.2s;
    }
    .delete-btn { background: #dc3545; color:#fff; }
    .btn-primary { background: #007bff; color:#fff; }
    .btn-warning { background: #ffc107; color:#000; }
    .btn-primary:hover, .delete-btn:hover, .btn-warning:hover { transform: scale(1.05); }
    .change-id-form { display: flex; gap: 6px; flex-wrap: wrap; }
    .change-id-form input {
        width: 70px;
        padding: 6px;
        border-radius: 5px;
        border: 1px solid #ccc;
    }
    @media (max-width: 768px) {
        .dns-table th, .dns-table td {
            font-size: 14px;
            padding: 8px;
        }
        .dashboard-container { padding: 15px; }
    }
</style>
</head>
<body>
<?php include 'includes/header.php'; ?>
<div class="dashboard-container">
    <h2>Adicionar DNS</h2>
    <div class="action-bar">
        <a href="./dns_create.php" class="btn btn-success">
            <i class="fas fa-plus"></i> Create
        </a>
        <div class="search-input">
            <i class="fas fa-search"></i>
            <input type="text" id="search" placeholder="Search Title / DNS"/>
        </div>
    </div>
    <div class="dns-table-wrapper">
        <table class="dns-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Change ID</th>
                    <th>DNS</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $db = new SQLite3('./api/.boxbrdb.db');
            $res = $db->query('SELECT * FROM dns');
            while ($row = $res->fetchArray()):
            ?>
                <tr>
                    <td><?= $row['id']; ?></td>
                    <td>
                        <form method="post" class="change-id-form">
                            <input type="hidden" name="current_id" value="<?= $row['id']; ?>" />
                            <input type="number" name="new_id" value="<?= $row['id']; ?>" required />
                            <button type="submit" name="submit_id_change" class="btn btn-warning">
                                <i class="fa fa-refresh"></i> Change
                            </button>
                        </form>
                    </td>
                    <td><?= htmlspecialchars($row['url']); ?></td>
                    <td>
                       <a href="./dns_update.php<?php echo $base_file ?>?update=<?= $row['id'] ?>" class="btn btn-warning">EDITAR</a>
<a href="./dns.php<?php echo $base_file ?>?delete=<?= $row['id'] ?>" class="btn btn-danger">DELETAR</a>

                                </a>
                              </td>
                </tr>
            <?php endwhile; $db->close(); ?>
            </tbody>
        </table>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    $("#search").keyup(function () {
        var value = this.value.toLowerCase().trim();
        $("table tbody tr").each(function () {
            var found = false;
            $(this).find("td").each(function () {
                if ($(this).text().toLowerCase().indexOf(value) >= 0) found = true;
            });
            $(this).toggle(found);
        });
    });
</script>
</body>
</html>
