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

// Count staff per store
$staffCounts = array();
foreach ($staff as $st) {
    $sid = isset($st['storeId']) ? $st['storeId'] : '';
    if ($sid !== '') {
        $staffCounts[$sid] = (isset($staffCounts[$sid]) ? $staffCounts[$sid] : 0) + 1;
    }
}
?>

<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-store text-primary me-2"></i>Stores Management</h4>
            <p class="text-muted mb-0">Overview of all active and registered branch locations.</p>
        </div>
        <div>
            <a href="<?php echo base_url('admin/add_store'); ?>" class="btn btn-primary">
                <i class="fa-solid fa-plus me-1"></i> Add New Store
            </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">Store Name</th>
                            <th>Location / City</th>
                            <th>Coordinates</th>
                            <th>Assigned Staff</th>
                            <th>Status</th>
                            <th>Created Date</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($stores)): ?>
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">
                                    <i class="fa-solid fa-store fs-1 opacity-25 mb-3 d-block"></i>
                                    No stores found. Click "Add New Store" to register a branch.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($stores as $storeId => $store): ?>
                                <tr>
                                    <td class="ps-4">
                                        <div class="d-flex align-items-center">
                                            <div class="p-2 bg-primary bg-opacity-10 text-primary rounded-3 me-3">
                                                <i class="fa-solid fa-shop fs-5"></i>
                                            </div>
                                            <div>
                                                <a href="<?php echo base_url('admin/store_details/' . $storeId); ?>" class="fw-bold text-dark text-decoration-none">
                                                    <?php echo htmlspecialchars(isset($store['name']) ? $store['name'] : 'Unnamed Store'); ?>
                                                </a>
                                                <div class="small text-muted">ID: <?php echo htmlspecialchars($storeId); ?></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="fw-semibold text-dark"><?php echo htmlspecialchars(isset($store['city']) ? $store['city'] : 'Islamabad'); ?></div>
                                        <div class="small text-muted"><?php echo htmlspecialchars(isset($store['address']) ? $store['address'] : ''); ?></div>
                                    </td>
                                    <td class="small text-muted font-monospace">
                                        <?php if (isset($store['lat']) && isset($store['lng'])): ?>
                                            <?php echo number_format((float)$store['lat'], 4); ?>, <?php echo number_format((float)$store['lng'], 4); ?>
                                        <?php else: ?>
                                            N/A
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-light text-dark border">
                                            <i class="fa-solid fa-users text-primary me-1"></i>
                                            <?php echo isset($staffCounts[$storeId]) ? $staffCounts[$storeId] : 0; ?> Staff
                                        </span>
                                    </td>
                                    <td>
                                        <?php if (!empty($store['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="small text-muted">
                                        <?php echo isset($store['createdAt']) ? date('M d, Y', $store['createdAt'] / 1000) : '-'; ?>
                                    </td>
                                    <td class="text-end pe-4">
                                        <div class="btn-group">
                                            <a href="<?php echo base_url('admin/store_details/' . $storeId); ?>" class="btn btn-sm btn-outline-primary" title="View Details">
                                                <i class="fa-solid fa-eye"></i>
                                            </a>
                                            <a href="<?php echo base_url('admin/store_delete/' . $storeId); ?>" onclick="return confirm('Are you sure you want to remove this store?');" class="btn btn-sm btn-outline-danger" title="Delete Store">
                                                <i class="fa-solid fa-trash-can"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
