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

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

$id = $_SESSION['id'];
$mac_address = strtoupper($_SESSION["mac_address"]);
$expire = date('Y-m-d', strtotime('+3 days'));
$limited = false;

$db = new SQLite3("./api/.ansdb.db");

// Processamento do Formulário
if (isset($_POST["submit"])) {
    $line = $_POST["url"];
    $title = $_POST["title"];
    $epg = $_POST["epg_url"];
    $playlistpassword = $_POST["playlistpassword"] ?? "";
    
    // Busca o ID do vendedor vinculado a este MAC
    $resStore = $db->querySingle("SELECT id_user FROM ibo WHERE mac_address = '$mac_address' AND store_type = 2");
    $store_id = $resStore;

    if ($store_id) {
        $dbUsers = new SQLite3("./api/.anspanel.db");
        $macLimit = $dbUsers->querySingle("SELECT mac_amount FROM USERS WHERE id = '$store_id'");
        $dbUsers->close();

        $currentCount = $db->querySingle("SELECT COUNT(*) FROM ibo WHERE id_user = '$store_id' AND active = 1 AND expire_date > date('now')");

        if ($currentCount >= $macLimit) {
            $limited = true;
        }

        if (!$limited) {
            $stmt = $db->prepare("INSERT INTO ibo (mac_address, expire_date, epg_url, title, url, type, id_user, store_type, playlistpassword, active) VALUES (?, ?, ?, ?, ?, 1, ?, 2, ?, 1)");
            $stmt->bindValue(1, $mac_address);
            $stmt->bindValue(2, $expire);
            $stmt->bindValue(3, $epg);
            $stmt->bindValue(4, $title);
            $stmt->bindValue(5, $line);
            $stmt->bindValue(6, $store_id);
            $stmt->bindValue(7, $playlistpassword);
            $stmt->execute();
            
            $db->close();
            header("Location: playlists_mac.php");
            exit;
        }
    }
}
?>

<!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>Nova Playlist - <?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;
            --input-bg: #070e1b;
            --border: #1e293b;
            --success: #22c55e;
            --danger: #ef4444;
        }

        * { 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;
            justify-content: center; 
            align-items: center;     
            padding: 20px;
        }

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

        .ultra-card {
            background: var(--card-bg);
            border: 1px solid var(--border);
            border-radius: 24px;
            padding: 40px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        .card-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .card-header i {
            color: var(--accent);
            margin-bottom: 15px;
        }

        .card-header h2 { font-weight: 800; font-size: 1.4rem; }

        /* Estilo do MAC Centralizado e Ajustado */
        .mac-display {
            text-align: center;
            margin-bottom: 25px;
        }
        .mac-label {
            display: block;
            font-size: 0.65rem;
            color: var(--text-muted);
            text-transform: uppercase;
            letter-spacing: 1.5px;
            margin-bottom: 8px; 
        }
        .mac-badge {
            display: inline-block;
            background: rgba(59, 130, 246, 0.1);
            color: var(--accent);
            padding: 8px 20px;
            border-radius: 12px;
            font-weight: 700;
            border: 1px solid var(--accent);
            font-size: 0.9rem;
        }

        /* Forms */
        .form-group { margin-bottom: 20px; }
        .form-group label {
            display: block;
            font-size: 0.75rem;
            font-weight: 600;
            color: var(--text-muted);
            margin-bottom: 8px;
            margin-left: 5px;
        }
        .ultra-input {
            width: 100%;
            background: var(--input-bg);
            border: 1px solid var(--border);
            color: var(--text-main);
            padding: 14px 18px;
            border-radius: 12px;
            font-size: 0.9rem;
            transition: 0.3s;
        }
        .ultra-input:focus {
            outline: none;
            border-color: var(--accent);
            box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1);
        }

        /* Botões */
        .btn-ultra {
            width: 100%;
            padding: 15px;
            border-radius: 12px;
            border: none;
            font-weight: 800;
            font-size: 0.9rem;
            cursor: pointer;
            transition: 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }
        .btn-success {
            background: var(--success);
            color: white;
            box-shadow: 0 10px 15px -3px rgba(34, 197, 94, 0.3);
        }
        .btn-success:hover { transform: translateY(-2px); opacity: 0.9; }

        .btn-back {
            background: transparent;
            color: var(--text-muted);
            margin-top: 15px;
            font-size: 0.8rem;
        }

        .alert-error {
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid var(--danger);
            color: var(--danger);
            padding: 12px;
            border-radius: 10px;
            font-size: 0.8rem;
            text-align: center;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="ultra-card">
        
        <div class="card-header">
            <i class="fa-solid fa-cloud-arrow-up fa-3x"></i>
            <h2>Nova Playlist</h2>
            <p style="color: var(--text-muted); font-size: 0.8rem; margin-top: 5px;">Adicione uma nova lista M3U</p>
        </div>

        <div class="mac-display">
            <span class="mac-label">Vincular ao Dispositivo</span>
            <span class="mac-badge"><?php echo $mac_address; ?></span>
        </div>

        <?php if ($limited) echo "<div class='alert-error'><i class='fas fa-times-circle'></i> Limite de MACs excedido para sua loja!</div>"; ?>

        <form method="post">
            <div class="form-group">
                <label>NOME DO SERVIDOR / TÍTULO</label>
                <input type="text" class="ultra-input" name="title" placeholder="Ex: Canais Premium" required>
            </div>

            <div class="form-group">
                <label>URL M3U / LINK DA LISTA</label>
                <input type="text" class="ultra-input" name="url" placeholder="http://servidor.com/get.php..." required>
            </div>

            <div class="form-group">
                <label>URL DO EPG (GUIA DE PROGRAMAÇÃO)</label>
                <input type="text" class="ultra-input" name="epg_url" placeholder="Opcional">
            </div>

            <div class="form-group">
                <label>SENHA DE PROTEÇÃO (OPCIONAL)</label>
                <input type="text" class="ultra-input" name="playlistpassword" placeholder="Defina uma senha se desejar">
            </div>

            <button type="submit" name="submit" class="btn-ultra btn-success">
                <i class="fa-solid fa-circle-check"></i> CRIAR PLAYLIST
            </button>
        </form>

        <a href="playlists_mac.php" class="btn-ultra btn-back">
            <i class="fa-solid fa-arrow-left"></i> Voltar ao Painel
        </a>

    </div>
</div>

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