<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Invoice - <?php echo $invoice['id']; ?></title>
 <style>
 @font-face {
 font-family: 'DejaVu Sans';
 src: local('DejaVu Sans');
 font-weight: normal;
 font-style: normal;
 }
 @page {
 margin: 0;
 size: A4;
 }
 body {
 font-family: 'DejaVu Sans', sans-serif;
 font-size: 10px;
 color: #334155;
 line-height: 1.4;
 margin: 0;
 padding: 0;
 background-color: #fff;
 }
 
 /* Full Page Border */
 .page-container {
 margin: 20px;
 border: 1px solid #6366f1;
 min-height: 1060px;
 position: relative;
 }

 /* Top Accent */
 .top-line {
 height: 4px;
 background: linear-gradient(90deg, #6366f1, #0891b2);
 }

 .section {
 padding: 15px 25px;
 }

 /* Header Layout */
 .header-table {
 width: 100%;
 border-collapse: collapse;
 }
 .institute-name {
 font-size: 18px;
 font-weight: bold;
 color: #1e293b;
 margin: 0;
 }
 .institute-sub {
 font-size: 9px;
 color: #64748b;
 margin-top: 2px;
 }
 .invoice-label {
 font-size: 24px;
 font-weight: bold;
 color: #6366f1;
 text-align: right;
 text-transform: uppercase;
 }

 /* Meta Bar */
 .meta-bar {
 background-color: #f8fafc;
 width: 100%;
 border-collapse: collapse;
 border-top: 1px solid #f1f5f9;
 border-bottom: 1px solid #f1f5f9;
 table-layout: fixed;
 }
 .meta-bar td {
 padding: 10px 20px;
 }
 .label {
 font-size: 7px;
 text-transform: uppercase;
 font-weight: bold;
 color: #94a3b8;
 }
 .value {
 font-size: 10px;
 font-weight: bold;
 color: #1e293b;
 }

 /* Grid */
 .grid-table {
 width: 100%;
 border-collapse: collapse;
 }
 .grid-table td {
 width: 50%;
 vertical-align: top;
 padding: 15px 25px;
 }
 .grid-title {
 color: #6366f1;
 font-size: 9px;
 font-weight: bold;
 text-transform: uppercase;
 margin-bottom: 5px;
 }

 /* Items */
 .items-table {
 width: 100%;
 border-collapse: collapse;
 }
 .items-table th {
 background-color: #f0f9ff;
 color: #0369a1;
 text-align: left;
 padding: 8px 25px;
 font-size: 8px;
 font-weight: bold;
 text-transform: uppercase;
 }
 .items-table td {
 padding: 12px 25px;
 border-bottom: 1px solid #f1f5f9;
 }
 .item-title {
 font-size: 11px;
 font-weight: bold;
 color: #1e293b;
 }

 /* Summary Area */
 .summary-wrapper {
 padding: 15px 25px;
 }
 .summary-table {
 width: 220px;
 float: right;
 border-collapse: collapse;
 }
 .summary-table td {
 padding: 4px 0;
 }
 .total-label { color: #64748b; font-size: 10px; }
 .total-val { text-align: right; font-weight: bold; color: #1e293b; font-size: 10px; }
 
 .grand-total-box {
 background-color: #6366f1;
 color: #fff;
 padding: 10px;
 border-radius: 6px;
 margin-top: 10px;
 }
 .gt-label { font-size: 8px; font-weight: bold; text-transform: uppercase; }
 .gt-val { font-size: 16px; font-weight: bold; text-align: right; }

 /* Badges */
 .status-badge {
 padding: 3px 10px;
 border-radius: 10px;
 font-size: 8px;
 font-weight: bold;
 text-transform: uppercase;
 }
 .status-paid { background-color: #dcfce7; color: #15803d; }
 .status-partial { background-color: #fef9c3; color: #a16207; }
 .status-pending { background-color: #fee2e2; color: #b91c1c; }

 /* Footer */
 .footer {
 position: absolute;
 bottom: 20px;
 left: 0;
 right: 0;
 text-align: center;
 font-size: 8px;
 color: #94a3b8;
 padding: 0 25px;
 }
 .rupee { font-family: 'DejaVu Sans', sans-serif; }
 </style>
</head>
<body>
 <div class="page-container">
 <div class="top-line"></div>
 
 <!-- Header -->
 <div class="section">
 <table class="header-table">
 <tr>
 <td>
 <?php 
 $logoHtml = '';
 if (!empty($settings['institute_logo'])) {
 $possiblePaths = [
 $_SERVER['DOCUMENT_ROOT'] . $settings['institute_logo'],
 realpath(__DIR__ . '/../../public') . $settings['institute_logo'],
 realpath(__DIR__ . '/../../') . $settings['institute_logo'],
 realpath(__DIR__ . '/../../public/') . ltrim($settings['institute_logo'], '/'),
 ];
 foreach ($possiblePaths as $path) {
 if (!empty($path) && file_exists($path) && is_file($path)) {
 $logoData = base64_encode(file_get_contents($path));
 $logoType = pathinfo($path, PATHINFO_EXTENSION);
 $logoHtml = '<img src="data:image/' . $logoType . ';base64,' . $logoData . '" style="max-height: 60px; margin-bottom: 10px;">';
 break;
 }
 }
 }
 echo $logoHtml;
 ?>
 <div class="institute-name"><?php echo htmlspecialchars($settings['institute_name'] ?? 'Smart Tuitions'); ?></div>
 <div class="institute-sub">
 <?php echo nl2br(htmlspecialchars($settings['institute_address'] ?? 'Address not set')); ?><br>
 <?php if(!empty($settings['institute_phone'])): ?>
 <span style="font-weight: bold; color: #334155;">Phone:</span> <?php echo htmlspecialchars($settings['institute_phone']); ?>
 <?php endif; ?>
 <?php if(!empty($settings['institute_email'])): ?>
 <?php echo !empty($settings['institute_phone']) ? ' | ' : ''; ?>
 <span style="font-weight: bold; color: #334155;">Email:</span> <?php echo htmlspecialchars($settings['institute_email']); ?>
 <?php endif; ?>
 </div>
 </td>
 <td class="invoice-label" style="vertical-align: middle;">Invoice</td>
 </tr>
 </table>
 </div>

 <!-- Meta Bar -->
 <table class="meta-bar">
 <tr>
 <td style="padding-left: 25px;">
 <div class="label">Invoice No.</div>
 <div class="value">#<?php echo str_pad($invoice['id'], 5, '0', STR_PAD_LEFT); ?></div>
 </td>
 <td>
 <div class="label">Issued Date</div>
 <div class="value"><?php echo date('d/m/Y', strtotime($invoice['created_at'])); ?></div>
 </td>
 <td>
 <div class="label">Due Date</div>
 <div class="value" style="color: #ef4444;"><?php echo date('d M, Y', strtotime($invoice['due_date'])); ?></div>
 </td>
 <td style="text-align: right; padding-right: 25px;">
 <div class="label">Status</div>
 <span class="status-badge status-<?php echo strtolower($invoice['status']); ?>"><?php echo $invoice['status']; ?></span>
 </td>
 </tr>
 </table>

 <!-- Billing Grid -->
 <table class="grid-table">
 <tr>
 <td>
 <div class="grid-title">Billed To</div>
 <div style="font-size: 12px; font-weight: bold; color: #1e293b;"><?php echo $invoice['student_name']; ?></div>
 <div style="color: #64748b; margin-top: 2px;">
 ID: <?php echo $invoice['student_id_code']; ?><br>
 <?php echo $invoice['student_phone']; ?>
 </div>
 </td>
 <td style="border-left: 1px solid #f1f5f9;">
 <div class="grid-title">Course Information</div>
 <div style="font-size: 12px; font-weight: bold; color: #1e293b;"><?php echo $invoice['course_name'] ?? 'General Fees'; ?></div>
 </td>
 </tr>
 </table>

 <!-- Items Table -->
 <table class="items-table">
 <thead>
 <tr>
 <th style="width: 75%;">Description</th>
 <th style="width: 25%; text-align: right;">Amount</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <td>
 <div class="item-title"><?php echo $invoice['title']; ?></div>
 <div style="color: #94a3b8; font-size: 9px; margin-top: 2px;">Academic tuition and related educational services for the period.</div>
 </td>
 <td style="text-align: right; font-size: 12px; font-weight: bold; color: #1e293b;">
 <span class="rupee">&#8377;</span><?php echo number_format($invoice['total_amount'], 2); ?>
 </td>
 </tr>
 </tbody>
 </table>

 <!-- Summary -->
 <div class="summary-wrapper">
 <?php 
 $balance = max(0, $invoice['total_amount'] - $invoice['paid_amount'] - $invoice['discount_amount']);
 ?>
 <table class="summary-table">
 <tr>
 <td class="total-label">Subtotal</td>
 <td class="total-val"><span class="rupee">&#8377;</span><?php echo number_format($invoice['total_amount'], 2); ?></td>
 </tr>
 <?php if($invoice['discount_amount'] > 0): ?>
 <tr>
 <td class="total-label" style="color: #10b981;">Discount</td>
 <td class="total-val" style="color: #10b981;">- <span class="rupee">&#8377;</span><?php echo number_format($invoice['discount_amount'], 2); ?></td>
 </tr>
 <?php endif; ?>
 <tr>
 <td class="total-label">Total Paid</td>
 <td class="total-val" style="color: #6366f1;"><span class="rupee">&#8377;</span><?php echo number_format($invoice['paid_amount'], 2); ?></td>
 </tr>
 <tr>
 <td colspan="2" style="padding-top: 10px;">
 <div class="grand-total-box">
 <table style="width: 100%; border-collapse: collapse; color: #fff;">
 <tr>
 <td style="vertical-align: middle;">
 <div class="gt-label">Total Due</div>
 </td>
 <td style="text-align: right; vertical-align: middle;">
 <div class="gt-val"><span class="rupee">&#8377;</span><?php echo number_format($balance, 2); ?></div>
 </td>
 </tr>
 </table>
 </div>
 </td>
 </tr>
 </table>
 <div style="clear: both;"></div>
 </div>

 <!-- Footer -->
 <div class="footer">
 Generated on <?php echo date('d/m/Y h:i A'); ?>. This is a computer generated document.
 </div>

 </div>
</body>
</html>
