<?php
/**
 * Orders
 *
 * Shows orders on the account page.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.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://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.7.0
 */

defined('ABSPATH') || exit;

do_action('woocommerce_before_account_orders', $has_orders);

if ($has_orders){
  $active_orders = [];
  $history_orders = [];
  foreach ($customer_orders->orders as $key => $active_customer_order){
    $order = wc_get_order($active_customer_order);
    $status = $order->get_status();
    $item_count = $order->get_item_count() - $order->get_item_count_refunded();
    if ($status == 'pending' || $status == 'processing' || $status == 'on-hold' || $status == 'failed'){
      $active_orders[] = $order;
    }else {
      $history_orders[] = $order;
    }
  }
  if(count($active_orders)){
  echo '<div class="active-orders">';
  echo '<h2>'. __('Aktívne objednávky','inoby') .'</h2>';
  foreach($active_orders as $order) {
      wc_get_template('myaccount/parts/order-item.php',["order" => $order, "item_count" => $item_count]);
    }
    echo '</div>';
  }
  if(count($history_orders)){
    echo '<div class="history-orders">';
    echo '<h2>'. __('História objednávok','inoby') .'</h2>';
    foreach($history_orders as $order) {
      wc_get_template('myaccount/parts/order-item.php',["order" => $order, "item_count" => $item_count]);
    }
    echo '</div>';
  }
}
else{ ?>
<div class="woocommerce-message woocommerce-info">
  <a class="button"
    href="<?php echo esc_url(apply_filters('woocommerce_return_to_shop_redirect', wc_get_page_permalink('shop'))); ?>"><?= __('Prejsť do obchodu', 'inoby'); ?></a>
  <?= __('Zatiaľ nemáte žiadne objednávky.', 'inoby') ?>
</div>
<?php
}
?>

<?php do_action('woocommerce_after_account_orders', $has_orders); ?>