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

/**
 * Premium Invoice Template - SaaS UI Refactor
 */
$afdp_order_id = isset( $_GET['order_id'] ) ? intval( $_GET['order_id'] ) : 0;
$afdp_order    = get_post( $afdp_order_id );

if ( ! $afdp_order ) {
	wp_die( esc_html__( 'Order resource not found.', 'food-delivery-pickup-soluition' ) );
}

// Data Normalization
$afdp_items = get_post_meta( $afdp_order_id, 'order_items', true );
if ( empty( $afdp_items ) ) {
	$afdp_items = get_post_meta( $afdp_order_id, 'items', true ) ?: array();
}

$afdp_total            = get_post_meta( $afdp_order_id, 'total_price', true ) ?: '0.00';
$afdp_cust_name        = get_post_meta( $afdp_order_id, 'customer_name', true ) ?: 'Guest Customer';
$afdp_cust_phone       = get_post_meta( $afdp_order_id, 'customer_phone', true ) ?: ( get_post_meta( $afdp_order_id, 'phone', true ) ?: 'N/A' );
$afdp_cust_email       = get_post_meta( $afdp_order_id, 'customer_email', true ) ?: ( get_post_meta( $afdp_order_id, 'email', true ) ?: 'N/A' );
$afdp_cust_address     = get_post_meta( $afdp_order_id, 'customer_address', true ) ?: get_post_meta( $afdp_order_id, 'address', true );
$afdp_order_type       = get_post_meta( $afdp_order_id, 'order_type', true ) ?: 'Delivery';
$afdp_order_status     = get_post_meta( $afdp_order_id, 'status', true ) ?: 'Pending';
$afdp_wp_date_format   = get_option( 'date_format' );
$afdp_formatted_date   = get_the_date( $afdp_wp_date_format, $afdp_order_id );
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Invoice - <?php echo esc_html( get_the_title( $afdp_order_id ) ); ?></title>
	<style>
		:root { 
			--fd-primary: #e53935; 
			--fd-text: #1e293b;
			--fd-muted: #64748b;
			--fd-border: #e2e8f0;
		}

		@page { size: A4; margin: 15mm; }
		
		body { 
			font-family: 'Inter', -apple-system, system-ui, sans-serif; 
			margin: 0; padding: 0; color: var(--fd-text); background: #fff; line-height: 1.5;
		}

		.invoice-wrapper { width: 180mm; margin: auto; }

		.header { 
			display: flex; justify-content: space-between; align-items: flex-end; 
			padding-bottom: 30px; border-bottom: 2px solid var(--fd-primary); margin-bottom: 40px; 
		}
		.brand h1 { margin: 0; color: var(--fd-primary); font-size: 28px; font-weight: 900; text-transform: uppercase; letter-spacing: -0.5px; }
		.brand p { margin: 2px 0; font-size: 12px; color: var(--fd-muted); font-weight: 500; }
		
		.meta-info { text-align: right; }
		.meta-info h2 { margin: 0; font-size: 32px; font-weight: 900; color: #0f172a; opacity: 0.1; position: absolute; right: 15mm; top: 10mm; }
		.meta-info p { margin: 0; font-size: 14px; font-weight: 700; }
		.order-tag { 
			display: inline-block; padding: 4px 12px; border-radius: 6px; font-size: 11px; 
			font-weight: 800; background: #fff5f5; color: var(--fd-primary); margin-top: 10px; 
			text-transform: uppercase; border: 1px solid #fecaca;
		}

		.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 60px; margin-bottom: 40px; }
		.info-section h3 { font-size: 11px; text-transform: uppercase; color: var(--fd-muted); letter-spacing: 1.2px; margin-bottom: 12px; font-weight: 800; border-bottom: 1px solid var(--fd-border); padding-bottom: 5px; }
		.info-section p { margin: 0; font-size: 14px; color: var(--fd-text); }
		.address-text { color: var(--fd-muted) !important; font-size: 13px !important; margin-top: 5px !important; line-height: 1.6; }

		table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
		th { background: #f8fafc; color: var(--fd-muted); font-size: 10px; text-transform: uppercase; padding: 12px 15px; text-align: left; letter-spacing: 1px; }
		td { padding: 15px; border-bottom: 1px solid var(--fd-border); font-size: 14px; }
		.td-qty { font-weight: 800; text-align: center; color: var(--fd-primary); }
		.td-price { text-align: right; color: var(--fd-muted); }
		.td-total { text-align: right; font-weight: 800; color: var(--fd-text); }

		.bottom-grid { display: grid; grid-template-columns: 1fr 280px; gap: 40px; }
		.notes-box { font-size: 12px; color: var(--fd-muted); padding-top: 20px; }
		.totals-card { background: #f8fafc; padding: 25px; border-radius: 12px; }
		.total-row { display: flex; justify-content: space-between; margin-bottom: 10px; font-size: 14px; font-weight: 600; }
		.total-row.grand-final { 
			margin-top: 15px; padding-top: 15px; border-top: 2px dashed #cbd5e1; 
			color: var(--fd-primary); font-size: 24px; font-weight: 900; 
		}

		.footer-stamp { 
			margin-top: 60px; text-align: center; font-size: 10px; color: #cbd5e1; 
			text-transform: uppercase; letter-spacing: 2px;
		}

		@media print {
			body { -webkit-print-color-adjust: exact; }
			.no-print { display: none; }
		}
	</style>
</head>
<body onload="window.print()">

	<div class="invoice-wrapper">
		<header class="header">
			<div class="brand">
				<h1>Spice of India</h1>
				<p>524 Hertford Road, London, EN3 5SS</p>
				<p>Tel: +44 208 443 2500 • info@spiceofindia.online</p>
			</div>
			<div class="meta-info">
				<h2>OFFICIAL</h2>
				<p>Order ID: #<?php echo esc_html( get_the_title( $afdp_order_id ) ); ?></p>
				<p style="color: var(--fd-muted); font-weight: 500;">Issued: <?php echo esc_html( $afdp_formatted_date ); ?></p>
				<span class="order-tag"><?php echo esc_html( $afdp_order_status ); ?></span>
			</div>
		</header>

		<main class="info-grid">
			<div class="info-section">
				<h3>Customer Information</h3>
				<p style="font-weight: 800; font-size: 16px;"><?php echo esc_html( $afdp_cust_name ); ?></p>
				<p class="address-text">
					Email: <?php echo esc_html( $afdp_cust_email ); ?><br>
					Phone: <?php echo esc_html( $afdp_cust_phone ); ?>
				</p>
			</div>
			<div class="info-section">
				<h3><?php echo esc_html( $afdp_order_type ); ?> Destination</h3>
				<p class="address-text" style="font-weight: 600; color: var(--fd-text) !important;">
					<?php 
					if ( ! empty( $afdp_cust_address ) ) {
						echo nl2br( esc_html( $afdp_cust_address ) );
					} else {
						echo "<strong>STORE PICKUP</strong><br>524 Hertford Road, London, EN3 5SS";
					}
					?>
				</p>
			</div>
		</main>

		<table>
			<thead>
				<tr>
					<th width="50%">Item Description</th>
					<th width="10%" style="text-align:center;">Qty</th>
					<th width="20%" style="text-align:right;">Unit Price</th>
					<th width="20%" style="text-align:right;">Line Total</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $afdp_items as $afdp_item ) : 
					$afdp_price = (float) ( $afdp_item['price'] ?? 0 );
					$afdp_qty   = (int) ( $afdp_item['qty'] ?? 1 );
					$afdp_line_total = $afdp_price * $afdp_qty;
				?>
				<tr>
					<td><strong style="color: #0f172a;"><?php echo esc_html( $afdp_item['name'] ); ?></strong></td>
					<td class="td-qty"><?php echo esc_html( $afdp_qty ); ?></td>
					<td class="td-price">£<?php echo esc_html( number_format( $afdp_price, 2 ) ); ?></td>
					<td class="td-total">£<?php echo esc_html( number_format( $afdp_line_total, 2 ) ); ?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<div class="bottom-grid">
			<div class="notes-box">
				<p style="font-weight: 800; color: var(--fd-text); margin-bottom: 5px;">Payment & Additional Info</p>
				<p>This invoice was generated automatically via the Spice of India SaaS Portal. Please retain this for your records. All food items are prepared fresh to order.</p>
				<p style="margin-top: 10px; font-weight: 700; color: var(--fd-primary);">Thank you for choosing Spice of India!</p>
			</div>
			<div class="totals-card">
				<div class="total-row">
					<span>Subtotal</span>
					<span>£<?php echo esc_html( number_format( (float) $afdp_total, 2 ) ); ?></span>
				</div>
				<div class="total-row">
					<span>Processing Fee</span>
					<span>£0.00</span>
				</div>
				<div class="total-row grand-final">
					<span>Total</span>
					<span>£<?php echo esc_html( number_format( (float) $afdp_total, 2 ) ); ?></span>
				</div>
			</div>
		</div>

		<div class="footer-stamp">
			Validated Invoice • Spice of India Digital Services • <?php echo esc_html( gmdate( 'Y' ) ); ?>
		</div>
	</div>

</body>
</html>