<?php
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
session_start();

if (!isset($_SESSION['mac_address'])) {
    header("Location: login_mac.php");
    exit;
}

$id_mac = $_SESSION['id'];
$mac_address = $_SESSION['mac_address'];
$db = new SQLite3("./api/.ansdb.db");

$macRes = $db->query("SELECT * FROM ibo WHERE mac_address = '$mac_address' AND store_type = 2");
$row_check = $macRes->fetchArray();
$validStore = (!empty($row_check['id_user'])) ? true : false;

if(!$validStore && isset($_POST['store_link'])){
    $dbUsers = new SQLite3("./api/.anspanel.db");
    $store_id = $_POST['store_id'];
    $exists = $dbUsers->querySingle("SELECT COUNT(*) FROM USERS WHERE id = '$store_id'");
    
    if($exists > 0){
        $macLimit = $dbUsers->querySingle("SELECT mac_amount FROM USERS WHERE id = '$store_id'");
        $currentActive = $db->querySingle("SELECT COUNT(*) FROM ibo WHERE id_user = '$store_id' AND active = 1 AND expire_date > date('now')");
        
        if($currentActive < $macLimit){
            $db->exec("UPDATE ibo SET id_user = '$store_id' WHERE mac_address = '$mac_address' AND store_type = 2");
            $validStore = true;
        } else { $limited = true; }
    } else { $storeExists = false; }
    $dbUsers->close();
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Painel Premium - <?php echo $mac_address; ?></title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;800&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --bg-body: #020617;
            --card-bg: #0f172a;
            --accent: #3b82f6;
            --text-main: #f8fafc;
            --text-muted: #94a3b8;
            --success: #22c55e;
            --warning: #f59e0b;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            background-color: var(--bg-body);
            color: var(--text-main);
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center; /* Centraliza horizontalmente */
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 1100px;
        }

        /* Nav Superior */
        .ultra-nav {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px;
            background: var(--card-bg);
            border: 1px solid #1e293b;
            border-radius: 20px;
            margin-bottom: 30px;
            width: 100%;
        }

        .user-info { display: flex; align-items: center; gap: 15px; }
        
        .label-device {
            display: block;
            font-size: 0.65rem;
            color: var(--text-muted);
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 5px; /* Ajuste para não colar no MAC */
        }

        .mac-badge {
            display: inline-block;
            background: rgba(59, 130, 246, 0.1);
            color: var(--accent);
            padding: 6px 15px;
            border-radius: 10px;
            font-weight: 700;
            border: 1px solid var(--accent);
            font-size: 0.9rem;
        }

        .btn-logout {
            color: #ef4444;
            text-decoration: none;
            font-weight: 700;
            background: rgba(239, 68, 68, 0.1);
            padding: 10px 20px;
            border-radius: 12px;
            transition: 0.3s;
        }
        .btn-logout:hover { background: #ef4444; color: white; }

        /* Card Centralizado */
        .ultra-card {
            background: var(--card-bg);
            border: 1px solid #1e293b;
            border-radius: 24px;
            padding: 35px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            width: 100%;
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .btn-create {
            background: var(--accent);
            color: white;
            text-decoration: none;
            padding: 12px 24px;
            border-radius: 12px;
            font-weight: 600;
            font-size: 0.9rem;
            transition: 0.3s;
            box-shadow: 0 10px 15px -3px rgba(59, 130, 246, 0.3);
        }

        /* Tabela */
        .table-responsive { width: 100%; overflow-x: auto; border-radius: 15px; }
        table { width: 100%; border-collapse: collapse; background: #070e1b; }
        th {
            text-align: left;
            padding: 18px;
            background: #1e293b;
            color: var(--accent);
            font-size: 0.7rem;
            text-transform: uppercase;
        }
        td { padding: 20px 18px; border-bottom: 1px solid #1e293b; font-size: 0.85rem; }

        /* Botão de Editar com Ícone */
        .btn-edit-action {
            background: var(--accent);
            color: white !important;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 10px;
            text-decoration: none;
            transition: 0.3s;
            margin: 0 auto;
        }
        .btn-edit-action:hover {
            transform: scale(1.1);
            background: #2563eb;
            box-shadow: 0 0 15px var(--accent);
        }

        .status-expire { color: var(--warning); font-weight: 600; }
        
        /* Box de Ativação */
        .activation-box {
            max-width: 450px;
            text-align: center;
            margin: auto;
        }
        .input-ultra {
            width: 100%;
            background: #070e1b;
            border: 1px solid #1e293b;
            color: white;
            padding: 15px;
            border-radius: 12px;
            margin: 20px 0;
            text-align: center;
            font-size: 1rem;
        }
    </style>
</head>
<body>

<div class="container">
    <nav class="ultra-nav">
        <div class="user-info">
            <i class="fa-solid fa-microchip fa-2xl" style="color: var(--accent);"></i>
            <div>
                <span class="label-device">Dispositivo Autorizado</span>
                <span class="mac-badge"><?php echo $mac_address; ?></span>
            </div>
        </div>
        <a href="logout_playlist.php" class="btn-logout">
            <i class="fa-solid fa-right-from-bracket"></i>
        </a>
    </nav>

    <?php if($validStore): ?>
        <div class="ultra-card">
            <div class="card-header">
                <h2 style="font-weight: 800;">Playlists</h2>
                <a href="playlists_create.php" class="btn-create">
                    <i class="fa-solid fa-plus mr-1"></i> ADICIONAR
                </a>
            </div>

            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>NOME / USER</th>
                            <th>VENCIMENTO</th>
                            <th>SERVIDOR</th>
                            <th style="text-align:center;">EDITAR</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $macRes = $db->query("SELECT * FROM ibo WHERE mac_address = '$mac_address' AND store_type = 2");
                        while ($row = $macRes->fetchArray()):
                            $display_name = ($row['dns'] == NULL) ? "List-".$row['id'] : $row['username'];
                        ?>
                        <tr>
                            <td>#<?php echo $row['id']; ?></td>
                            <td style="color: white; font-weight: 600;"><?php echo $display_name; ?></td>
                            <td><span class="status-expire"><?php echo $row['expire_date']; ?></span></td>
                            <td><?php echo substr($row['url'] ?? $row['dns'], 0, 20); ?>...</td>
                            <td>
                                <a href="playlists_update.php?update=<?php echo $row['id']; ?>" class="btn-edit-action">
                                    <i class="fa-solid fa-pen-to-square"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>

    <?php else: ?>
        <div class="ultra-card activation-box">
            <i class="fa-solid fa-lock fa-3x" style="color: var(--accent); margin-bottom: 20px;"></i>
            <h2 style="font-weight: 800;">Vincular Loja</h2>
            <p style="color: var(--text-muted); font-size: 0.9rem;">Insira o código do seu revendedor para ativar.</p>

            <form method="post">
                <input type="text" name="store_id" class="input-ultra" placeholder="Código do Revendedor" required>
                <button type="submit" name="store_link" class="btn-create" style="width: 100%; border: none; cursor: pointer;">
                    ATIVAR AGORA
                </button>
            </form>
        </div>
    <?php endif; ?>
</div>

</body>
</html>
<?php $db->close(); ?>