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

$db = new SQLite3('a/.eggziedb.db');
$db->exec("PRAGMA foreign_keys = ON;");

/* =========================
   EXCLUIR LINK
========================= */
if (isset($_GET['delete'])) {
    $delId = (int)$_GET['delete'];
    if ($delId > 0) {
        $stmt = $db->prepare("DELETE FROM client_links WHERE id = :id");
        $stmt->bindValue(':id', $delId, SQLITE3_INTEGER);
        $stmt->execute();
    }
    header("Location: create_link.php");
    exit;
}

/* =========================
   VALIDAR ID
========================= */
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    header("Location: create_link.php");
    exit;
}

/* =========================
   BUSCAR DADOS
========================= */
$stmt = $db->prepare("
    SELECT 
        cl.id,
        cl.url,
        i.id AS client_id,
        i.title,
        i.mac_address
    FROM client_links cl
    JOIN ibo i ON i.id = cl.ibo_id
    WHERE cl.id = :id
    LIMIT 1
");
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
$data = $stmt->execute()->fetchArray(SQLITE3_ASSOC);

if (!$data) {
    header("Location: create_link.php");
    exit;
}

/* =========================
   SALVAR EDIÇÃO
========================= */
$msg = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $link = trim($_POST['link'] ?? '');

    if ($link !== '') {
        $stmt = $db->prepare("
            UPDATE client_links
            SET url = :url
            WHERE id = :id
        ");
        $stmt->bindValue(':url', $link, SQLITE3_TEXT);
        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
        $stmt->execute();

        $msg = 'Link atualizado com sucesso';
        $success = true;
    } else {
        $msg = 'Informe o link';
    }
}

include 'includes/header.php';
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Editar Link</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    body{ font-family:"Segoe UI",sans-serif; background: radial-gradient(circle at top,#1b2a45,#03040a); color:#f0f4ff; padding:20px; }
    .wrapper{ max-width:600px; margin:40px auto; }
    .card{ background:rgba(15,27,52,0.9); padding:25px; border-radius:18px; border:1px solid rgba(114,159,255,.3); }
    h2{margin-bottom:20px}
    label{ font-size:13px; color:#8ba7ff; display:block; margin-bottom:8px; }
    .info{ font-size:14px; margin-bottom:15px; padding:10px; background:rgba(0,0,0,0.2); border-radius:8px; }
    input{ width:100%; padding:12px; background:#090e1e; border:1px solid #7191e8; border-radius:8px; color:#fff; margin-bottom:20px; }
    .btn{ padding:12px; width:100%; border:none; border-radius:10px; background:linear-gradient(135deg,#52ffb8,#2fcc8a); font-weight:700; cursor:pointer; color:#050712; }
    .alert{ padding:15px; border-radius:10px; margin-bottom:20px; font-weight:bold; }
    .alert-success{ background:rgba(82,255,184,0.1); color:#52ffb8; border:1px solid #52ffb8; }
    .alert-danger{ background:rgba(255,82,82,0.1); color:#ff6b81; border:1px solid #ff6b81; }
    .actions{ display:flex; gap:10px; margin-top:20px; }
    .actions a{ flex:1; text-align:center; padding:12px; border-radius:10px; text-decoration:none; font-weight:600; }
    .btn-delete{ background:#ff6b81; color:#fff; }
    .btn-back{ color:#8ba7ff; text-align:center; display:block; margin-top:20px; text-decoration:none; }
</style>
</head>
<body>
<div class="wrapper">
    <div class="card">
        <h2><i class="fas fa-edit"></i> Editar Link</h2>

        <?php if ($msg): ?>
            <div class="alert <?= $success ? 'alert-success' : 'alert-danger' ?>"><?= htmlspecialchars($msg) ?></div>
        <?php endif; ?>

        <div class="info"><strong>Cliente:</strong> <?= htmlspecialchars($data['title'] ?? 'Sem Nome') ?></div>
        <div class="info"><strong>MAC:</strong> <?= htmlspecialchars($data['mac_address']) ?></div>

        <form method="post">
            <label>Link de Pagamento</label>
            <input type="url" name="link" value="<?= htmlspecialchars($data['url']) ?>" required>
            <button type="submit" class="btn">Salvar Alterações</button>
        </form>

        <div class="actions">
            <a class="btn-delete" href="?delete=<?= $data['id'] ?>&id=<?= $id ?>" onclick="return confirm('Excluir este link?')">Excluir Link</a>
        </div>

        <a class="btn-back" href="create_link.php">← Voltar para Lista</a>
    </div>
</div>
</body>
</html>
<?php $db->close(); ?>
