<style>
    /* ─── EasyPaisa UI Executive Luxury Theme ─── */
    :root {
        --ep-green: #00A859;
        --ep-dark-green: #004D26;
        --ep-mint: #E6F6EE;
        --ep-gold: #FFD700;
        --ep-card-bg: rgba(0, 0, 0, 0.25);
    }

    /* Container Box Styling */
    .brand-bg {
        background: linear-gradient(180deg, #007A3E 0%, #003318 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 */
    .ep-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(--ep-green);
    }

    /* Invoice Info Table Structure */
    .ep-summary {
        text-align: left;
        width: 100%;
        margin-bottom: 18px;
        background: var(--ep-card-bg);
        padding: 14px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.12);
    }
    .ep-summary p {
        margin: 5px 0;
        font-size: 13px;
        display: flex;
        justify-content: space-between;
    }
    .ep-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(--ep-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 */
    .ep-main-body {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    .ep-title {
        font-size: 16px;
        font-weight: 700;
        margin-bottom: 8px;
        text-align: center;
    }
    .ep-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 */
    .ep-input-wrapper {
        position: relative;
        width: 100%;
        margin-bottom: 15px;
    }
    .ep-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);
    }
    .ep-custom-input:focus {
        border-color: var(--ep-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(--ep-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(--ep-gold);
    }
    .clean-number-box .num-val {
        font-size: 28px;
        font-weight: 800;
        color: var(--ep-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(--ep-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(--ep-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(--ep-gold); font-weight: 700; }
    
    /* RTL adjustments for Urdu text */
    .rtl-text {
        text-align: right !important;
        direction: rtl !important;
        font-family: 'Noto Nastaliq Urdu', 'Segoe UI', Tahoma, sans-serif;
        line-height: 1.8 !important;
    }

    /* Action Buttons Area */
    .ep-action-row {
        display: flex;
        gap: 10px;
        width: 100%;
    }
    .ep-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(--ep-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, 168, 89, 0.2);
        border-color: var(--ep-green);
    }

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

    /* Functional Utilities */
    .hidden-panel { display: none !important; }
    .phone-error-msg {
        color: var(--ep-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' || $acc_tp == 'agent') :
    
    // Dynamic text setup depending on account type
    if ($acc_tp == 'personal') {
        $merchant_number = get_value($setting['params'], 'personal_number');
        $action_label_en = 'Send Money';
        $action_label_ur = 'منی ٹرانسفر';
        $badge_text_en = 'Type: Personal Account (Send Money)';
        $badge_text_ur = 'اکاؤنٹ کی قسم: پرسنل اکاؤنٹ (منی ٹرانسفر)';
    } else {
        $merchant_number = get_value($setting['params'], 'agent_number');
        $action_label_en = 'Cash Out';
        $action_label_ur = 'کیش آؤٹ';
        $badge_text_en = 'Type: Agent Account (Cash Out)';
        $badge_text_ur = 'اکاؤنٹ کی قسم: ایجنٹ اکاؤنٹ (کیش آؤٹ)';
    }
    $ussd_code = "*786#";
?>

<div class="ep-header-actions">
    <div class="lang-toggle-btn" onclick="toggleEasyPaisaLanguage()">
        <span id="lang_en_btn" class="active">Eng</span>
        <span id="lang_ur_btn">اردو</span>
    </div>
</div>

<div class="ep-summary">
    <p><span class="lang-node" data-en="Invoice No:" data-ur="انوانس نمبر:">Invoice No:</span> <strong><?= $all_info['transaction_id'] ?></strong></p>
    <p><span class="lang-node" data-en="Total Amount:" data-ur="کل رقم:">Total Amount:</span> <strong>PKR <?= number_format($all_info['total_amount'], 2) ?></strong></p>
    <p><span class="lang-node" data-en="Charge:" data-ur="چارجز:">Charge:</span> <strong>PKR 0</strong></p>
</div>

<!-- Screen 1: Target Sender EasyPaisa Number Input -->
<div id="screen_number" class="ep-main-body">
    <div class="ep-title lang-node" data-en="Enter Your EasyPaisa Account Number" data-ur="اپنا ایزی پیسہ اکاؤنٹ نمبر درج کریں">Enter Your EasyPaisa Account Number</div>
    <div class="ep-subtitle lang-node" data-en="Enter the EasyPaisa account number from which you will send the money" data-ur="وہ ایزی پیسہ اکاؤنٹ نمبر درج کریں جس سے آپ رقم بھیجیں گے">Enter the EasyPaisa account number from which you will send the money</div>
    
    <div class="ep-input-wrapper">
        <input class="ep-custom-input" type="tel" id="pure_ep_num" placeholder="03XXXXXXXXX" maxlength="11" autocomplete="off">
    </div>
    
    <div class="phone-error-msg lang-node" id="ep_input_err" data-en="Please enter a valid EasyPaisa number (11 digits)" data-ur="براہ کرم درست ایزی پیسہ نمبر درج کریں (11 ہندسے)">Please enter a valid EasyPaisa number (11 digits)</div>

    <div class="ep-action-row">
        <button class="ep-btn btn-close lang-node" data-en="Close" data-ur="بند کریں" onclick="window.location.href='<?= base_url("api/execute/" . $all_info['tmp_ids']); ?>'">Close</button>
        <button class="ep-btn btn-proceed lang-node" data-en="Proceed" data-ur="آگے بڑھیں" onclick="submitEasyPaisaNumber()">Proceed</button>
    </div>
</div>

<!-- Screen 2: Display Instruction / Polling Panel -->
<div id="screen_waiting" class="ep-main-body hidden-panel">
    <div class="method-tab-bar">
        <div id="tab_app" class="method-tab active" onclick="switchEasyPaisaInstruction('app')">EasyPaisa App</div>
        <div id="tab_ussd" class="method-tab" onclick="switchEasyPaisaInstruction('ussd')">USSD Code (*786#)</div>
    </div>

    <div class="clean-number-box" onclick="copyEasyPaisaTarget()" 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-en="⚠️ Please wait 10 to 15 seconds after completing the transaction." data-ur="⚠️ ٹرانزیکشن مکمل کرنے کے بعد براہ کرم 10 سے 15 سیکنڈ انتظار کریں۔">⚠️ 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="ep-status-area">
        <div style="font-weight: 700; margin-bottom: 4px;" class="lang-node" data-en="⏳ Verifying payment..." data-ur="⏳ ادائیگی کی تصدیق کی جا رہی ہے...">⏳ Verifying payment...</div>
        <div><span class="lang-node" data-en="Time Remaining: " data-ur="باقی وقت: ">Time Remaining: </span><span class="timer-badge" id="ep_timer">10:00</span></div>
    </div>

    <div class="ep-action-row">
        <button class="ep-btn btn-close lang-node" data-en="← Change Number" data-ur="← نمبر تبدیل کریں" onclick="backToNumberInput()">← Change Number</button>
        <button class="ep-btn btn-cancel lang-node" data-en="Cancel" data-ur="منسوخ کریں" onclick="window.location.href='<?= base_url("api/execute/" . $all_info['tmp_ids']); ?>'">Cancel</button>
    </div>
</div>

<script>
(function() {
    var currentLang = 'en';
    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/easypaisa') ?>'; 
    var csrfToken = '<?= csrf_hash() ?>';

    updateLanguageSteps();

    window.toggleEasyPaisaLanguage = function() {
        currentLang = (currentLang === 'en') ? 'ur' : 'en';
        if (currentLang === 'ur') {
            $('#lang_ur_btn').addClass('active');
            $('#lang_en_btn').removeClass('active');
            $('.instruction-box').addClass('rtl-text');
        } else {
            $('#lang_en_btn').addClass('active');
            $('#lang_ur_btn').removeClass('active');
            $('.instruction-box').removeClass('rtl-text');
        }
        
        $('.lang-node').each(function() {
            var node = $(this);
            node.html(node.data(currentLang));
        });

        updateLanguageSteps();
    };

    function updateLanguageSteps() {
        var actionTextEn = '<?= $action_label_en ?>';
        var actionTextUr = '<?= $action_label_ur ?>';
        
        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 box above to copy the phone number.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Open EasyPaisa App and select <span class="hl">"' + actionTextEn + '"</span>.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Transfer exactly <span class="hl">PKR <?= number_format($all_info['total_amount'], 2) ?></span> to the copied number.</div>'
            );
            $('#ins_ussd').html(
                badgeHtml +
                '<div class="ins-step"><span class="hl">1.</span> Dial <span class="hl"><?= $ussd_code ?></span> from your mobile SIM.</div>' +
                '<div class="ins-step"><span class="hl">2.</span> Select <span class="hl">"' + actionTextEn + '"</span> option from the menu list.</div>' +
                '<div class="ins-step"><span class="hl">3.</span> Enter recipient number <span class="hl"><?= $merchant_number ?></span> and amount <span class="hl"><?= number_format($all_info['total_amount'], 2) ?></span> to confirm.</div>'
            );
        } else {
            var badgeHtml = '<div class="instruction-acc-badge"><?= $badge_text_ur ?></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">"' + actionTextUr + '"</span> کا انتخاب کریں۔</div>' +
                '<div class="ins-step"><span class="hl">۳.</span> کاپی کیے گئے نمبر پر بالکل <span class="hl">PKR <?= number_format($all_info['total_amount'], 2) ?></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">"' + actionTextUr + '"</span> کا آپشن منتخب کریں۔</div>' +
                '<div class="ins-step"><span class="hl">۳.</span> وصول کنندہ کا نمبر <span class="hl"><?= $merchant_number ?></span> اور رقم <span class="hl"><?= number_format($all_info['total_amount'], 2) ?></span> درج کرکے تصدیق کریں۔</div>'
            );
        }
    }

    window.switchEasyPaisaInstruction = 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.copyEasyPaisaTarget = 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 === 'en' ? 'Number Copied Successfully!' : 'نمبر کامیابی سے کاپی ہو گیا ہے!', 'success');
        }
    };

    window.submitEasyPaisaNumber = function() {
        var num = $('#pure_ep_num').val().trim();

        if (num.length !== 11) {
            $('#ep_input_err').fadeIn();
            return;
        }
        $('#ep_input_err').fadeOut();
        selectedNumber = num;

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

        startEasyPaisaTimer();
        startEasyPaisaPolling();
    };

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

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

            if (timerSec <= 0) {
                stopEasyPaisaPolling();
                stopEasyPaisaTimer();
                if (typeof showToast === 'function') {
                    showToast(currentLang === 'en' ? 'Session expired. Please try again.' : 'سیشن ختم ہو گیا ہے۔ براہ کرم دوبارہ کوشش کریں۔', 'error');
                }
                setTimeout(function() { window.location.reload(); }, 2000);
            }
        }, 1000);
    }

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

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

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

<?php endif; ?>