<?php
$managers = isset($managers) ? $managers : array();
$stores = isset($stores) ? $stores : array();
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1"><i class="fa-solid fa-user-tie text-primary me-2"></i>Managers &amp; Shift Supervisors</h4>
            <p class="text-muted mb-0">Store managers, supervisors and operational leads supervising daily routines.</p>
        </div>
        <a href="<?php echo base_url('admin/add_staff'); ?>" class="btn btn-primary">
            <i class="fa-solid fa-user-plus me-1"></i> Add Supervisor
        </a>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4">Manager Name</th>
                            <th>Staff ID</th>
                            <th>Designation</th>
                            <th>Supervising Store</th>
                            <th>Phone Contact</th>
                            <th>Shift</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($managers)): ?>
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">
                                    No managers or supervisors found.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($managers as $id => $m): ?>
                                <?php
                                $storeName = 'Not Assigned';
                                if (!empty($m['storeId']) && isset($stores[$m['storeId']]['name'])) {
                                    $storeName = $stores[$m['storeId']]['name'];
                                }
                                ?>
                                <tr>
                                    <td class="ps-4">
                                        <div class="d-flex align-items-center">
                                            <img src="<?php echo htmlspecialchars(!empty($m['imageUrl']) ? $m['imageUrl'] : 'https://upload.wikimedia.org/wikipedia/commons/9/9e/Placeholder_Person.jpg'); ?>" class="rounded-circle object-fit-cover me-3 border" width="40" height="40" alt="Avatar">
                                            <div>
                                                <div class="fw-bold text-dark"><?php echo htmlspecialchars(isset($m['fullName']) ? $m['fullName'] : 'Manager'); ?></div>
                                                <div class="small text-muted"><?php echo htmlspecialchars(isset($m['phone']) ? $m['phone'] : ''); ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-light text-dark font-monospace border">
                                            <?php echo htmlspecialchars(isset($m['staffId']) ? $m['staffId'] : 'N/A'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge bg-primary bg-opacity-10 text-primary fw-semibold px-3 py-1">
                                            <?php echo htmlspecialchars(isset($m['designation']) ? $m['designation'] : 'Shift Supervisor'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <div class="fw-medium text-dark"><i class="fa-solid fa-store text-muted me-1"></i><?php echo htmlspecialchars($storeName); ?></div>
                                    </td>
                                    <td>
                                        <?php echo htmlspecialchars(isset($m['phone']) ? $m['phone'] : '-'); ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-secondary"><?php echo htmlspecialchars(isset($m['shift']) ? $m['shift'] : 'Morning'); ?></span>
                                    </td>
                                    <td>
                                        <?php if (!empty($m['isActive'])): ?>
                                            <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-1">Active</span>
                                        <?php else: ?>
                                            <span class="badge bg-danger bg-opacity-10 text-danger rounded-pill px-3 py-1">Inactive</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
