<?php
if(!defined('ABSPATH')) exit;

/**
 * View Item Detail - SaaS Premium UI
 * UPDATED: Dynamic Currency Support for Base Price and Extras
 */
function afdp_view_item_tab($item_id){

    $item = get_post($item_id);
    if(!$item || $item->post_type !== 'afdp_food_item'){
        echo '<div class="notice notice-error"><p>Item not found.</p></div>';
        return;
    }

    // --- DYNAMIC CURRENCY LOGIC ---
    $currency_code = get_option('afdp_currency', 'USD');
    $symbols = [
        'USD'=>'$', 'GBP'=>'£', 'EUR'=>'€', 'INR'=>'₹', 
        'AED'=>'د.إ', 'BDT'=>'৳', 'CAD'=>'C$', 'SAR'=>'SR'
    ];
    $currency = isset($symbols[$currency_code]) ? $symbols[$currency_code] : '$';

    // Data Fetching
    $price      = get_post_meta($item_id, 'afdp_food_price', true);
    $cats       = wp_get_post_terms($item_id, 'afdp_food_category');
    $extra_ids  = get_post_meta($item_id, 'afdp_item_extras', true);
    
    // Pulling from 'fd_extras'
    $extras_all = get_option('fd_extras', []);
    ?>

    <style>
        :root { 
            --fd-primary: #e53935; 
            --fd-primary-hover: #c62828;
            --fd-border: #ccd0d4; 
            --fd-text: #1d2327;
            --fd-muted: #64748b;
        }

        .afdp-view-wrapper { margin-top: 10px; max-width: 1000px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
        .afdp-card { background: #fff; border: 1px solid var(--fd-border); border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); overflow: hidden; }
        .afdp-card-body { padding: 40px; display: grid; grid-template-columns: 300px 1fr; gap: 40px; }

        .afdp-img-frame { border: 1px solid #eee; border-radius: 12px; overflow: hidden; width: 100%; aspect-ratio: 1/1; background: #f8f9fa; box-shadow: 0 4px 10px rgba(0,0,0,0.05); }
        .afdp-img-frame img { width: 100%; height: 100%; object-fit: cover; }
        .afdp-price-display { font-size: 36px; font-weight: 800; color: var(--fd-primary); display: block; margin-top: 20px; text-align: center; }

        .afdp-section-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1.2px; color: var(--fd-muted); margin-bottom: 15px; display: block; }
        .afdp-tag-cat { display: inline-flex; padding: 6px 14px; border-radius: 8px; font-size: 13px; font-weight: 600; background: #fff5f5; color: var(--fd-primary); border: 1px solid #fecaca; margin-bottom: 30px; }

        .afdp-extras-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
        .afdp-extra-card { 
            background: #fff; border: 1px solid #e2e8f0; border-radius: 10px; padding: 12px; 
            display: flex; align-items: center; gap: 12px; transition: 0.2s;
        }
        .afdp-extra-card:hover { border-color: var(--fd-primary); background: #fdfafa; }
        .afdp-ex-thumb { width: 40px; height: 40px; border-radius: 8px; background: #f1f5f9; display: flex; align-items: center; justify-content: center; overflow: hidden; flex-shrink: 0; }
        .afdp-ex-thumb img { width: 100%; height: 100%; object-fit: cover; }
        .afdp-ex-info { display: flex; flex-direction: column; }
        .afdp-ex-name { font-size: 13px; font-weight: 700; color: var(--fd-text); }
        .afdp-ex-price { font-size: 12px; font-weight: 600; color: var(--fd-primary); }

        .btn-fd-main { display: inline-flex; align-items: center; padding: 10px 20px; border-radius: 8px; background: var(--fd-primary); color: #fff; text-decoration: none; font-weight: 600; font-size: 14px; transition: 0.2s; }
        .btn-fd-main:hover { background: var(--fd-primary-hover); }
    </style>

    <div class="wrap afdp-view-wrapper">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;">
            <h1 style="font-weight: 800; font-size: 26px; margin:0;"><?php echo esc_html($item->post_title); ?></h1>
            <div style="display: flex; gap: 10px;">
                <a href="?page=awesome_food_delivery&tab=items&sub=all" class="btn-fd-main" style="background:#64748b;">Back to Menu</a>
                <a href="?page=awesome_food_delivery&tab=items&sub=add&edit=<?php echo esc_attr($item_id); ?>" class="btn-fd-main">Modify Item</a>
            </div>
        </div>

        <div class="afdp-card">
            <div class="afdp-card-body">
                
                <div class="afdp-sidebar">
                    <div class="afdp-img-frame">
                        <?php if(has_post_thumbnail($item_id)): echo get_the_post_thumbnail($item_id, 'medium'); ?>
                        <?php else: ?><div style="height:100%; display:flex; align-items:center; justify-content:center; color:#cbd5e1;"><span class="dashicons dashicons-format-image" style="font-size:60px; width:60px; height:60px;"></span></div>
                        <?php endif; ?>
                    </div>
                    <span class="afdp-price-display">
    <?php echo esc_html( $currency ); ?>
    <?php echo esc_html( number_format( floatval( $price ), 2 ) ); ?>
</span>
                    <span class="afdp-section-title" style="text-align:center; margin-top:-5px;">Base Price</span>
                </div>

                <div class="afdp-main">
                    <span class="afdp-section-title">Description</span>
                    <div style="font-size: 15px; color: #475569; line-height: 1.7; margin-bottom: 30px;">
                        <?php echo $item->post_content ? wp_kses_post(wpautop($item->post_content)) : '<i>No description set for this item.</i>'; ?>
                    </div>

                    <span class="afdp-section-title">Category</span>
                    <div>
                        <?php if($cats): foreach($cats as $c): ?>
                            <span class="afdp-tag-cat"><?php echo esc_html($c->name); ?></span>
                        <?php endforeach; else: echo '<p style="color:#94a3b8">Uncategorized</p>'; endif; ?>
                    </div>

                    <span class="afdp-section-title" style="margin-top: 30px;">Customization Extras</span>
                    <div class="afdp-extras-grid">
                        <?php 
                        if(!empty($extra_ids) && is_array($extra_ids)): 
                            foreach($extra_ids as $id): 
                                if(isset($extras_all[$id])): 
                                    $ex = $extras_all[$id];
                                    $ex_img = !empty($ex['file_id']) ? wp_get_attachment_thumb_url($ex['file_id']) : '';
                                    ?>
                                    <div class="afdp-extra-card">
                                        <div class="afdp-ex-thumb">
                                            <?php if($ex_img): ?>
                                                <img src="<?php echo esc_url($ex_img); ?>">
                                            <?php else: ?>
                                                <span class="dashicons dashicons-plus" style="font-size:16px;"></span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="afdp-ex-info">
                                            <span class="afdp-ex-name"><?php echo esc_html($ex['name']); ?></span>
                                            <span class="afdp-ex-price">
    +<?php echo esc_html( $currency ); ?><?php echo esc_html( number_format( floatval( $ex['price'] ), 2 ) ); ?>
</span>
                                        </div>
                                    </div>
                                <?php endif; 
                            endforeach; 
                        else: ?>
                            <div style="grid-column: 1/-1; padding: 25px; background: #f8f9fa; border: 1px dashed var(--fd-border); border-radius: 12px; text-align: center; color: var(--fd-muted);">
                                No extras linked to this item.
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <?php
}