<?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-users text-primary me-2"></i>Staff Management</h4>
            <p class="text-muted mb-0">Roster of on-duty personnel, supervisors, and branch assignments.</p>
        </div>
        <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 New Staff
            </a>
        </div>
    </div>

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

    <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">Staff Member</th>
                            <th>Staff ID</th>
                            <th>Designation</th>
                            <th>Assigned Store</th>
                            <th>Phone &amp; PIN</th>
                            <th>Shift</th>
                            <th>Status</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($staff)): ?>
                            <tr>
                                <td colspan="8" class="text-center py-5 text-muted">
                                    <i class="fa-solid fa-users-slash fs-1 opacity-25 mb-3 d-block"></i>
                                    No staff members found. Click "Add New Staff" to register team members.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($staff as $id => $st): ?>
                                <?php
                                $storeName = 'Not Assigned';
                                if (!empty($st['storeId']) && isset($stores[$st['storeId']]['name'])) {
                                    $storeName = $stores[$st['storeId']]['name'];
                                }
                                ?>
                                <tr>
                                    <td class="ps-4">
                                        <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-3 border" width="44" height="44" alt="Avatar">
                                            <div>
                                                <div class="fw-bold text-dark"><?php echo htmlspecialchars(isset($st['fullName']) ? $st['fullName'] : 'Unnamed'); ?></div>
                                                <div class="small text-muted"><?php echo isset($st['createdAt']) ? 'Joined ' . date('M Y', $st['createdAt'] / 1000) : ''; ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-light text-dark font-monospace border px-2 py-1">
                                            <?php echo htmlspecialchars(isset($st['staffId']) ? $st['staffId'] : 'N/A'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <div class="fw-semibold text-dark"><?php echo htmlspecialchars(isset($st['designation']) ? $st['designation'] : 'Staff'); ?></div>
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center text-dark">
                                            <i class="fa-solid fa-store text-muted me-2"></i>
                                            <span class="fw-medium"><?php echo htmlspecialchars($storeName); ?></span>
                                        </div>
                                    </td>
                                    <td class="small">
                                        <div><i class="fa-solid fa-phone text-muted me-1"></i><?php echo htmlspecialchars(isset($st['phone']) ? $st['phone'] : '-'); ?></div>
                                        <div class="text-muted"><i class="fa-solid fa-key text-muted me-1"></i>PIN: <span class="font-monospace fw-bold">••••</span></div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info bg-opacity-10 text-info rounded-pill px-3 py-1">
                                            <?php echo htmlspecialchars(isset($st['shift']) ? $st['shift'] : 'Morning'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if (!empty($st['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>
                                    <td class="text-end pe-4">
                                        <div class="btn-group">
                                            <a href="<?php echo base_url('admin/staff_delete/' . $id); ?>" onclick="return confirm('Delete this staff member?');" class="btn btn-sm btn-outline-danger" title="Delete Staff">
                                                <i class="fa-solid fa-trash-can"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
