<?php
require_once 'protege.php';
require_once '../config.php';

if (isset($_GET['remover'])) {
    $id = (int) $_GET['remover'];

    $stmt = $pdo->prepare("DELETE FROM ativacoes WHERE id = ?");
    $stmt->execute([$id]);

    header('Location: computadores.php');
    exit;
}

$busca = trim($_GET['busca'] ?? '');

if ($busca) {
    $stmt = $pdo->prepare("
        SELECT 
            a.*,
            l.chave,
            l.tipo,
            l.status AS status_licenca,
            c.nome AS cliente_nome,
            c.email AS cliente_email
        FROM ativacoes a
        INNER JOIN licencas l ON l.id = a.licenca_id
        LEFT JOIN clientes c ON c.id = l.cliente_id
        WHERE 
            a.nome_pc LIKE ? OR
            a.usuario_windows LIKE ? OR
            a.hardware_id LIKE ? OR
            l.chave LIKE ? OR
            c.nome LIKE ? OR
            c.email LIKE ?
        ORDER BY a.ultimo_acesso DESC
    ");
    $like = "%{$busca}%";
    $stmt->execute([$like, $like, $like, $like, $like, $like]);
} else {
    $stmt = $pdo->query("
        SELECT 
            a.*,
            l.chave,
            l.tipo,
            l.status AS status_licenca,
            c.nome AS cliente_nome,
            c.email AS cliente_email
        FROM ativacoes a
        INNER JOIN licencas l ON l.id = a.licenca_id
        LEFT JOIN clientes c ON c.id = l.cliente_id
        ORDER BY a.ultimo_acesso DESC
    ");
}

$computadores = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Computadores - PromoCreative LED Mapper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{box-sizing:border-box}
body{
    margin:0;
    background:
        radial-gradient(circle at top left,rgba(0,170,255,.16),transparent 32%),
        radial-gradient(circle at bottom right,rgba(255,122,0,.13),transparent 28%),
        #060b16;
    color:#fff;
    font-family:Arial,Helvetica,sans-serif;
}
.sidebar{
    width:280px;height:100vh;position:fixed;left:0;top:0;
    background:rgba(8,15,29,.96);
    border-right:1px solid rgba(255,255,255,.08);
    padding:26px;
}
.logo{margin-bottom:34px}
.logo strong{display:block;font-size:21px;line-height:1.2}
.logo span{display:block;margin-top:8px;color:#7f95bd;font-size:13px}
.sidebar a{
    display:block;color:#b7c7e8;text-decoration:none;
    padding:14px 15px;border-radius:13px;margin-bottom:8px;
}
.sidebar a:hover,.sidebar a.active{
    background:linear-gradient(135deg,rgba(255,122,0,.18),rgba(0,170,255,.18));
    color:#fff;
}
.main{margin-left:280px;padding:36px}
.header{
    display:flex;justify-content:space-between;align-items:center;
    margin-bottom:26px;gap:16px;
}
.header h1{margin:0;font-size:30px}
.header p{margin:8px 0 0;color:#8fa3c7}
.card{
    background:rgba(13,24,43,.92);
    border:1px solid rgba(255,255,255,.08);
    border-radius:24px;
    padding:24px;
    box-shadow:0 24px 70px rgba(0,0,0,.35);
    overflow-x:auto;
}
.search{
    display:flex;gap:12px;margin-bottom:22px;
}
.search input{
    flex:1;
    padding:14px;
    border-radius:14px;
    border:1px solid rgba(255,255,255,.12);
    background:#08111f;
    color:#fff;
    outline:none;
}
.search button{
    padding:14px 18px;
    border:0;
    border-radius:14px;
    background:#13223a;
    color:#fff;
    cursor:pointer;
}
.table{width:100%;border-collapse:collapse;min-width:1050px}
.table th,.table td{
    padding:15px 13px;
    border-bottom:1px solid rgba(255,255,255,.07);
    text-align:left;
    font-size:14px;
    vertical-align:top;
}
.table th{color:#7f95bd;font-weight:normal}
.small{color:#7f95bd;font-size:13px}
.key{
    font-family:Consolas,monospace;
    color:#00d4ff;
    font-weight:bold;
}
.hardware{
    font-family:Consolas,monospace;
    color:#b8c7e6;
    word-break:break-all;
    max-width:280px;
}
.badge{
    display:inline-block;
    padding:6px 11px;
    border-radius:999px;
    font-size:12px;
}
.ativa{background:rgba(0,255,160,.12);color:#9fffc8}
.expirada{background:rgba(255,200,0,.12);color:#ffe58a}
.suspensa{background:rgba(255,122,0,.13);color:#ffc38a}
.bloqueada{background:rgba(255,60,60,.12);color:#ffc1c1}
.remove{
    display:inline-block;
    padding:8px 11px;
    border-radius:10px;
    background:rgba(255,60,60,.12);
    color:#ffb3b3;
    text-decoration:none;
}
.empty{
    text-align:center;
    padding:35px;
    color:#8fa3c7;
}
@media(max-width:950px){
    .sidebar{position:relative;width:100%;height:auto}
    .main{margin-left:0;padding:20px}
    .header{flex-direction:column;align-items:flex-start}
    .search{flex-direction:column}
}
</style>
</head>

<body>

<div class="sidebar">
    <div class="logo">
        <strong>PromoCreative<br>LED Mapper</strong>
        <span>Painel de licenças</span>
    </div>

    <a href="dashboard.php">Dashboard</a>
    <a href="clientes.php">Clientes</a>
    <a href="licencas.php">Licenças</a>
    <a href="gerar_licenca.php">Gerar Licença</a>
    <a class="active" href="computadores.php">Computadores</a>
    <a href="logout.php">Sair</a>
</div>

<div class="main">
    <div class="header">
        <div>
            <h1>Computadores ativados</h1>
            <p>Veja quais máquinas estão usando cada licença e remova acessos quando necessário.</p>
        </div>
    </div>

    <div class="card">
        <form class="search" method="GET">
            <input type="text" name="busca" value="<?= htmlspecialchars($busca); ?>" placeholder="Buscar por cliente, licença, PC, usuário ou Hardware ID">
            <button type="submit">Buscar</button>
        </form>

        <?php if (empty($computadores)): ?>
            <div class="empty">Nenhum computador ativado ainda.</div>
        <?php else: ?>
            <table class="table">
                <thead>
                    <tr>
                        <th>Cliente</th>
                        <th>Licença</th>
                        <th>Computador</th>
                        <th>Hardware ID</th>
                        <th>Ativado em</th>
                        <th>Último acesso</th>
                        <th>Status</th>
                        <th>Ação</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($computadores as $pc): ?>
                    <tr>
                        <td>
                            <?= htmlspecialchars($pc['cliente_nome'] ?? 'Sem cliente'); ?><br>
                            <span class="small"><?= htmlspecialchars($pc['cliente_email'] ?? '-'); ?></span>
                        </td>

                        <td>
                            <span class="key"><?= htmlspecialchars($pc['chave']); ?></span><br>
                            <span class="small"><?= htmlspecialchars($pc['tipo']); ?></span>
                        </td>

                        <td>
                            <?= htmlspecialchars($pc['nome_pc'] ?: 'Não informado'); ?><br>
                            <span class="small"><?= htmlspecialchars($pc['usuario_windows'] ?: '-'); ?></span>
                        </td>

                        <td>
                            <div class="hardware"><?= htmlspecialchars($pc['hardware_id']); ?></div>
                        </td>

                        <td><?= date('d/m/Y H:i', strtotime($pc['ativado_em'])); ?></td>
                        <td><?= date('d/m/Y H:i', strtotime($pc['ultimo_acesso'])); ?></td>

                        <td>
                            <span class="badge <?= htmlspecialchars($pc['status_licenca']); ?>">
                                <?= htmlspecialchars($pc['status_licenca']); ?>
                            </span>
                        </td>

                        <td>
                            <a class="remove"
                               href="computadores.php?remover=<?= (int)$pc['id']; ?>"
                               onclick="return confirm('Remover este computador desta licença?');">
                                Remover
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>

</body>
</html>