<?php
require_once '../includes/auth.php';
require_once '../db.php';

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die('Invalid ID');

$inv = $conn->query("
    SELECT si.*, c.name AS client_name, c.phone AS client_phone, c.address AS client_address
    FROM sales_invoices si
    JOIN clients c ON c.id = si.client_id
    WHERE si.id = $id AND si.company_id = $cid
")->fetch_assoc();
if (!$inv) die('Invoice not found.');

$items = $conn->query("
    SELECT si.*, p.name AS product_name, p.unit
    FROM sales_items si
    JOIN products p ON p.id = si.product_id
    WHERE si.sales_invoice_id = $id
");
$statusColor = $inv['status'] === 'final' ? '#10b981' : '#f59e0b';
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: Inter, sans-serif; font-size: 13px; color: #1e293b; padding: 20px; background: #fff; }
.header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; gap: 12px; }
.inv-no { font-size: 20px; font-weight: 700; color: #1e293b; }
.muted { color: #64748b; font-size: 12px; margin-top: 3px; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 11px; font-weight: 600; }
table { width: 100%; border-collapse: collapse; }
thead th { background: #f1f5f9; text-transform: uppercase; font-size: 11px; letter-spacing: .4px; padding: 8px 10px; text-align: left; color: #475569; }
tbody td { padding: 7px 10px; border-bottom: 1px solid #f1f5f9; }
tbody tr:last-child td { border-bottom: none; }
.totals-table { width: 240px; margin-left: auto; margin-top: 14px; }
.totals-table td { padding: 5px 8px; }
.totals-table .label { color: #64748b; }
.totals-table .grand td { font-weight: 700; font-size: 14px; border-top: 2px solid #e2e8f0; padding-top: 8px; }
</style>
</head>
<body>

<div class="header">
    <div>
        <div class="inv-no"><?= htmlspecialchars($inv['invoice_no']) ?></div>
        <div class="muted"><?= date('d M Y', strtotime($inv['invoice_date'])) ?></div>
    </div>
    <div style="text-align:right;">
        <div style="font-weight:600;"><?= htmlspecialchars($inv['client_name']) ?></div>
        <?php if ($inv['client_phone']): ?>
        <div class="muted"><?= htmlspecialchars($inv['client_phone']) ?></div>
        <?php endif; ?>
        <div style="margin-top:6px;">
            <span class="badge" style="background:<?= $statusColor ?>22;color:<?= $statusColor ?>;"><?= ucfirst($inv['status']) ?></span>
        </div>
    </div>
</div>

<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Product</th>
            <th>Batch</th>
            <th style="text-align:right;">Qty</th>
            <th style="text-align:right;">Rate (incl.)</th>
            <th style="text-align:right;">Total</th>
        </tr>
    </thead>
    <tbody>
    <?php $i = 1; while ($item = $items->fetch_assoc()): ?>
    <tr>
        <td style="color:#94a3b8;"><?= $i++ ?></td>
        <td>
            <strong><?= htmlspecialchars($item['product_name']) ?></strong>
            <?php if ($item['unit']): ?><span style="font-size:11px;color:#94a3b8;margin-left:4px;"><?= htmlspecialchars($item['unit']) ?></span><?php endif; ?>
            <?php if ($item['description']): ?><div style="font-size:11px;color:#64748b;"><?= htmlspecialchars($item['description']) ?></div><?php endif; ?>
        </td>
        <td style="font-size:11px;color:#64748b;"><?= htmlspecialchars($item['batch_no']) ?></td>
        <td style="text-align:right;"><?= number_format($item['sold_qty'], 0) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['selling_price_incl'],2) ?></td>
        <td style="text-align:right;font-weight:600;">&#8377;&nbsp;<?= number_format($item['total_amount'],2) ?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<table class="totals-table">
    <tr>
        <td class="label">Gross Total</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($inv['gross_total'],2) ?></td>
    </tr>
    <?php if ($inv['overall_discount_value'] > 0): ?>
    <tr>
        <td class="label">Discount (<?= $inv['overall_discount_type'] === 'percent' ? $inv['overall_discount_value'].'%' : 'Flat' ?>)</td>
        <td style="text-align:right;color:#ef4444;">- &#8377;&nbsp;<?= number_format($inv['overall_discount_type'] === 'percent' ? $inv['gross_total'] * $inv['overall_discount_value'] / 100 : $inv['overall_discount_value'], 2) ?></td>
    </tr>
    <?php endif; ?>
    <tr>
        <td class="label">GST</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($inv['total_gst'],2) ?></td>
    </tr>
    <tr class="grand">
        <td>Net Total</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($inv['net_total'],2) ?></td>
    </tr>
    <?php if ($inv['received_amount'] > 0): ?>
    <tr>
        <td class="label">Received (<?= htmlspecialchars($inv['payment_mode'] ?? '') ?>)</td>
        <td style="text-align:right;color:#10b981;">&#8377;&nbsp;<?= number_format($inv['received_amount'],2) ?></td>
    </tr>
    <tr>
        <td class="label">Balance</td>
        <td style="text-align:right;color:<?= $inv['balance_amount'] > 0 ? '#ef4444' : '#10b981' ?>;">
            &#8377;&nbsp;<?= number_format($inv['balance_amount'],2) ?>
        </td>
    </tr>
    <?php endif; ?>
</table>

</body>
</html>
