<?php
ob_start();
header('Content-Type: text/html; charset=utf-8');
include 'includes/header.php';
?>

<style>
    /* Sidebar Redesign (Offcanvas) */
    .sidebar { background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%); color: #f8fafc; border-right: 1px solid #334155; }
    .sidebar .nav-link { color: #cbd5e1; padding: 12px 16px; margin-bottom: 8px; border-radius: 10px; font-weight: 600; font-size: 15px; transition: all 0.3s ease; border: 1px solid transparent; display: flex; align-items: center; }
    .sidebar .nav-link:hover { background: rgba(255,255,255,0.05); color: #fff; transform: translateX(5px); }
    .sidebar .nav-link.active { background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%); color: white; box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4); border-color: #60a5fa; }
    .sidebar .nav-link i { margin-right: 12px; width: 24px; text-align: center; font-size: 1.1rem; }
    .sidebar-heading { font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: #64748b; margin-top: 24px; margin-bottom: 12px; padding-left: 16px; font-weight: 800; }
    
    .main-content { padding-left: 15px; }
    
    /* Advanced Card Redesign */
    .card-advanced { border: none; box-shadow: 0 4px 20px rgba(0,0,0,0.06); border-radius: 16px; overflow: hidden; background: #fff; }
    .card-advanced .card-header { background: #f8fafc; border-bottom: 1px solid #e2e8f0; padding: 16px 24px; font-weight: 800; font-size: 1.1rem; }
    pre.json-result { background: #1e293b; color: #a3e635; padding: 20px; border-radius: 12px; max-height: 400px; overflow-y: auto; font-size: 14px; border: 1px solid #334155; }
    
    /* Dashboard Cards */
    .dashboard-card { border-radius: 16px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.05); transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); background: #ffffff; position: relative; overflow: hidden; z-index: 1; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; }
    .dashboard-card::after { content: ''; position: absolute; bottom: 0; left: 0; width: 100%; height: 4px; background: linear-gradient(90deg, #3b82f6, #8b5cf6); transform: scaleX(0); transition: transform 0.3s ease; transform-origin: left; z-index: -1; }
    .dashboard-card:hover { transform: translateY(-8px); box-shadow: 0 15px 30px rgba(0,0,0,0.1); }
    .dashboard-card:hover::after { transform: scaleX(1); }
    .dashboard-card .card-icon { font-size: 3rem; margin-bottom: 16px; background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
    .dashboard-card .card-title { font-weight: 700; color: #1e293b; font-size: 1.1rem; }

    /* Special Cards */
    .dob-finder-card { background: linear-gradient(135deg, #1a3a6e 0%, #0d5c3a 100%); border: 2px solid #22c55e !important; border-radius: 16px !important; transition: all .3s cubic-bezier(0.4, 0, 0.2, 1); position: relative; overflow: hidden; box-shadow: 0 10px 25px rgba(0,0,0,0.1); }
    .dob-finder-card::before { content: ''; position: absolute; top: 0; left: -100%; width: 100%; height: 100%; background: linear-gradient(90deg, transparent, rgba(255,255,255,.1), transparent); transition: left .6s ease; }
    .dob-finder-card:hover::before { left: 100%; }
    .dob-finder-card:hover { transform: translateY(-8px); box-shadow: 0 15px 35px rgba(34,197,94,.25) !important; border-color: #4ade80 !important; }
    .dob-finder-card .card-icon { color: #4ade80 !important; }
    .dob-finder-card .card-title { color: #f0fdf4 !important; }
    .dob-new-badge { position: absolute; top: 12px; right: 12px; background: #22c55e; color: #fff; font-size: 11px; font-weight: 700; padding: 4px 10px; border-radius: 20px; letter-spacing: .5px; box-shadow: 0 4px 10px rgba(34,197,94,.4); }
</style>

<div class="container-fluid mt-3">
    
    <!-- Sidebar Offcanvas -->
    <div class="offcanvas offcanvas-start sidebar shadow" tabindex="-1" id="sidebarMenu" aria-labelledby="sidebarMenuLabel" style="width: 280px;">
        <div class="offcanvas-header border-bottom border-secondary mb-3 pb-3">
            <h5 class="offcanvas-title text-white font-weight-bold" id="sidebarMenuLabel"><i class="fas fa-building text-primary me-2"></i>অফিস সার্ভিসেস</h5>
            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close"></button>
        </div>
        <div class="offcanvas-body pt-0">
            <div class="nav flex-column nav-pills px-2" id="v-pills-tab" role="tablist" aria-orientation="vertical">
                    
                    <div class="sidebar-heading">প্রধান ড্যাশবোর্ড</div>
                    <button class="nav-link active text-left" id="v-pills-dashboard-tab" data-bs-toggle="pill" data-bs-target="#v-pills-dashboard" type="button" role="tab"><i class="fas fa-home"></i> মূল ড্যাশবোর্ড</button>
                    
                    <div class="sidebar-heading mt-3">নতুন সার্ভিসেস</div>
                    <button class="nav-link text-left" id="v-pills-nid-tab" data-bs-toggle="pill" data-bs-target="#v-pills-nid" type="button" role="tab"><i class="fas fa-id-card"></i> এনআইডি যাচাই</button>
                    <button class="nav-link text-left" id="v-pills-cancel-tab" data-bs-toggle="pill" data-bs-target="#v-pills-cancel" type="button" role="tab"><i class="fas fa-ban"></i> সার্টিফিকেট বাতিল (লিস্ট)</button>
                    <a href="cancel-application-bypass.php" class="nav-link text-left"><i class="fas fa-trash-alt text-danger"></i> নিবন্ধন বাতিল আবেদন</a>
                    <button class="nav-link text-left" id="v-pills-parent-tab" data-bs-toggle="pill" data-bs-target="#v-pills-parent" type="button" role="tab"><i class="fas fa-users"></i> পিতামাতার তথ্য</button>
                    <button class="nav-link text-left" id="v-pills-adv-tab" data-bs-toggle="pill" data-bs-target="#v-pills-adv" type="button" role="tab"><i class="fas fa-search-plus"></i> অ্যাডভান্সড তথ্য</button>
                    <button class="nav-link text-left" id="v-pills-status-tab" data-bs-toggle="pill" data-bs-target="#v-pills-status" type="button" role="tab"><i class="fas fa-tasks"></i> আবেদনের স্ট্যাটাস</button>
                    <button class="nav-link text-left" id="v-pills-alive-tab" data-bs-toggle="pill" data-bs-target="#v-pills-alive" type="button" role="tab"><i class="fas fa-heartbeat"></i> জীবিত স্ট্যাটাস</button>
                    
                    <div class="sidebar-heading mt-3">পাবলিক সার্ভার</div>
                    <a href="https://bdris.gov.bd/br/application" target="_blank" class="nav-link text-left"><i class="fas fa-baby" style="color:#4ade80;"></i> জন্ম নিবন্ধন আবেদন</a>
                    <a href="https://bdris.gov.bd/br/correction" target="_blank" class="nav-link text-left"><i class="fas fa-edit" style="color:#4ade80;"></i> তথ্য সংশোধন আবেদন</a>
                    <a href="https://bdris.gov.bd/br/reprint/add" target="_blank" class="nav-link text-left"><i class="fas fa-print" style="color:#4ade80;"></i> সনদ পুনঃ মুদ্রন</a>
                    <a href="application-print.php?service=br" class="nav-link text-left"><i class="fas fa-file-pdf" style="color:#4ade80;"></i> আবেদন পত্র প্রিন্ট</a>
                    <a href="https://everify.bdris.gov.bd" target="_blank" class="nav-link text-left"><i class="fas fa-search" style="color:#4ade80;"></i> জন্ম তথ্য অনুসন্ধান</a>
                    
                    <a href="https://bdris.gov.bd/dr/application" target="_blank" class="nav-link text-left mt-2"><i class="fas fa-bed" style="color:#ff9a9e;"></i> মৃত্যু নিবন্ধন আবেদন</a>
                    <a href="https://bdris.gov.bd/dr/correction" target="_blank" class="nav-link text-left"><i class="fas fa-edit" style="color:#ff9a9e;"></i> মৃত্যু তথ্য সংশোধন</a>
                    <a href="https://bdris.gov.bd/dr/reprint/add" target="_blank" class="nav-link text-left"><i class="fas fa-print" style="color:#ff9a9e;"></i> মৃত্যু সনদ পুনঃ মুদ্রন</a>
                    <a href="application-print.php?service=dr" class="nav-link text-left"><i class="fas fa-file-pdf" style="color:#ff9a9e;"></i> মৃত্যু আবেদন পত্র প্রিন্ট</a>
                    <a href="https://everify.bdris.gov.bd/UDRNVerification" target="_blank" class="nav-link text-left"><i class="fas fa-search-minus" style="color:#ff9a9e;"></i> মৃত্যু তথ্য অনুসন্ধান</a>
                </div>
            </div>
        </div>
    </div> <!-- End offcanvas -->

    <!-- Toggle Button for Sidebar (Floating) -->
    <div class="position-relative">
        <button class="btn btn-primary shadow-sm" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" style="position: absolute; top: 0; left: 15px; border-radius: 8px; padding: 10px 16px; z-index: 1050;">
            <i class="fas fa-bars fa-lg"></i>
        </button>
    </div>

    <div class="row">
        <!-- Main Content -->
        <div class="col-12 main-content px-4">
            
            <div class="tab-content" id="v-pills-tabContent">
                
                <!-- DASHBOARD HOME -->
                <div class="tab-pane fade show active" id="v-pills-dashboard" role="tabpanel">
                    
                    <div class="text-center pb-3">
                        <h2 class="dashboard-title">&#2437;&#2475;&#2495;&#2488; &#2488;&#2495;&#2488;&#2509;&#2463;&#2503;&#2478; &#2465;&#2509;&#2479;&#2494;&#2486;&#2476;&#2507;&#2480;&#2509;&#2465;</h2>
                        <div class="alert alert-success d-inline-block py-1 px-3"><strong>Access: ON</strong></div>
                    </div>



                    <div class="row g-4 mb-5">
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="search-birth.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-search"></i></div><h5 class="card-title text-dark">জন্ম নিবন্ধন তথ্য অনুসন্ধান</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="search-death.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-search-minus"></i></div><h5 class="card-title text-dark">মৃত্যু নিবন্ধন তথ্য অনুসন্ধান</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="register-by-app-id.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-file-alt"></i></div><h5 class="card-title text-dark">আবেদন নম্বর দিয়ে জন্ম নিবন্ধন (১৭ ডিজিট)</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="universal-dashboard.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-eye"></i></div><h5 class="card-title text-dark">ইউনিভার্সাল রিড-অনলি ড্যাশবোর্ড</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="print-application.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-print"></i></div><h5 class="card-title text-dark">আবেদনপত্র প্রিন্ট</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="union-docs.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-folder-open"></i></div><h5 class="card-title text-dark">বিভিন্ন ইউনিয়নের আবেদন ও ডকুমেন্টস প্রিন্ট</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="duplicates.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-clone"></i></div><h5 class="card-title text-dark">Possible Duplicate</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="print-certificate.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-certificate"></i></div><h5 class="card-title text-dark">জন্ম নিবন্ধন সনদ প্রিন্ট</h5></div></a>
                        </div>
                        <div class="col-md-4 col-sm-6 mb-3">
                            <a href="search-by-name.php" class="text-decoration-none"><div class="card dashboard-card p-4 text-center"><div class="card-icon"><i class="fas fa-search-location"></i></div><h5 class="card-title text-dark">নাম দিয়ে গ্লোবাল সার্চ</h5></div></a>
                        </div>
                    </div>
                </div>

                <!-- 1. NID Check -->
                <div class="tab-pane fade" id="v-pills-nid" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-primary"><i class="fas fa-id-card"></i> এনআইডি (NID) যাচাই</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="nid_check">
                                <div class="row">
                                    <div class="col-md-5 mb-3"><label>এনআইডি নম্বর (NID)</label><input type="text" class="form-control" name="nid" required></div>
                                    <div class="col-md-5 mb-3"><label>জন্ম তারিখ (DOB)</label><input type="date" class="form-control" name="dob" required></div>
                                    <div class="col-md-2 mb-3 d-flex align-items-end"><button class="btn btn-primary w-100" type="submit">যাচাই করুন</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

                <!-- 2. Cancel List -->
                <div class="tab-pane fade" id="v-pills-cancel" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-danger"><i class="fas fa-ban"></i> বাতিল হওয়া আবেদন ও সার্চ</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="cancel_list">
                                <div class="row">
                                    <div class="col-md-9 mb-3"><label>সার্চ ভ্যালু (UBRN / App ID)</label><input type="text" class="form-control" name="searchValue" placeholder="খালি রাখলে লেটেস্ট ৫০টি আবেদন আসবে"></div>
                                    <div class="col-md-3 mb-3 d-flex align-items-end"><button class="btn btn-danger w-100" type="submit">লিস্ট আনুন</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

                <!-- 3. Parent Info -->
                <div class="tab-pane fade" id="v-pills-parent" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-success"><i class="fas fa-users"></i> পিতামাতার তথ্য ও কততম সন্তান</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="parent_info">
                                <div class="row">
                                    <div class="col-md-9 mb-3"><label>জন্ম নিবন্ধন নম্বর (UBRN)</label><input type="text" class="form-control" name="ubrn" required></div>
                                    <div class="col-md-3 mb-3 d-flex align-items-end"><button class="btn btn-success w-100" type="submit">অনুসন্ধান</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

                <!-- 4. Advanced Info -->
                <div class="tab-pane fade" id="v-pills-adv" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-info"><i class="fas fa-search-plus"></i> অ্যাডভান্সড তথ্য যাচাই</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="adv_info">
                                <div class="row">
                                    <div class="col-md-9 mb-3"><label>জন্ম নিবন্ধন নম্বর (UBRN)</label><input type="text" class="form-control" name="ubrn" required></div>
                                    <div class="col-md-3 mb-3 d-flex align-items-end"><button class="btn btn-info w-100 text-white" type="submit">পূর্ণাঙ্গ তথ্য আনুন</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

                <!-- 5. App Status -->
                <div class="tab-pane fade" id="v-pills-status" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-warning"><i class="fas fa-tasks"></i> আবেদনের বর্তমান স্ট্যাটাস</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="app_status">
                                <div class="row">
                                    <div class="col-md-9 mb-3"><label>আবেদন আইডি (App ID) অথবা UBRN</label><input type="text" class="form-control" name="searchValue" required></div>
                                    <div class="col-md-3 mb-3 d-flex align-items-end"><button class="btn btn-warning w-100" type="submit">স্ট্যাটাস দেখুন</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

                <!-- 6. Alive Status -->
                <div class="tab-pane fade" id="v-pills-alive" role="tabpanel">
                    <div class="card card-advanced mb-4">
                        <div class="card-header text-secondary"><i class="fas fa-heartbeat"></i> জীবিত স্ট্যাটাস চেক</div>
                        <div class="card-body">
                            <form class="ajax-form">
                                <input type="hidden" name="action" value="alive_status">
                                <div class="row">
                                    <div class="col-md-9 mb-3"><label>জন্ম নিবন্ধন নম্বর (UBRN)</label><input type="text" class="form-control" name="ubrn" required></div>
                                    <div class="col-md-3 mb-3 d-flex align-items-end"><button class="btn btn-secondary w-100" type="submit">যাচাই করুন</button></div>
                                </div>
                            </form>
                            <div class="result-area mt-3" style="display:none;"></div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    // Handles AJAX forms dynamically
    $(document).ready(function(){
        $('.ajax-form').on('submit', function(e) {
            e.preventDefault();
            const form = $(this);
            const btn = form.find('button[type="submit"]');
            const originalText = btn.html();
            const resultArea = form.next('.result-area');
            
            btn.html('<i class="fas fa-spinner fa-spin"></i> লোডিং...');
            btn.prop('disabled', true);
            resultArea.hide();

            $.ajax({
                url: 'proxy-services.php',
                type: 'POST',
                data: form.serialize(),
                dataType: 'json',
                success: function(res) {
                    btn.html(originalText);
                    btn.prop('disabled', false);
                    
                    if (res.status === 'success') {
                        let html = '<div class="alert alert-success"><i class="fas fa-check-circle"></i> সফলভাবে ডাটা পাওয়া গেছে!</div>';
                        html += '<h5>বিস্তারিত তথ্য:</h5>';
                        html += '<pre class="json-result">' + JSON.stringify(res.data, null, 4) + '</pre>';
                        resultArea.html(html).slideDown();
                    } else {
                        const ubrnInput = form.find('input[name="ubrn"]');
                        const ubrnVal = ubrnInput.length ? ubrnInput.val().trim() : '';
                        const errorMsg = res.message || '';
                        const debugMsg = res.debug ? res.debug.toString() : '';
                        const isAreaError = errorMsg.includes('আওতাধীন নয়') || debugMsg.includes('আওতাধীন নয়') || errorMsg.includes('পিতামাতার তথ্য পাওয়া যায়নি') || errorMsg.includes('কোনো তথ্য পাওয়া যায়নি') || errorMsg.includes('স্ট্যাটাস জানা যায়নি');
                        
                        if (isAreaError && ubrnVal) {
                            let html = '<div class="alert alert-warning">';
                            html += '<i class="fas fa-exclamation-triangle me-2"></i> এটি অন্য এলাকার তথ্য (আওতাধীন নয়)।<br>';
                            html += 'বাইপাস করতে নিচের বাটনটি চাপুন — ব্রাউজার রিলে দিয়ে তথ্য আনা হবে।<br><br>';
                            html += '<button type="button" class="btn btn-success fw-bold btn-sm" onclick="triggerRelayBypass(\'' + ubrnVal + '\', this)">';
                            html += '<i class="fas fa-external-link-alt"></i> রিলে দিয়ে তথ্য আনুন</button>';
                            html += '</div>';
                            resultArea.html(html).slideDown();
                        } else {
                            let html = '<div class="alert alert-danger"><i class="fas fa-times-circle"></i> ' + (res.message || 'Error') + '</div>';
                            if(res.debug) {
                                let escapedDebug = res.debug.toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
                                html += '<p><strong>সার্ভার রেসপন্স:</strong></p><pre class="json-result" style="background:#4a1515; color:#f87171;">' + escapedDebug + '</pre>';
                            }
                            resultArea.html(html).slideDown();
                        }
                    }
                },
                error: function(xhr) {
                    btn.html(originalText);
                    btn.prop('disabled', false);
                    resultArea.html('<div class="alert alert-danger">সার্ভার এরর অথবা টাইমআউট! (HTTP ' + xhr.status + ')</div>').slideDown();
                }
            });
        });
    });

    function triggerRelayBypass(ubrn, btnEl) {
        const origHtml = btnEl.innerHTML;
        btnEl.disabled = true;
        btnEl.innerHTML = '<i class="fas fa-spinner fa-spin"></i> রিলে সক্রিয় হচ্ছে...';
        
        const w = 600, h = 480;
        const left = (screen.width/2)-(w/2);
        const top = (screen.height/2)-(h/2);
        const relayUrl = 'bdris-relay.php?type=ubrn&key=' + encodeURIComponent(ubrn);
        
        const popup = window.open(relayUrl, 'BDRISBypassRelay', `width=${w},height=${h},top=${top},left=${left}`);
        
        const pollClose = setInterval(() => {
            if (popup && popup.closed) {
                clearInterval(pollClose);
                btnEl.disabled = false;
                btnEl.innerHTML = origHtml;
            }
        }, 700);
        
        function relayHandler(e) {
            if (!e.data || e.data.type !== 'bdris_result' || e.data.ubrn !== ubrn) return;
            clearInterval(pollClose);
            window.removeEventListener('message', relayHandler);
            popup.close();
            
            // Find the active tab form and re-submit it
            const activeForm = $('.tab-pane.active form.ajax-form');
            if (activeForm.length) {
                activeForm.submit();
            } else {
                $('form.ajax-form').each(function() {
                    const inp = $(this).find('input[name="ubrn"]');
                    if (inp.length && inp.val().trim() === ubrn) {
                        $(this).submit();
                    }
                });
            }
        }
        window.addEventListener('message', relayHandler);
    }
</script>

<?php include 'includes/footer.php'; ?>
