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

// Obtener estadísticas de clientes
$clients = getUserClients($db, $currentUser['id']);
$total_clients = count($clients);
$expired_clients = 0;
$near_expiry_clients = 0;
$today = time();

foreach ($clients as $client) {
    $expiry_time = strtotime($client['expire_date']);
    if ($expiry_time < $today) {
        $expired_clients++;
    } elseif ($expiry_time < ($today + (1 * 24 * 60 * 60))) { // 1 día
        $near_expiry_clients++;
    }
}

// Lógica de DNS (Solo para admin)
$table_dns = "dns";
$dns_list = $db->select($table_dns, '*', '', '');

?>

<div class="row mb-4">
    <div class="col-4">
        <div class="card stat-card">
            <div class="stat-value stat-total"><?=$total_clients?></div>
            <div class="stat-label">Usuarios Totales</div>
        </div>
    </div>
    <div class="col-4">
        <div class="card stat-card">
            <div class="stat-value stat-expired"><?=$expired_clients?></div>
            <div class="stat-label">Vencidos</div>
        </div>
    </div>
    <div class="col-4">
        <div class="card stat-card">
            <div class="stat-value stat-warning"><?=$near_expiry_clients?></div>
            <div class="stat-label">Por Vencer</div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4 class="m-0 text-white"><i class="fa fa-info-circle"></i> Resumen del Panel</h4>
            </div>
            <div class="card-body">
                <div class="text-center py-4">
                    <h2 class="welcome-title mb-3">Bienvenido, <?=$currentUser['username']?></h2>
                    <p class="lead mb-4">Gestiona tus clientes y revendedores de forma eficiente desde este panel centralizado.</p>
                    <hr class="my-4 border-secondary">
                    <div class="row justify-content-center">
                        <div class="col-6 col-md-auto mb-2">
                            <a href="clients.php?create" class="btn btn-info btn-lg btn-block">
                                <i class="fa fa-plus"></i> Nuevo Registro
                            </a>
                        </div>
                        <div class="col-6 col-md-auto mb-2">
                            <a href="clients.php" class="btn btn-primary btn-lg btn-block">
                                <i class="fa fa-users"></i> Ver Clientes
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php if ($userRole === 'admin'): ?>
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4 class="m-0 text-white"><i class="fa fa-globe"></i> DNS Configurados</h4>
            </div>
            <div class="card-body">
                <?php if (!empty($dns_list)): ?>
                <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 ($dns_list as $dns): ?>
                            <tr>
                                <td><?=$dns['title']?></td>
                                <td><code><?=$dns['url']?></code></td>
                                <td>
                                    <a href="dns_settings.php?update=<?=$dns['id']?>" class="btn btn-sm btn-info"><i class="fa fa-edit"></i></a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <p class="text-center text-muted mb-0">No hay DNS configurados. <a href="dns_settings.php?create">Agregar uno</a></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

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