<?php
session_start();
include "../db.php";

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

if(isset($_POST['generate'])){

    $token  = bin2hex(random_bytes(16));

    $expiry_option = $_POST['expiry'];

    switch($expiry_option){
        case "1":
            $expiry = date("Y-m-d H:i:s", strtotime("+1 hour"));
            break;
        case "6":
            $expiry = date("Y-m-d H:i:s", strtotime("+6 hours"));
            break;
        case "24":
            $expiry = date("Y-m-d H:i:s", strtotime("+1 day"));
            break;
        case "72":
            $expiry = date("Y-m-d H:i:s", strtotime("+3 days"));
            break;
        default:
            $expiry = date("Y-m-d H:i:s", strtotime("+1 day"));
    }

    $stmt = $conn->prepare("INSERT INTO capture_links (token, expires_at) VALUES (?, ?)");
    $stmt->bind_param("ss",$token,$expiry);
    $stmt->execute();

    $link = "https://img.urlnowshort.com/capture.php?token=".$token;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Generate Secure Link</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{
    background: linear-gradient(135deg,#0f2027,#203a43,#2c5364);
    min-height:100vh;
}
.page-wrapper{
    min-height:calc(100vh - 70px);
    display:flex;
    justify-content:center;
    align-items:center;
}
.card-custom{
    border-radius:20px;
    padding:40px;
}
</style>
</head>
<body>

<?php include "navbar.php"; ?>

<div class="page-wrapper">

<div class="card shadow-lg card-custom" style="width:100%; max-width:650px;">
    
    <h3 class="text-center mb-4 text-dark">🔐 Generate Secure Capture Link</h3>

    <form method="POST">

        <div class="mb-3">
            <label class="form-label fw-bold">Select Expiry Time:</label>
            <select name="expiry" class="form-select" required>
                <option value="1">1 Hour</option>
                <option value="6">6 Hours</option>
                <option value="24" selected>24 Hours</option>
                <option value="72">3 Days</option>
            </select>
        </div>

        <div class="text-center">
            <button name="generate" class="btn btn-success btn-lg px-5">
                Generate Link
            </button>
        </div>

    </form>

    <?php if(isset($link)){ ?>
        <hr>
        <div class="mt-3">
            <label class="form-label fw-bold">Generated Link:</label>
            <div class="input-group">
                <input type="text" id="generatedLink" class="form-control" value="<?php echo $link; ?>" readonly>
                <button class="btn btn-primary" onclick="copyLink()">Copy</button>
            </div>
            <small class="text-muted">Valid until: <?php echo $expiry; ?></small>
        </div>
    <?php } ?>

</div>
</div>

<script>
function copyLink() {
    const copyText = document.getElementById("generatedLink");
    navigator.clipboard.writeText(copyText.value)
        .then(() => alert("Link copied successfully!"))
        .catch(() => alert("Copy failed"));
}
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>