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

$db = new SQLite3("a/.eggziedb.db");
$res = $db->query("SELECT * FROM ibo WHERE id='" . $_GET["update"] . "'");
$row = $res->fetchArray();
$id_mac = $row["id"];
$mac_address = $row["mac_address"];
$username = $row["username"];
$password = $row["password"];
$expire_date = $row["expire_date"];
$url = $row["url"];
$title = $row["title"];
$created_at = $row["created_at"];

if (isset($_POST["submit"])) {
    $we = strtotime($_POST["expire_date"]);
    $ne = date("Y-m-d", $we);
    
    $db->exec("UPDATE ibo SET
        mac_address='" . strtoupper($_POST["mac_address"]) . "',
        username='" . $_POST["username"] . "',
        password='" . $_POST["password"] . "',
        expire_date='" . $ne . "',
        url='" . $_POST["url"] . "',
        title='" . $_POST["title"] . "'
        WHERE id='" . $_POST["id"] . "'");
    
    $return = "users.php";
    header("Location: $return");
}

include "includes/header.php";
?>

<div class="container-fluid">
    <h1 class="h3 mb-1 text-gray-800">Atualizar Usuário</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> Editar Usuário</h6>
        </div>
        
        <div class="card-body">
            <form method="post">
                <input type="hidden" name="id" value="<?php echo $id_mac; ?>">
                
                <!-- Adicionando campo para o link M3U -->
                <div class="form-group">
                    <label class="control-label" for="m3u_address"><strong>Link M3U</strong></label>
                    <div class="input-group">
                        <input class="form-control text-primary" id="m3u_address" name="m3u_address" value="" type="text"/>
                        <div class="input-group-append">
                            <button class="btn btn-success" id="extract_button" onclick="extract(event)">
                                <span class="icon text-white-50"><i class="fa fa-expand"></i></span>
                                <span class="text">Extrair M3U</span>
                            </button>
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="mac_address"><strong>Endereço MAC</strong></label>
                    <input class="form-control text-primary" id="mac_address" name="mac_address" value="<?php echo $mac_address; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="username"><strong>Usuário</strong></label>
                    <input class="form-control text-primary" id="username" name="username" value="<?php echo $username; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="password"><strong>Senha</strong></label>
                    <input class="form-control text-primary" id="password" name="password" value="<?php echo $password; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="expire_date"><strong>Data de Expiração</strong></label>
                    <input class="form-control text-primary" id="expire_date" name="expire_date" value="<?php echo $expire_date; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="url"><strong>Link M3U</strong></label>
                    <input class="form-control text-primary" id="url" name="url" value="<?php echo $url; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <label class="control-label" for="title"><strong>Nome</strong></label>
                    <input class="form-control text-primary" id="title" name="title" value="<?php echo $title; ?>" type="text" required/>
                </div>
                
                <div class="form-group">
                    <button class="btn btn-success btn-icon-split" name="submit" type="submit">
                        <span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Atualizar</span>
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php
include "includes/footer.php";
?>

<script>
function extract(event) {
    event.preventDefault(); // Prevenir atualização da página
    
    var m3uLink = document.getElementById("m3u_address").value;
    
    // Extrair apenas o DNS do link M3U e adicionar prefixo "http://"
    var dns = extractDNS(m3uLink);
    var completeURL = "http://" + dns;
    document.getElementById("url").value = completeURL;
    
    // Extrair nome de usuário e senha, se presentes, e preencher os campos correspondentes
    var username = extractUsername(m3uLink);
    document.getElementById("username").value = username;
    
    var password = extractPassword(m3uLink);
    document.getElementById("password").value = password;
    
    // Preencher o campo "Nome" com o nome de usuário
    document.getElementById("title").value = username;
}

function extractDNS(url) {
    var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
    return matches && matches[1];
}

function extractUsername(url) {
    var usernameRegex = /username=([^&]+)/;
    var match = url.match(usernameRegex);
    return match ? decodeURIComponent(match[1]) : '';
}

function extractPassword(url) {
    var passwordRegex = /password=([^&]+)/;
    var match = url.match(passwordRegex);
    return match ? decodeURIComponent(match[1]) : '';
}
</script>
