<?php
include 'db_connect.php';
if(isset($_GET['id'])){
	$qry = $conn->query("SELECT * FROM sales_list where id=".$_GET['id'])->fetch_array();
	foreach($qry as $k => $val){
		$$k = $val;
	}
	$inv = $conn->query("SELECT * FROM inventory where type=2 and form_id=".$_GET['id']);
	if($customer_id > 0){
		$cust = $conn->query("SELECT * FROM customer_list where id = $customer_id ");
		$cname = $cust->num_rows > 0 ? $cust->fetch_array()['name']: "Walk-in";
	}else{
		$cname = "Walk-in";
	}

	// Check if there's a debt for this sale
	$debt = $conn->query("SELECT * FROM debt_list WHERE sales_id = ".$_GET['id'])->fetch_assoc();
}
$product = $conn->query("SELECT * FROM product_list order by name asc");
while($row=$product->fetch_assoc()):
	$prod[$row['id']] = $row;
endwhile;

// Get system settings
$system = $conn->query("SELECT * FROM system_settings LIMIT 1")->fetch_assoc();
$store_name = $system['name'] ?? 'PHARMACY';
?>
<!DOCTYPE html>
<html>
<head>
	<title>Receipt #<?php echo $ref_no; ?></title>
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}

		@page {
			size: 80mm auto;
			margin: 0;
		}

		body {
			font-family: 'Courier New', monospace;
			font-size: 12px;
			width: 80mm;
			max-width: 80mm;
			margin: 0 auto;
			padding: 5mm;
			background: white;
		}

		.receipt {
			width: 100%;
		}

		.header {
			text-align: center;
			padding-bottom: 8px;
			border-bottom: 1px dashed #000;
			margin-bottom: 8px;
		}

		.store-name {
			font-size: 16px;
			font-weight: bold;
			text-transform: uppercase;
			letter-spacing: 1px;
		}

		.store-tagline {
			font-size: 10px;
			color: #555;
		}

		.receipt-title {
			font-size: 11px;
			margin-top: 5px;
			padding: 3px 0;
			background: #000;
			color: #fff;
			letter-spacing: 2px;
		}

		.info-section {
			padding: 8px 0;
			border-bottom: 1px dashed #000;
			margin-bottom: 8px;
		}

		.info-row {
			display: flex;
			justify-content: space-between;
			margin-bottom: 3px;
			font-size: 11px;
		}

		.info-label {
			color: #555;
		}

		.items-section {
			padding: 5px 0;
		}

		.items-header {
			display: flex;
			font-weight: bold;
			font-size: 10px;
			padding: 5px 0;
			border-bottom: 1px solid #000;
			text-transform: uppercase;
		}

		.item-row {
			display: flex;
			padding: 4px 0;
			border-bottom: 1px dotted #ccc;
			font-size: 11px;
		}

		.item-name {
			flex: 1;
			padding-right: 5px;
		}

		.item-qty {
			width: 25px;
			text-align: center;
		}

		.item-price {
			width: 55px;
			text-align: right;
		}

		.item-total {
			width: 60px;
			text-align: right;
			font-weight: bold;
		}

		.totals-section {
			padding: 8px 0;
			border-top: 1px dashed #000;
			margin-top: 5px;
		}

		.total-row {
			display: flex;
			justify-content: space-between;
			padding: 3px 0;
			font-size: 11px;
		}

		.total-row.grand-total {
			font-size: 14px;
			font-weight: bold;
			padding: 8px 0;
			border-top: 2px solid #000;
			border-bottom: 2px solid #000;
			margin: 5px 0;
		}

		.total-row.debt {
			color: #c00;
			font-weight: bold;
			background: #fff0f0;
			padding: 5px;
			margin: 3px 0;
		}

		.total-row.paid {
			color: #080;
		}

		.footer {
			text-align: center;
			padding-top: 10px;
			border-top: 1px dashed #000;
			margin-top: 10px;
		}

		.footer-text {
			font-size: 10px;
			color: #555;
			margin-bottom: 3px;
		}

		.thank-you {
			font-size: 12px;
			font-weight: bold;
			margin: 8px 0;
		}

		.barcode {
			font-family: 'Libre Barcode 39', cursive;
			font-size: 30px;
			letter-spacing: 5px;
			margin: 5px 0;
		}

		.qr-placeholder {
			margin: 10px auto;
			font-size: 10px;
		}

		.divider {
			border-top: 1px dashed #000;
			margin: 8px 0;
		}

		.customer-section {
			padding: 5px 0;
			font-size: 11px;
		}

		@media print {
			body {
				width: 80mm;
				padding: 2mm;
			}

			.no-print {
				display: none;
			}
		}
	</style>
</head>
<body>
	<div class="receipt">
		<!-- Header -->
		<div class="header">
			<div class="store-name"><?php echo strtoupper($store_name); ?></div>
			<div class="store-tagline">Your Health, Our Priority</div>
			<div class="receipt-title">SALES RECEIPT</div>
		</div>

		<!-- Info Section -->
		<div class="info-section">
			<div class="info-row">
				<span class="info-label">Receipt #:</span>
				<span><strong><?php echo $ref_no; ?></strong></span>
			</div>
			<div class="info-row">
				<span class="info-label">Date:</span>
				<span><?php echo date("M d, Y", strtotime($date_updated)); ?></span>
			</div>
			<div class="info-row">
				<span class="info-label">Time:</span>
				<span><?php echo date("h:i A", strtotime($date_updated)); ?></span>
			</div>
			<div class="info-row">
				<span class="info-label">Customer:</span>
				<span><?php echo ucwords($cname); ?></span>
			</div>
		</div>

		<!-- Items Section -->
		<div class="items-section">
			<div class="items-header">
				<span class="item-qty">Qty</span>
				<span class="item-name">Item</span>
				<span class="item-total">Amount</span>
			</div>

			<?php
			$item_count = 0;
			while($row = $inv->fetch_assoc()):
				foreach(json_decode($row['other_details']) as $k=>$v){
					$row[$k] = $v;
				}
				$item_count++;
			?>
			<div class="item-row">
				<span class="item-qty"><?php echo $row['qty']; ?></span>
				<span class="item-name">
					<?php echo $prod[$row['product_id']]['name']; ?>
					<?php if($row['qty'] > 1): ?>
					<br><small style="color:#666;">@TZS <?php echo number_format($row['price'], 0); ?></small>
					<?php endif; ?>
				</span>
				<span class="item-total">TZS <?php echo number_format($row['price'] * $row['qty'], 0); ?></span>
			</div>
			<?php endwhile; ?>
		</div>

		<!-- Totals Section -->
		<div class="totals-section">
			<div class="total-row">
				<span>Items:</span>
				<span><?php echo $item_count; ?></span>
			</div>

			<div class="total-row grand-total">
				<span>TOTAL:</span>
				<span>TZS <?php echo number_format($total_amount, 0); ?></span>
			</div>

			<div class="total-row">
				<span>Cash:</span>
				<span>TZS <?php echo number_format($amount_tendered, 0); ?></span>
			</div>

			<?php if($amount_change > 0): ?>
			<div class="total-row">
				<span>Change:</span>
				<span>TZS <?php echo number_format($amount_change, 0); ?></span>
			</div>
			<?php endif; ?>

			<?php if($debt && $debt['balance'] > 0): ?>
			<div class="total-row debt">
				<span>BALANCE DUE:</span>
				<span>TZS <?php echo number_format($debt['balance'], 0); ?></span>
			</div>
			<?php elseif($debt && $debt['status'] == 'paid'): ?>
			<div class="total-row paid">
				<span>Status:</span>
				<span>FULLY PAID</span>
			</div>
			<?php endif; ?>
		</div>

		<!-- Footer -->
		<div class="footer">
			<div class="thank-you">THANK YOU!</div>
			<div class="footer-text">Please keep this receipt for your records</div>
			<div class="footer-text">For inquiries, please contact us</div>
			<div class="divider"></div>
			<div class="footer-text"><?php echo date("Y-m-d H:i:s"); ?></div>
			<div style="font-size: 9px; margin-top: 5px;">*<?php echo $ref_no; ?>*</div>
		</div>
	</div>

	<script>
		// Auto print when page loads
		window.onload = function() {
			// window.print();
		}
	</script>
</body>
</html>
