<?php
defined('ABSPATH') || exit;

if (function_exists('wc_load_cart')) {
  wc_load_cart();
}

if (!function_exists('WC') || !WC()->cart) {
  echo '<section class="fjord-cart-page"><div class="fjord-cart-empty"><p>Cart is unavailable.</p></div></section>';
  return;
}

do_action('woocommerce_before_cart');
?>

<section class="fjord-cart-page">
  <section class="fjord-shop__intro">
    <h1 class="fjord-shop__title"><?php esc_html_e('Carrito', 'fjord-clone'); ?></h1>
    <p class="fjord-shop__lead"><?php esc_html_e('Revisa tus productos antes de finalizar la compra.', 'fjord-clone'); ?></p>
  </section>

  <?php if (WC()->cart->is_empty()) : ?>
    <div class="fjord-cart-empty">
      <p><?php esc_html_e('Your cart is currently empty.', 'woocommerce'); ?></p>
      <a class="fjord-cart-empty__button" href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>">
        <?php esc_html_e('Return to shop', 'woocommerce'); ?>
      </a>
    </div>
  <?php else : ?>
    <div class="fjord-cart-grid">
      <form class="fjord-cart-panel" action="<?php echo esc_url(wc_get_cart_url()); ?>" method="post">
        <div class="fjord-cart-list">
          <?php
          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) {
              continue;
            }

            $product_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $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('woocommerce_thumbnail'), $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);
            ?>

            <article class="fjord-cart-item">
              <div class="fjord-cart-item__media">
                <?php if ($product_permalink) : ?>
                  <a href="<?php echo esc_url($product_permalink); ?>">
                    <?php echo $thumbnail; ?>
                  </a>
                <?php else : ?>
                  <?php echo $thumbnail; ?>
                <?php endif; ?>
              </div>
              <div class="fjord-cart-item__details">
                <div class="fjord-cart-item__title">
                  <?php if ($product_permalink) : ?>
                    <a href="<?php echo esc_url($product_permalink); ?>"><?php echo esc_html($product_name); ?></a>
                  <?php else : ?>
                    <?php echo esc_html($product_name); ?>
                  <?php endif; ?>
                </div>
                <?php echo wc_get_formatted_cart_item_data($cart_item); ?>
                <div class="fjord-cart-item__qty">
                  <?php
                  if ($_product->is_sold_individually()) {
                    echo sprintf(
                      '<span class="fjord-cart-item__qty-value">1</span><input type="hidden" name="cart[%s][qty]" value="1" />',
                      esc_attr($cart_item_key)
                    );
                  } else {
                    $quantity_input = woocommerce_quantity_input([
                      'input_name' => "cart[{$cart_item_key}][qty]",
                      'input_value' => $cart_item['quantity'],
                      'max_value' => $_product->get_max_purchase_quantity(),
                      'min_value' => '0',
                    ], $_product, false);
                    echo '<div class="fjord-cart-item__qty-control" data-cart-qty-control>';
                    echo '<button type="button" class="fjord-cart-item__qty-btn" data-cart-qty-change="-1" aria-label="' . esc_attr__('Decrease quantity', 'fjord-clone') . '">−</button>';
                    echo $quantity_input;
                    echo '<button type="button" class="fjord-cart-item__qty-btn" data-cart-qty-change="1" aria-label="' . esc_attr__('Increase quantity', 'fjord-clone') . '">+</button>';
                    echo '</div>';
                  }
                  ?>
                </div>
              </div>
              <div class="fjord-cart-item__aside">
                <div class="fjord-cart-item__subtotal"><?php echo wp_kses_post($product_subtotal); ?></div>
                <?php
                echo apply_filters(
                  'woocommerce_cart_item_remove_link',
                  sprintf(
                    '<a href="%s" class="fjord-cart-item__remove" aria-label="%s" data-product_id="%s" data-cart_item_key="%s">&times;</a>',
                    esc_url(wc_get_cart_remove_url($cart_item_key)),
                    esc_attr__('Remove this item', 'woocommerce'),
                    esc_attr($product_id),
                    esc_attr($cart_item_key)
                  ),
                  $cart_item_key
                );
                ?>
              </div>
            </article>
          <?php } ?>
        </div>

        <div class="fjord-cart-actions">
          <?php if (wc_coupons_enabled()) : ?>
            <div class="fjord-cart-coupon">
              <input type="text" name="coupon_code" class="fjord-cart-coupon__input" placeholder="<?php esc_attr_e('Coupon code', 'woocommerce'); ?>">
              <button type="submit" class="fjord-cart-coupon__button" name="apply_coupon" value="<?php esc_attr_e('Apply coupon', 'woocommerce'); ?>">
                <?php esc_html_e('Apply', 'woocommerce'); ?>
              </button>
            </div>
          <?php endif; ?>
          <button type="submit" class="fjord-cart-update" name="update_cart" value="<?php esc_attr_e('Update cart', 'woocommerce'); ?>">
            <?php esc_html_e('Update cart', 'woocommerce'); ?>
          </button>
        </div>

        <?php wp_nonce_field('woocommerce-cart', 'woocommerce-cart-nonce'); ?>
      </form>

      <aside class="fjord-cart-summary">
        <?php
        if (function_exists('woocommerce_cart_totals')) {
          woocommerce_cart_totals();
        } elseif (function_exists('wc_get_template')) {
          wc_get_template('cart/cart-totals.php');
        }
        ?>
      </aside>
    </div>
  <?php endif; ?>
</section>

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