<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
global $wpdb;
$s        = ROS_DB::get_all_settings();
$currency = $s['currency_symbol'] ?? '$';

// Get all sessions grouped
$sessions = ROS_DB::get_sessions( 200 );
?>
<div class="ros-admin-wrap">
  <div class="ros-admin-header">
    <h1>📋 Orders per Klant</h1>
    <small style="color:#888;font-weight:400">Alle orders gegroepeerd per tafelbezoek</small>
  </div>

  <?php if ( empty( $sessions ) ): ?>
    <div class="ros-form-card"><p style="color:#888;text-align:center;padding:30px">Nog geen orders.</p></div>
  <?php endif; ?>

  <?php foreach ( $sessions as $session ): ?>
    <?php
    $order_ids    = array_map('intval', explode(',', $session['order_ids']));
    $all_orders   = ROS_DB::get_session_orders( $session['table_session'] );
    $status_class = 'ros-status-' . str_replace('_','-', $session['last_status']);
    $status_label = ucfirst( str_replace('_',' ', $session['last_status']) );
    $started      = date( 'd-m-Y H:i', strtotime( $session['started_at'] ) );
    $updated      = date( 'd-m-Y H:i', strtotime( $session['last_update'] ) );
    $pay          = $session['payment_method'] ? ucfirst( $session['payment_method'] ) : '—';
    ?>
    <div class="ros-session-card">
      <div class="ros-session-header">
        <div class="ros-session-left">
          <div class="ros-session-table">🪑 <?php echo esc_html( $session['table_name'] ?? 'Tafel' ); ?></div>
          <div class="ros-session-meta">
            <?php echo esc_html( $started ); ?> &nbsp;·&nbsp;
            <?php echo esc_html( $session['order_count'] ); ?> order<?php echo $session['order_count'] > 1 ? 's' : ''; ?> &nbsp;·&nbsp;
            Betaling: <?php echo esc_html( $pay ); ?>
          </div>
        </div>
        <div class="ros-session-right">
          <span class="ros-status-pill <?php echo esc_attr( $status_class ); ?>"><?php echo esc_html( $status_label ); ?></span>
          <div class="ros-session-total"><?php echo esc_html( $currency . number_format( $session['grand_total'], 2 ) ); ?></div>
        </div>
      </div>

      <?php foreach ( $all_orders as $order ): ?>
        <?php $items = ROS_DB::get_order_items( $order['id'] ); ?>
        <div class="ros-order-sub">
          <div class="ros-order-sub-header">
            <span class="ros-order-sub-id">Order #<?php echo esc_html( $order['id'] ); ?></span>
            <span class="ros-status-pill ros-status-<?php echo esc_attr( str_replace('_','-',$order['status']) ); ?>" style="font-size:0.72rem">
              <?php echo esc_html( ucfirst( str_replace('_',' ',$order['status']) ) ); ?>
            </span>
            <span class="ros-order-sub-time"><?php echo esc_html( date('H:i', strtotime($order['created_at'])) ); ?></span>
            <span class="ros-order-sub-total"><?php echo esc_html( $currency . number_format($order['total'],2) ); ?></span>
          </div>
          <ul class="ros-order-items-list">
            <?php foreach ( $items as $item ): ?>
              <li>
                <span class="ros-item-qty"><?php echo esc_html( $item['quantity'] ); ?>×</span>
                <?php echo esc_html( $item['dish_name'] ); ?>
                <span class="ros-item-price"><?php echo esc_html( $currency . number_format($item['subtotal'],2) ); ?></span>
              </li>
            <?php endforeach; ?>
          </ul>
          <?php if ( $order['notes'] ): ?>
            <div class="ros-order-notes">📝 <?php echo esc_html( $order['notes'] ); ?></div>
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endforeach; ?>
</div>

<style>
.ros-session-card { background:#fff; border-radius:12px; margin-bottom:16px; box-shadow:0 2px 12px rgba(0,0,0,.06); overflow:hidden; }
.ros-session-header { display:flex; justify-content:space-between; align-items:center; padding:14px 18px; background:#1A1A1A; gap:16px; flex-wrap:wrap; }
.ros-session-table { font-size:1rem; font-weight:700; color:#F0D9A8; margin-bottom:3px; }
.ros-session-meta  { font-size:0.78rem; color:rgba(255,255,255,.45); }
.ros-session-right { display:flex; flex-direction:column; align-items:flex-end; gap:4px; flex-shrink:0; }
.ros-session-total { font-size:1.15rem; font-weight:800; color:#C9A96E; }
.ros-order-sub { padding:10px 18px; border-bottom:1px solid #f0f0f0; }
.ros-order-sub:last-child { border-bottom:none; }
.ros-order-sub-header { display:flex; align-items:center; gap:10px; margin-bottom:6px; flex-wrap:wrap; }
.ros-order-sub-id { font-weight:700; font-size:0.82rem; color:#333; }
.ros-order-sub-time { font-size:0.75rem; color:#999; margin-left:auto; }
.ros-order-sub-total { font-weight:700; font-size:0.88rem; color:#A6813C; }
.ros-order-items-list { list-style:none; padding:0; margin:0; }
.ros-order-items-list li { display:flex; align-items:center; gap:6px; font-size:0.82rem; color:#444; padding:2px 0; }
.ros-item-qty { font-weight:700; color:#888; min-width:20px; }
.ros-item-price { margin-left:auto; font-weight:600; color:#666; }
.ros-order-notes { font-size:0.78rem; color:#888; font-style:italic; margin-top:5px; }
.ros-status-customer-closed { background:#fef3cd; color:#856404; }
</style>
