<?php
$pageTitle = 'Suppliers';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../auth/session.php';

require_login();
require_permission($pdo, 'suppliers.view');

$branchId = current_branch_id();
$canManageSuppliers = can($pdo, 'suppliers.manage');
$search = trim($_GET['q'] ?? '');

$where = ['branch_id = ?'];
$params = [$branchId];

if ($search !== '') {
    $where[] = '(name LIKE ? OR phone LIKE ? OR email LIKE ?)';
    $like = '%' . $search . '%';
    $params[] = $like;
    $params[] = $like;
    $params[] = $like;
}

$stmt = $pdo->prepare(
    'SELECT id, name, phone, email, created_at
     FROM suppliers
     WHERE ' . implode(' AND ', $where) . '
     ORDER BY name ASC
     LIMIT 300'
);
$stmt->execute($params);
$suppliers = $stmt->fetchAll();

$flash = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);

include __DIR__ . '/../includes/header.php';
?>

<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-3">
    <div>
        <h4 class="mb-0">Suppliers</h4>
        <small class="text-muted">Manage supplier contacts for the current branch.</small>
    </div>
    <?php if ($canManageSuppliers): ?>
        <a class="btn btn-primary" href="<?= app_url('suppliers/add.php') ?>">
            <i class="bi bi-plus-lg me-1"></i>
            Add Supplier
        </a>
    <?php endif; ?>
</div>

<?php if ($flash): ?>
    <div class="alert alert-<?= htmlspecialchars($flash['type']) ?> alert-dismissible fade show" role="alert">
        <?= htmlspecialchars($flash['message']) ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
<?php endif; ?>

<div class="table-card mb-3">
    <form class="row g-2 align-items-end" method="get">
        <div class="col-md-9">
            <label class="form-label">Search</label>
            <input
                type="search"
                name="q"
                class="form-control"
                placeholder="Search by name, phone, or email"
                value="<?= htmlspecialchars($search) ?>"
            >
        </div>
        <div class="col-md-3 d-grid">
            <button class="btn btn-outline-primary" type="submit">
                <i class="bi bi-search me-1"></i>
                Search
            </button>
        </div>
    </form>
</div>

<div class="table-card">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h5 class="mb-0">Supplier List</h5>
        <span class="badge text-bg-light"><?= count($suppliers) ?> shown</span>
    </div>

    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
                <tr>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Created</th>
                    <?php if ($canManageSuppliers): ?>
                        <th class="text-end">Actions</th>
                    <?php endif; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($suppliers as $supplier): ?>
                    <tr>
                        <td class="fw-semibold"><?= htmlspecialchars($supplier['name']) ?></td>
                        <td><?= htmlspecialchars($supplier['phone'] ?: '-') ?></td>
                        <td><?= htmlspecialchars($supplier['email'] ?: '-') ?></td>
                        <td><?= htmlspecialchars(date('M d, Y', strtotime($supplier['created_at']))) ?></td>
                        <?php if ($canManageSuppliers): ?>
                            <td class="text-end">
                                <div class="btn-group btn-group-sm" role="group" aria-label="Supplier actions">
                                    <a class="btn btn-outline-primary" href="<?= app_url('suppliers/edit.php?id=' . (int)$supplier['id']) ?>">
                                        <i class="bi bi-pencil-square"></i>
                                    </a>
                                    <a class="btn btn-outline-danger" href="<?= app_url('suppliers/delete.php?id=' . (int)$supplier['id']) ?>">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </div>
                            </td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>

                <?php if (!$suppliers): ?>
                    <tr>
                        <td colspan="<?= $canManageSuppliers ? 5 : 4 ?>" class="text-center text-muted py-4">
                            No suppliers found for this branch.
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
