<?php
include ('includes/header.php');

$table_name = "dns";
$page = "main.php";

$res = $db->select($table_name, '*', '', '');

@$resU = $db->select($table_name, '*', 'id = :id', '', [':id' => $_GET['update']]);

if(isset($_POST['submitU'])){
	unset($_POST['submitU']);
	$updateData = $_POST;
	$db->update($table_name, $updateData, 'id = :id', [':id' => $_GET['update']]);
	echo "<script>window.location.href='".$page."?status=1'</script>";
}

if(isset($_POST['submit'])){
	unset($_POST['submit']);
	$db->insert($table_name, $_POST);
	$db->close();
	echo "<script>window.location.href='".$page."?status=1'</script>";
}

if(isset($_GET['delete'])){
	$db->delete($table_name, 'id = :id', [':id' => $_GET['delete']]);
	echo "<script>window.location.href='".$page."?status=2'</script>";
}
?>

<!-- DELETE MODAL -->
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h2><i class="fas fa-trash-alt" style="color:var(--yt-red);margin-right:10px"></i>Confirmar exclusão</h2>
      </div>
      <div class="modal-body" style="color:var(--yt-text-muted);font-size:14px;padding:20px 24px">
        Tem certeza que deseja excluir este registro? Esta ação não pode ser desfeita.
      </div>
      <div class="modal-footer">
        <button type="button" class="yt-btn-ghost" data-dismiss="modal">Cancelar</button>
        <a class="yt-btn-red btn-ok"><i class="fas fa-trash"></i> Excluir</a>
      </div>
    </div>
  </div>
</div>

<?php if (isset($_GET['create'])): ?>

<!-- ══ CREATE FORM ══ -->
<div class="yt-page-header">
  <div class="yt-page-title">
    <h1><i class="fas fa-server" style="color:var(--yt-red);margin-right:8px"></i>Novo DNS</h1>
  </div>
  <a href="<?= $page ?>" class="yt-btn-ghost"><i class="fas fa-arrow-left"></i> Voltar</a>
</div>

<div class="yt-card" style="max-width:560px">
  <div class="yt-card-header">
    <span class="yt-card-title"><i class="fas fa-plus-circle"></i> Adicionar DNS</span>
  </div>
  <div class="yt-card-body">
    <form method="post">
      <div class="yt-form-grid">
        <div class="yt-form-group">
          <label>Título</label>
          <input type="text" name="title" placeholder="Ex: Servidor Principal">
        </div>
        <div class="yt-form-group">
          <label>DNS / URL</label>
          <input type="text" name="url" placeholder="Ex: dns.meuservidor.com">
        </div>
      </div>
      <div class="yt-form-footer">
        <a href="<?= $page ?>" class="yt-btn-ghost">Cancelar</a>
        <button type="submit" name="submit" class="yt-btn-red"><i class="fas fa-check"></i> Salvar</button>
      </div>
    </form>
  </div>
</div>

<?php elseif (isset($_GET['update'])): ?>

<!-- ══ EDIT FORM ══ -->
<div class="yt-page-header">
  <div class="yt-page-title">
    <h1><i class="fas fa-pencil-alt" style="color:var(--yt-red);margin-right:8px"></i>Editar DNS</h1>
  </div>
  <a href="<?= $page ?>" class="yt-btn-ghost"><i class="fas fa-arrow-left"></i> Voltar</a>
</div>

<div class="yt-card" style="max-width:560px">
  <div class="yt-card-header">
    <span class="yt-card-title"><i class="fas fa-edit"></i> Atualizar registro</span>
  </div>
  <div class="yt-card-body">
    <form method="post">
      <input type="hidden" name="id" value="<?= htmlspecialchars($_GET['update']) ?>">
      <div class="yt-form-grid">
        <div class="yt-form-group">
          <label>Título</label>
          <input type="text" name="title" value="<?= htmlspecialchars($resU[0]['title'] ?? '') ?>" placeholder="Título">
        </div>
        <div class="yt-form-group">
          <label>DNS / URL</label>
          <input type="text" name="url" value="<?= htmlspecialchars($resU[0]['url'] ?? '') ?>" placeholder="DNS">
        </div>
      </div>
      <div class="yt-form-footer">
        <a href="<?= $page ?>" class="yt-btn-ghost">Cancelar</a>
        <button type="submit" name="submitU" class="yt-btn-red"><i class="fas fa-check"></i> Atualizar</button>
      </div>
    </form>
  </div>
</div>

<?php else: ?>

<!-- ══ MAIN TABLE ══ -->
<div class="yt-page-header">
  <div class="yt-page-title">
    <h1>DNS Settings</h1>
    <span class="yt-badge" id="dnsBadge"><?= count($res) ?> registros</span>
  </div>
  <a href="<?= $page ?>?create" class="yt-btn-red"><i class="fas fa-plus"></i> Novo DNS</a>
</div>

<!-- Stats -->
<div class="yt-stats">
  <div class="yt-stat">
    <div class="yt-stat-icon red"><i class="fas fa-server"></i></div>
    <div class="yt-stat-info"><span>Total DNS</span><strong><?= count($res) ?></strong></div>
  </div>
  <div class="yt-stat">
    <div class="yt-stat-icon green"><i class="fas fa-check-circle"></i></div>
    <div class="yt-stat-info"><span>Ativos</span><strong><?= count($res) ?></strong></div>
  </div>
  <div class="yt-stat">
    <div class="yt-stat-icon blue"><i class="fas fa-clock"></i></div>
    <div class="yt-stat-info">
      <span>Último adicionado</span>
      <strong style="font-size:13px;font-weight:400">
        <?= !empty($res) ? htmlspecialchars(end($res)['title']) : '—' ?>
      </strong>
    </div>
  </div>
</div>

<!-- Table -->
<div class="yt-card">
  <div class="yt-card-header">
    <span class="yt-card-title"><i class="fas fa-list"></i> Registros DNS</span>
  </div>
  <div class="yt-table-wrap">
    <?php if(empty($res)): ?>
    <div class="yt-empty">
      <i class="fas fa-satellite-dish"></i>
      <p>Nenhum DNS cadastrado.<br>Clique em <strong>Novo DNS</strong> para começar.</p>
    </div>
    <?php else: ?>
    <table class="yt-table">
      <thead>
        <tr>
          <th>#</th>
          <th>Título</th>
          <th>DNS / URL</th>
          <th>Ações</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($res as $i => $row): ?>
        <tr>
          <td style="color:var(--yt-text-muted);font-size:12px"><?= $i + 1 ?></td>
          <td>
            <div style="display:flex;align-items:center;gap:10px">
              <div style="width:34px;height:34px;border-radius:8px;background:var(--yt-surface2);display:flex;align-items:center;justify-content:center;color:var(--yt-red);flex-shrink:0">
                <i class="fas fa-globe"></i>
              </div>
              <span style="font-weight:500"><?= htmlspecialchars($row['title']) ?></span>
            </div>
          </td>
          <td><span class="yt-dns-badge"><?= htmlspecialchars($row['url']) ?></span></td>
          <td>
            <div class="yt-act-wrap">
              <a class="yt-act-btn" href="<?= $page ?>?update=<?= $row['id'] ?>" title="Editar">
                <i class="fas fa-pencil-alt"></i>
              </a>
              <a class="yt-act-btn danger" href="#"
                data-href="<?= $page ?>?delete=<?= $row['id'] ?>"
                data-toggle="modal" data-target="#confirm-delete" title="Excluir">
                <i class="fas fa-trash"></i>
              </a>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php endif; ?>
  </div>
</div>

<?php endif; ?>

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