<?php
$pdo=new PDO("mysql:host=localhost;dbname=iolaptop_link;charset=utf8mb4","iolaptop_user","@Er.3003237");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

define("SECRET_KEY","9f8a3c7e2b1d4f6a8c9e0b2d3f4a5c6e");

if($_SERVER['REQUEST_METHOD']==='POST'){
    sleep(2); // anti brute-force

    $code = trim($_POST['code'] ?? '');

    if(strlen($code) !== 6){
        $error="کد نامعتبر است";
    } else {

        $stmt=$pdo->prepare("SELECT * FROM links WHERE id=?");
        $stmt->execute([$code]);
        $row=$stmt->fetch();

        if($row && time() <= $row['expire_at']){

            $text=openssl_decrypt(
                $row['text'],
                "AES-256-CBC",
                SECRET_KEY,
                0,
                hex2bin($row['iv'])
            );

            if($text === false){
                $error="خطا در رمزگشایی";
            } else {
                // حذف بعد از مشاهده
                $pdo->prepare("DELETE FROM links WHERE id=?")->execute([$code]);
                $found=true;
                $expireLeft = $row['expire_at'] - time();
            }

        } else {
            $error="کد نامعتبر یا منقضی شده";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">

<style>
@font-face { font-family:dana; src:url('/font/dana-regular.woff2'); }
*{font-family:dana; box-sizing:border-box;}

body{
    background:#020617;
    display:flex;
    justify-content:center;
    align-items:center;
    height:100vh;
    color:#fff;
}

.card{
    width:420px;
    background:#1e293b;
    padding:25px;
    border-radius:16px;
    text-align:center;
}

input,button{
    width:100%;
    padding:12px;
    margin-top:10px;
    border:none;
    border-radius:10px;
}

button{
    background:#3b82f6;
    color:#fff;
    cursor:pointer;
}

.text{
    background:#020617;
    padding:15px;
    border-radius:10px;
    margin-top:15px;
    direction:ltr;
    text-align:left;
    word-break:break-all;
    font-size:13px;
}

.copy{
    background:#22c55e;
}

.timer{
    margin-top:10px;
    font-size:13px;
    color:#94a3b8;
}
</style>

<script>
function copyText(){
    const btn = document.getElementById("copyBtn");
    const text = document.getElementById("t").innerText;

    navigator.clipboard.writeText(text);

    btn.innerHTML = "✅ کپی شد";
    btn.style.background = "#16a34a";

    setTimeout(()=>{
        btn.innerHTML = "📋 کپی متن";
        btn.style.background = "#22c55e";
    },2000);
}

// auto copy
function autoCopy(){
    const text = document.getElementById("t").innerText;
    navigator.clipboard.writeText(text);
}

// self destruct UI
function selfDestruct(){
    setTimeout(()=>{
        document.getElementById("t").innerText = "⛔️ این متن حذف شد";
    },15000);
}

// countdown
function startTimer(seconds){
    let t = seconds;
    const el = document.getElementById("timer");

    setInterval(()=>{
        if(t <= 0){
            el.innerText = "منقضی شد";
            return;
        }
        let m = Math.floor(t/60);
        let s = t % 60;
        el.innerText = m + ":" + ("0"+s).slice(-2);
        t--;
    },1000);
}
</script>

</head>

<body>

<div class="card">

<?php if(!isset($found)): ?>

<h3>📥 دریافت متن</h3>

<form method="POST">
<input name="code" placeholder="کد ۶ رقمی">
<button>نمایش</button>
</form>

<p style="color:red"><?= $error ?? '' ?></p>

<p style="font-size:12px;color:#94a3b8;">
این متن فقط یک‌بار قابل مشاهده است
</p>

<?php else: ?>

<h3>📄 متن</h3>

<div id="t" class="text"><?= htmlspecialchars($text) ?></div>

<button id="copyBtn" class="copy" onclick="copyText()">📋 کپی متن</button>

<div id="timer" class="timer"></div>

<script>
autoCopy();
selfDestruct();
startTimer(<?= max(0,$expireLeft) ?>);
</script>

<?php endif; ?>

</div>

</body>
</html>