<?php
require_once __DIR__ . '/includes/ProxyHelper.php';
$saved = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cookie = trim($_POST['ck'] ?? '');
    // Strip "Cookie: " prefix if user copied the whole header line
    if (stripos($cookie, 'cookie:') === 0) {
        $cookie = trim(substr($cookie, 7));
    }
    if ($cookie !== '' && strlen($cookie) > 20) {
        $ph = new ProxyHelper();
        $ph->saveCookies($cookie, [], null);
        // Write directly too
        @file_put_contents(__DIR__ . '/bdris_cookie.txt', $cookie);
        $cookiesDir = dirname(__DIR__) . '/cookies';
        if (!is_dir($cookiesDir)) @mkdir($cookiesDir, 0755, true);
        @file_put_contents($cookiesDir . '/raw_cookie.txt', $cookie);
        @file_put_contents($cookiesDir . '/active_profile.txt', 'manual_quick');
        $dataFile = $cookiesDir . '/cookie_data_manual_quick.json';
        $data = ['cookie_string' => $cookie, 'updated_at' => time(), 'last_status' => 'active', 'sender_name' => 'Quick Connect'];
        @file_put_contents($dataFile, json_encode($data));
        $saved = true;
    } else {
        $error = 'Cookie খুব ছোট বা খালি। সম্পূর্ণ Cookie paste করুন।';
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quick Connect - BDRIS Session</title>
<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Hind Siliguri', sans-serif; background: #0f172a; color: #e2e8f0; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.box { background: #1e293b; border-radius: 20px; padding: 36px 40px; max-width: 680px; width: 100%; border: 1px solid #334155; box-shadow: 0 20px 60px rgba(0,0,0,.5); }
h1 { font-size: 24px; font-weight: 700; color: #f1f5f9; margin-bottom: 6px; }
.sub { color: #94a3b8; font-size: 14px; margin-bottom: 28px; }
.steps { background: #0f172a; border-radius: 14px; padding: 20px 24px; margin-bottom: 24px; border: 1px solid #1e3a5f; }
.step { display: flex; gap: 14px; margin-bottom: 16px; align-items: flex-start; }
.step:last-child { margin-bottom: 0; }
.num { background: #2563eb; color: #fff; width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0; margin-top: 1px; }
.step-text { font-size: 14px; color: #cbd5e1; line-height: 1.7; }
.step-text b { color: #93c5fd; }
.step-text code { background: #1e293b; border: 1px solid #334155; padding: 2px 7px; border-radius: 4px; color: #4ade80; font-size: 13px; }
textarea { width: 100%; background: #0f172a; border: 2px solid #334155; border-radius: 12px; padding: 14px 16px; color: #f1f5f9; font-size: 13px; font-family: monospace; resize: vertical; min-height: 110px; outline: none; transition: border-color .2s; }
textarea:focus { border-color: #3b82f6; }
textarea::placeholder { color: #475569; }
.btn { width: 100%; padding: 16px; background: linear-gradient(135deg, #2563eb, #0891b2); color: white; border: none; border-radius: 12px; font-size: 17px; font-weight: 700; cursor: pointer; margin-top: 14px; font-family: inherit; transition: all .2s; }
.btn:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(37,99,235,.4); }
.success { background: #064e3b; border: 1px solid #10b981; border-radius: 12px; padding: 20px; text-align: center; margin-bottom: 20px; }
.success h2 { color: #34d399; font-size: 20px; margin-bottom: 8px; }
.success p { color: #6ee7b7; font-size: 14px; }
.error { background: #450a0a; border: 1px solid #ef4444; border-radius: 10px; padding: 14px 18px; color: #fca5a5; margin-bottom: 16px; font-size: 14px; }
.links { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 20px; }
.link-btn { flex: 1; min-width: 140px; padding: 12px; background: #1e293b; border: 1px solid #334155; border-radius: 10px; color: #93c5fd; text-align: center; text-decoration: none; font-size: 13px; font-weight: 600; transition: all .2s; }
.link-btn:hover { background: #2563eb; border-color: #2563eb; color: white; }
.img-hint { background: #0f172a; border: 1px dashed #334155; border-radius: 10px; padding: 16px; margin: 16px 0; font-size: 13px; color: #64748b; text-align: center; }
.img-hint span { color: #f59e0b; font-weight: 700; font-size: 15px; display: block; margin-bottom: 4px; }
</style>
</head>
<body>
<div class="box">
    <?php if ($saved): ?>
    <div class="success">
        <h2>✅ সংযোগ সফল!</h2>
        <p>BDRIS session সেভ হয়েছে। এখন যেকোনো ইউনিয়নের নিবন্ধন নম্বর সার্চ করা যাবে।</p>
    </div>
    <div class="links">
        <a href="dob-finder.php" class="link-btn">📅 জন্ম তারিখ সার্চ</a>
        <a href="search-birth.php" class="link-btn">🔍 জন্ম নিবন্ধন সার্চ</a>
        <a href="index.php" class="link-btn">🏠 ড্যাশবোর্ড</a>
    </div>
    <?php else: ?>
    <h1>⚡ Quick Connect</h1>
    <p class="sub">BDRIS-এর Cookie মাত্র ১ মিনিটে সেটআপ করুন</p>

    <?php if ($error): ?>
    <div class="error">⚠️ <?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="steps">
        <div class="step">
            <div class="num">১</div>
            <div class="step-text">ব্রাউজারের অন্য ট্যাবে <b>bdris.gov.bd/admin/</b> খুলুন (লগিন করা থাকলে ভালো)</div>
        </div>
        <div class="step">
            <div class="num">২</div>
            <div class="step-text">কিবোর্ডে <b>F12</b> চাপুন → উপরে <b>Network</b> ট্যাবে ক্লিক করুন</div>
        </div>
        <div class="step">
            <div class="num">৩</div>
            <div class="step-text"><b>F5</b> চাপুন (পেজ reload) → বামে যেকোনো request (যেমন <code>admin</code> বা <code>user/profile</code>) এ ক্লিক করুন</div>
        </div>
        <div class="step">
            <div class="num">৪</div>
            <div class="step-text">ডানে <b>Request Headers</b> খুঁজুন → <code>Cookie:</code> এর পুরো value এ right-click করুন → <b>Copy value</b></div>
        </div>
        <div class="step">
            <div class="num">৫</div>
            <div class="step-text">নিচের বক্সে <b>Paste</b> করুন (Ctrl+V) → <b>Save</b> বাটন চাপুন</div>
        </div>
    </div>

    <div class="img-hint">
        <span>💡 টিপস</span>
        Cookie অনেক লম্বা হবে — পুরোটাই কপি করুন, কিছু বাদ দেবেন না।
    </div>

    <form method="POST">
        <textarea name="ck" placeholder="TS0108b707=01ab52...; SESSION=ZmVkYWU...; bdris_persist=!jRc1..." required></textarea>
        <button type="submit" class="btn">💾 Save &amp; Connect</button>
    </form>

    <div class="links" style="margin-top:16px;">
        <a href="index.php" class="link-btn">🏠 হোম</a>
        <a href="connect-setup.php" class="link-btn">⚙️ বিস্তারিত সেটআপ</a>
        <a href="session-manager.php" class="link-btn">👁 Session Manager</a>
    </div>
    <?php endif; ?>
</div>
</body>
</html>
