<div class="card-box">
    <div class="card-box-header">
        <h5><i class="fa-solid fa-cart-shopping text-primary"></i> Purchase Entry Details</h5>
        <div class="d-flex gap-2">
            <a href="<?= site_url('purchase_entry/edit/'.$row->id) ?>" class="btn btn-sm btn-edit">
                <i class="fa-solid fa-pen-to-square me-1"></i> Edit
            </a>
            <a href="<?= site_url('purchase_entry') ?>" class="btn btn-sm" style="background:#f1f5f9;color:#374151;">
                <i class="fa-solid fa-arrow-left me-1"></i> Back
            </a>
        </div>
    </div>

    <div class="row mb-3">
        <div class="col-md-6">
            <table class="table table-bordered" style="font-size:13.5px;">
                <tr><th width="40%" class="bg-light">Supplier</th><td><strong><?= htmlspecialchars($row->supplier_name) ?></strong></td></tr>
                <tr><th class="bg-light">Invoice No</th><td><code><?= $row->invoice_no ?: '—' ?></code></td></tr>
                <tr><th class="bg-light">Purchase Date</th><td><?= date('d M Y', strtotime($row->purchase_date)) ?></td></tr>
                <tr><th class="bg-light">Notes</th><td><?= nl2br(htmlspecialchars($row->notes ?: '—')) ?></td></tr>
                <tr>
                    <th class="bg-light">Attachment</th>
                    <td>
                        <?php if(!empty($row->attachment)):
                            $ext = strtolower(pathinfo($row->attachment, PATHINFO_EXTENSION));
                            $file_url = base_url('uploads/purchase_entry/'.$row->attachment);
                        ?>
                            <?php if(in_array($ext, ['jpg','jpeg','png'])): ?>
                                <a href="<?= $file_url ?>" target="_blank">
                                    <img src="<?= $file_url ?>" alt="Attachment" style="max-height:60px;border-radius:4px;border:1px solid #e2e8f0;">
                                </a>
                            <?php else: ?>
                                <a href="<?= $file_url ?>" target="_blank">
                                    <i class="fa-solid fa-file-pdf text-danger me-1"></i> View PDF
                                </a>
                            <?php endif; ?>
                        <?php else: ?>
                            —
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>
        <div class="col-md-4">
            <table class="table table-bordered" style="font-size:13.5px;">
                <tr><th class="bg-light">Total Amount (excl. GST)</th><td><strong>₹<?= (int)($row->total_amount) ?></strong></td></tr>
                <tr><th class="bg-light">Grand Total (incl. GST)</th><td><strong class="text-primary fs-6">₹<?= (int)($row->grand_total) ?></strong></td></tr>
            </table>
        </div>
    </div>

    <div class="p-3 rounded" style="background:#f8fafc;border:1px solid #e2e8f0;">
        <div class="fw-bold text-muted mb-2" style="font-size:12px;letter-spacing:.8px;text-transform:uppercase;">
            <i class="fa-solid fa-boxes-stacked me-1"></i> Items
        </div>
        <div class="table-responsive">
            <table class="table table-bordered mb-0" style="font-size:13px;">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Product</th>
                        <th>Unit</th>
                        <th class="text-end">Qty</th>
                        <th class="text-end">Rate (₹)</th>
                        <th class="text-end">GST %</th>
                        <th class="text-end">Amount (₹)</th>
                        <th class="text-end">Total (₹)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($items)): $i = 1; foreach($items as $item): ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td><?= htmlspecialchars($item->product_name) ?></td>
                        <td><small class="text-muted"><?= $item->unit_name ?></small></td>
                        <td class="text-end"><?= (int)($item->qty) ?></td>
                        <td class="text-end">₹<?= (int)($item->rate) ?></td>
                        <td class="text-end"><?= $item->gst_percent ?>%</td>
                        <td class="text-end">₹<?= (int)($item->amount) ?></td>
                        <td class="text-end fw-bold">₹<?= (int)($item->total) ?></td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="9" class="text-center text-muted">No items found.</td></tr>
                    <?php endif; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="7" class="text-end fw-bold">Grand Total (incl. GST):</td>
                        <td class="text-end fw-bold">₹<?= (int)($row->total_amount) ?></td>
                        <td class="text-end fw-bold text-primary">₹<?= (int)($row->grand_total) ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>