<?php

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$no_supplier_label = __( 'No Supplier', 'restaurant-costing' );
$supplier_filter   = isset( $_GET['supplier'] ) ? sanitize_text_field( wp_unslash( $_GET['supplier'] ) ) : '';

$groups = array();
foreach ( $items as $item ) {
	$sup_name = (string) ( $item->supplier_name ?? $item->supplier ?? '' );
	if ( '' === $sup_name ) {
		$sup_name = $no_supplier_label;
	}
	if ( ! isset( $groups[ $sup_name ] ) ) {
		$groups[ $sup_name ] = array();
	}
	$groups[ $sup_name ][] = $item;
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title><?php echo esc_html( (string) ( $inventory->name ?? 'Inventory' ) ); ?></title>
	<style>
		@page { size: letter; margin: 0.75in; }
		body { font-family: 'Times New Roman', Times, serif; font-size: 11pt; line-height: 1.4; color: #000; margin: 0; padding: 0; }
		.print-header { position: fixed; top: 0; left: 0; right: 0; background: #fff; text-align: center; padding: 10px 0 4px; z-index: 100; border-bottom: 1px solid #ccc; }
		.print-header h1 { font-size: 16pt; margin: 0 0 2px; }
		.print-header .date { color: #555; font-size: 10pt; margin: 0; }
		body { padding-top: 70px; }
		@media screen { .print-header { display: none; } body { padding-top: 0; } }
		.no-print { text-align: center; padding: 12px; }
		.no-print .btn { display: inline-block; padding: 6px 16px; font-size: 13px; line-height: 1.4; border-radius: 3px; cursor: pointer; text-decoration: none; margin: 0 4px; border: 1px solid; }
		.no-print .btn-primary { background: #2271b1; color: #fff; border-color: #2271b1; }
		.no-print .btn-primary:hover { background: #135e96; border-color: #135e96; }
		.no-print .btn-secondary { background: #f6f7f7; color: #2271b1; border-color: #2271b1; }
		.no-print .btn-secondary:hover { background: #f0f0f1; }
		@media print { .no-print { display: none; } }
		.supplier-section { width: 100%; border-collapse: collapse; margin-bottom: 20px; page-break-inside: avoid; }
		.supplier-section .sup-title { background: #f0f0f0; font-weight: bold; font-size: 12pt; padding: 6px 8px; border: 1px solid #ccc; text-align: left; }
		.supplier-section .col-header { background: #f8f8f8; font-weight: bold; font-size: 10pt; padding: 4px 6px; border: 1px solid #ccc; text-align: left; }
		.supplier-section .col-header.center { text-align: center; }
		.supplier-section td { padding: 3px 6px; border: 1px solid #ccc; font-size: 10pt; vertical-align: top; }
		.supplier-section .item-name { }
		.supplier-section .item-qty { text-align: center; font-weight: bold; }
		.supplier-section .item-min { text-align: center; color: #666; }
		.col-divider { width: 11px; border-left: none; border-right: none; padding: 0; }
		.col-divider-inner { width: 3px; background: #000; height: 100%; margin: 0 auto; }
		.col-group { width: 50%; }
	</style>
</head>
<body>

<div class="print-header">
	<h1><?php echo esc_html( (string) ( $inventory->name ?? '' ) ); ?></h1>
	<div class="date"><?php echo esc_html( (string) ( $inventory->created_at ?? '' ) ); ?></div>
</div>

<p class="no-print">
	<button type="button" onclick="window.print();" class="btn btn-primary"><?php esc_html_e( 'Print', 'restaurant-costing' ); ?></button>
	<button type="button" onclick="window.close();" class="btn btn-secondary"><?php esc_html_e( 'Close', 'restaurant-costing' ); ?></button>
</p>

<?php foreach ( $groups as $sup_name => $sup_items ) : ?>
	<table class="supplier-section">
		<thead>
			<tr>
				<th class="sup-title" colspan="7"><?php echo esc_html( $sup_name ); ?></th>
			</tr>
			<tr>
				<th class="col-header"><?php esc_html_e( 'Item', 'restaurant-costing' ); ?></th>
				<th class="col-header center"><?php esc_html_e( 'Min', 'restaurant-costing' ); ?></th>
				<th class="col-header center"><?php esc_html_e( 'Order Qty', 'restaurant-costing' ); ?></th>
				<th class="col-divider"></th>
				<th class="col-header"><?php esc_html_e( 'Item', 'restaurant-costing' ); ?></th>
				<th class="col-header center"><?php esc_html_e( 'Min', 'restaurant-costing' ); ?></th>
				<th class="col-header center"><?php esc_html_e( 'Order Qty', 'restaurant-costing' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php
			$count = count( $sup_items );
			for ( $i = 0; $i < $count; $i += 2 ) :
				$left  = $sup_items[ $i ];
				$right = isset( $sup_items[ $i + 1 ] ) ? $sup_items[ $i + 1 ] : null;
			?>
			<tr>
				<td class="item-name"><?php echo esc_html( (string) ( $left->ingredient_name ?? '' ) ); ?></td>
				<td class="item-min"><?php echo (int) ( $left->min_level ?? 0 ); ?></td>
				<td class="item-qty"><?php echo esc_html( rc_fmt_num( $left->order_qty ?? '' ) ); ?></td>
				<td class="col-divider"><div class="col-divider-inner"></div></td>
				<?php if ( $right ) : ?>
				<td class="item-name"><?php echo esc_html( (string) ( $right->ingredient_name ?? '' ) ); ?></td>
				<td class="item-min"><?php echo (int) ( $right->min_level ?? 0 ); ?></td>
				<td class="item-qty"><?php echo esc_html( rc_fmt_num( $right->order_qty ?? '' ) ); ?></td>
				<?php else : ?>
				<td colspan="3"></td>
				<?php endif; ?>
			</tr>
			<?php endfor; ?>
		</tbody>
	</table>
<?php endforeach; ?>
</body>
</html>
