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

$label = isset($label) ? $label : '';
$name = isset($name) ? $name : '';
$type = isset($type) ? $type : 'text';
$value = isset($value) ? $value : '';
$options = isset($options) && is_array($options) ? $options : array();
$placeholder = isset($placeholder) ? $placeholder : '';
$help = isset($help) ? $help : '';
$id = isset($id) ? $id : ($name !== '' ? $name : uniqid('erp_field_'));
$class = isset($class) ? $class : '';
$control_class = isset($control_class) ? $control_class : '';
$attrs = isset($attrs) && is_array($attrs) ? $attrs : array();
$checked = !empty($checked);
$rows = isset($rows) ? (int) $rows : 3;
$control_html = isset($control_html) ? $control_html : '';

$wrapper_class = 'erp-field';
if ($type === 'checkbox' || $type === 'switch') {
	$wrapper_class .= ' erp-field--toggle';
}
if ($class !== '') {
	$wrapper_class .= ' ' . $class;
}

$attr_parts = array();
foreach ($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) : '';

if ($control_html === '') {
	if ($type === 'textarea') {
		$control_html = '<textarea name="' . html_escape($name) . '" id="' . html_escape($id) . '" rows="' . max(2, $rows) . '" class="erp-control erp-textarea' . ($control_class ? ' ' . html_escape($control_class) : '') . '" placeholder="' . html_escape($placeholder) . '"' . $attr_string . '>' . html_escape($value) . '</textarea>';
	} elseif ($type === 'select') {
		$control_html = '<select name="' . html_escape($name) . '" id="' . html_escape($id) . '" class="erp-control erp-select' . ($control_class ? ' ' . html_escape($control_class) : '') . '"' . $attr_string . '>';
		foreach ($options as $option_value => $option_label) {
			$selected = (string) $option_value === (string) $value ? ' selected' : '';
			$control_html .= '<option value="' . html_escape($option_value) . '"' . $selected . '>' . html_escape($option_label) . '</option>';
		}
		$control_html .= '</select>';
	} elseif ($type === 'checkbox' || $type === 'switch') {
		$toggle_class = $type === 'switch' ? ' erp-switch-input' : ' erp-check-input';
		$toggle_ui_class = $type === 'switch' ? 'erp-switch-ui' : 'erp-check-ui';
		$control_html = '<label class="erp-toggle"><input type="checkbox" name="' . html_escape($name) . '" id="' . html_escape($id) . '" class="erp-toggle-input' . $toggle_class . ($control_class ? ' ' . html_escape($control_class) : '') . '"' . ($checked ? ' checked' : '') . $attr_string . '><span class="' . $toggle_ui_class . '"></span><span class="erp-toggle-copy"><span class="erp-label">' . html_escape($label) . '</span>';
		if ($help !== '') {
			$control_html .= '<span class="erp-help">' . html_escape($help) . '</span>';
		}
		$control_html .= '</span></label>';
	} else {
		$input_type = $type === 'date' ? 'date' : $type;
		$control_html = '<input type="' . html_escape($input_type) . '" name="' . html_escape($name) . '" id="' . html_escape($id) . '" value="' . html_escape($value) . '" class="erp-control' . ($type === 'date' ? ' erp-date-control' : '') . ($control_class ? ' ' . html_escape($control_class) : '') . '" placeholder="' . html_escape($placeholder) . '"' . $attr_string . '>';
	}
}
?>

<div class="<?= $wrapper_class; ?>">
	<?php if ($type !== 'checkbox' && $type !== 'switch' && $label !== '') { ?>
		<label for="<?= html_escape($id); ?>" class="erp-label"><?= html_escape($label); ?></label>
	<?php } ?>
	<?= $control_html; ?>
	<?php if ($type !== 'checkbox' && $type !== 'switch' && $help !== '') { ?>
		<p class="erp-help"><?= html_escape($help); ?></p>
	<?php } ?>
</div>
