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

// Verificar que el usuario es admin
if (!isAdmin($db)) {
	header("Location: main.php");
	exit;
}

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

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

// Actualizar DNS
@$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=updated'</script>";
}

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

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

?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content bg-dark text-white">
			<div class="modal-header border-secondary">
				<h5 class="modal-title">Confirmar Eliminación</h5>
			</div>
			<div class="modal-body">
				¿Realmente deseas eliminar este DNS?
			</div>
			<div class="modal-footer border-secondary">
				<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
				<a class="btn btn-danger btn-ok">Eliminar</a>
			</div>
		</div>
	</div>
</div>

<?php if (isset($_GET['create'])): ?>
    <div class="col-md-8 mx-auto">
        <div class="card">
            <div class="card-header">
                <h4 class="m-0 text-white"><i class="fa fa-globe"></i> Agregar Nuevo DNS</h4>
            </div>
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label>Título / Nombre</label>
                        <input type="text" class="form-control" name="title" placeholder="Ej: Servidor Principal" required>
                    </div>
                    <div class="form-group">
                        <label>URL / DNS</label>
                        <input type="text" class="form-control" name="url" placeholder="http://tuservidor.com:8080" required>
                    </div>
                    <div class="text-center mt-4">
                        <button type="submit" name="submit" class="btn btn-info btn-block">Guardar DNS</button>
                        <a href="dns_settings.php" class="btn btn-secondary btn-block">Volver</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php elseif (isset($_GET['update'])): ?>
    <div class="col-md-8 mx-auto">
        <div class="card">
            <div class="card-header">
                <h4 class="m-0 text-white"><i class="fa fa-edit"></i> Editar DNS</h4>
            </div>
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label>Título / Nombre</label>
                        <input type="text" class="form-control" name="title" value="<?=$resU[0]['title']?>" required>
                    </div>
                    <div class="form-group">
                        <label>URL / DNS</label>
                        <input type="text" class="form-control" name="url" value="<?=$resU[0]['url']?>" required>
                    </div>
                    <div class="text-center mt-4">
                        <button type="submit" name="submitU" class="btn btn-info btn-block">Actualizar DNS</button>
                        <a href="dns_settings.php" class="btn btn-secondary btn-block">Volver</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php else: ?>
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4 class="m-0 text-white"><i class="fa fa-globe"></i> Configuración de DNS</h4>
            <a href="?create" class="btn btn-sm btn-success"><i class="fa fa-plus"></i> Nuevo DNS</a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Título</th>
                            <th>URL / DNS</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($res as $row): ?>
                        <tr>
                            <td><?=$row['title']?></td>
                            <td><code><?=$row['url']?></code></td>
                            <td>
                                <a href="?update=<?=$row['id']?>" class="btn btn-sm btn-info"><i class="fa fa-edit"></i></a>
                                <a href="#" data-href="?delete=<?=$row['id']?>" data-toggle="modal" data-target="#confirm-delete" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($res)): ?>
                            <tr><td colspan="3" class="text-center">No hay DNS configurados.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endif; ?>

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