<?php
require 'config.php';

// Reseller-only page
if (!isset($_SESSION['reseller_id'])) {
    redirect('reseller_login.php');
}

// Fetch the logged-in reseller's data
$stmt = $pdo->prepare("SELECT * FROM resellers WHERE id = ?");
$stmt->execute([$_SESSION['reseller_id']]);
$reseller = $stmt->fetch();

if (!$reseller) {
    session_destroy();
    redirect('reseller_login.php');
}

// JSON Sample formatting
$sample_json_string = <<<JSON
{

    "Owner": "Fast info BD",

    "nameBangla": "মোছাঃ জোহুরা খাতুন",

    "nameEnglish": "MST. JOHURA KHATUN",

    "nationalId": "1920355656",

    "dateOfBirth": "04 Aug 1956",

    "dateOfToday": "15/08/2025",

    "pin": "19568815069927563",

    "gender": "female",

    "religion": "",

    "bloodGroup": "B-",

    "occupation": "",

    "fatherName": "আলহাজ আব্দুল মজিদ",

    "nidFather": "",

    "motherName": "মোছাঃ রাহেলা খাতুন",

    "nidMother": "",

    "spouseName": "",

    "mobile": "",

    "voterNo": "815069927563",

    "voterArea": "জর্জিরা পশ্চিমপাড়া",

    "voterAreaCode": "256606",

    "birthPlace": "সিরাজগঞ্জ",

    "presentAdditionalVillageOrRoad": "জজিরাপশ্চিম পাড়া",

    "presentUnionOrWard": "নিশ্চিন্তপুর",

    "presentUpozila": "কাজীপুর",

    "presentDistrict": "সিরাজগঞ্জ",

    "photo": "https://fastinfobd.store/photo/1920355656.png",

    "sign": ""

}
JSON;
$sample_json_formatted = json_encode(json_decode($sample_json_string), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

require_once 'header.php';
?>

<style>
    :root {
        --gov-green: #006a4e;
        --gov-red: #f42a41;
        --gov-light: #f0f4f1;
    }
    body { background-color: #f9fbf9; font-family: 'Kalpurush', Arial, sans-serif; }
    
    /* Govt Top Header */
    .gov-top-bar {
        background: var(--gov-green);
        color: white;
        padding: 20px 0;
        border-bottom: 5px solid var(--gov-red);
        margin-bottom: 30px;
        box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    }
    .gov-seal { height: 70px; width: auto; }
    
    /* Dashboard Cards */
    .card { border: none; border-radius: 4px; box-shadow: 0 2px 15px rgba(0,0,0,0.05); overflow: hidden; }
    .card-header { 
        background: white !important; 
        border-bottom: 2px solid var(--gov-green) !important;
        color: var(--gov-green) !important;
        font-weight: bold;
        padding: 15px;
    }
    
    /* Balance Badge */
    .balance-box {
        background: var(--gov-light);
        border-left: 5px solid var(--gov-green);
        padding: 20px;
        border-radius: 4px;
    }
    .balance-amount { color: var(--gov-green); font-size: 2rem; font-weight: 800; }
    
    /* API Section */
    code { color: #d63384; word-break: break-all; }
    pre { background: #2d2d2d; color: #f8f8f2; padding: 15px; border-radius: 5px; max-height: 300px; }
    
    .btn-gov { background: var(--gov-green); color: white; border-radius: 2px; font-weight: bold; }
    .btn-gov:hover { background: #00563f; color: white; }
    
    .breadcrumb-item a { color: var(--gov-green); text-decoration: none; }
</style>

<div class="gov-top-bar text-center">
    <div class="container">
        <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Government_Seal_of_Bangladesh.svg/1200px-Government_Seal_of_Bangladesh.svg.png" class="gov-seal mb-2" alt="BD Seal">
        <h3 class="fw-bold mb-0">অনলাইন সার্ভিসেস পোর্টাল</h3>
        <p class="mb-0 small text-uppercase fw-bold">রিসেলার ড্যাশবোর্ড | গণপ্রজাতন্ত্রী বাংলাদেশ সরকার</p>
    </div>
</div>

<div class="container pb-5">
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb bg-white p-3 shadow-sm">
            <li class="breadcrumb-item"><a href="#"><i class="bi bi-house-door-fill"></i> হোম</a></li>
            <li class="breadcrumb-item active">ড্যাশবোর্ড</li>
        </ol>
    </nav>

    <?php show_flash(); ?>

    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card mb-4">
                <div class="card-header"><i class="bi bi-person-badge-fill me-2"></i>প্রোফাইল তথ্য</div>
                <div class="card-body">
                    <p class="mb-1 text-muted">রিসেলার নাম:</p>
                    <h5 class="fw-bold"><?php echo h($reseller['name']); ?></h5>
                    <hr>
                    <div class="balance-box mb-3 text-center">
                        <p class="mb-0 fw-bold text-uppercase small">বর্তমান ব্যালেন্স</p>
                        <div class="balance-amount">৳ <?php echo number_format($reseller['balance'], 2); ?></div>
                    </div>
                    
                    <p class="fw-bold mb-2 small text-uppercase">আপনার API কী (গোপন রাখুন):</p>
                    <div class="input-group mb-3">
                        <input type="text" id="apiKey" class="form-control border-success" value="<?php echo h($reseller['api_key']); ?>" readonly>
                        <button class="btn btn-gov" onclick="copyText('apiKey')"><i class="bi bi-clipboard"></i></button>
                    </div>

                    <?php if(!empty($settings['admin_whatsapp_number'])): ?>
                    <a href="https://wa.me/8801304499290<?php echo ltrim($settings['admin_whatsapp_number'], '0'); ?>" target="_blank" class="btn btn-success w-100 py-2">
                        <i class="bi bi-whatsapp me-2"></i>রিচার্জের জন্য যোগাযোগ করুন
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-code-slash me-2"></i>এপিআই ডকুমেন্টেশন (Developer Guide)</span>
                    <span class="badge bg-success">Status: Live</span>
                </div>
                <div class="card-body">
                    <h6 class="fw-bold text-dark"><i class="bi bi-link-45deg"></i> এন্ডপয়েন্ট ইউআরএল (Endpoint):</h6>
                    <div class="p-3 bg-light border mb-4">
                        <code class="fw-bold">https://infobd.shop/api.php</code>
                    </div>

                    <h6 class="fw-bold text-dark"><i class="bi bi-gear-fill"></i> রিকোয়েস্ট মেথড:</h6>
                    <p><span class="badge bg-primary px-3">GET</span></p>
                    <hr>

                    <h6 class="fw-bold text-dark">রিকোয়েস্ট স্যাম্পল (Example):</h6>
                    <div class="input-group mb-4">
                        <input type="text" id="apiUrl" class="form-control" value="https://infobd.shop/api.php?key=<?php echo h($reseller['api_key']); ?>&nid=1234567890&dob=1990-01-01" readonly>
                        <button class="btn btn-gov" onclick="copyText('apiUrl')">Copy URL</button>
                    </div>

                    <h6 class="fw-bold text-dark"><i class="bi bi-braces"></i> সাকসেস রেসপন্স (Success Response):</h6>
                    <pre><code><?php echo h($sample_json_formatted); ?></code></pre>
                </div>
            </div>
        </div>
    </div>
</div>

<footer class="text-center py-4 bg-white border-top mt-5">
    <div class="container">
        <p class="mb-0 text-muted small">© ২০২৪ অনলাইন সার্ভিস পোর্টাল | কারিগরি সহযোগিতায়: <b>গণপ্রজাতন্ত্রী বাংলাদেশ সরকার</b></p>
    </div>
</footer>

<script>
function copyText(elementId) {
    const el = document.getElementById(elementId);
    el.select();
    document.execCommand('copy');
    
    // Custom Toast/Alert
    const btn = event.currentTarget;
    const originalText = btn.innerHTML;
    btn.innerHTML = '<i class="bi bi-check-lg"></i>';
    setTimeout(() => { btn.innerHTML = originalText; }, 2000);
}
</script>

<?php require_once 'footer.php'; ?>