<?php
/**
 * ManiPay — on-site "Complete Your Payment" page for FluentCart.
 *
 * @package ManiPay\FluentCart
 */

namespace ManiPay\FluentCart;

use FluentCart\App\Models\Order;
use FluentCart\App\Models\OrderTransaction;

/**
 * Renders and handles the standalone payment page, and records the submitted
 * Transaction ID on the FluentCart transaction for the merchant to verify.
 */
final class PaymentPage {

    const QUERY_VAR        = 'manipay-fc-complete';
    const TRANSIENT_PREFIX = 'manipay_fc_';

    public static function boot(): void {
        add_action( 'template_redirect', array( __CLASS__, 'maybe_render' ), 1 );
    }

    public static function maybe_render(): void {
        if ( empty( $_GET[ self::QUERY_VAR ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            return;
        }

        $ref    = sanitize_text_field( wp_unslash( $_GET[ self::QUERY_VAR ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $record = get_transient( self::TRANSIENT_PREFIX . $ref );

        if ( empty( $record ) || empty( $record['transaction_uuid'] ) ) {
            wp_safe_redirect( home_url( '/' ) );
            exit;
        }

        if ( 'awaiting' !== ( $record['status'] ?? '' ) ) {
            wp_safe_redirect( home_url( '/' ) );
            exit;
        }

        $error = '';

        if ( 'POST' === ( $_SERVER['REQUEST_METHOD'] ?? '' ) && isset( $_POST['manipay_complete_submit'] ) ) {
            $error = self::handle_submit( $ref, $record );
            if ( '' === $error ) {
                exit; // handle_submit() already redirected.
            }
        }

        self::render( $ref, $record, $error );
        exit;
    }

    /**
     * @param string $ref
     * @param array  $record
     * @return string Empty on success (already redirected), error message otherwise.
     */
    private static function handle_submit( string $ref, array $record ): string {
        if ( ! isset( $_POST['manipay_complete_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['manipay_complete_nonce'] ) ), 'manipay_complete_' . $ref ) ) {
            return __( 'Security check failed, please try again.', 'manipay-manual-payment-for-fluentcart' );
        }

        if ( empty( $_POST['manipay_trx'] ) ) {
            return __( 'Please select a payment method and enter your Transaction ID.', 'manipay-manual-payment-for-fluentcart' );
        }

        if ( ! class_exists( OrderTransaction::class ) ) {
            return __( 'Could not reach FluentCart to record this payment. Please contact support.', 'manipay-manual-payment-for-fluentcart' );
        }

        $transaction = OrderTransaction::query()->where( 'uuid', $record['transaction_uuid'] )->first();
        if ( ! $transaction ) {
            return __( 'We could not find your order. Please contact support.', 'manipay-manual-payment-for-fluentcart' );
        }

        $trx    = sanitize_text_field( wp_unslash( $_POST['manipay_trx'] ) );
        $method = isset( $_POST['manipay_method'] ) ? sanitize_text_field( wp_unslash( $_POST['manipay_method'] ) ) : '';

        $notesParts = array( sprintf( 'Trx ID: %s', $trx ) );
        if ( ! empty( $_POST['manipay_sender_no'] ) ) {
            $notesParts[] = sprintf( 'Sender: %s', sanitize_text_field( wp_unslash( $_POST['manipay_sender_no'] ) ) );
        }
        if ( ! empty( $_POST['manipay_sender_email'] ) ) {
            $notesParts[] = sprintf( 'Email: %s', sanitize_email( wp_unslash( $_POST['manipay_sender_email'] ) ) );
        }

        $slip_url = '';
        if ( ! empty( $_FILES['manipay_slip']['name'] ) ) {
            $filetype           = wp_check_filetype_and_ext(
                $_FILES['manipay_slip']['tmp_name'],
                sanitize_file_name( wp_unslash( $_FILES['manipay_slip']['name'] ) )
            );
            $allowed_slip_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' );

            if ( empty( $filetype['type'] ) || ! in_array( $filetype['type'], $allowed_slip_types, true ) ) {
                return __( 'The payment slip must be an image file (JPG, PNG, GIF, or WEBP).', 'manipay-manual-payment-for-fluentcart' );
            }
            if ( $_FILES['manipay_slip']['size'] > 5 * MB_IN_BYTES ) {
                return __( 'The payment slip image is too large. Please upload a file under 5MB.', 'manipay-manual-payment-for-fluentcart' );
            }

            require_once ABSPATH . 'wp-admin/includes/file.php';
            require_once ABSPATH . 'wp-admin/includes/image.php';
            require_once ABSPATH . 'wp-admin/includes/media.php';

            $attachment_id = media_handle_upload( 'manipay_slip', 0 );
            if ( ! is_wp_error( $attachment_id ) ) {
                $slip_url     = wp_get_attachment_url( $attachment_id );
                $notesParts[] = sprintf( 'Slip: %s', $slip_url );
            } else {
                return __( 'The payment slip could not be uploaded. Please try again.', 'manipay-manual-payment-for-fluentcart' );
            }
        }

        // Record the Trx ID where FluentCart's own Order Details screen shows it
        // ("Gateway ID" column reads the vendor_charge_id field). The order/transaction
        // status is deliberately left as-is (Pending) — verification is manual.
        $transaction->update( array(
            'vendor_charge_id' => $trx,
            'payment_note'     => implode( ' | ', $notesParts ),
        ) );

        // Keep the full submission (including slip URL) in a transient too, as a
        // backup in case 'payment_note' isn't a real column on this FluentCart version.
        $record['status']       = 'submitted';
        $record['trx']          = $trx;
        $record['method']       = $method;
        $record['slip_url']     = $slip_url;
        $record['sender_no']    = sanitize_text_field( wp_unslash( $_POST['manipay_sender_no'] ?? '' ) );
        $record['sender_email'] = sanitize_email( wp_unslash( $_POST['manipay_sender_email'] ?? '' ) );
        set_transient( self::TRANSIENT_PREFIX . $ref, $record, 30 * DAY_IN_SECONDS );

        $redirect_url = method_exists( $transaction, 'getReceiptPageUrl' )
            ? $transaction->getReceiptPageUrl()
            : home_url( '/' );

        wp_redirect( esc_url_raw( $redirect_url ) );
        exit;
    }

    private static function opt( $settings, $name, $default = '' ) {
        return isset( $settings[ $name ] ) && '' !== $settings[ $name ] ? $settings[ $name ] : $default;
    }

    /**
     * CSS for the standalone page shell (not part of the shared plugin
     * stylesheet, since it only applies to this one page). Returned as a
     * string for use with wp_add_inline_style() rather than a raw <style> tag.
     *
     * @return string
     */
    private static function page_shell_css(): string {
        return "
            body.manipay-standalone { background:#f6f3ea; margin:0; padding:48px 16px; font-family:'Inter',sans-serif; }
            .manipay-page-wrap { max-width:560px; margin:0 auto; }
            .manipay-page-brand { text-align:center; margin-bottom:22px; font-family:'Space Grotesk',sans-serif; font-weight:700; font-size:18px; color:#15241d; }
            .manipay-page-brand a { color:inherit; text-decoration:none; }
            .manipay-page-heading { text-align:center; margin-bottom:22px; }
            .manipay-page-heading h1 { font-family:'Space Grotesk',sans-serif; font-size:26px; font-weight:700; color:#15241d; margin:0 0 8px; }
            .manipay-page-heading p { color:#5b6a63; font-size:15px; margin:0; line-height:1.5; }
            .manipay-error-banner { background:#fdecee; border:1px solid #f3b9c1; color:#b31e32; padding:12px 16px; border-radius:12px; font-size:14.5px; font-weight:500; margin-bottom:18px; text-align:center; }
            .manipay-submit-btn { width:100%; margin-top:18px; padding:17px; border:none; border-radius:14px; background:#d7263d; color:#fff; font-family:'Space Grotesk',sans-serif; font-weight:700; font-size:15.5px; letter-spacing:0.01em; cursor:pointer; }
            .manipay-submit-btn:hover { background:#b31e32; }
            .manipay-back-link { display:block; text-align:center; margin-top:20px; font-size:13.5px; color:#8a938c; text-decoration:none; }
        ";
    }

    /**
     * Tiny page-specific JS (records which method the customer picked into a
     * hidden field before submit). Returned as a string for use with
     * wp_add_inline_script() rather than a raw <script> tag.
     *
     * @return string
     */
    private static function set_method_js(): string {
        return "
            function manipaySetMethod(name) {
                var f = document.getElementById('manipay-method-field');
                if (f) f.value = name;
            }
        ";
    }

    private static function render( string $ref, array $record, string $error = '' ): void {
        $gateway  = new Gateway();
        $defaults = Settings::getDefaults();
        $get      = function ( $key ) use ( $gateway, $defaults ) {
            $val = $gateway->settings->get( $key );
            return ( null === $val || '' === $val ) ? ( $defaults[ $key ] ?? '' ) : $val;
        };

        $assets_url = MANIPAY_FC_URL . 'assets/images/';
        $logos      = array(
            'bkash'   => $assets_url . 'bkash.png',
            'nagad'   => $assets_url . 'nagad.png',
            'rocket'  => $assets_url . 'rocket.png',
            'aibl'    => $assets_url . 'al-arafa-islami-bank.png',
            'pubali'  => $assets_url . 'Pubali-Bank-plc.png',
            'dbbl'    => $assets_url . 'dach-bangla-plc.png',
            'ibbl'    => $assets_url . 'Islamic-bank-plc.png',
            'redot'   => $assets_url . 'redotapay.png',
        );

        $mob_count = count( array_filter( array( 'yes' === $get( 'enable_bkash' ), 'yes' === $get( 'enable_nagad' ), 'yes' === $get( 'enable_rocket' ) ) ) );

        $bank_tab_enabled = 'yes' === $get( 'enable_ibanking' );
        $bank_count       = $bank_tab_enabled ? count( array_filter( array(
            '' !== $get( 'aibl_acc_no' ), '' !== $get( 'pubali_acc_no' ), '' !== $get( 'dbbl_acc_no' ), '' !== $get( 'ibbl_acc_no' ),
        ) ) ) : 0;

        $crypto_tab_enabled = 'yes' === $get( 'enable_crypto' );
        $crypto_count       = $crypto_tab_enabled ? count( array_filter( array( '' !== $get( 'redot_id' ) ) ) ) : 0;

        $visible_tabs = array();
        if ( $mob_count > 0 ) { $visible_tabs[] = 'mob'; }
        if ( $bank_count > 0 ) { $visible_tabs[] = 'bank'; }
        if ( $crypto_count > 0 ) { $visible_tabs[] = 'intl'; }
        $first_tab         = $visible_tabs[0] ?? '';
        $show_tab_switcher = count( $visible_tabs ) > 1;

        $intro_map = array(
            'mob'  => array( 'title' => $get( 'intro_mob_title' ), 'desc' => $get( 'intro_mob_desc' ) ),
            'bank' => array( 'title' => $get( 'intro_bank_title' ), 'desc' => $get( 'intro_bank_desc' ) ),
            'intl' => array( 'title' => $get( 'intro_crypto_title' ), 'desc' => $get( 'intro_crypto_desc' ) ),
        );
        $intro = $intro_map[ $first_tab ] ?? $intro_map['mob'];

        $total    = isset( $record['amount'] ) ? $record['amount'] : '';
        $currency = ! empty( $record['currency'] ) ? $record['currency'] : '৳';

        wp_enqueue_style( 'manipay-fc-style', MANIPAY_FC_URL . 'assets/css/pxelcode-pay-style.css', array(), MANIPAY_FC_VERSION );
        wp_add_inline_style( 'manipay-fc-style', self::page_shell_css() );
        wp_enqueue_script( 'manipay-fc-script', MANIPAY_FC_URL . 'assets/js/pxelcode-pay-script.js', array(), MANIPAY_FC_VERSION, true );
        wp_add_inline_script( 'manipay-fc-script', self::set_method_js() );
        wp_localize_script( 'manipay-fc-script', 'pxpayI18n', array(
            'mobTitle'      => $get( 'intro_mob_title' ),
            'mobDesc'       => $get( 'intro_mob_desc' ),
            'bankTitle'     => $get( 'intro_bank_title' ),
            'bankDesc'      => $get( 'intro_bank_desc' ),
            'cryptoTitle'   => $get( 'intro_crypto_title' ),
            'cryptoDesc'    => $get( 'intro_crypto_desc' ),
            'personalLabel' => __( 'Personal', 'manipay-manual-payment-for-fluentcart' ),
            'emailIdLabel'  => __( 'Email/ID', 'manipay-manual-payment-for-fluentcart' ),
            'senderNoPh'    => __( 'Enter sender number...', 'manipay-manual-payment-for-fluentcart' ),
            'cryptoEmailPh' => __( 'Your RedotPay Email', 'manipay-manual-payment-for-fluentcart' ),
            'trxPh'         => __( 'Enter Transaction ID...', 'manipay-manual-payment-for-fluentcart' ),
            'trxRefPh'      => __( 'Transaction ID / Ref No', 'manipay-manual-payment-for-fluentcart' ),
            'slipLabel'     => __( 'Upload Payment Slip (Required)', 'manipay-manual-payment-for-fluentcart' ),
            'bankLabel'     => __( 'Bank', 'manipay-manual-payment-for-fluentcart' ),
            'nameLabel'     => __( 'Name', 'manipay-manual-payment-for-fluentcart' ),
            'accNoLabel'    => __( 'A/C No', 'manipay-manual-payment-for-fluentcart' ),
            'branchLabel'   => __( 'Branch', 'manipay-manual-payment-for-fluentcart' ),
            'routingLabel'  => __( 'Routing', 'manipay-manual-payment-for-fluentcart' ),
            'swiftLabel'    => __( 'SWIFT', 'manipay-manual-payment-for-fluentcart' ),
            'qrScanLabel'   => __( 'Or scan with your app', 'manipay-manual-payment-for-fluentcart' ),
        ) );

        nocache_headers();
        ?><!DOCTYPE html>
        <html <?php language_attributes(); ?>>
        <head>
            <meta charset="<?php bloginfo( 'charset' ); ?>">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title><?php esc_html_e( 'Complete Your Payment', 'manipay-manual-payment-for-fluentcart' ); ?> — <?php bloginfo( 'name' ); ?></title>
            <?php wp_head(); ?>
        </head>
        <body class="manipay-standalone">
            <div class="manipay-page-wrap">
                <div class="manipay-page-brand"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></div>
                <div class="manipay-page-heading">
                    <h1><?php esc_html_e( 'Complete Your Payment', 'manipay-manual-payment-for-fluentcart' ); ?></h1>
                    <p><?php esc_html_e( 'Choose a method below, send the payment, then submit your Transaction ID.', 'manipay-manual-payment-for-fluentcart' ); ?></p>
                </div>

                <?php if ( $error ) : ?>
                    <div class="manipay-error-banner"><?php echo esc_html( $error ); ?></div>
                <?php endif; ?>

                <form method="post" enctype="multipart/form-data">
                    <?php wp_nonce_field( 'pxpay_complete_' . $ref, 'pxpay_complete_nonce' ); ?>
                    <input type="hidden" name="manipay_complete_submit" value="1">

                    <div class="pxpay-fintech">
                        <div id="pxpay-toast"><?php esc_html_e( 'Copied to Clipboard!', 'manipay-manual-payment-for-fluentcart' ); ?></div>

                        <div class="pxpay-strip">
                            <span class="pxpay-strip-lock" aria-hidden="true">🔒</span>
                            <span><?php esc_html_e( 'Secure Manual Payment', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                        </div>

                        <div class="pxpay-intro">
                            <h4 id="pxpay-intro-title"><?php echo esc_html( $intro['title'] ); ?></h4>
                            <p id="pxpay-intro-desc"><?php echo esc_html( $intro['desc'] ); ?></p>
                        </div>

                        <?php if ( $show_tab_switcher ) : ?>
                        <div class="pxpay-header">
                            <?php if ( $mob_count > 0 ) : ?>
                                <button type="button" class="pxpay-tab-btn <?php echo 'mob' === $first_tab ? 'active' : ''; ?>" onclick="pxpaySwTab(event, 'pxpay-mob')"><?php esc_html_e( 'Mobile Banking', 'manipay-manual-payment-for-fluentcart' ); ?></button>
                            <?php endif; ?>
                            <?php if ( $bank_count > 0 ) : ?>
                                <button type="button" class="pxpay-tab-btn <?php echo 'bank' === $first_tab ? 'active' : ''; ?>" onclick="pxpaySwTab(event, 'pxpay-bank')"><?php esc_html_e( 'iBanking', 'manipay-manual-payment-for-fluentcart' ); ?></button>
                            <?php endif; ?>
                            <?php if ( $crypto_count > 0 ) : ?>
                                <button type="button" class="pxpay-tab-btn <?php echo 'intl' === $first_tab ? 'active' : ''; ?>" onclick="pxpaySwTab(event, 'pxpay-intl')"><?php esc_html_e( 'Crypto', 'manipay-manual-payment-for-fluentcart' ); ?></button>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>

                        <?php if ( $mob_count > 0 ) : ?>
                        <div id="pxpay-mob" class="pxpay-body <?php echo 'mob' === $first_tab ? 'active' : ''; ?>">
                            <div class="pxpay-grid" style="grid-template-columns: repeat(<?php echo (int) $mob_count; ?>, 1fr);">
                                <?php if ( 'yes' === $get( 'enable_bkash' ) ) : ?>
                                    <div class="pxpay-card" onclick="manipaySetMethod('bKash'); pxpayShDet(this, 'bKash', '<?php echo esc_js( $get( 'bkash_number' ) ); ?>', '<?php echo esc_url( $logos['bkash'] ); ?>', 'mobile', '<?php echo esc_url( $get( 'bkash_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['bkash'] ); ?>"><span>bKash</span>
                                    </div>
                                <?php endif; ?>
                                <?php if ( 'yes' === $get( 'enable_nagad' ) ) : ?>
                                    <div class="pxpay-card" onclick="manipaySetMethod('Nagad'); pxpayShDet(this, 'Nagad', '<?php echo esc_js( $get( 'nagad_number' ) ); ?>', '<?php echo esc_url( $logos['nagad'] ); ?>', 'mobile', '<?php echo esc_url( $get( 'nagad_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['nagad'] ); ?>"><span>Nagad</span>
                                    </div>
                                <?php endif; ?>
                                <?php if ( 'yes' === $get( 'enable_rocket' ) ) : ?>
                                    <div class="pxpay-card" onclick="manipaySetMethod('Rocket'); pxpayShDet(this, 'Rocket', '<?php echo esc_js( $get( 'rocket_number' ) ); ?>', '<?php echo esc_url( $logos['rocket'] ); ?>', 'mobile', '<?php echo esc_url( $get( 'rocket_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['rocket'] ); ?>"><span>Rocket</span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>

                        <?php if ( $bank_count > 0 ) : ?>
                        <div id="pxpay-bank" class="pxpay-body <?php echo 'bank' === $first_tab ? 'active' : ''; ?>">
                            <div class="pxpay-grid">
                                <?php if ( '' !== $get( 'aibl_acc_no' ) ) : ?>
                                    <div class="pxpay-bank-card" onclick="manipaySetMethod('AIBL'); pxpayShBank(this, '<?php echo esc_js( $get( 'aibl_name' ) ); ?>', '<?php echo esc_js( $get( 'aibl_acc_name' ) ); ?>', '<?php echo esc_js( $get( 'aibl_acc_no' ) ); ?>', '<?php echo esc_js( $get( 'aibl_branch' ) ); ?>', '<?php echo esc_js( $get( 'aibl_routing' ) ); ?>', '<?php echo esc_js( $get( 'aibl_swift' ) ); ?>', '<?php echo esc_url( $get( 'aibl_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['aibl'] ); ?>"><span>AIBL</span>
                                    </div>
                                <?php endif; ?>
                                <?php if ( '' !== $get( 'pubali_acc_no' ) ) : ?>
                                    <div class="pxpay-bank-card" onclick="manipaySetMethod('Pubali'); pxpayShBank(this, '<?php echo esc_js( $get( 'pubali_name' ) ); ?>', '<?php echo esc_js( $get( 'pubali_acc_name' ) ); ?>', '<?php echo esc_js( $get( 'pubali_acc_no' ) ); ?>', '<?php echo esc_js( $get( 'pubali_branch' ) ); ?>', '<?php echo esc_js( $get( 'pubali_routing' ) ); ?>', '<?php echo esc_js( $get( 'pubali_swift' ) ); ?>', '<?php echo esc_url( $get( 'pubali_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['pubali'] ); ?>"><span>Pubali</span>
                                    </div>
                                <?php endif; ?>
                                <?php if ( '' !== $get( 'dbbl_acc_no' ) ) : ?>
                                    <div class="pxpay-bank-card" onclick="manipaySetMethod('DBBL'); pxpayShBank(this, '<?php echo esc_js( $get( 'dbbl_name' ) ); ?>', '<?php echo esc_js( $get( 'dbbl_acc_name' ) ); ?>', '<?php echo esc_js( $get( 'dbbl_acc_no' ) ); ?>', '<?php echo esc_js( $get( 'dbbl_branch' ) ); ?>', '<?php echo esc_js( $get( 'dbbl_routing' ) ); ?>', '<?php echo esc_js( $get( 'dbbl_swift' ) ); ?>', '<?php echo esc_url( $get( 'dbbl_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['dbbl'] ); ?>"><span>DBBL</span>
                                    </div>
                                <?php endif; ?>
                                <?php if ( '' !== $get( 'ibbl_acc_no' ) ) : ?>
                                    <div class="pxpay-bank-card" onclick="manipaySetMethod('IBBL'); pxpayShBank(this, '<?php echo esc_js( $get( 'ibbl_name' ) ); ?>', '<?php echo esc_js( $get( 'ibbl_acc_name' ) ); ?>', '<?php echo esc_js( $get( 'ibbl_acc_no' ) ); ?>', '<?php echo esc_js( $get( 'ibbl_branch' ) ); ?>', '<?php echo esc_js( $get( 'ibbl_routing' ) ); ?>', '<?php echo esc_js( $get( 'ibbl_swift' ) ); ?>', '<?php echo esc_url( $get( 'ibbl_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['ibbl'] ); ?>"><span>IBBL</span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>

                        <?php if ( $crypto_count > 0 ) : ?>
                        <div id="pxpay-intl" class="pxpay-body <?php echo 'intl' === $first_tab ? 'active' : ''; ?>">
                            <div class="pxpay-grid" style="grid-template-columns: repeat(<?php echo (int) $crypto_count; ?>, 1fr);">
                                <?php if ( '' !== $get( 'redot_id' ) ) : ?>
                                    <div class="pxpay-card" onclick="manipaySetMethod('RedotPay'); pxpayShDet(this, 'RedotPay', '<?php echo esc_js( $get( 'redot_id' ) ); ?>', '<?php echo esc_url( $logos['redot'] ); ?>', 'crypto', '<?php echo esc_url( $get( 'redot_qr' ) ); ?>')">
                                        <img src="<?php echo esc_url( $logos['redot'] ); ?>"><span>RedotPay</span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>

                        <?php if ( empty( $visible_tabs ) ) : ?>
                            <div style="padding:30px 22px;text-align:center;color:#5b6a63;font-size:14px;">
                                <?php esc_html_e( 'No payment methods are currently configured. Please contact us to complete this order.', 'manipay-manual-payment-for-fluentcart' ); ?>
                            </div>
                        <?php endif; ?>

                        <?php if ( ! empty( $visible_tabs ) ) : ?>
                        <div id="pxpay-panel" style="display:none;">
                            <div class="pxpay-panel-eyebrow"><?php esc_html_e( 'Payment Details', 'manipay-manual-payment-for-fluentcart' ); ?></div>
                            <input type="hidden" name="manipay_method" id="manipay-method-field" value="">
                            <div class="pxpay-info-box">
                                <div id="pxpay-dynamic-rows"></div>
                                <div class="pxpay-info-row pxpay-info-row-blue">
                                    <div class="pxpay-row-left">
                                        <div class="pxpay-icon-circle" style="background:#fff;width:32px;height:32px;border:1.5px solid #2563eb;">
                                            <span style="color:#2563eb;font-weight:bold;font-size:16px;"><?php echo esc_html( $currency ); ?></span>
                                        </div>
                                        <div>
                                            <span class="pxpay-label"><?php esc_html_e( 'Payable Amount', 'manipay-manual-payment-for-fluentcart' ); ?></span>
                                            <div class="pxpay-value pxpay-value-amount"><?php echo esc_html( $currency . $total ); ?></div>
                                        </div>
                                    </div>
                                    <button type="button" class="pxpay-btn-copy" onclick="pxpayCopyVal('<?php echo esc_js( $total ); ?>')">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" width="18" height="18"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
                                    </button>
                                </div>
                            </div>
                            <div id="pxpay-qr-wrap"></div>
                            <div id="pxpay-input-area"></div>
                            <button type="submit" class="manipay-submit-btn"><?php esc_html_e( "I've Sent the Payment — Submit", 'manipay-manual-payment-for-fluentcart' ); ?></button>
                        </div>
                        <?php endif; ?>
                    </div>
                </form>

                <a class="manipay-back-link" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php esc_html_e( '← Back to site', 'manipay-manual-payment-for-fluentcart' ); ?></a>
            </div>
            <?php wp_footer(); ?>
        </body>
        </html>
        <?php
    }
}
