/*
Theme Name:   Carenow child theme
Description:  Write here a brief description about your child-theme
Author:       Themesflat
Author URL:   https://themesflat.com/carenowwp/
Template:     carenow
Version:      1.0.0
Tested up to: 5.9
Requires PHP: 5.6
License:      GNU General Public License v2 or later
License URI:  http://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  carenow-child-theme
*/

/**
 * MAIB Payment Gateway Integration
 * Handles payment success and failure pages with shortcodes
 */

// Add payment page styles
function maib_payment_page_styles() {
    ?>
    <style>
        .maib-payment-container {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            text-align: center;
            max-width: 500px;
            width: 90%;
            margin: 50px auto;
        }
        .maib-success-icon {
            width: 80px;
            height: 80px;
            background: #10b981;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            color: white;
            font-size: 40px;
        }
        .maib-error-icon {
            width: 80px;
            height: 80px;
            background: #ef4444;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            color: white;
            font-size: 40px;
        }
        .maib-payment-details {
            background: #f9fafb;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
            text-align: left;
        }
        .maib-payment-details.error {
            background: #fef2f2;
            border-left: 4px solid #ef4444;
        }
        .maib-payment-details strong {
            color: #374151;
        }
        .maib-btn {
            background: #3b82f6;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            text-decoration: none;
            display: inline-block;
            margin: 10px;
            transition: background 0.3s;
        }
        .maib-btn:hover {
            background: #2563eb;
            color: white;
            text-decoration: none;
        }
        .maib-btn-secondary {
            background: #6b7280;
        }
        .maib-btn-secondary:hover {
            background: #4b5563;
        }
        .maib-btn-retry {
            background: #10b981;
        }
        .maib-btn-retry:hover {
            background: #059669;
        }
    </style>
    <?php
}

// Add payment success shortcode
function maib_payment_success_shortcode($atts) {
    // Get payment parameters
    $pay_id = $_GET['payId'] ?? '';
    $order_id = $_GET['orderId'] ?? '';
    
    // Log the success callback
    if ($pay_id && $order_id) {
        error_log("MAIB Payment Success: payId={$pay_id}, orderId={$order_id}");
        
        // Log to MAIB payment system if function exists
        if (function_exists('maib_payment_log')) {
            maib_payment_log($pay_id, $order_id, 'payment_success_page', 'Payment success page accessed', array(
                'pay_id' => $pay_id,
                'order_id' => $order_id,
                'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
                'ip' => $_SERVER['REMOTE_ADDR'] ?? ''
            ));
        }
    }
    
    // Add styles
    maib_payment_page_styles();
    
    // Add auto-redirect script
    ?>
    <script>
        console.log('MAIB Payment Success Page Loaded');
        console.log('Payment ID:', '<?php echo esc_js($pay_id); ?>');
        console.log('Order ID:', '<?php echo esc_js($order_id); ?>');
        
        // Auto-redirect after 10 seconds
        setTimeout(function() {
            window.location.href = '<?php echo esc_js(home_url()); ?>';
        }, 10000);
    </script>
    <?php
    
    $site_url = home_url();
    
    ob_start();
    ?>
    <div class="maib-payment-container">
        <div class="maib-success-icon">✓</div>
        <h1>Payment Successful!</h1>
        <p>Thank you for your payment. Your transaction has been completed successfully.</p>
        
        <?php if ($pay_id && $order_id): ?>
        <div class="maib-payment-details">
            <p><strong>Payment ID:</strong> <?php echo esc_html($pay_id); ?></p>
            <p><strong>Order ID:</strong> <?php echo esc_html($order_id); ?></p>
            <p><strong>Status:</strong> <span style="color: #10b981;">✓ Completed</span></p>
        </div>
        <?php endif; ?>
        
        <p>You will receive a confirmation email shortly with your payment details.</p>
        
        <div>
            <a href="<?php echo esc_url($site_url); ?>" class="maib-btn">Return to Homepage</a>
            <a href="<?php echo esc_url($site_url . '/contact'); ?>" class="maib-btn maib-btn-secondary">Contact Support</a>
        </div>
    </div>
    <?php
    return ob_get_clean();
}

// Add payment failure shortcode
function maib_payment_failed_shortcode($atts) {
    // Get payment parameters
    $pay_id = $_GET['payId'] ?? '';
    $order_id = $_GET['orderId'] ?? '';
    $error = $_GET['error'] ?? '';
    
    // Log the failure callback
    if ($pay_id && $order_id) {
        error_log("MAIB Payment Failed: payId={$pay_id}, orderId={$order_id}, error={$error}");
        
        // Log to MAIB payment system if function exists
        if (function_exists('maib_payment_log')) {
            maib_payment_log($pay_id, $order_id, 'payment_failed_page', 'Payment failed page accessed', array(
                'pay_id' => $pay_id,
                'order_id' => $order_id,
                'error' => $error,
                'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
                'ip' => $_SERVER['REMOTE_ADDR'] ?? ''
            ));
        }
    }
    
    // Add styles
    maib_payment_page_styles();
    
    // Add auto-redirect script
    ?>
    <script>
        console.log('MAIB Payment Failed Page Loaded');
        console.log('Payment ID:', '<?php echo esc_js($pay_id); ?>');
        console.log('Order ID:', '<?php echo esc_js($order_id); ?>');
        console.log('Error:', '<?php echo esc_js($error); ?>');
        
        // Auto-redirect after 15 seconds
        setTimeout(function() {
            window.location.href = '<?php echo esc_js(home_url()); ?>';
        }, 15000);
    </script>
    <?php
    
    $site_url = home_url();
    
    ob_start();
    ?>
    <div class="maib-payment-container">
        <div class="maib-error-icon">✕</div>
        <h1>Payment Failed</h1>
        <p>We're sorry, but your payment could not be processed. Please try again or contact support if the problem persists.</p>
        
        <?php if ($pay_id && $order_id): ?>
        <div class="maib-payment-details error">
            <p><strong>Payment ID:</strong> <?php echo esc_html($pay_id); ?></p>
            <p><strong>Order ID:</strong> <?php echo esc_html($order_id); ?></p>
            <p><strong>Status:</strong> <span style="color: #ef4444;">✕ Failed</span></p>
            <?php if ($error): ?>
            <p><strong>Error:</strong> <?php echo esc_html($error); ?></p>
            <?php endif; ?>
        </div>
        <?php endif; ?>
        
        <p>Don't worry, your card has not been charged. You can try the payment again.</p>
        
        <div>
            <a href="javascript:history.back()" class="maib-btn maib-btn-retry">Try Again</a>
            <a href="<?php echo esc_url($site_url); ?>" class="maib-btn">Return to Homepage</a>
            <a href="<?php echo esc_url($site_url . '/contact'); ?>" class="maib-btn maib-btn-secondary">Contact Support</a>
        </div>
    </div>
    <?php
    return ob_get_clean();
}

// Register shortcodes
add_shortcode('maib_payment_success', 'maib_payment_success_shortcode');
add_shortcode('maib_payment_failed', 'maib_payment_failed_shortcode');

// Handle payment page URLs
function maib_handle_payment_pages() {
    // Get current page slug
    $current_page = get_query_var('pagename');
    
    // Handle payment success page
    if ($current_page === 'payment-success') {
        // Set page title
        add_filter('wp_title', function() {
            return 'Payment Successful - ' . get_bloginfo('name');
        });
        
        // Add shortcode to page content
        add_filter('the_content', function($content) {
            return do_shortcode('[maib_payment_success]');
        });
        
        return;
    }
    
    // Handle payment failure page
    if ($current_page === 'payment-failed') {
        // Set page title
        add_filter('wp_title', function() {
            return 'Payment Failed - ' . get_bloginfo('name');
        });
        
        // Add shortcode to page content
        add_filter('the_content', function($content) {
            return do_shortcode('[maib_payment_failed]');
        });
        
        return;
    }
}

// Hook into WordPress init
add_action('init', 'maib_handle_payment_pages');
