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

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

$sr = $conn->query("SELECT * FROM sales_returns WHERE id = $id AND company_id = $cid")->fetch_assoc();
if (!$sr) die("Return not found");

$inv    = $conn->query("SELECT * FROM sales_invoices WHERE id = {$sr['sales_invoice_id']}")->fetch_assoc();
$client = $conn->query("SELECT * FROM clients WHERE id = {$sr['client_id']}")->fetch_assoc();

$items = $conn->query("
    SELECT sri.*, p.name AS product_name, p.hsn_code, p.unit
    FROM sales_return_items sri
    JOIN products p ON p.id = sri.product_id
    WHERE sri.sales_return_id = $id
");
?>

<div class="page-header">
    <h2><i class="fas fa-rotate-left" style="color:var(--primary);margin-right:8px;"></i>Credit Note / Sales Return</h2>
    <div style="display:flex;gap:10px;">
        <?php if ($sr['status'] === 'final'): ?>
        <button onclick="openPrintModal('sales_return', <?= $id ?>, 'invoice', 'thermal')" class="btn btn-primary">
            <i class="fas fa-print"></i> Print Thermal
        </button>
        <?php endif; ?>
        <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
    </div>
</div>

<!-- Return Info -->
<div class="panel" style="margin-bottom:16px;">
    <div class="panel-body">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;">
            <?php foreach ([
                'Credit Note No' => $sr['return_no'],
                'Return Date'    => date('d-m-Y', strtotime($sr['return_date'])),
                'Invoice No'     => $inv['invoice_no'],
                'Client'         => $client['name'],
            ] as $k => $v): ?>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;"><?= $k ?></div>
                <div style="font-size:14px;font-weight:600;"><?= htmlspecialchars($v) ?></div>
            </div>
            <?php endforeach; ?>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Status</div>
                <?php $s = strtolower($sr['status']); ?>
                <span class="badge <?= $s === 'final' ? 'badge-success' : 'badge-warning' ?>"><?= ucfirst($s) ?></span>
            </div>
        </div>
    </div>
</div>

<!-- Items -->
<div class="panel" style="margin-bottom:16px;">
    <div class="panel-header"><span class="panel-title">Returned Items</span></div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Product</th>
                    <th>Batch</th>
                    <th style="text-align:center;">Qty</th>
                    <th style="text-align:right;">Rate (Incl GST)</th>
                    <th style="text-align:center;">GST%</th>
                    <th style="text-align:right;">Taxable</th>
                    <th style="text-align:right;">GST</th>
                    <th style="text-align:right;">Total</th>
                </tr>
            </thead>
            <tbody>
            <?php $i = 1; while ($row = $items->fetch_assoc()): ?>
            <tr>
                <td style="color:var(--text-muted);"><?= $i++ ?></td>
                <td><strong><?= htmlspecialchars($row['product_name']) ?></strong></td>
                <td style="font-size:12px;color:var(--text-secondary);"><?= htmlspecialchars($row['batch_no']) ?></td>
                <td style="text-align:center;"><?= $row['return_qty'] ?></td>
                <td style="text-align:right;">&#8377; <?= number_format($row['rate_incl_gst'], 2) ?></td>
                <td style="text-align:center;"><?= $row['gst_percent'] ?>%</td>
                <td style="text-align:right;">&#8377; <?= number_format($row['taxable_amount'], 2) ?></td>
                <td style="text-align:right;">&#8377; <?= number_format($row['gst_amount'], 2) ?></td>
                <td style="text-align:right;font-weight:700;">&#8377; <?= number_format($row['total_amount'], 2) ?></td>
            </tr>
            <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Totals -->
<div style="display:flex;justify-content:flex-end;">
    <div class="totals-box" style="min-width:260px;">
        <div class="totals-row"><span>Taxable Amount</span><span>&#8377; <?= number_format($sr['total_taxable'], 2) ?></span></div>
        <div class="totals-row"><span>GST Amount</span><span>&#8377; <?= number_format($sr['total_gst'], 2) ?></span></div>
        <div class="totals-row grand-total"><span>Total Return</span><span>&#8377; <?= number_format($sr['total_amount'], 2) ?></span></div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>
