<?php
$staff = isset($staff) ? $staff : 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-people-arrows text-primary me-2"></i>Staff Store Assignment</h4>
            <p class="text-muted mb-0">Manage and map team deployments across store branches.</p>
        </div>
        <a href="<?php echo base_url('admin/staff'); ?>" class="btn btn-outline-secondary">
            <i class="fa-solid fa-list me-1"></i> Staff List
        </a>
    </div>

    <div class="row g-4">
        <?php if (empty($stores)): ?>
            <div class="col-12 text-center py-5 text-muted">
                No stores available. Please create stores first.
            </div>
        <?php else: ?>
            <?php foreach ($stores as $sId => $s): ?>
                <?php
                // Filter staff assigned to this store
                $storeStaff = array();
                foreach ($staff as $stId => $st) {
                    if (isset($st['storeId']) && $st['storeId'] === $sId) {
                        $storeStaff[$stId] = $st;
                    }
                }
                ?>
                <div class="col-md-6 col-xl-4">
                    <div class="card border-0 shadow-sm rounded-4 h-100">
                        <div class="card-header bg-transparent border-0 pt-4 px-4 pb-2 d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="fw-bold text-dark mb-1"><?php echo htmlspecialchars(isset($s['name']) ? $s['name'] : $sId); ?></h6>
                                <div class="small text-muted"><i class="fa-solid fa-location-dot me-1"></i><?php echo htmlspecialchars(isset($s['city']) ? $s['city'] : 'Islamabad'); ?></div>
                            </div>
                            <span class="badge bg-primary bg-opacity-10 text-primary rounded-pill px-3 py-1">
                                <?php echo count($storeStaff); ?> Staff
                            </span>
                        </div>
                        <div class="card-body px-4 pb-4">
                            <?php if (empty($storeStaff)): ?>
                                <div class="p-3 text-center text-muted small bg-light rounded-3 my-2">
                                    No staff currently assigned to this branch.
                                </div>
                            <?php else: ?>
                                <div class="list-group list-group-flush">
                                    <?php foreach ($storeStaff as $stId => $st): ?>
                                        <div class="list-group-item px-0 py-2 border-light d-flex align-items-center justify-content-between">
                                            <div class="d-flex align-items-center">
                                                <img src="<?php echo htmlspecialchars(!empty($st['imageUrl']) ? $st['imageUrl'] : 'https://upload.wikimedia.org/wikipedia/commons/9/9e/Placeholder_Person.jpg'); ?>" class="rounded-circle object-fit-cover me-2" width="32" height="32" alt="Avatar">
                                                <div>
                                                    <div class="fw-semibold text-dark small"><?php echo htmlspecialchars(isset($st['fullName']) ? $st['fullName'] : 'Staff'); ?></div>
                                                    <div class="text-muted" style="font-size: 0.75rem;"><?php echo htmlspecialchars(isset($st['designation']) ? $st['designation'] : 'Staff'); ?> · <?php echo htmlspecialchars(isset($st['staffId']) ? $st['staffId'] : ''); ?></div>
                                                </div>
                                            </div>
                                            <span class="badge bg-light text-dark border"><?php echo htmlspecialchars(isset($st['shift']) ? $st['shift'] : 'Morning'); ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>
