<?php
require_once dirname(__DIR__) . "/includes/auth.php";

$db = new SQLite3(dirname(__DIR__) . "/api/db.db");

$base_file = basename($_SERVER["SCRIPT_NAME"]);

if (isset($_POST["submit"])) {
    $nome = trim($_POST["nome"]);
    $url = trim($_POST["url"]);

    if ($nome && $url) {
        $stmt = $db->prepare("INSERT OR REPLACE INTO dns (nome, url) VALUES (:n, :u)");
        $stmt->bindValue(":n", $nome, SQLITE3_TEXT);
        $stmt->bindValue(":u", $url, SQLITE3_TEXT);
        $stmt->execute();
        header("Location: dns.php?r=adicionado");
        exit;
    }
}

if (isset($_POST["submitU"])) {
    $nome = trim($_POST["nome"]);
    $url = trim($_POST["url"]);
    $old = $_GET["update"];

    $stmt = $db->prepare("UPDATE dns SET nome=:n, url=:u WHERE nome=:o");
    $stmt->bindValue(":n", $nome, SQLITE3_TEXT);
    $stmt->bindValue(":u", $url, SQLITE3_TEXT);
    $stmt->bindValue(":o", $old, SQLITE3_TEXT);
    $stmt->execute();
    header("Location: dns.php?r=atualizado");
    exit;
}

if (isset($_GET["delete"])) {
    $stmt = $db->prepare("DELETE FROM dns WHERE nome=:n");
    $stmt->bindValue(":n", $_GET["delete"], SQLITE3_TEXT);
    $stmt->execute();
    header("Location: dns.php?r=excluido");
    exit;
}

$res = $db->query("SELECT * FROM dns ORDER BY nome ASC");

$rowU = null;
if (isset($_GET["update"])) {
    $stmt = $db->prepare("SELECT * FROM dns WHERE nome=:n");
    $stmt->bindValue(":n", $_GET["update"], SQLITE3_TEXT);
    $r = $stmt->execute();
    $rowU = $r->fetchArray(SQLITE3_ASSOC);
}
?>

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

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

<style>
.dns-wrap{
    max-width: 1100px;
    margin: 0 auto;
}
.dns-card{
    border-radius: 18px;
    overflow: hidden;
}
.dns-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap: 16px;
    padding: 16px 18px;
}
.dns-title{
    display:flex;
    align-items:center;
    gap: 12px;
}
.dns-title .ic{
    width:44px;
    height:44px;
    border-radius:14px;
    display:flex;
    align-items:center;
    justify-content:center;
    background: rgba(13,110,253,.12);
}
.dns-title .ic i{
    font-size: 22px;
    color: #0d6efd;
}
.dns-title h5{
    margin:0;
    font-weight:900;
}
.dns-title small{
    opacity:.75;
}
.dns-actions{
    display:flex;
    gap: 10px;
    flex-wrap:wrap;
    justify-content:flex-end;
}
.btn{
    border-radius: 14px;
    height: 44px;
    padding: 0 16px;
    display:inline-flex;
    align-items:center;
    justify-content:center;
    gap: 8px;
    font-weight: 800;
}
.badge-soft{
    background: rgba(25,135,84,.12);
    color: #198754;
    border: 1px solid rgba(25,135,84,.25);
    border-radius: 999px;
    padding: 7px 12px;
    font-size: 12px;
    font-weight: 900;
    display:inline-flex;
    align-items:center;
    gap: 7px;
}
.table{
    margin:0;
}
.table thead th{
    font-size: 12px;
    letter-spacing:.3px;
    text-transform:uppercase;
    opacity: .75;
}
.table td{
    vertical-align: middle;
    padding-top: 14px;
    padding-bottom: 14px;
}
.row-actions{
    display:flex;
    gap: 8px;
    justify-content:flex-end;
    flex-wrap:wrap;
}
.pill{
    display:inline-flex;
    align-items:center;
    gap: 7px;
    padding: 7px 12px;
    border-radius: 999px;
    font-weight: 900;
    font-size: 12px;
    border: 1px solid rgba(0,0,0,.12);
    background: rgba(0,0,0,.03);
}
.money{
    font-weight: 900;
    font-size: 14px;
}
@media (max-width: 768px){
    .row-actions{ justify-content:flex-start; }
}

/* SCROLLBAR CINZA - PC E MOBILE */
html{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

body{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

*::-webkit-scrollbar{
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-track{
  background: #1f1f1f;
  border-radius: 999px;
}

*::-webkit-scrollbar-thumb{
  background: linear-gradient(180deg, #b8b8b8, #7c7c7c);
  border-radius: 999px;
  border: 2px solid #1f1f1f;
}

*::-webkit-scrollbar-thumb:hover{
  background: linear-gradient(180deg, #d0d0d0, #909090);
}

.table-responsive{
  scrollbar-width: thin;
  scrollbar-color: #8f8f8f #1f1f1f;
}

@media (max-width: 768px){
  *::-webkit-scrollbar{
    width: 7px;
    height: 7px;
  }
}

</style>

<div class="dns-wrap">
    <div class="card radius-10 dns-card">

<?php if (isset($_GET["create"])) { ?>

<div class="card-header bg-transparent dns-header">
    <div class="dns-title">
        <div class="ic"><i class="bi bi-plus-circle-fill"></i></div>
        <div>
            <h5>Adicionar DNS</h5>
            <small>Cadastre um novo DNS no sistema</small>
        </div>
    </div>
    <div class="dns-actions">
        <span class="badge-soft"><i class="bi bi-globe-americas"></i> DNS</span>
        <a href="dns.php" class="btn btn-outline-secondary"><i class="bi bi-arrow-left"></i> Voltar</a>
    </div>
</div>

<div class="card-body">
<form method="post">
    <div class="form-group mb-4">
        <label style="font-weight:900;">Nome</label>
        <input class="form-control" name="nome" required style="border-radius:14px;height:46px;">
    </div>
    <div class="form-group mb-4">
        <label style="font-weight:900;">DNS</label>
        <input class="form-control" name="url" required style="border-radius:14px;height:46px;">
    </div>
    <div class="d-flex gap-2 flex-wrap">
        <button class="btn btn-primary" name="submit"><i class="bi bi-check2-circle"></i> Salvar</button>
        <a href="migrar_dns.php" class="btn btn-warning"><i class="bi bi-shuffle"></i> Trocar DNS All</a>
        <a href="dns.php" class="btn btn-outline-secondary"><i class="bi bi-x-circle"></i> Cancelar</a>
    </div>
</form>
</div>

<?php } elseif (isset($_GET["update"])) { ?>

<div class="card-header bg-transparent dns-header">
    <div class="dns-title">
        <div class="ic"><i class="bi bi-pencil-square"></i></div>
        <div>
            <h5>Editar DNS</h5>
            <small>Atualize dados do DNS selecionado</small>
        </div>
    </div>
    <div class="dns-actions">
        <span class="badge-soft"><i class="bi bi-globe2"></i> Editar</span>
        <a href="dns.php" class="btn btn-outline-secondary"><i class="bi bi-arrow-left"></i> Voltar</a>
    </div>
</div>

<div class="card-body">
<form method="post">
    <div class="form-group mb-4">
        <label style="font-weight:900;">Nome</label>
        <input class="form-control" name="nome" value="<?= htmlspecialchars($rowU["nome"]) ?>" required style="border-radius:14px;height:46px;">
    </div>
    <div class="form-group mb-4">
        <label style="font-weight:900;">DNS</label>
        <input class="form-control" name="url" value="<?= htmlspecialchars($rowU["url"]) ?>" required style="border-radius:14px;height:46px;">
    </div>
    <div class="d-flex gap-2 flex-wrap">
        <button class="btn btn-primary" name="submitU"><i class="bi bi-save2"></i> Salvar alterações</button>
        <a href="migrar_dns.php" class="btn btn-warning"><i class="bi bi-shuffle"></i> Trocar DNS All</a>
        <a href="dns.php" class="btn btn-outline-secondary"><i class="bi bi-x-circle"></i> Cancelar</a>
    </div>
</form>
</div>

<?php } else { ?>

<div class="card-header bg-transparent dns-header">
    <div class="dns-title">
        <div class="ic"><i class="bi bi-globe"></i></div>
        <div>
            <h5>DNS Cadastrados</h5>
            <small>Gerencie e troque DNS em massa com segurança</small>
        </div>
    </div>

    <div class="dns-actions">
        <span class="badge-soft"><i class="bi bi-shield-check"></i> Premium</span>
        <a href="migrar_dns.php" class="btn btn-warning"><i class="bi bi-shuffle"></i> Trocar DNS All</a>
        <a href="dns.php?create" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Adicionar DNS</a>
    </div>
</div>

<div class="card-body p-0">
    <div class="table-responsive">
        <table class="table table-hover table-sm align-middle">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>DNS</th>
                    <th class="text-end">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php while ($r = $res->fetchArray(SQLITE3_ASSOC)) { ?>
                <tr>
                    <td>
                        <div style="display:flex;flex-direction:column;gap:4px;">
                            <span style="font-weight:900;font-size:14px;"><?= htmlspecialchars($r["nome"]) ?></span>
                            <span style="opacity:.75;font-size:12px;">
                                <i class="bi bi-link-45deg"></i> Registro DNS
                            </span>
                        </div>
                    </td>

                    <td>
                        <span class="pill"><i class="bi bi-globe2"></i> <?= htmlspecialchars($r["url"]) ?></span>
                    </td>

                    <td class="text-end">
                        <div class="row-actions">
                            <a href="dns.php?update=<?= urlencode($r["nome"]) ?>" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-pencil-square"></i> Editar
                            </a>
                            <a href="dns.php?delete=<?= urlencode($r["nome"]) ?>" class="btn btn-sm btn-outline-danger">
                                <i class="bi bi-trash3-fill"></i> Excluir
                            </a>
                        </div>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<?php } ?>

    </div>
</div>

<?php
if (isset($_GET["r"])) {
    if ($_GET["r"] == "adicionado") echo "<script>Swal.fire('DNS adicionado','','success')</script>";
    if ($_GET["r"] == "atualizado") echo "<script>Swal.fire('DNS atualizado','','success')</script>";
    if ($_GET["r"] == "excluido") echo "<script>Swal.fire('DNS excluído','','success')</script>";
}
?>

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