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

    /* Container Box Styling */
    .brand-bg {
        background: linear-gradient(180deg, #00803d 0%, #004d24 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 */
    .tap-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(--tap-green);
    }

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

    /* Embedded 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(--tap-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 */
    .tap-main-body {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    .tap-title {
        font-size: 16px;
        font-weight: 700;
        margin-bottom: 8px;
        text-align: center;
    }
    .tap-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 */
    .tap-input-wrapper {
        position: relative;
        width: 100%;
        margin-bottom: 15px;
    }
    .tap-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);
    }
    .tap-custom-input:focus {
        border-color: var(--tap-gold);
        box-shadow: 0 0 10px rgba(255, 215, 0, 0.6);
    }

    /* Tab Layout for App/USSD */
    .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(--tap-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(--tap-gold);
    }
    .clean-number-box .num-val {
        font-size: 28px;
        font-weight: 800;
        color: var(--tap-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(--tap-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(--tap-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(--tap-gold); font-weight: 700; }

    /* Action Buttons Area */
    .tap-action-row {
        display: flex;
        gap: 10px;
        width: 100%;
    }
    .tap-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(--tap-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(--tap-green);
    }

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

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

    .brand-bg .transaction, .payment_submit_done { display: none !important; }
</style>

<?php
$acc_tp = decrypt($_GET['acc_tp']);
if ($acc_tp == 'personal' || $acc_tp == 'agent') :
    $merchant_number = ($acc_tp == 'personal')
        ? get_value($setting['params'], 'personal_number')
        : get_value($setting['params'], 'agent_number');
    $action_label_en = ($acc_tp == 'personal') ? 'Send Money' : 'Cash Out';
    $action_label_bn = ($acc_tp == 'personal') ? 'সেন্ড মানি' : 'ক্যাশ আউট';
    $ussd_code = "*733#";
    
    $badge_text_bn = ($acc_tp == 'personal') ? 'টাইপ: পার্সোনাল একাউন্ট (Send Money)' : 'টাইপ: এজেন্ট একাউন্ট (Cash Out)';
    $badge_text_en = ($acc_tp == 'personal') ? 'Type: Personal Account (Send Money)' : 'Type: Agent Account (Cash Out)';
?>

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

<div class="tap-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><?= currency_format($all_info['total_amount']) ?></strong></p>
    <p><span class="lang-node" data-bn="চার্জ:" data-en="Charge:">চার্জ:</span> <strong>BDT 0.00</strong></p>
</div>

<div id="screen_number" class="tap-main-body">
    <div class="tap-title lang-node" data-bn="আপনার TAP একাউন্ট নম্বর দিন" data-en="Your TAP Account Number">আপনার TAP একাউন্ট নম্বর দিন</div>
    <div class="tap-subtitle lang-node" data-bn="যে নম্বর থেকে টাকা পাঠাবেন, এখানে সেই TAP একাউন্ট নম্বরটি দিন" data-en="Please enter the TAP account number from which you will send the money">যে নম্বর থেকে টাকা পাঠাবেন, এখানে সেই TAP একাউন্ট নম্বরটি দিন</div>
    
    <div class="tap-input-wrapper">
        <input class="tap-custom-input" type="tel" id="pure_tap_num" placeholder="01XXXXXXXXX" maxlength="11" autocomplete="off">
    </div>
    
    <div class="phone-error-msg" id="tap_input_err" class="lang-node" data-bn="সঠিক ১১ ডিজিটের TAP নম্বর প্রদান করুন" data-en="Please enter a valid 11 digit TAP number">সঠিক ১১ ডিজিটের TAP নম্বর প্রদান করুন</div>

    <div class="tap-action-row">
        <button class="tap-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="tap-btn btn-proceed lang-node" data-bn="Proceed" data-en="Proceed" onclick="submitTapNumber()">Proceed</button>
    </div>
</div>

<div id="screen_waiting" class="tap-main-body hidden-panel">
    <div class="method-tab-bar">
        <div id="tab_app" class="method-tab active" onclick="switchTapInstruction('app')">TAP App</div>
        <div id="tab_ussd" class="method-tab" onclick="switchTapInstruction('ussd')">USSD Code (*733#)</div>
    </div>

    <div class="clean-number-box" onclick="copyNumberTarget()" 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 transaction.">⚠️ টাকা পাঠানোর পর ১০ থেকে ১৫ সেকেন্ড অপেক্ষা করুন।</p>
    </div>

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

    <div class="tap-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="tap_timer">10:00</span></div>
    </div>

    <div class="tap-action-row">
        <button class="tap-btn btn-close lang-node" data-bn="← নম্বর পরিবর্তন" data-en="← Change Number" onclick="backToNumberInput()">← নম্বর পরিবর্তন</button>
        <button class="tap-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/tap') ?>';
    var csrfToken = '<?= csrf_hash() ?>';

    updateLanguageSteps();

    window.toggleTapLanguage = 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() {
        var actionText = (accTp === '<?= encrypt("personal") ?>') ? 'Send Money' : 'Cash Out';
        var actionTextBn = (accTp === '<?= encrypt("personal") ?>') ? 'সেন্ড মানি' : 'ক্যাশ আউট';
        
        if (currentLang === 'en') {
            var badgeHtml = '<div class="instruction-acc-badge"><?= $badge_text_en ?></div>';
            $('#ins_app').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">1.</span> Tap the white number box above to copy it.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Open TAP App and select <span class="hl">"' + actionText + '"</span>.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Send exactly <span class="hl"><?= currency_format($all_info['total_amount']) ?></span> to the copied number.</div>' +
                '<div class="ins-step"><span class="hl">4.</span> Wait for confirmation message and payment will be verified automatically.</div>'
            );
            $('#ins_ussd').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">1.</span> Dial <span class="hl"><?= $ussd_code ?></span> from your mobile device.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Choose <span class="hl">"' + actionText + '"</span> option from the operator menu list.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Enter recipient as <span class="hl"><?= $merchant_number ?></span> and amount as <span class="hl"><?= currency_format($all_info['total_amount']) ?></span>.</div>' +
                '<div class="ins-step"><span class="hl">4.</span> Enter your TAP Mobile Menu PIN to confirm.</div>'
            );
        } else {
            var badgeHtml = '<div class="instruction-acc-badge"><?= $badge_text_bn ?></div>';
            $('#ins_app').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">১.</span> উপরের সাদা নম্বর বক্সের ওপর ক্লিক করে নম্বরটি কপি করুন।</div>' +
                '<div class="ins-step"><span class="hl">২.</span> TAP অ্যাপ-এ গিয়ে <span class="hl">\'' + actionTextBn + '\'</span> অপশনটি সিলেক্ট করুন।</div>' +
                '<div class="ins-step"><span class="hl">৩.</span> কপি করা নম্বরে নির্দিষ্ট পরিমাণ <span class="hl"><?= currency_format($all_info['total_amount']) ?></span> পেমেন্ট সম্পন্ন করুন।</div>' +
                '<div class="ins-step"><span class="hl">৪.</span> সবকিছু ঠিক থাকলে ট্রানজেকশনটি অটোমেটিক ভেরিফাই হয়ে যাবে।</div>'
            );
            $('#ins_ussd').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">১.</span> আপনার মোবাইল ফোন থেকে <span class="hl"><?= $ussd_code ?></span> ডায়াল করুন।</div>' +
                '<div class="ins-step"><span class="hl">২.</span> মেনু স্ক্রিন থেকে <span class="hl">\'' + actionText + '\'</span> অপশনটি বেছে নিন।</div>' +
                '<div class="ins-step"><span class="hl">৩.</span> প্রাপক নম্বর হিসেবে <span class="hl"><?= $merchant_number ?></span> ও এমাউন্ট <span class="hl"><?= currency_format($all_info['total_amount']) ?></span> দিন।</div>' +
                '<div class="ins-step"><span class="hl">৪.</span> নিশ্চিত করতে আপনার TAP মোবাইল মেনু পিন প্রদান করুন।</div>'
            );
        }
    }

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

    window.copyNumberTarget = 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.submitTapNumber = function() {
        var num = $('#pure_tap_num').val().trim();

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

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

        startTapTimer();
        startTapPolling();
    };

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

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

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

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

    function startTapPolling() {
        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') {
                    stopTapPolling();
                    stopTapTimer();
                    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) {
                    stopTapPolling();
                    stopTapTimer();
                    if (typeof showToast === 'function') showToast(res.message, 'error');
                }
            }, 'json');
        }, 5000);
    }

    function stopTapPolling() {
        if (pollId) { clearInterval(pollId); pollId = null; }
    }
})();
</script>
<?php endif; ?>