<?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);
$pr = $conn->query("
    SELECT pr.*, po.po_no, v.name AS vendor_name
    FROM purchase_returns pr
    JOIN purchase_orders po ON po.id = pr.purchase_order_id
    JOIN vendors v ON v.id = pr.vendor_id
    WHERE pr.id = $id AND pr.company_id = $cid
")->fetch_assoc();
if (!$pr) die("Purchase Return not found.");

$items = $conn->query("
    SELECT pri.*, p.name AS product_name
    FROM purchase_return_items pri
    JOIN products p ON p.id = pri.product_id
    WHERE pri.purchase_return_id = $id
");
?>


<div class="page-header">
    <h2><i class="fas fa-rotate-left" style="color:var(--primary);margin-right:8px;"></i>Debit Note / Purchase Return</h2>
    <div style="display:flex;gap:10px;">
        <?php if ($pr['status'] === 'final'): ?>
        <button onclick="openPrintModal('purchase_returns', <?= $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>

<div class="panel" style="margin-bottom:16px;">
    <div class="panel-header"><span class="panel-title">Return Information</span></div>
    <div class="panel-body">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;">
            <?php foreach ([
                'Debit Note No' => $pr['return_no'],
                'Date'          => $pr['return_date'],
                'Purchase Order'=> $pr['po_no'],
                'Vendor'        => $pr['vendor_name'],
                'Reason'        => $pr['reason'] ?? '—',
            ] 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($pr['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">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;">Price</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['purchase_price'], 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($pr['total_amount'], 2) ?></span></div>
        <div class="totals-row"><span>GST Amount</span><span>&#8377; <?= number_format($pr['gst_amount'], 2) ?></span></div>
        <div class="totals-row grand-total"><span>Net Amount</span><span>&#8377; <?= number_format($pr['net_amount'], 2) ?></span></div>
    </div>
</div>

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