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

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

$sr = $conn->query("
    SELECT sr.*, inv.invoice_no, c.name AS client_name, c.phone AS client_phone
    FROM sales_returns sr
    JOIN sales_invoices inv ON inv.id = sr.sales_invoice_id
    JOIN clients c ON c.id = sr.client_id
    WHERE sr.id = $id AND sr.company_id = $cid
")->fetch_assoc();
if (!$sr) die('Sales return not found.');

$items = $conn->query("
    SELECT sri.*, p.name AS product_name, p.unit
    FROM sales_return_items sri
    JOIN products p ON p.id = sri.product_id
    WHERE sri.sales_return_id = $id
");
$statusColor = $sr['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; }
.doc-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="doc-no"><?= htmlspecialchars($sr['return_no']) ?></div>
        <div class="muted"><?= date('d M Y', strtotime($sr['return_date'])) ?></div>
        <div class="muted">Against Invoice: <?= htmlspecialchars($sr['invoice_no']) ?></div>
    </div>
    <div style="text-align:right;">
        <div style="font-weight:600;"><?= htmlspecialchars($sr['client_name']) ?></div>
        <?php if ($sr['client_phone']): ?>
        <div class="muted"><?= htmlspecialchars($sr['client_phone']) ?></div>
        <?php endif; ?>
        <div style="margin-top:6px;">
            <span class="badge" style="background:<?= $statusColor ?>22;color:<?= $statusColor ?>;"><?= ucfirst($sr['status']) ?></span>
        </div>
    </div>
</div>

<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Product</th>
            <th>Batch</th>
            <th style="text-align:right;">Rate (incl.)</th>
            <th style="text-align:center;">GST%</th>
            <th style="text-align:right;">Return Qty</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 ($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; ?>
        </td>
        <td style="font-size:11px;color:#64748b;"><?= htmlspecialchars($item['batch_no']) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['rate_incl_gst'],2) ?></td>
        <td style="text-align:center;"><?= $item['gst_percent'] ?>%</td>
        <td style="text-align:right;"><?= number_format($item['return_qty'], 0) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['taxable_amount'],2) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['gst_amount'],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">Taxable Amount</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($sr['total_taxable'],2) ?></td>
    </tr>
    <tr>
        <td class="label">GST Amount</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($sr['total_gst'],2) ?></td>
    </tr>
    <tr class="grand">
        <td>Total Return</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($sr['total_amount'],2) ?></td>
    </tr>
</table>

</body>
</html>
