<?php
/**
 * Starter 2026 — Checkout Cart Summary (Premium)
 * Compact cart items display within checkout flow
 * CodeIgniter 3.1.9
 */
$currency = $store_setting['currency_sign'] ?? '$';
?>

<form id="checkout-cart-form">
    <?php if (!empty($products)): ?>
    <div class="s26-checkout-cart">
        <?php
        $subtotal = 0;
        foreach ($products as $key => $product):
            $item_total = ($product['product_price'] + $product['variation_price']) * $product['quantity'];
            $subtotal += $item_total;

            $combinationString = "";
            if(isset($product['variation']) && !empty($product['variation'])) {
                $variation = json_decode($product['variation']);
                foreach ($variation as $vkey => $value) {
                    if($vkey == 'colors') {
                        $combinationString .= ($combinationString == "") ? explode("-", $value)[1] : ", " . explode("-", $value)[1];
                    } else {
                        $combinationString .= ($combinationString == "") ? $value : ", " . $value;
                    }
                }
            }
        ?>
        <div class="s26-checkout-cart__item">
            <div class="s26-checkout-cart__img">
                <img src="<?= $product['product_featured_image'] ?>"
                     alt="<?= htmlspecialchars($product['product_name']) ?>"
                     onerror="this.src='<?= base_url('assets/store/default/img/pr-img.png') ?>'">
                <span class="s26-checkout-cart__qty-badge"><?= $product['quantity'] ?></span>
            </div>
            <div class="s26-checkout-cart__info">
                <a href="<?= $product['link'] ?>" class="s26-checkout-cart__name">
                    <?= htmlspecialchars($product['product_name']) ?>
                </a>
                <?php if ($combinationString != ""): ?>
                <small class="s26-checkout-cart__variant"><?= $combinationString ?></small>
                <?php endif; ?>
                <div class="s26-checkout-cart__meta">
                    <div class="s26-qty-control cart-counter">
                        <button type="button" class="s26-qty-btn"><i class="fas fa-minus"></i></button>
                        <input type="text" name="quantity[<?= $product['cart_id'] ?>]" value="<?= $product['quantity'] ?>" class="qty-input">
                        <button type="button" class="s26-qty-btn add plus"><i class="fas fa-plus"></i></button>
                    </div>
                    <span class="s26-checkout-cart__price"><?= c_format($product['product_price'] + $product['variation_price']) ?></span>
                </div>
            </div>
            <div class="s26-checkout-cart__actions">
                <span class="s26-checkout-cart__total"><?= c_format($item_total) ?></span>
                <button type="button" class="btn btn-xs btn-remove-cart s26-checkout-cart__remove"
                        data-href="<?= $base_url . "cart/?checkout_page=true&remove=" . $product['cart_id'] ?>">
                    <i class="fas fa-trash-alt"></i>
                </button>
            </div>
        </div>
        <?php endforeach; ?>

        <!-- Totals -->
        <?php if (!empty($totals)): ?>
        <div class="s26-checkout-cart__totals">
            <?php foreach ($totals as $key => $value): 
                // The controller provides 'title' and 'amount' keys
                $label  = isset($value['title']) ? $value['title'] : (isset($value['text']) ? $value['text'] : '');
                $amount = isset($value['amount']) ? $value['amount'] : (isset($value['value']) ? $value['value'] : 0);
                
                // Fallback labels if title is empty
                if (empty($label)) {
                    $fallback_labels = [
                        'subtotal' => __('store.subtotal') ?? 'Subtotal',
                        'shipping' => __('store.shipping') ?? 'Shipping',
                        'tax'      => __('store.tax') ?? 'Tax',
                        'total'    => __('store.total') ?? 'Total',
                        'discount' => __('store.discount') ?? 'Discount',
                        'coupon'   => __('store.coupon') ?? 'Coupon',
                    ];
                    $label = isset($fallback_labels[$key]) ? $fallback_labels[$key] : ucfirst(str_replace('_', ' ', $key));
                }
                $is_grand = (strtolower(trim($label)) == 'total' || strtolower(trim($label)) == strtolower(__('store.total')));
            ?>
            <div class="s26-checkout-cart__total-row <?= $is_grand ? 's26-checkout-cart__total-row--grand' : '' ?>">
                <span><?= $label ?></span>
                <span class="fw-bold"><?= c_format($amount) ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
    <?php else: ?>
    <div class="s26-empty-state" style="padding:40px 20px">
        <div class="s26-empty-state__icon" style="width:64px;height:64px;margin-bottom:16px">
            <i class="fas fa-shopping-bag" style="font-size:24px"></i>
        </div>
        <p class="text-muted"><?= __('store.cart_is_blank') ?? 'Your cart is empty' ?></p>
        <a href="<?= $base_url ?>category" class="s26-btn-outline s26-btn--sm">
            <?= __('store.continue_shopping') ?? 'Continue Shopping' ?>
        </a>
    </div>
    <?php endif; ?>
</form>
