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

$db = new SQLite3("./api/.ansdb.db");
$res = $db->query("SELECT * FROM ibo WHERE id='" . $_GET["update"] . "'");
$row = $res->fetchArray();

if (!$row) {
    header("Location: playlists_mac.php");
    exit;
}

$id_mac = $row["id"];
$mac_address = strtoupper($row["mac_address"]);
$title = $row["title"];
$url = $row["url"];
$epg_url = $row["epg_url"];
$playlistpassword = $row['playlistpassword'];

$pwd_req = !empty($playlistpassword);
$auth = false;
$invalid = false;

// Lógica de Autenticação da Playlist (se houver senha)
if (isset($_POST['auth'])) {
    $auth = ($playlistpassword == $_POST['password']);
    if (!$auth) $invalid = true;
} else if (isset($_POST["submit"])) {
    $auth = true;
    $new_title = $_POST["title"];
    $new_url = $_POST["url"];
    $new_epg = $_POST["epg_url"];
    $new_pwd = $_POST["playlistpassword"];

    $db->exec("UPDATE ibo SET title='$new_title', url='$new_url', epg_url='$new_epg', playlistpassword='$new_pwd' WHERE id='$id_mac'");
    $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>Editar 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; /* Centraliza Horizontal */
            align-items: center;     /* Centraliza Vertical */
            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 com espaçamento corrigido */
        .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; /* Espaço entre label e o MAC */
        }
        .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">
        
        <?php if (!$pwd_req || $auth): ?>
            <div class="card-header">
                <i class="fa-solid fa-pen-to-square fa-3x"></i>
                <h2>Editar Playlist</h2>
            </div>

            <div class="mac-display">
                <span class="mac-label">Endereço de Hardware</span>
                <span class="mac-badge"><?php echo $mac_address; ?></span>
            </div>

            <form method="post">
                <input type="hidden" name="id" value="<?php echo $id_mac; ?>">

                <div class="form-group">
                    <label>NOME DO SERVIDOR</label>
                    <input type="text" class="ultra-input" name="title" value="<?php echo $title; ?>" required>
                </div>

                <div class="form-group">
                    <label>URL M3U8 / LISTA</label>
                    <input type="text" class="ultra-input" name="url" value="<?php echo $url; ?>" required>
                </div>

                <div class="form-group">
                    <label>URL DO EPG (OPCIONAL)</label>
                    <input type="text" class="ultra-input" name="epg_url" value="<?php echo $epg_url; ?>">
                </div>

                <div class="form-group">
                    <label>SENHA DA PLAYLIST (OPCIONAL)</label>
                    <input type="text" class="ultra-input" name="playlistpassword" value="<?php echo $playlistpassword; ?>" placeholder="Deixe vazio para não usar senha">
                </div>

                <button type="submit" name="submit" class="btn-ultra btn-success">
                    <i class="fa-solid fa-floppy-disk"></i> SALVAR ALTERAÇÕES
                </button>
            </form>

        <?php else: ?>
            <div class="card-header">
                <i class="fa-solid fa-lock fa-3x"></i>
                <h2>Playlist Protegida</h2>
                <p style="color: var(--text-muted); font-size: 0.8rem; margin-top: 10px;">Esta lista requer uma senha para edição.</p>
            </div>

            <?php if ($invalid) echo "<div class='alert-error'>Senha Incorreta! Tente novamente.</div>"; ?>

            <form method="post">
                <div class="form-group">
                    <label>DIGITE A SENHA DA LISTA</label>
                    <input type="password" class="ultra-input" name="password" autofocus required>
                </div>
                <button type="submit" name="auth" class="btn-ultra btn-success">
                    <i class="fa-solid fa-unlock"></i> DESBLOQUEAR
                </button>
            </form>
        <?php endif; ?>

        <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(); ?>