<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Premium Kitchen Ticket - 80mm Thermal Refactor
 */
if ( isset( $_GET['action'], $_GET['order_id'] ) && 'print' === $_GET['action'] ) {

	// Validation, Unslashing, and Sanitization
	$afdp_order_id    = intval( $_GET['order_id'] );
	$afdp_order_title = get_the_title( $afdp_order_id );

	// Data Retrieval (Prefixing all variables)
	$afdp_customer_name    = get_post_meta( $afdp_order_id, 'customer_name', true );
	$afdp_customer_phone   = get_post_meta( $afdp_order_id, 'customer_phone', true );
	$afdp_customer_address = get_post_meta( $afdp_order_id, 'customer_address', true );
	$afdp_notes            = get_post_meta( $afdp_order_id, 'notes', true );
	$afdp_status           = get_post_meta( $afdp_order_id, 'status', true ) ?: 'pending';
	$afdp_items            = get_post_meta( $afdp_order_id, 'order_items', true ) ?: ( get_post_meta( $afdp_order_id, 'items', true ) ?: array() );
	$afdp_total            = get_post_meta( $afdp_order_id, 'total_price', true ) ?: 0;
	$afdp_order_date       = get_the_date( 'd M Y - H:i', $afdp_order_id );

	?>
	<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Kitchen Ticket #<?php echo esc_attr( $afdp_order_id ); ?></title>
		<style>
			@page { margin: 0; }
			body {
				font-family: "Courier New", Courier, monospace;
				width: 76mm; /* Optimized for 80mm rolls with margins */
				margin: 0 auto;
				padding: 5mm;
				color: #000;
				line-height: 1.3;
				background-color: #fff;
			}
			.no-print { text-align: center; margin-bottom: 15px; }
			.print-btn { 
				padding: 10px 20px; background: #e53935; color: #fff; 
				border: none; border-radius: 4px; cursor: pointer; font-weight: bold; 
			}

			.center { text-align: center; }
			.divider { border-top: 2px dashed #000; margin: 10px 0; }
			.double-divider { border-top: 3px solid #000; margin: 10px 0; }

			.header { padding-bottom: 5px; }
			.ticket-type { font-size: 20px; font-weight: 900; background: #000; color: #fff; display: inline-block; padding: 2px 8px; margin-bottom: 5px; }
			.order-number { font-size: 38px; font-weight: 900; margin: 5px 0; }
			
			.info-block { font-size: 14px; margin-bottom: 10px; }
			.info-row { display: flex; justify-content: space-between; }

			table { width: 100%; border-collapse: collapse; }
			th { text-align: left; font-size: 12px; border-bottom: 1px solid #000; padding: 5px 0; }
			td { padding: 10px 0; vertical-align: top; border-bottom: 1px solid #eee; }

			.qty-cell { width: 50px; font-size: 24px; font-weight: 900; text-align: left; }
			.item-cell { font-size: 18px; font-weight: 800; text-transform: uppercase; }

			.kitchen-notes { 
				margin-top: 15px; 
				padding: 8px; 
				border: 2px solid #000; 
				font-size: 16px; 
				font-weight: 900;
				background: #f0f0f0;
			}

			.summary { margin-top: 15px; font-size: 14px; }
			.status-tag { font-weight: bold; text-transform: uppercase; border: 1px solid #000; padding: 1px 4px; }
			
			@media print {
				.no-print { display: none; }
				body { padding: 0; width: 100%; }
			}
		</style>
	</head>
	<body onload="window.print()">

		<div class="no-print">
			<button class="print-btn" onclick="window.print()">PRINT KITCHEN TICKET</button>
			<p style="font-size: 10px; margin-top: 5px;">Format: 80mm Thermal</p>
		</div>

		<div class="header center">
			<div class="ticket-type">KITCHEN</div>
			<div class="order-number">#<?php echo esc_html( $afdp_order_title ); ?></div>
			<div style="font-weight: bold; font-size: 14px;"><?php echo esc_html( $afdp_order_date ); ?></div>
		</div>

		<div class="double-divider"></div>

		<div class="info-block">
			<strong>CUSTOMER:</strong> <?php echo esc_html( $afdp_customer_name ?: 'Guest' ); ?><br>
			<?php if ( $afdp_customer_phone ) : ?>
				<strong>TEL:</strong> <?php echo esc_html( $afdp_customer_phone ); ?><br>
			<?php endif; ?>
			<?php if ( $afdp_customer_address ) : ?>
				<strong>ADDR:</strong> <?php echo esc_html( $afdp_customer_address ); ?>
			<?php endif; ?>
		</div>

		<div class="divider"></div>

		<table>
			<thead>
				<tr>
					<th class="qty-cell">QTY</th>
					<th class="item-cell">ITEM DESCRIPTION</th>
				</tr>
			</thead>
			<tbody>
				<?php if ( ! empty( $afdp_items ) ) : foreach ( $afdp_items as $afdp_item ) : 
					$afdp_qty  = isset( $afdp_item['qty'] ) ? intval( $afdp_item['qty'] ) : 1;
					$afdp_name = ! empty( $afdp_item['name'] ) ? $afdp_item['name'] : 'Unknown Item';
				?>
					<tr>
						<td class="qty-cell"><?php echo esc_html( $afdp_qty ); ?>x</td>
						<td class="item-cell"><?php echo esc_html( $afdp_name ); ?></td>
					</tr>
				<?php endforeach; else : ?>
					<tr><td colspan="2" class="center">No items found</td></tr>
				<?php endif; ?>
			</tbody>
		</table>

		<?php if ( $afdp_notes ) : ?>
			<div class="kitchen-notes">
				⚠️ KITCHEN NOTES:<br>
				<?php echo nl2br( esc_html( $afdp_notes ) ); ?>
			</div>
		<?php endif; ?>

		<div class="summary">
			<div class="info-row">
				<span>Payment Status:</span>
				<span class="status-tag"><?php echo esc_html( $afdp_status ); ?></span>
			</div>
			<div class="info-row" style="margin-top: 5px; font-weight: bold; font-size: 16px;">
				<span>Order Total:</span>
				<span>£<?php echo esc_html( number_format( floatval( $afdp_total ), 2 ) ); ?></span>
			</div>
		</div>

		<div class="divider"></div>
		<div class="center" style="font-weight: bold; font-size: 12px; letter-spacing: 2px;">
			*** END OF TICKET ***
		</div>

	</body>
	</html>
	<?php
	exit; 
}