<?php defined('BASEPATH') or exit('No direct script access allowed');

$tabs = isset($tabs) && is_array($tabs) ? $tabs : array();
$label = isset($label) ? $label : lang('tabs');
?>

<?php if (!empty($tabs)) { ?>
	<nav class="erp-tabs" aria-label="<?= html_escape($label); ?>">
		<?php foreach ($tabs as $tab) {
			$tab_label = isset($tab['label']) ? $tab['label'] : '';
			$tab_href = isset($tab['href']) ? $tab['href'] : '';
			$tab_count = isset($tab['count']) ? $tab['count'] : '';
			$tab_active = !empty($tab['active']);
			$tab_attrs = isset($tab['attrs']) && is_array($tab['attrs']) ? $tab['attrs'] : array();
			$tab_class = 'erp-tab' . ($tab_active ? ' erp-tab--active' : '');
			$tag = $tab_href !== '' ? 'a' : 'button';
			$attr_parts = array();
			foreach ($tab_attrs as $attr_key => $attr_value) {
				if ($attr_value === null || $attr_value === false) {
					continue;
				}
				if ($attr_value === true) {
					$attr_parts[] = html_escape($attr_key);
					continue;
				}
				$attr_parts[] = html_escape($attr_key) . '="' . html_escape($attr_value) . '"';
			}
			$attr_string = !empty($attr_parts) ? ' ' . implode(' ', $attr_parts) : '';
		?>
			<<?= $tag; ?>
				<?= $tab_href !== '' ? 'href="' . html_escape($tab_href) . '"' : 'type="button"'; ?><?= $attr_string; ?>
				class="<?= $tab_class; ?>">
				<span><?= html_escape($tab_label); ?></span>
				<?php if ($tab_count !== '') { ?>
					<span class="erp-tab-count"><?= html_escape($tab_count); ?></span>
				<?php } ?>
			</<?= $tag; ?>>
		<?php } ?>
	</nav>
<?php } ?>
