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

do_action('woocommerce_before_mini_cart');

if (!WC()->cart->is_empty()) : ?>
  <ul class="woocommerce-mini-cart cart_list product_list_widget">
    <?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);
      if (!$_product || !$_product->exists() || $cart_item['quantity'] <= 0) {
        continue;
      }
      $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', ['class' => 'fjord-cart-drawer__thumb-img']),
        $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_permalink = apply_filters('woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink($cart_item) : '', $cart_item, $cart_item_key);
      $line_total = WC()->cart->get_product_subtotal($_product, $cart_item['quantity']);
      $regular_price = $_product->get_regular_price();
      $sale_price = $_product->get_sale_price();
      $regular_total = null;
      if ($sale_price && $regular_price && $regular_price > $sale_price) {
        $regular_total = wc_price(wc_get_price_to_display($_product, ['price' => $regular_price]) * $cart_item['quantity']);
      }
      ?>
      <li class="woocommerce-mini-cart-item <?php echo esc_attr(apply_filters('woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key)); ?>">
        <div class="fjord-cart-drawer__item">
          <?php if ($product_permalink) : ?>
            <a class="fjord-cart-drawer__thumb" href="<?php echo esc_url($product_permalink); ?>">
              <?php echo $thumbnail; ?>
            </a>
          <?php else : ?>
            <div class="fjord-cart-drawer__thumb">
              <?php echo $thumbnail; ?>
            </div>
          <?php endif; ?>

          <div class="fjord-cart-drawer__details">
            <?php if ($product_permalink) : ?>
              <a class="fjord-cart-drawer__name" href="<?php echo esc_url($product_permalink); ?>">
                <?php echo esc_html($product_name); ?>
              </a>
            <?php else : ?>
              <span class="fjord-cart-drawer__name"><?php echo esc_html($product_name); ?></span>
            <?php endif; ?>

            <?php echo wc_get_formatted_cart_item_data($cart_item); ?>

            <div class="fjord-cart-drawer__qty" data-cart-qty data-cart-key="<?php echo esc_attr($cart_item_key); ?>">
              <button type="button" class="fjord-cart-drawer__qty-btn" data-cart-qty-change="-1" aria-label="<?php esc_attr_e('Decrease quantity', 'fjord-clone'); ?>">−</button>
              <span class="fjord-cart-drawer__qty-value" data-cart-qty-value><?php echo esc_html($cart_item['quantity']); ?></span>
              <button type="button" class="fjord-cart-drawer__qty-btn" data-cart-qty-change="1" aria-label="<?php esc_attr_e('Increase quantity', 'fjord-clone'); ?>">+</button>
            </div>
          </div>

          <div class="fjord-cart-drawer__aside">
            <div class="fjord-cart-drawer__price">
              <span class="fjord-cart-drawer__price-sale"><?php echo wp_kses_post($line_total); ?></span>
              <?php if ($regular_total) : ?>
                <span class="fjord-cart-drawer__price-regular"><?php echo wp_kses_post($regular_total); ?></span>
              <?php endif; ?>
            </div>
            <?php
            echo apply_filters(
              '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">&times;</a>',
                esc_url(wc_get_cart_remove_url($cart_item_key)),
                esc_attr__('Remove this item', 'woocommerce'),
                esc_attr($_product->get_id()),
                esc_attr($cart_item_key)
              ),
              $cart_item_key
            );
            ?>
          </div>
        </div>
      </li>
      <?php
    }
    ?>
  </ul>

  <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'); ?>
<?php else : ?>
  <p class="woocommerce-mini-cart__empty-message"><?php esc_html_e('No products in the cart.', 'woocommerce'); ?></p>
<?php endif; ?>

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