<?php 

if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Shortcode for Order Confirmation (Thanks) Page
 * Usage: [afdp_thanks]
 */
add_shortcode('afdp_thanks', 'afdp_thanks_shortcode');

function afdp_thanks_shortcode() {
    // 1. SETTINGS & CURRENCY
    $afdp_currency_code = get_option('afdp_currency', 'USD');
    $afdp_symbols = [
        'USD' => '$', 'GBP' => '£', 'EUR' => '€', 'INR' => '₹', 'BDT' => '৳',
        'AUD' => 'A$', 'CAD' => 'C$', 'JPY' => '¥', 'AED' => 'د.إ', 'SAR' => 'SR'
    ];
    $currency = isset($afdp_symbols[$afdp_currency_code]) ? $afdp_symbols[$afdp_currency_code] : $afdp_currency_code;

    // 2. FETCH THE ORDER
    $args = [
        'post_type'      => 'afdp_food_order',
        'posts_per_page' => 1,
        'post_status'    => 'publish',
    ];

    if (isset($_GET['order_id'])) {
        $args['p'] = intval(wp_unslash($_GET['order_id']));
    } elseif (is_user_logged_in()) {
        $args['meta_query'] = [['key' => 'afdp_customer_id', 'value' => get_current_user_id()]];
        $args['orderby'] = 'date';
        $args['order']   = 'DESC';
    }

    $orders = get_posts($args);

    ob_start();
    ?>

    <style>
        @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
        
        .afdp-thanks-wrapper { font-family: 'Plus Jakarta Sans', sans-serif; color: #212529; max-width: 800px; margin: 40px auto; padding: 0 15px; }
        
        /* Main Card */
        .afdp-success-card { background: #fff; border-radius: 20px; box-shadow: 0 1rem 3rem rgba(0,0,0,.175); overflow: hidden; border: 1px solid rgba(0,0,0,.125); }
        .afdp-header-accent { background: #FF385C; height: 6px; width: 100%; }
        
        /* Header Styling */
        .afdp-success-header { padding: 40px 20px; text-align: center; background-color: #f8f9fa; border-bottom: 1px solid #dee2e6; }
        .afdp-status-badge { display: inline-block; padding: .35em .65em; font-size: .75em; font-weight: 700; line-height: 1; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25rem; text-transform: uppercase; margin-bottom: 15px; }
        .status-pending { background-color: #ffc107; color: #000; }
        .status-processing { background-color: #0dcaf0; color: #fff; }
        .status-completed { background-color: #198754; color: #fff; }

        .afdp-success-header h2 { font-size: 2.5rem; font-weight: 800; margin: 0; color: #212529; }
        .afdp-order-id-pill { color: #6c757d; font-size: 0.9rem; margin-top: 10px; font-weight: 600; }

        .afdp-card-body { padding: 30px; }

        /* Bootstrap-style Grid */
        .afdp-info-row { display: flex; flex-wrap: wrap; margin-bottom: 2rem; border: 1px solid #dee2e6; border-radius: 10px; padding: 20px; background: #fff; }
        .afdp-info-col { flex: 1 0 50%; margin-bottom: 10px; }
        .afdp-info-label { font-size: 11px; text-transform: uppercase; color: #6c757d; font-weight: 700; display: block; }
        .afdp-info-val { font-weight: 600; font-size: 15px; }

        /* Bootstrap-style Table */
        .table-afdp { width: 100%; margin-bottom: 1rem; color: #212529; vertical-align: top; border-color: #dee2e6; border-collapse: collapse; }
        .table-afdp thead th { vertical-align: bottom; border-bottom: 2px solid #dee2e6; padding: .75rem; text-align: left; font-size: 13px; text-transform: uppercase; color: #495057; }
        .table-afdp td { padding: 1rem .75rem; vertical-align: middle; border-top: 1px solid #dee2e6; }
        .table-afdp tbody tr:hover { background-color: rgba(0,0,0,.04); }
        
        .afdp-item-name { font-weight: 700; font-size: 15px; color: #212529; }
        .afdp-item-sub { font-size: 12px; color: #6c757d; display: block; }

        /* Summary Box */
        .afdp-summary-card { background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 10px; padding: 20px; margin-top: 20px; margin-left: auto; max-width: 350px; }
        .summary-row { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px; }
        .summary-total { display: flex; justify-content: space-between; margin-top: 12px; padding-top: 12px; border-top: 2px solid #dee2e6; font-size: 1.25rem; font-weight: 800; color: #FF385C; }

        .btn-bootstrap { display: block; width: 100%; padding: .75rem 1rem; font-size: 1rem; font-weight: 700; border-radius: .3rem; text-align: center; text-decoration: none; transition: .15s ease-in-out; margin-top: 30px; border: 1px solid #FF385C; background: #FF385C; color: #fff; }
        .btn-bootstrap:hover { background: #e03150; border-color: #e03150; color: #fff; transform: translateY(-1px); }

        @media (max-width: 576px) {
            .afdp-info-col { flex: 1 0 100%; }
        }
    </style>

    <div class="afdp-thanks-wrapper">
        <?php if ($orders) : 
            $order = $orders[0];
            $order_id = $order->ID;
            $items    = get_post_meta($order_id, 'afdp_order_items', true);
            $subtotal = get_post_meta($order_id, 'afdp_subtotal', true);
            $delivery = get_post_meta($order_id, 'afdp_delivery_fee', true); 
            $total    = get_post_meta($order_id, 'afdp_total_price', true);
            $address  = get_post_meta($order_id, 'afdp_customer_address', true);
            $name     = get_post_meta($order_id, 'afdp_customer_name', true);
            $type     = get_post_meta($order_id, 'afdp_order_type', true);
            $status   = get_post_meta($order_id, 'afdp_order_status', true) ?: 'pending';
        ?>

        <div class="afdp-success-card">
            <div class="afdp-header-accent"></div>
            
            <div class="afdp-success-header">
                <span class="afdp-status-badge status-<?php echo esc_attr($status); ?>">
                    Status: <?php echo esc_html($status); ?>
                </span>
                <h2>Order Confirmed!</h2>
                <div class="afdp-order-id-pill">Receipt ID: #<?php echo esc_html($order_id); ?></div>
            </div>

            <div class="afdp-card-body">
                <div class="afdp-info-row">
                    <div class="afdp-info-col">
                        <span class="afdp-info-label">Customer</span>
                        <span class="afdp-info-val"><?php echo esc_html($name); ?></span>
                    </div>
                    <div class="afdp-info-col">
                        <span class="afdp-info-label">Method</span>
                        <span class="afdp-info-val"><?php echo esc_html(ucfirst($type)); ?></span>
                    </div>
                    <?php if($address && $type !== 'pickup'): ?>
                    <div class="afdp-info-col" style="flex: 1 0 100%;">
                        <span class="afdp-info-label">Deliver To</span>
                        <span class="afdp-info-val"><?php echo esc_html($address); ?></span>
                    </div>
                    <?php endif; ?>
                </div>

                <table class="table-afdp">
                    <thead>
                        <tr>
                            <th>Item Description</th>
                            <th style="text-align: center;">Qty</th>
                            <th style="text-align: right;">Price</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(is_array($items)): foreach($items as $item): 
                            $price = isset($item['unitPrice']) ? $item['unitPrice'] : 0;
                            $qty   = isset($item['qty']) ? intval($item['qty']) : 1;
                        ?>
                        <tr>
                            <td>
                                <span class="afdp-item-name"><?php echo esc_html($item['title'] ?? 'Item'); ?></span>
                                <?php if(!empty($item['extras'])): ?>
                                    <span class="afdp-item-sub">+ <?php echo esc_html(implode(', ', wp_list_pluck($item['extras'], 'name'))); ?></span>
                                <?php endif; ?>
                            </td>
                            <td style="text-align: center; font-weight: 700;">x<?php echo esc_html($qty); ?></td>
                            <td style="text-align: right; font-weight: 700;">
                                <?php echo esc_html($currency . number_format($price * $qty, 2)); ?>
                            </td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>

                <div class="afdp-summary-card">
                    <div class="summary-row">
                        <span>Subtotal</span>
                        <span><?php echo esc_html($currency . number_format((float)$subtotal, 2)); ?></span>
                    </div>
                    <?php if($delivery > 0): ?>
                    <div class="summary-row">
                        <span>Delivery Fee</span>
                        <span><?php echo esc_html($currency . number_format((float)$delivery, 2)); ?></span>
                    </div>
                    <?php endif; ?>
                    <div class="summary-total">
                        <span>Total</span>
                        <span><?php echo esc_html($currency . number_format((float)$total, 2)); ?></span>
                    </div>
                </div>

                <a href="<?php echo esc_url(home_url('/')); ?>" class="btn-bootstrap">Continue Shopping</a>
            </div>
        </div>

        <?php else: ?>
            <div style="text-align:center; padding: 50px; border: 1px solid #dee2e6; border-radius: 10px;">
                <h3>Order not found.</h3>
                <a href="<?php echo esc_url(home_url('/')); ?>" class="btn-bootstrap" style="max-width: 250px; margin: 20px auto;">Back to Menu</a>
            </div>
        <?php endif; ?>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            localStorage.removeItem('afdp_final_cart');
            localStorage.removeItem('afdp_cart_save');
        });
    </script>

    <?php
    return ob_get_clean();
}