<?php

/**
 * Mini-cart
 *
 * Contains the markup for the mini-cart, used by the cart widget.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 10.0.0
 */

defined('ABSPATH') || exit;

do_action('woocommerce_before_mini_cart'); ?>

<?php if (WC()->cart && ! WC()->cart->is_empty()) : ?>



    <ul class="rt-mini-cart-list">
        <?php
        do_action('woocommerce_before_mini_cart_contents');

        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            $_product   = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key);
            $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);

            if ($_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key)) {
                $product_name      = apply_filters('woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key);
                $thumbnail         = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key);
                $product_price     = apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key);
                $product_subtotal  = apply_filters('woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $cart_item_key);
                $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $cart_item, $cart_item_key);

                if ($_product->is_sold_individually()) {
                    $min_qty = 1;
                    $max_qty = 1;
                } else {
                    $min_qty = 0;
                    $max_qty = $_product->get_max_purchase_quantity();
                }
        ?>
                <li class="rt-mini-cart-item <?php echo esc_attr(apply_filters('woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key)); ?>" data-product-id="<?php echo esc_attr($_product->get_id()); ?>">

                    <div class="rt-mini-cart-item__thumbnail">
                        <?php if (empty($product_permalink)) : ?>
                            <?php echo wp_kses_post($thumbnail); ?>
                        <?php else : ?>
                            <a href="<?php echo esc_url($product_permalink); ?>" tabindex="-1">
                                <?php echo wp_kses_post($thumbnail); ?>
                            </a>
                        <?php endif; ?>
                    </div>

                    <div class="rt-mini-cart-item__content">
                        <div class="rt-mini-cart-item__top">
                            <div class="rt-mini-cart-item__title">
                                <?php if (empty($product_permalink)) : ?>
                                    <?php echo wp_kses_post($product_name); ?>
                                <?php else : ?>
                                    <a href="<?php echo esc_url($product_permalink); ?>"><?php echo wp_kses_post($product_name); ?></a>
                                <?php endif; ?>
                            </div>
                            <div class="rt-mini-cart-item__subtotal hidden md:block js-mini-cart-item-subtotal">
                                <?php echo wp_kses_post($product_subtotal); ?>
                            </div>
                        </div>

                        <?php echo wc_get_formatted_cart_item_data($cart_item); // phpcs:ignore 
                        ?>

                        <div class="rt-mini-cart-item__price">
                            <?php echo wp_kses_post($product_price); ?>
                        </div>

                        <div class="rt-mini-cart-item__footer">
                            <div class="rt-quantity js-mini-cart-update-qty js-quantity-increment"
                                data-cart-item-key="<?php echo esc_attr($cart_item_key); ?>"
                                data-nonce="<?php echo esc_attr(wp_create_nonce('woocommerce-cart')); ?>">
                                <button type="button" class="rt-quantity__button increment-down" aria-label="<?php esc_attr_e('Decrease quantity', 'kubelio'); ?>"
                                    <?php echo ($cart_item['quantity'] <= $min_qty) ? 'disabled' : ''; ?>>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="2" viewBox="0 0 10 2" fill="none" aria-hidden="true">
                                        <path d="M1 1H9" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
                                    </svg>
                                </button>
                                <input type="number"
                                    class="rt-quantity__input input-text"
                                    value="<?php echo esc_attr($cart_item['quantity']); ?>"
                                    min="<?php echo esc_attr($min_qty); ?>"
                                    <?php echo ($max_qty > 0) ? 'max="' . esc_attr($max_qty) . '"' : ''; ?>
                                    step="1"
                                    aria-label="<?php esc_attr_e('Product quantity', 'kubelio'); ?>"
                                    inputmode="numeric">
                                <button type="button" class="rt-quantity__button increment-up" aria-label="<?php esc_attr_e('Increase quantity', 'kubelio'); ?>"
                                    <?php echo ($max_qty > 0 && $cart_item['quantity'] >= $max_qty) ? 'disabled' : ''; ?>>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10" fill="none" aria-hidden="true">
                                        <path d="M5 1V9M1 5H9" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
                                    </svg>
                                </button>
                            </div>

                            <div class="rt-mini-cart-item__remove">
                                <?php
                                echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                    'woocommerce_cart_item_remove_link',
                                    sprintf(
                                        '<a href="%s" class="remove remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="15" viewBox="0 0 14 15" fill="none" aria-hidden="true">
                                                <path d="M0.5 3H13.5M4.5 3V2C4.5 1.448 4.948 1 5.5 1H8.5C9.052 1 9.5 1.448 9.5 2V3M5.5 6.5V11.5M8.5 6.5V11.5M1.5 3L2.3 12.6C2.366 13.4 3.033 14 3.836 14H10.164C10.967 14 11.634 13.4 11.7 12.6L12.5 3H1.5Z" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
                                            </svg>
                                        </a>',
                                        esc_url(wc_get_cart_remove_url($cart_item_key)),
                                        /* translators: %s is the product name */
                                        esc_attr(sprintf(__('Remove %s from cart', 'kubelio'), wp_strip_all_tags($product_name))),
                                        esc_attr($product_id),
                                        esc_attr($cart_item_key),
                                        esc_attr($_product->get_sku())
                                    ),
                                    $cart_item_key
                                );
                                ?>
                            </div>
                        </div>
                    </div>
                </li>
        <?php
            }
        }

        do_action('woocommerce_mini_cart_contents');
        ?>
    </ul>

    <div class="woocommerce-mini-cart__footer">
        <p class="woocommerce-mini-cart__total total">
            <?php do_action('woocommerce_widget_shopping_cart_total'); ?>
        </p>

        <?php do_action('woocommerce_widget_shopping_cart_before_buttons'); ?>

        <p class="woocommerce-mini-cart__buttons buttons"><?php do_action('woocommerce_widget_shopping_cart_buttons'); ?></p>

        <?php do_action('woocommerce_widget_shopping_cart_after_buttons'); ?>
    </div>

<?php else : ?>

    <div class="woocommerce-mini-cart__empty-content">
        <p class="woocommerce-mini-cart__empty-message"><?php esc_html_e('No products in the cart.', 'kubelio'); ?></p>
        <div class="woocommerce-mini-cart__empty-buttons mt-30">
            <a href="<?php echo esc_url(apply_filters('woocommerce_return_to_shop_redirect', wc_get_page_permalink('shop'))); ?>" class="button">
                <?php esc_html_e('Start Shopping', 'kubelio'); ?>
            </a>
        </div>
    </div>

<?php endif; ?>

<?php do_action('woocommerce_after_mini_cart'); ?>