<?php
/**
 * Starter 2026 — Shopping Cart Page (Premium)
 * Clean table layout with quantity controls
 * CodeIgniter 3.1.9
 */
?>

<div class="container">
    <nav class="s26-breadcrumb" aria-label="Breadcrumb">
        <a href="<?= $home_link ?? $base_url ?>"><?= __('store.home') ?? 'Home' ?></a>
        <span class="separator"><i class="fas fa-chevron-right"></i></span>
        <span class="current"><?= __('store.shopping_cart') ?? 'Shopping Cart' ?></span>
    </nav>
</div>

<section class="s26-cart-page">
    <div class="container">

        <?php if($products) { ?>

        <div class="s26-cart-header-row">
            <h1 class="s26-page-title"><?= __('store.shopping_cart') ?? 'Shopping Cart' ?></h1>
        </div>

        <form method="POST" id="cart-form">
            <div class="s26-cart-content">
                <div class="s26-cart-table-wrap cart-table">
                    <?php include_once "cart_products_table.php"; ?>
                </div>

                <div class="s26-cart-actions">
                    <a href="<?= $base_url ?>" class="s26-btn-outline">
                        <i class="fas fa-arrow-left"></i>
                        <?= __('store.continue_shopping') ?? 'Continue Shopping' ?>
                    </a>
                    <a href="<?= $base_url ?>checkout" class="s26-btn-primary">
                        <?= __('store.checkout') ?? 'Checkout' ?>
                        <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
            </div>
        </form>

        <?php } else { ?>

        <!-- Empty Cart State -->
        <div class="s26-empty-state">
            <div class="s26-empty-state__icon">
                <i class="fas fa-shopping-bag"></i>
            </div>
            <h2 class="s26-empty-state__title"><?= __('store.shopping_cart_is_empty') ?? 'Your cart is empty' ?></h2>
            <p class="s26-empty-state__text"><?= __('store.empty_cart_message') ?? 'Looks like you haven\'t added any products to your cart yet.' ?></p>
            <a href="<?= $base_url ?>" class="s26-btn-primary">
                <i class="fas fa-shopping-bag"></i>
                <?= __('store.continue_shopping') ?? 'Start Shopping' ?>
            </a>
        </div>

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

<script type="text/javascript">
var xhr;
$("#cart-form").delegate(".qty-input","change",function(){
    if(xhr && xhr.readyState != 4) xhr.abort();
    $this = $(this);
    xhr = $.ajax({
        url:'',
        type:'POST',
        dataType:'json',
        data:$("#cart-form").serialize(),
        beforeSend:function(){},
        complete:function(){},
        success:function(json){
            $('.cart-table').html(json.html);
            updateCart();
            if(json.success == false){
                $(".print-message").html('<div class="alert alert-danger alert-dismissible fade show" role="alert">' +
                    '<i class="bi bi-exclamation-triangle-fill me-2"></i>&nbsp;' + json['message'] +
                    '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
                removeAlertsAfterTimeout();
            }
        }
    })
    return false;
});

$(document).delegate(".s26-qty-btn","click",function(){
    var val = parseInt($(this).parent().find("input").val());
    if($(this).hasClass("add")) { val++ }
    else { val-- }
    if(val <= 0) val = 1;
    $(this).parent().find("input").val(val).trigger("change");
});

// Also support old class
$(document).delegate(".cart-counter button","click",function(){
    var val = parseInt($(this).parent().find("input").val());
    if($(this).hasClass("add")) { val++ }
    else { val-- }
    if(val <= 0) val = 1;
    $(this).parent().find("input").val(val).trigger("change");
});
</script>
