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

requirePermission('master_sales', 'view', BASE_URL . '/dashboard.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 sr.*, si.invoice_no, si.master_invoice_id, mi.master_no, cm.company_name
    FROM sales_returns sr
    JOIN sales_invoices si ON si.id = sr.sales_invoice_id
    JOIN master_invoices mi ON mi.id = si.master_invoice_id
    JOIN company_master cm ON cm.id = sr.company_id
    WHERE sr.id = $id
      AND si.master_invoice_id IS NOT NULL
")->fetch_assoc();
if (!$sr) die('Return not found.');

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

$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
");
?>

<div class="page-header">
    <h2><i class="fas fa-rotate-left" style="color:var(--primary);margin-right:8px;"></i>Credit Note / Master Return</h2>
    <div style="display:flex;gap:8px;">
        <a href="../sales_return/layout_loader_thermal.php?id=<?= $id ?>"
           onclick="window.open(this.href,'thermal_<?= $id ?>','width=560,height=780,scrollbars=yes');return false;"
           class="btn btn-secondary">
            <i class="fas fa-receipt"></i> Print Thermal
        </a>
        <a href="index.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Back</a>
    </div>
</div>

<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;">
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Credit Note No</div>
                <div style="font-size:15px;font-weight:700;"><?= htmlspecialchars($sr['return_no']) ?></div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Return Date</div>
                <div style="font-size:14px;"><?= date('d M Y', strtotime($sr['return_date'])) ?></div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Master Ref</div>
                <div style="font-size:14px;font-weight:600;">
                    <a href="../master_sales/view.php?id=<?= $sr['master_invoice_id'] ?>" style="color:var(--primary);">
                        <?= htmlspecialchars($sr['master_no']) ?>
                    </a>
                </div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Company</div>
                <div style="font-size:14px;"><?= htmlspecialchars($sr['company_name']) ?></div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Invoice No</div>
                <div style="font-size:14px;"><?= htmlspecialchars($sr['invoice_no']) ?></div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Client</div>
                <div style="font-size:14px;font-weight:600;"><?= htmlspecialchars($client['name']) ?></div>
            </div>
            <div>
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:var(--text-muted);margin-bottom:4px;">Status</div>
                <?php $s = $sr['status']; ?>
                <span class="badge <?= $s === 'final' ? 'badge-success' : 'badge-warning' ?>"><?= ucfirst($s) ?></span>
            </div>
        </div>
    </div>
</div>

<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;"><?= number_format($row['return_qty'], 0) ?></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>

<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'; ?>
