<?php
/**
 * View Order Detail - Aligned with User Dashboard Shortcode
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$afdp_order_id = isset( $_GET['order_id'] ) ? intval( $_GET['order_id'] ) : 0;
if ( ! $afdp_order_id ) {
	return;
}

// 1. ALIGN WITH SHORTCODE: Get the User ID using the key from your dashboard
$afdp_user_id  = get_post_meta( $afdp_order_id, 'afdp_customer_id', true );
$afdp_userdata = $afdp_user_id ? get_userdata( $afdp_user_id ) : false;

// 2. DATA RETRIEVAL (Prioritizing Shortcode Keys)
if ( $afdp_userdata ) {
	$afdp_customer_name    = $afdp_userdata->display_name;
	$afdp_customer_email   = $afdp_userdata->user_email;
	$afdp_customer_phone   = get_user_meta( $afdp_user_id, 'phone', true ) ?: get_post_meta( $afdp_order_id, 'afdp_customer_phone', true );
	$afdp_customer_address = get_user_meta( $afdp_user_id, 'address', true ) ?: get_post_meta( $afdp_order_id, 'afdp_customer_address', true );
	$afdp_customer_type    = 'Registered Member';
	$afdp_profile_link     = get_edit_user_link( $afdp_user_id );
} else {
	$afdp_customer_name    = get_post_meta( $afdp_order_id, 'afdp_customer_name', true ) ?: 'Guest Customer';
	$afdp_customer_email   = get_post_meta( $afdp_order_id, 'afdp_customer_email', true ) ?: 'No Email';
	$afdp_customer_phone   = get_post_meta( $afdp_order_id, 'afdp_customer_phone', true ) ?: 'Unlisted';
	$afdp_customer_address = get_post_meta( $afdp_order_id, 'afdp_customer_address', true ) ?: 'Store Pickup';
	$afdp_customer_type    = 'Guest Checkout';
	$afdp_profile_link     = '#';
}

// Order Specifics
$afdp_notes  = get_post_meta( $afdp_order_id, 'afdp_order_notes', true );
$afdp_status = strtolower( get_post_meta( $afdp_order_id, 'afdp_order_status', true ) ?: 'pending' );
$afdp_total  = get_post_meta( $afdp_order_id, 'afdp_total_price', true );
$afdp_items  = get_post_meta( $afdp_order_id, 'afdp_order_items', true ) ?: array();

// Get Currency Symbol based on your shortcode logic
$afdp_currency_code = get_option( 'afdp_currency', 'USD' );
$afdp_symbols       = array( 'USD' => '$', 'GBP' => '£', 'EUR' => '€', 'INR' => '₹', 'BDT' => '৳', 'AUD' => 'A$', 'CAD' => 'C$', 'JPY' => '¥', 'AED' => 'د.إ', 'SAR' => 'SR' );
$afdp_currency      = isset( $afdp_symbols[ $afdp_currency_code ] ) ? $afdp_symbols[ $afdp_currency_code ] : $afdp_currency_code;
?>

<style>
    :root { --fd-primary: #FF385C; --fd-text: #0F172A; --fd-muted: #64748B; --fd-border: #E2E8F0; }
    .afdp-view-wrapper { margin-top: 20px; max-width: 1200px; font-family: 'Plus Jakarta Sans', sans-serif; }
    .afdp-card { background: #fff; border: 1px solid var(--fd-border); border-radius: 16px; margin-bottom: 25px; overflow: hidden; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); }
    .afdp-card-header { padding: 20px 24px; border-bottom: 1px solid var(--fd-border); background: #fafafa; }
    .afdp-card-header h2 { margin: 0; font-size: 14px; font-weight: 800; color: var(--fd-text); text-transform: uppercase; }
    .fd-view-table { width: 100%; border-collapse: collapse; }
    .fd-view-table td { padding: 16px; border-bottom: 1px solid #f8fafc; font-size: 14px; }
    .fd-qty-badge { background: #0F172A; color: #fff; padding: 2px 8px; border-radius: 6px; font-size: 11px; margin-right: 10px; }
    .status-badge { padding: 5px 12px; border-radius: 8px; font-size: 11px; font-weight: 700; text-transform: uppercase; }
    .status-pending { background: #FEF3C7; color: #D97706; }
    .status-completed { background: #DCFCE7; color: #166534; }
    .customer-avatar { border-radius: 12px; border: 1px solid var(--fd-border); margin-right: 15px; }
</style>

<div class="wrap afdp-view-wrapper">
    <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:30px;">
        <div>
            <h1 style="font-weight:900; color:var(--fd-text);">Order #<?php echo esc_html( $afdp_order_id ); ?></h1>
            <p style="color:var(--fd-muted); margin:0;">Placed on <?php echo esc_html( get_the_date( 'F j, Y' ) ); ?> at <?php echo esc_html( get_the_time() ); ?></p>
        </div>
        <div>
            <a href="?page=awesome_food_delivery" class="button button-large">Back to List</a>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 1fr 380px; gap: 30px;">
        <div>
            <div class="afdp-card">
                <div class="afdp-card-header" style="display:flex; justify-content:space-between; align-items:center;">
                    <h2>Items Ordered</h2>
                    <span class="status-badge status-<?php echo esc_attr( $afdp_status ); ?>"><?php echo esc_html( $afdp_status ); ?></span>
                </div>
                <div class="afdp-card-body">
                    <table class="fd-view-table">
                        <tbody>
                            <?php if ( ! empty( $afdp_items ) && is_array( $afdp_items ) ) : 
                                foreach ( $afdp_items as $afdp_item ) : ?>
                                <tr>
                                    <td>
                                        <span class="fd-qty-badge"><?php echo esc_html( $afdp_item['qty'] ); ?>x</span>
                                        <strong><?php echo esc_html( $afdp_item['name'] ?? $afdp_item['title'] ); ?></strong>
                                    </td>
                                    <td style="text-align:right; font-weight:700;">
                                        <?php echo esc_html( $afdp_currency . number_format( $afdp_item['qty'] * ( $afdp_item['price'] ?? 0 ), 2 ) ); ?>
                                    </td>
                                </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
                <div style="padding:24px; background:#f8fafc; border-top:1px solid var(--fd-border); text-align:right;">
                    <span style="color:var(--fd-muted); font-weight:700; margin-right:15px;">TOTAL AMOUNT</span>
                    <span style="font-size:28px; font-weight:900; color:var(--fd-primary);"><?php echo esc_html( $afdp_currency . number_format( (float) $afdp_total, 2 ) ); ?></span>
                </div>
            </div>

            <div class="afdp-card">
                <div class="afdp-card-header"><h2>Customer Notes</h2></div>
                <div style="padding:24px; color:var(--fd-muted); font-style:italic;">
                    <?php echo $afdp_notes ? nl2br( esc_html( $afdp_notes ) ) : 'No notes provided.'; ?>
                </div>
            </div>
        </div>

        <div>
            <div class="afdp-card">
                <div class="afdp-card-header"><h2>Customer Details</h2></div>
                <div style="padding:24px;">
                    <div style="display:flex; align-items:center; margin-bottom:20px;">
                        <?php echo get_avatar( $afdp_user_id ?: 0, 50, 'retro', '', array( 'class' => 'customer-avatar' ) ); ?>
                        <div>
                            <div style="font-weight:800; font-size:16px; color:var(--fd-text);"><?php echo esc_html( $afdp_customer_name ); ?></div>
                            <span style="font-size:10px; background:#f1f5f9; padding:2px 8px; border-radius:4px; font-weight:700;"><?php echo esc_html( $afdp_customer_type ); ?></span>
                        </div>
                    </div>

                    <div style="margin-bottom:15px;">
                        <label style="display:block; font-size:10px; font-weight:800; color:var(--fd-muted); text-transform:uppercase;">Email Address</label>
                        <div style="font-weight:600;"><?php echo esc_html( $afdp_customer_email ); ?></div>
                    </div>

                    <div style="margin-bottom:15px;">
                        <label style="display:block; font-size:10px; font-weight:800; color:var(--fd-muted); text-transform:uppercase;">Phone Number</label>
                        <div style="font-weight:600; color:var(--fd-primary);"><?php echo esc_html( $afdp_customer_phone ); ?></div>
                    </div>

                    <div style="margin-bottom:20px;">
                        <label style="display:block; font-size:10px; font-weight:800; color:var(--fd-muted); text-transform:uppercase;">Delivery Address</label>
                        <div style="font-weight:500; font-size:13px; line-height:1.5; color:#475569;">
                            <?php echo nl2br( esc_html( $afdp_customer_address ) ); ?>
                        </div>
                    </div>

                    <?php if ( $afdp_user_id ) : ?>
                        <a href="<?php echo esc_url( $afdp_profile_link ); ?>" class="button button-primary button-large" style="width:100%; text-align:center; background:#0F172A; border:none; height:45px; line-height:43px; border-radius:10px;">View User Profile</a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>