<div class="card-box">

    <div class="card-box-header">
        <h5><i class="fa-solid fa-cart-shopping" style="color:#2563eb;"></i> Purchase Entry</h5>
        <a href="<?= site_url('purchase_entry/create') ?>" class="btn btn-primary btn-sm">
            <i class="fa-solid fa-plus me-1"></i> Add Purchase
        </a>
    </div>

    <?php if($this->session->flashdata('success')): ?>
    <div class="alert alert-success alert-dismissible fade show py-2">
        <i class="fa-solid fa-circle-check me-2"></i><?= $this->session->flashdata('success') ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <div class="table-responsive">
        <table class="table table-bordered table-hover dt-table mb-0" style="font-size:13px;">
            <thead>
                <tr>
                    <th width="45">#</th>
                    <th>Date</th>
                    <th>Invoice No</th>
                    <th>Supplier</th>
                    <th class="text-end">Amount (₹)</th>
                    <th class="text-end">Grand Total (₹)</th>
                    <th width="70" class="text-center">Attachment</th>
                    <th width="130" class="text-center">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($rows)): $i = 1; foreach($rows as $row): ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><?= date('d M Y', strtotime($row->purchase_date)) ?></td>
                    <td><code style="font-size:12px;"><?= $row->invoice_no ?: '—' ?></code></td>
                    <td><strong><?= htmlspecialchars($row->supplier_name) ?></strong></td>
                    <td class="text-end">₹<?= (int)($row->total_amount) ?></td>
                    <td class="text-end fw-bold text-primary">₹<?= (int)($row->grand_total) ?></td>
                    <td class="text-center">
                        <?php if(!empty($row->attachment)):
                            $ext = strtolower(pathinfo($row->attachment, PATHINFO_EXTENSION));
                        ?>
                            <a href="<?= base_url('uploads/purchase_entry/'.$row->attachment) ?>" target="_blank" title="View Attachment">
                                <i class="fa-solid <?= in_array($ext, ['jpg','jpeg','png']) ? 'fa-image' : 'fa-file-pdf' ?> text-primary"></i>
                            </a>
                        <?php else: ?>
                            <span class="text-muted">—</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-center">
                        <a href="<?= site_url('purchase_entry/view/'.$row->id) ?>" class="btn btn-sm btn-view me-1" title="View"><i class="fa-solid fa-eye"></i></a>
                         
                        <a href="<?= site_url('purchase_entry/edit/'.$row->id) ?>" class="btn btn-sm btn-edit me-1" title="Edit"><i class="fa-solid fa-pen-to-square"></i></a>
                        <!--<a href="<?= site_url('purchase_entry/delete/'.$row->id) ?>" class="btn btn-sm btn-delete btn-delete-confirm" title="Delete"><i class="fa-solid fa-trash"></i></a>-->
                    </td>
                </tr>
                <?php endforeach; else: ?>
                <tr><td colspan="8" class="text-center py-4 text-muted">
                    No purchase entries found.
                    <a href="<?= site_url('purchase_entry/create') ?>">Add first entry</a>
                </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>