<style>
    /* ─── Cellfin UI Executive Luxury Theme ─── */
    :root {
        --cellfin-green: #00803d;
        --cellfin-dark-green: #005c2b;
        --cellfin-gold: #FFD700;
        --cellfin-card-bg: rgba(0, 0, 0, 0.25);
    }

    /* Container Box Styling */
    .brand-bg {
        background: linear-gradient(180deg, #009647 0%, #005c2b 100%) !important;
        padding: 20px !important;
        border-radius: 18px !important;
        font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
        color: #ffffff;
        box-sizing: border-box;
        overflow: hidden;
        box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    }

    /* Lang Toggle Header */
    .cellfin-header-actions {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 15px;
        width: 100%;
    }
    .lang-toggle-btn {
        background: rgba(0, 0, 0, 0.3);
        border: 1px solid rgba(255, 255, 255, 0.3);
        border-radius: 20px;
        display: flex;
        overflow: hidden;
        cursor: pointer;
        padding: 2px;
    }
    .lang-toggle-btn span {
        padding: 4px 14px;
        font-size: 11px;
        font-weight: 700;
        color: rgba(255, 255, 255, 0.8);
        border-radius: 15px;
        transition: all 0.2s ease;
    }
    .lang-toggle-btn span.active {
        background: #ffffff;
        color: var(--cellfin-green);
    }

    /* Invoice Info Table Structure */
    .cellfin-summary {
        text-align: left;
        width: 100%;
        margin-bottom: 18px;
        background: var(--cellfin-card-bg);
        padding: 14px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.12);
    }
    .cellfin-summary p {
        margin: 5px 0;
        font-size: 13px;
        display: flex;
        justify-content: space-between;
    }
    .cellfin-summary strong {
        color: #ffffff;
    }

    /* Account Type Badge Inside Instruction */
    .instruction-acc-badge {
        background: rgba(255, 215, 0, 0.15);
        border: 1px solid rgba(255, 215, 0, 0.4);
        padding: 6px 12px;
        border-radius: 6px;
        font-size: 11px;
        font-weight: 700;
        color: var(--cellfin-gold);
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 12px;
        display: inline-block;
        width: 100%;
        box-sizing: border-box;
        text-align: center;
    }

    /* Main Section Body */
    .cellfin-main-body {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    .cellfin-title {
        font-size: 16px;
        font-weight: 700;
        margin-bottom: 8px;
        text-align: center;
    }
    .cellfin-subtitle {
        font-size: 12px;
        color: rgba(255, 255, 255, 0.9);
        text-align: center;
        margin-bottom: 18px;
        line-height: 1.4;
    }

    /* Modern Unified Mobile Input Field */
    .cellfin-input-wrapper {
        position: relative;
        width: 100%;
        margin-bottom: 15px;
    }
    .cellfin-custom-input {
        width: 100%;
        background: #ffffff;
        border: 2px solid transparent;
        border-radius: 12px;
        padding: 14px 16px;
        font-size: 22px;
        font-weight: 700;
        letter-spacing: 3px;
        color: #222222;
        text-align: center;
        outline: none;
        box-sizing: border-box;
        transition: all 0.2s ease;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.15);
    }
    .cellfin-custom-input:focus {
        border-color: var(--cellfin-gold);
        box-shadow: 0 0 10px rgba(255, 215, 0, 0.6);
    }

    /* Tab Layout for Cellfin / iSmart */
    .method-tab-bar {
        display: flex;
        width: 100%;
        background: rgba(0,0,0,0.3);
        border-radius: 8px;
        margin-bottom: 15px;
        padding: 3px;
    }
    .method-tab {
        flex: 1;
        text-align: center;
        padding: 9px 0;
        font-size: 12px;
        font-weight: 600;
        cursor: pointer;
        border-radius: 6px;
        color: rgba(255,255,255,0.8);
        transition: all 0.2s;
    }
    .method-tab.active {
        background: #ffffff;
        color: var(--cellfin-green);
        font-weight: 700;
    }

    /* Click to copy Clean Number Only Design */
    .clean-number-box {
        width: 100%;
        background: #ffffff;
        border-radius: 12px;
        padding: 16px;
        text-align: center;
        margin-bottom: 15px;
        cursor: pointer;
        box-sizing: border-box;
        box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        transition: transform 0.1s ease, box-shadow 0.2s;
        border: 2px solid transparent;
    }
    .clean-number-box:active {
        transform: scale(0.98);
    }
    .clean-number-box:hover {
        border-color: var(--cellfin-gold);
    }
    .clean-number-box .num-val {
        font-size: 28px;
        font-weight: 800;
        color: var(--cellfin-green);
        letter-spacing: 1.5px;
        margin: 0;
        line-height: 1;
    }

    /* Highlighted Wait Alert Box */
    .wait-alert-box {
        background: rgba(255, 215, 0, 0.15);
        border: 1px dashed var(--cellfin-gold);
        border-radius: 8px;
        padding: 12px;
        width: 100%;
        text-align: center;
        margin-bottom: 15px;
        box-sizing: border-box;
    }
    .wait-alert-box p {
        margin: 0;
        font-size: 12px;
        font-weight: 700;
        color: var(--cellfin-gold);
        line-height: 1.4;
    }

    /* Instructions Box */
    .instruction-box {
        width: 100%;
        background: rgba(0,0,0,0.18);
        border: 1px solid rgba(255,255,255,0.1);
        border-radius: 10px;
        padding: 14px;
        margin-bottom: 18px;
        box-sizing: border-box;
    }
    .ins-step {
        font-size: 12px;
        line-height: 1.5;
        margin-bottom: 8px;
        color: #f5f5f5;
        text-align: left;
    }
    .ins-step:last-child { margin-bottom: 0; }
    .ins-step .hl { color: var(--cellfin-gold); font-weight: 700; }

    /* Action Buttons Area */
    .cellfin-action-row {
        display: flex;
        gap: 10px;
        width: 100%;
    }
    .cellfin-btn {
        flex: 1;
        padding: 12px;
        border-radius: 8px;
        font-size: 13px;
        font-weight: 700;
        border: none;
        cursor: pointer;
        text-align: center;
        transition: all 0.2s;
    }
    .btn-proceed {
        background: #ffffff;
        color: var(--cellfin-dark-green);
        order: 2;
    }
    .btn-close {
        background: rgba(0, 0, 0, 0.35);
        border: 1px solid rgba(255, 255, 255, 0.25);
        color: #ffffff;
        order: 1;
    }
    .btn-cancel {
        background: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.2);
        color: #ffcccc;
        order: 2;
    }
    .btn-cancel:hover {
        background: rgba(0, 128, 61, 0.2);
        border-color: var(--cellfin-green);
    }

    /* Bottom Countdown Panel */
    .cellfin-status-area {
        text-align: center;
        font-size: 12px;
        margin-bottom: 15px;
    }
    .timer-badge {
        color: var(--cellfin-gold);
        font-weight: 700;
    }

    /* Functional Utilities */
    .hidden-panel { display: none !important; }
    .phone-error-msg {
        color: var(--cellfin-gold);
        font-size: 11px;
        margin-top: -8px;
        margin-bottom: 15px;
        display: none;
        font-weight: 600;
    }

    /* Clear global styling clutter */
    .brand-bg .transaction, .payment_submit_done { display: none !important; }
</style>

<?php
$acc_tp = decrypt($_GET['acc_tp']);
if ($acc_tp == 'personal') :
    $merchant_number = get_value($setting['params'], 'personal_number');
?>

<div class="cellfin-header-actions">
    <div class="lang-toggle-btn" onclick="toggleCellfinLanguage()">
        <span id="lang_bn_btn" class="active">বাং</span>
        <span id="lang_en_btn">Eng</span>
    </div>
</div>

<div class="cellfin-summary">
    <p><span class="lang-node" data-bn="ইনভয়েস নম্বর:" data-en="Invoice No:">ইনভয়েস নম্বর:</span> <strong><?= $all_info['transaction_id'] ?></strong></p>
    <p><span class="lang-node" data-bn="মোট টাকা:" data-en="Total Amount:">মোট টাকা:</span> <strong>BDT <?= number_format($all_info['total_amount'], 2) ?></strong></p>
    <p><span class="lang-node" data-bn="চার্জ:" data-en="Charge:">চার্জ:</span> <strong>BDT 0</strong></p>
</div>

<!-- Screen 1: Target Sender Cellfin/Mobile Number Input -->
<div id="screen_number" class="cellfin-main-body">
    <div class="cellfin-title lang-node" data-bn="আপনার সেলফিন একাউন্ট নম্বর দিন" data-en="Your Cellfin Account Number">আপনার সেলফিন একাউন্ট নম্বর দিন</div>
    <div class="cellfin-subtitle lang-node" data-bn="যে মোবাইল নম্বর থেকে টাকা ট্রান্সফার করবেন, এখানে সেই নম্বরটি দিন" data-en="Please enter the mobile number from which you will transfer the fund">যে মোবাইল নম্বর থেকে টাকা ট্রান্সফার করবেন, এখানে সেই নম্বরটি দিন</div>
    
    <div class="cellfin-input-wrapper">
        <input class="cellfin-custom-input" type="tel" id="pure_cellfin_num" placeholder="01XXXXXXXXX" maxlength="11" autocomplete="off">
    </div>
    
    <div class="phone-error-msg lang-node" id="cellfin_input_err" data-bn="সকরুণ ভাবে সঠিক ১১ ডিজিটের মোবাইল নম্বর প্রদান করুন" data-en="Please enter a valid 11 digit mobile number">সঠিক ১১ ডিজিটের মোবাইল নম্বর প্রদান করুন</div>

    <div class="cellfin-action-row">
        <button class="cellfin-btn btn-close lang-node" data-bn="Close" data-en="Close" onclick="window.location.href='<?= base_url("api/execute/" . $all_info['tmp_ids']); ?>'">Close</button>
        <button class="cellfin-btn btn-proceed lang-node" data-bn="Proceed" data-en="Proceed" onclick="submitCellfinNumber()">Proceed</button>
    </div>
</div>

<!-- Screen 2: Display Instruction / Polling Panel -->
<div id="screen_waiting" class="cellfin-main-body hidden-panel">
    <div class="method-tab-bar">
        <div id="tab_app" class="method-tab active" onclick="switchCellfinInstruction('app')">Cellfin App</div>
        <div id="tab_ismart" class="method-tab" onclick="switchCellfinInstruction('ismart')">IBBL iSmart</div>
    </div>

    <div class="clean-number-box" onclick="copyCellfinTarget()" title="Click to copy">
        <div class="num-val" id="target_num"><?= $merchant_number ?></div>
    </div>

    <div class="wait-alert-box">
        <p class="lang-node" data-bn="⚠️ টাকা ফান্ড ট্রান্সফার করার পর ১০ থেকে ১৫ সেকেন্ড অপেক্ষা করুন।" data-en="⚠️ Please wait 10 to 15 seconds after completing the fund transfer.">⚠️ টাকা ফান্ড ট্রান্সফার করার পর ১০ থেকে ১৫ সেকেন্ড অপেক্ষা করুন।</p>
    </div>

    <div id="ins_app" class="instruction-box"></div>
    <div id="ins_ismart" class="instruction-box hidden-panel"></div>

    <div class="cellfin-status-area">
        <div style="font-weight: 700; margin-bottom: 4px;" class="lang-node" data-bn="⏳ পেমেন্ট ভেরিফাই করা হচ্ছে..." data-en="⏳ Verifying payment...">⏳ পেমেন্ট ভেরিফাই করা হচ্ছে...</div>
        <div><span class="lang-node" data-bn="অবশিষ্ট সময়: " data-en="Time Remaining: ">অবশিষ্ট সময়: </span><span class="timer-badge" id="cellfin_timer">10:00</span></div>
    </div>

    <div class="cellfin-action-row">
        <button class="cellfin-btn btn-close lang-node" data-bn="← নম্বর পরিবর্তন" data-en="← Change Number" onclick="backToNumberInput()">← নম্বর পরিবর্তন</button>
        <button class="cellfin-btn btn-cancel lang-node" data-bn="Cancel" data-en="Cancel" onclick="window.location.href='<?= base_url("api/execute/" . $all_info['tmp_ids']); ?>'">Cancel</button>
    </div>
</div>

<script>
(function() {
    var currentLang = 'bn';
    var selectedNumber = '';
    var timerSec = 600; 
    var timerId = null;
    var pollId = null;

    var tmpId = '<?= session('tmp_ids') ?>';
    var accTp = '<?= $_GET['acc_tp'] ?? '' ?>';
    var checkUrl = '<?= base_url('api/check_payment/cellfin') ?>'; 
    var csrfToken = '<?= csrf_hash() ?>';

    updateLanguageSteps();

    window.toggleCellfinLanguage = function() {
        currentLang = (currentLang === 'bn') ? 'en' : 'bn';
        if (currentLang === 'en') {
            $('#lang_en_btn').addClass('active');
            $('#lang_bn_btn').removeClass('active');
        } else {
            $('#lang_bn_btn').addClass('active');
            $('#lang_en_btn').removeClass('active');
        }
        
        $('.lang-node').each(function() {
            var node = $(this);
            node.html(node.data(currentLang));
        });

        updateLanguageSteps();
    };

    function updateLanguageSteps() {
        if (currentLang === 'en') {
            var badgeHtml = '<div class="instruction-acc-badge">Type: Cellfin / IBBL Fund Transfer</div>';
            $('#ins_app').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">1.</span> Tap the white number box above to copy the destination number.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Open your <span class="hl">CELLFIN App</span> and tap on <span class="hl">"Fund Transfer"</span>.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Select <span class="hl">"Cellfin"</span> as transfer type, enter the copied number and exact amount <span class="hl">BDT <?= number_format($all_info['total_amount'], 2) ?></span> to complete.</div>'
            );
            $('#ins_ismart').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">1.</span> Log into your <span class="hl">IBBL iSmart App</span> or Web Portal.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Go to <span class="hl">"Fund Transfer"</span> and choose <span class="hl">"Transfer to Cellfin"</span>.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Send exactly <span class="hl">BDT <?= number_format($all_info['total_amount'], 2) ?></span> to the copied number <span class="hl"><?= $merchant_number ?></span>.</div>'
            );
        } else {
            var badgeHtml = '<div class="instruction-acc-badge">টাইপ: সেলফিন / ইসলামী ব্যাংক ফান্ড ট্রান্সফার</div>';
            $('#ins_app').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">১.</span> উপরের সাদা নম্বর বক্সের ওপর ক্লিক করে নম্বরটি কপি করুন।</div>' +
                '<div class="ins-step"><span class="hl">২.</span> আপনার ফোনের <span class="hl">CELLFIN অ্যাপে</span> প্রবেশ করে <span class="hl">"Fund Transfer"</span> অপশনে যান।</div>' +
                '<div class="ins-step"><span class="hl">৩.</span> ট্রান্সফার মাধ্যম <span class="hl">"Cellfin"</span> সিলেক্ট করে কপি করা নম্বরে ঠিক <span class="hl">BDT <?= number_format($all_info['total_amount'], 2) ?></span> টাকা ট্রান্সফার সম্পন্ন করুন।</div>'
            );
            $('#ins_ismart').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">১.</span> আপনার <span class="hl">IBBL iSmart অ্যাপ</span> অথবা পোর্টালে লগইন করুন।</div>' +
                '<div class="ins-step"><span class="hl">২.</span> <span class="hl">"Fund Transfer"</span> মেনু থেকে <span class="hl">"Transfer to Cellfin"</span> অপশনটি সিলেক্ট করুন।</div>' +
                '<div class="ins-step"><span class="hl">৩.</span> প্রাপক নম্বর হিসেবে <span class="hl"><?= $merchant_number ?></span> বক্সে ঠিক <span class="hl">BDT <?= number_format($all_info['total_amount'], 2) ?></span> টাকা পাঠিয়ে পিন কনফার্ম করুন।</div>'
            );
        }
    }

    window.switchCellfinInstruction = function(type) {
        $('.method-tab').removeClass('active');
        if (type === 'app') {
            $('#tab_app').addClass('active');
            $('#ins_app').removeClass('hidden-panel');
            $('#ins_ismart').addClass('hidden-panel');
        } else {
            $('#tab_ismart').addClass('active');
            $('#ins_ismart').removeClass('hidden-panel');
            $('#ins_app').addClass('hidden-panel');
        }
    };

    window.copyCellfinTarget = function() {
        var num = $('#target_num').text().trim();
        var dummy = document.createElement("textarea");
        document.body.appendChild(dummy);
        dummy.value = num;
        dummy.select();
        document.execCommand("copy");
        document.body.removeChild(dummy);
        if (typeof showToast === 'function') {
            showToast(currentLang === 'bn' ? 'নম্বর সফলভাবে কপি হয়েছে!' : 'Number Copied Successfully!', 'success');
        }
    };

    window.submitCellfinNumber = function() {
        var num = $('#pure_cellfin_num').val().trim();

        if (!/^01[3-9]\d{8}$/.test(num)) {
            $('#cellfin_input_err').fadeIn();
            return;
        }
        $('#cellfin_input_err').fadeOut();
        selectedNumber = num;

        $('#screen_number').addClass('hidden-panel');
        $('#screen_waiting').removeClass('hidden-panel');

        startCellfinTimer();
        startCellfinPolling();
    };

    window.backToNumberInput = function() {
        stopCellfinPolling();
        stopCellfinTimer();
        $('#screen_waiting').addClass('hidden-panel');
        $('#screen_number').removeClass('hidden-panel');
        timerSec = 600;
    };

    function startCellfinTimer() {
        timerId = setInterval(function() {
            timerSec--;
            var mins = Math.floor(timerSec / 60);
            var secs = timerSec % 60;
            $('#cellfin_timer').text((mins < 10 ? '0' : '') + mins + ':' + (secs < 10 ? '0' : '') + secs);

            if (timerSec <= 0) {
                stopCellfinPolling();
                stopCellfinTimer();
                if (typeof showToast === 'function') {
                    showToast(currentLang === 'bn' ? 'সেশনের সময় শেষ হয়েছে। অনুগ্রহ করে আবার চেষ্টা করুন।' : 'Session expired. Please try again.', 'error');
                }
                setTimeout(function() { window.location.reload(); }, 2000);
            }
        }, 1000);
    }

    function stopCellfinTimer() {
        if (timerId) { clearInterval(timerId); timerId = null; }
    }

    function startCellfinPolling() {
        pollId = setInterval(function() {
            $.post(checkUrl, {
                token: csrfToken,
                tmp_id: tmpId,
                sender_number: selectedNumber,
                t_type: 'mobile',
                acc_tp: accTp
            }, function(res) {
                if (res.status === 'success') {
                    stopCellfinPolling();
                    stopCellfinTimer();
                    if (typeof showToast === 'function') showToast(res.message, 'success');
                    if (res.redirect) {
                        setTimeout(function() { window.location.replace(res.redirect); }, 1000);
                    }
                } else if (res.status === 'error' && res.fatal) {
                    stopCellfinPolling();
                    stopCellfinTimer();
                    if (typeof showToast === 'function') showToast(res.message, 'error');
                }
            }, 'json');
        }, 5000);
    }

    function stopCellfinPolling() {
        if (pollId) { clearInterval(pollId); pollId = null; }
    }
})();
</script>

<?php endif; ?>