<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
global $wpdb;
$s          = ROS_DB::get_all_settings();
$currency   = $s['currency_symbol'] ?? '$';
$date_from  = sanitize_text_field( $_GET['date_from'] ?? date( 'Y-m-01' ) );
$date_to    = sanitize_text_field( $_GET['date_to']   ?? date( 'Y-m-d' ) );
$orders     = ROS_DB::get_orders_report( $date_from, $date_to );
$total_rev  = array_sum( array_column( $orders, 'total' ) );
$by_table   = [];
foreach ( $orders as $o ) {
    $tn = $o['table_name'] ?? 'Table';
    if ( ! isset( $by_table[ $tn ] ) ) $by_table[ $tn ] = [ 'count' => 0, 'total' => 0 ];
    $by_table[ $tn ]['count']++;
    $by_table[ $tn ]['total'] += $o['total'];
}
arsort( $by_table );
?>
<div class="ros-admin-wrap">
  <div class="ros-admin-header">
    <h1>📊 Sales Report</h1>
  </div>

  <form method="get" class="ros-form-inline">
    <input type="hidden" name="page" value="ros-report">
    <div class="ros-field-inline">
      <label>From</label>
      <input type="date" name="date_from" value="<?php echo esc_attr( $date_from ); ?>">
    </div>
    <div class="ros-field-inline">
      <label>To</label>
      <input type="date" name="date_to" value="<?php echo esc_attr( $date_to ); ?>">
    </div>
    <button type="submit" class="ros-btn ros-btn-primary">Filter</button>
  </form>

  <div class="ros-stat-grid">
    <div class="ros-stat-card ros-stat-blue">
      <div class="ros-stat-icon">📋</div>
      <div class="ros-stat-number"><?php echo count( $orders ); ?></div>
      <div class="ros-stat-label">Closed Orders</div>
    </div>
    <div class="ros-stat-card ros-stat-gold">
      <div class="ros-stat-icon">💰</div>
      <div class="ros-stat-number"><?php echo $currency . number_format( $total_rev, 2 ); ?></div>
      <div class="ros-stat-label">Total Revenue</div>
    </div>
    <div class="ros-stat-card ros-stat-green">
      <div class="ros-stat-icon">📈</div>
      <div class="ros-stat-number"><?php echo count( $orders ) ? $currency . number_format( $total_rev / count( $orders ), 2 ) : '-'; ?></div>
      <div class="ros-stat-label">Avg Order Value</div>
    </div>
  </div>

  <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:30px">
    <div class="ros-form-card">
      <h2>Revenue by Table</h2>
      <table class="widefat">
        <thead><tr><th>Table</th><th>Orders</th><th>Revenue</th></tr></thead>
        <tbody>
        <?php foreach ( $by_table as $table_name => $data ): ?>
          <tr>
            <td><?php echo esc_html( $table_name ); ?></td>
            <td><?php echo esc_html( $data['count'] ); ?></td>
            <td><?php echo esc_html( $currency . number_format( $data['total'], 2 ) ); ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <div class="ros-form-card">
      <h2>Top Dishes</h2>
      <?php
      $date_from_esc = esc_sql( $date_from );
      $date_to_esc   = esc_sql( $date_to );
      $top_dishes = $wpdb->get_results(
          "SELECT oi.dish_name, SUM(oi.quantity) as total_qty, SUM(oi.subtotal) as total_rev
           FROM {$wpdb->prefix}ros_order_items oi
           JOIN {$wpdb->prefix}ros_orders o ON o.id = oi.order_id
           WHERE o.status = 'closed'
             AND DATE(o.created_at) >= '$date_from_esc'
             AND DATE(o.created_at) <= '$date_to_esc'
           GROUP BY oi.dish_name ORDER BY total_qty DESC LIMIT 10",
          ARRAY_A
      );
      ?>
      <table class="widefat">
        <thead><tr><th>Dish</th><th>Qty</th><th>Revenue</th></tr></thead>
        <tbody>
        <?php foreach ( $top_dishes as $d ): ?>
          <tr>
            <td><?php echo esc_html( $d['dish_name'] ); ?></td>
            <td><?php echo esc_html( $d['total_qty'] ); ?></td>
            <td><?php echo esc_html( $currency . number_format( $d['total_rev'], 2 ) ); ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>

  <div class="ros-form-card">
    <h2>All Closed Orders</h2>
    <table class="widefat ros-table">
      <thead><tr><th>#</th><th>Table</th><th>Total</th><th>Date</th></tr></thead>
      <tbody>
      <?php foreach ( $orders as $o ): ?>
        <tr>
          <td>#<?php echo esc_html( $o['id'] ); ?></td>
          <td><?php echo esc_html( $o['table_name'] ); ?></td>
          <td><?php echo esc_html( $currency . number_format( $o['total'], 2 ) ); ?></td>
          <td><?php echo esc_html( date( 'Y-m-d H:i', strtotime( $o['created_at'] ) ) ); ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
