<?php if (!defined('APS_ADS_VER')) exit('restricted access'); ?> 
<?php
/**
 * Dynamic 728x90 Ad Unit for APS Products
 * Insert this file where you want the banner to appear.
 * Ensure $pid (Product ID) is available in the scope.
 */

if (!defined('APS_VER')) exit('restricted access');

// --- 1. CONFIGURATION & DATA RETRIEVAL ---

// =================================================================
// 🔴 STEP 1: PASTE YOUR AFFILIATE LINK HERE
// This single link will update the Image, Title, and Button automatically.
// =================================================================
$affiliate_url = 'https://amzn.to/3Mflaej'; 
// =================================================================


// Fallback: If $pid is not set, try to get it from the global post object
if (!isset($pid) && isset($GLOBALS['post'])) {
    $pid = $GLOBALS['post']->ID;
}

// Get settings and sizes
global $images_settings; 
$thumb_width = isset($images_settings['product-thumb']['width']) ? $images_settings['product-thumb']['width'] : 150;
$thumb_height = isset($images_settings['product-thumb']['height']) ? $images_settings['product-thumb']['height'] : 150;
$thumb_crop = isset($images_settings['product-thumb']['crop']) ? $images_settings['product-thumb']['crop'] : true;

// Get Image
$thumb = get_product_image($thumb_width, $thumb_height, $thumb_crop, $pid);

// Get General Data (Price, SKU, Links)
$general = get_aps_product_general_data($pid);
$item_on_sale = aps_product_on_sale($general);
$currency = aps_get_base_currency(); 

// Logic: Check if price is available to determine Button Text
if (isset($general['price']) && $general['price'] > 0) {
    $btn_text = "Buy Now";
} else {
    // If price is 0, empty, or 'Coming Soon'
    $btn_text = "Pre-order";
}

// Calculate Discount % for display
$discount_percent = 0;
if ($item_on_sale && isset($general['price']) && $general['price'] > 0) {
    $calc_discount = aps_calc_discount($general['price'], $general['sale-price']);
    $discount_percent = $calc_discount['percent'];
}
?>

<!-- --- 2. CSS STYLING --- -->
<style>
    /* Reset & Fonts */
    .aps-ad-728-wrapper, .aps-ad-728-wrapper * {
        box-sizing: border-box;
    }

    /* Main Container 728x90 */
    .aps-ad-728-wrapper {
        width: 100%;
        height: 90px;
        background: #fff;
        border: 1px solid #d5d9d9;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        /* Removed "Amazon Ember" to inherit theme font */
        overflow: hidden;
        margin: 10px auto;
        padding: 0; 
        box-shadow: 0 2px 5px rgba(0,0,0,0.02);
        position: relative;
        transition: all 0.2s ease-in-out;
    }
    
    /* Hover Effect */
    .aps-ad-728-wrapper:hover {
        border-color: #a88734; /* Amazon Gold Border */
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        transform: translateY(-1px);
    }

    /* Common Section Styles */
    .aps-ad-section {
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0 16px;
        position: relative;
    }
    
    /* Section 1: Image (Left) */
    .aps-ad-section-image {
        width: 110px; 
        align-items: center;
        justify-content: center; /* Center horizontally */
        background-color: #fbfbfb;
        border-right: 1px solid #f0f0f0;
        padding: 5px; /* Added padding to prevent edge touching */
    }
    .aps-ad-img {
        max-width: 90px;  /* Prevent overflow width */
        max-height: 80px; /* Prevent overflow height */
        width: auto;      /* Maintain aspect ratio */
        height: auto;     /* Maintain aspect ratio */
        object-fit: contain;
        mix-blend-mode: multiply;
        transition: transform 0.3s ease;
    }
    .aps-ad-728-wrapper:hover .aps-ad-img {
        transform: scale(1.05);
    }

    /* Section 2: Title & Trust Signals (Middle) */
    .aps-ad-section-title {
        flex: 1; 
        align-items: flex-start;
        padding-left: 20px;
        justify-content: center;
    }
    .aps-ad-title h3 {
        font-size: 15px;
        line-height: 1.3;
        margin: 0 0 6px 0;
        padding: 0;
        color: #0F1111; /* Amazon Black */
        font-weight: 500;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-align: left;
    }
    
    /* Trust Badges Row */
    .aps-meta-row {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
        font-size: 12px;
        line-height: 1;
        color: #565959;
    }
    .aps-stock-indicator {
        color: #007600; /* Amazon Stock Green */
        font-weight: 600;
        display: flex;
        align-items: center;
    }
    .aps-shipping-text {
        color: #565959;
    }
    .aps-prime-check {
        color: #007185; /* Amazon Link Blue */
        font-weight: 600;
        display: flex;
        align-items: center;
    }
    .aps-prime-check::before {
        content: "✓";
        margin-right: 3px;
        font-weight: bold;
    }

    /* Section 3: Price (Standalone) */
    .aps-ad-section-price {
        min-width: 150px; 
        align-items: flex-end; /* Right align price */
        padding-right: 24px;
        border-left: 1px solid #f5f5f5;
    }
    
    /* Amazon Branding Lockup (Text Only) */
    .aps-amazon-branding {
        display: flex;
        align-items: center;
        margin-bottom: 4px;
        opacity: 0.6;
        transition: opacity 0.2s;
    }
    .aps-ad-728-wrapper:hover .aps-amazon-branding {
        opacity: 1;
    }
    .aps-amazon-text {
        font-weight: bold;
        font-size: 13px;
        color: #565959;
        letter-spacing: -0.2px;
    }
    .aps-ad-728-wrapper:hover .aps-amazon-text {
        color: #FF9900;
    }
    
    .aps-price-group {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        text-align: right;
    }
    
    .aps-ad-price {
        font-size: 22px;
        font-weight: 500;
        color: #B12704; /* Amazon Price Red */
        letter-spacing: -0.5px;
        line-height: 1;
    }
    .aps-ad-price span {
        font-size: 13px;
        position: relative;
        top: -0.5em;
    }

    /* Price Sub-row (Old Price + Badge) */
    .aps-price-sub {
        display: flex;
        align-items: center;
        gap: 6px;
        margin-top: 4px;
    }
    .aps-old-price {
        text-decoration: line-through;
        color: #565959;
        font-size: 12px;
    }
    .aps-save-badge {
        background: #CC0C39; 
        color: #fff;
        font-size: 11px;
        padding: 2px 5px;
        border-radius: 2px;
        font-weight: 500;
        display: inline-block;
        line-height: 1;
    }

    /* Section 4: Button (Right) */
    .aps-ad-section-btn {
        width: 170px; 
        align-items: center;
        justify-content: center; /* Center flex items */
        background-color: #fcfcfc;
        border-left: 1px solid #e7e7e7;
        padding: 0 10px;
    }
    
    /* High-Conversion Button Style */
    .aps-ad-btn {
        display: inline-flex; /* Use inline-flex for perfect centering */
        align-items: center;
        justify-content: center;
        gap: 8px; /* Space between icon and text */
        
        background: #f0c14b;
        background: linear-gradient(to bottom, #f7dfa5, #f0c14b);
        border: 1px solid;
        border-color: #a88734 #9c7e31 #846a29;
        color: #111;
        text-decoration: none;
        padding: 0 16px;
        height: 34px;
        border-radius: 17px; /* Pill shape */
        font-size: 13px;
        font-weight: 500;
        width: 130px;
        box-shadow: 0 1px 0 rgba(255,255,255,0.4) inset;
        cursor: pointer;
        transition: all 0.2s;
        white-space: nowrap;
    }
    .aps-ad-btn:hover {
        background: linear-gradient(to bottom, #f5d78e, #eeb933);
        border-color: #a88734 #9c7e31 #846a29;
        box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    }
    .aps-btn-icon {
        font-size: 18px; /* Slightly larger icon */
        margin: 0; 
        color: #111;
    }
    .aps-btn-main {
        display: block;
        line-height: 1;
    }
    
    /* Responsive Mobile Fallback */
    @media (max-width: 740px) {
        .aps-ad-728-wrapper {
            width: 100%;
            height: auto;
            flex-direction: column;
            padding: 15px;
            text-align: center;
        }
        .aps-ad-section {
            width: 100%;
            border: none;
            padding: 8px 0;
            align-items: center;
        }
        .aps-ad-section-image { background: none; border: none; width: auto; padding: 0; }
        .aps-ad-section-title { padding: 0; }
        .aps-ad-title h3 { text-align: center; }
        .aps-ad-section-price { border: none; padding: 0; align-items: center; min-width: auto; }
        .aps-price-group { align-items: center; }
        .aps-ad-section-btn { background: none; width: 100%; border: none; }
        .aps-meta-row { justify-content: center; }
    }
</style>

<!-- --- 3. HTML OUTPUT --- -->
<div class="aps-ad-728-wrapper">
    <!-- Ensure Font Awesome is loaded for the icon. Remove if your theme already has it. -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" />

    <!-- Section 1: Image -->
    <div class="aps-ad-section aps-ad-section-image">
        <a href="<?php echo esc_url($affiliate_url); ?>" target="_blank" rel="nofollow noopener">
            <img class="aps-ad-img" 
                 src="<?php echo esc_url($thumb['url']); ?>" 
                 alt="<?php echo esc_attr(get_the_title($pid)); ?>" />
        </a>
    </div>

    <!-- Section 2: Title & Trust Signals -->
    <div class="aps-ad-section aps-ad-section-title">
        <div class="aps-ad-title">
            <a href="<?php echo esc_url($affiliate_url); ?>" target="_blank" rel="nofollow noopener" style="text-decoration:none;">
                <h3><?php echo get_the_title($pid); ?></h3>
            </a>
            
            <div class="aps-meta-row">
                <span class="aps-stock-indicator">In Stock</span>
                <span class="aps-shipping-text">&bull; Free Shipping</span>
                <span class="aps-prime-check">Verified</span>
            </div>
        </div>
    </div>
    
    <!-- Section 3: Price -->
    <div class="aps-ad-section aps-ad-section-price">
        <!-- Amazon Branding: Text Only -->
        <div class="aps-amazon-branding">
            <span class="aps-amazon-text">Amazon</span>
        </div>

        <div class="aps-price-group">
            <?php 
            // Check if price exists and is positive
            $has_price = (isset($general['price']) && $general['price'] > 0);
            
            if ($has_price): 
                if ($item_on_sale): ?>
                    <!-- 1. Sale Price (Top) -->
                    <span class="aps-ad-price"><?php echo aps_format_product_price($currency, $general['sale-price']); ?></span>
                    
                    <!-- 2. Old Price + Badge (Bottom) -->
                    <div class="aps-price-sub">
                        <span class="aps-old-price"><?php echo aps_format_product_price($currency, $general['price']); ?></span>
                        <?php if($discount_percent > 0): ?>
                            <span class="aps-save-badge">Save <?php echo esc_html($discount_percent); ?>%</span>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <!-- Regular Price if not on sale -->
                    <span class="aps-ad-price">
                        <?php echo aps_format_product_price($currency, $general['price']); ?>
                    </span>
                <?php endif; 
            else: ?>
                <!-- No Price / Coming Soon -->
                <span class="aps-ad-price" style="font-size: 18px; color: #565959; font-weight: 600;">Coming Soon</span>
            <?php endif; ?>
        </div>
    </div>

    <!-- Section 4: Amazon Button -->
    <div class="aps-ad-section aps-ad-section-btn">
        <a href="<?php echo esc_url($affiliate_url); ?>" class="aps-ad-btn" target="_blank" rel="nofollow noopener">
            <!-- Font Awesome Icon -->
            <i class="fab fa-amazon aps-btn-icon"></i>
            <span class="aps-btn-main"><?php echo esc_html($btn_text); ?></span>
        </a>
    </div>

</div>