<?php
if (empty($customerContainers) || !is_array($customerContainers)) return;

$groupedContainers = [];

/* group identical containers */
foreach ($customerContainers as $cc) {
    $key = implode('|', [
        $cc['cc_type'],
        $cc['cc_owner_type'],
        $cc['cc_has_lock'],
        $cc['cc_has_wheel'],
        $cc['cc_has_woodframe'],
    ]);

    if (!isset($groupedContainers[$key])) {
        $groupedContainers[$key] = [
            'cc'  => $cc,
            'cnt' => 0
        ];
    }
    $groupedContainers[$key]['cnt']++;
}
?>

<div class="container-chips inline-chips">
<?php foreach ($groupedContainers as $g): ?>
  <?php
    $cc  = $g['cc'];
    $cnt = $g['cnt'];

    $type = strtoupper($cc['cc_type']);

    if (strpos($type, 'D') !== false) {
        $icon = 'bi-database-fill';
    } elseif (strpos($type, 'B') !== false && strpos($type, 'BUCKET') === false) {
        $icon = 'bi-archive-fill';
    } else {
        $icon = 'bi-box2';
    }

    $isRestaurantOwned = ($cc['cc_owner_type'] === 'O');
    $chipClass = $isRestaurantOwned ? 'chip chip-restaurant' : 'chip chip-company';

    $optIcons = '';
    if ($cc['cc_has_lock'] === 'Y') $optIcons .= ' <i class="bi bi-lock-fill"></i>';
    if ($cc['cc_has_wheel'] === 'Y') $optIcons .= ' <i class="bi bi-gear-fill"></i>';
    if ($cc['cc_has_woodframe'] === 'Y') $optIcons .= ' <i class="bi bi-bounding-box-circles"></i>';
  ?>

  <span class="<?=$chipClass?>"
        title="<?=$isRestaurantOwned ? 'Restaurant-owned (Do not remove)' : 'Customer container'?>">
    <i class="bi <?=$icon?>"></i>
    <?=$cc['cc_type']?>
    <?=$optIcons?>

    <?php if ($isRestaurantOwned): ?>
      <span class="chip-flag">O</span>
    <?php endif; ?>

    <?php if ($cnt > 1): ?>
      <span class="chip-count">x<?=$cnt?></span>
    <?php endif; ?>
  </span>
<?php endforeach; ?>
</div>
