<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$db = new SQLite3('./a/.eggziedb.db');
$db->exec('CREATE TABLE IF NOT EXISTS ibo(id INTEGER PRIMARY KEY NOT NULL, mac_address VARCHAR(100), username VARCHAR(100), password VARCHAR(100), expire_date VARCHAR(100), url VARCHAR(100), title VARCHAR(100), created_at VARCHAR(100))');

if (isset($_POST['submit'])) {
    $we = strtotime($_POST['expire_date']);
    $ne = empty($_POST['expire_date']) ? '9000-12-30' : date('Y-m-d', $we);
    $start = date('Y-m-d');
    $end = date('h:m:s');
    $full = $start . 'T' . $end . '.000000Z';

    // Preparar a consulta SQL
    $stmt = $db->prepare('INSERT INTO ibo(mac_address, username, password, expire_date, url, title, created_at) VALUES(:mac_address, :username, :password, :expire_date, :url, :title, :created_at)');
    
    // Vincular os parâmetros
    $stmt->bindValue(':mac_address', strtoupper($_POST['mac_address']), SQLITE3_TEXT);
    $stmt->bindValue(':username', $_POST['username'], SQLITE3_TEXT);
    $stmt->bindValue(':password', $_POST['password'], SQLITE3_TEXT);
    $stmt->bindValue(':expire_date', $ne, SQLITE3_TEXT);
    $stmt->bindValue(':url', $_POST['dns'], SQLITE3_TEXT);
    $stmt->bindValue(':title', $_POST['title'], SQLITE3_TEXT);
    $stmt->bindValue(':created_at', $full, SQLITE3_TEXT);

    // Executar a consulta
    $stmt->execute();

    // Fechar o banco
    $db->close();

    // Redirecionar para a página de usuários
    header('Location: users.php');
    exit;
}

$playlistNames = [
    'Ultimate Entertainment',
    'Global TV Hub',
    'Prime Streaming Zone',
    'All-in-One IPTV Lounge',
    'HD Channel Showcase',
    // Adicione mais opções se necessário
];

$randomPlaylistName = $playlistNames[array_rand($playlistNames)];

include 'includes/header.php';
?>

<div class="container-fluid">
    <h1 class="h3 mb-1 text-gray-800">Cadastrar Cliente (Xtream Codes)</h1>

    <div class="card border-left-primary shadow h-100 card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-user"></i> Cadastro Rápido</h6>
        </div>
        <div class="card-body">
            <form method="post">
                <div class="form-group">
                    <label for="mac_address"><strong>Endereço MAC</strong></label>
                    <input type="text" class="form-control text-primary" name="mac_address" id="mac_address" placeholder="Digite o Endereço MAC" required />
                </div>

                <!-- Campo 'Nome' oculto, preenchido automaticamente com o valor do campo 'Usuário' -->
                <div class="form-group" style="display: none;">
                    <label for="title"><strong>Nome</strong></label>
                    <input type="text" class="form-control text-primary" name="title" id="title" value="<?php echo $randomPlaylistName; ?>" placeholder="Nome do Cliente" />
                </div>

                <div class="form-group">
                    <label for="dns"><strong>DNS</strong></label>
                    <input type="text" class="form-control text-primary" name="dns" id="dns" placeholder="Digite o DNS" required />
                </div>

                <div class="form-group">
                    <label for="username"><strong>Usuário</strong></label>
                    <input type="text" class="form-control text-primary" name="username" id="username" placeholder="Usuário" required />
                </div>

                <div class="form-group">
                    <label for="password"><strong>Senha</strong></label>
                    <input type="text" class="form-control text-primary" name="password" id="password" placeholder="Senha" required />
                </div>

                <!-- Campo de validade oculto -->
                <div class="form-group" style="display: none;">
                    <label for="expire_date"><strong>Data de Expiração</strong></label>
                    <input type="text" class="form-control text-primary" name="expire_date" id="expire_date" placeholder="Data de Expiração (ANO-MES-DIA)" />
                </div>

                <button type="submit" name="submit" class="btn btn-success btn-icon-split">
                    <span class="icon text-white-50"><i class="fas fa-check"></i></span>
                    <span class="text">Cadastrar</span>
                </button>
            </form>
        </div>
    </div>
</div>

<script>
// Preencher o campo "Nome" automaticamente com o "Usuário" e ocultá-lo
document.getElementById('username').addEventListener('input', function() {
    var username = document.getElementById('username').value;
    document.getElementById('title').value = username; // Preenche o campo 'Nome' com o valor do campo 'Usuário'
});
</script>

<?php include 'includes/footer.php'; ?>
</html>
