<?php
/**
 * @var object $suppliers
 * @var string $search
 * @var string $status
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-truck me-2 text-gold"></i>Suppliers</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Suppliers</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('suppliers_create')): ?>
                <a href="<?= url('suppliers/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Supplier
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3 g-2">
            <div class="col-md-6">
                <form method="GET" action="<?= url('suppliers') ?>" class="d-flex gap-2">
                    <div class="input-group">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                               placeholder="Search by name, contact, email or phone..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search)): ?>
                            <a href="<?= url('suppliers') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
            <div class="col-md-2">
                <select name="status" class="form-select form-select-sm bg-transparent" onchange="filterStatus(this)">
                    <option value="">All Status</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Contact Person</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>City</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($suppliers->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-truck-loading fa-2x mb-2"></i>
                                <p class="mb-0">No suppliers found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($suppliers->currentPage - 1) * $suppliers->perPage + 1; ?>
                        <?php foreach ($suppliers->data as $supplier): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <strong><?= e($supplier->name) ?></strong>
                                </td>
                                <td><?= e($supplier->contact_person ?? '-') ?></td>
                                <td><?= e($supplier->email ?? '-') ?></td>
                                <td><?= e($supplier->phone ?? '-') ?></td>
                                <td><?= e($supplier->city ?? '-') ?></td>
                                <td>
                                    <span class="badge bg-<?= $supplier->is_active ? 'success' : 'secondary' ?>">
                                        <?= $supplier->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <?php if (can('suppliers_edit')): ?>
                                            <a href="<?= url('suppliers/edit/' . $supplier->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('suppliers_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $supplier->id ?>"
                                                    data-name="<?= e($supplier->name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($suppliers->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($suppliers->currentPage - 1) * $suppliers->perPage) + 1 ?>
                    to <?= min($suppliers->currentPage * $suppliers->perPage, $suppliers->total) ?>
                    of <?= $suppliers->total ?> entries
                </div>
                <?= paginateLinks($suppliers, url('suppliers')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
function filterStatus(el) {
    const url = new URL(window.location.href);
    url.searchParams.set('status', el.value);
    url.searchParams.delete('page');
    window.location.href = url.toString();
}

EOS;
$inlineScripts = str_replace('__URL__', rtrim(APP_URL, '/'), $inlineScripts);
?>
