<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * Admin page to manage equipment types (Hutschienen-Komponenten)
 */

$res = 0;
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res) die("Include of main fails");

require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
dol_include_once('/kundenkarte/lib/kundenkarte.lib.php');
dol_include_once('/kundenkarte/class/equipmenttype.class.php');

$langs->loadLangs(array('admin', 'kundenkarte@kundenkarte', 'products'));

// Security check
if (!$user->admin && !$user->hasRight('kundenkarte', 'admin')) {
	accessforbidden();
}

$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
$typeId = GETPOSTINT('typeid');

// System filter - save in session for persistence
$sessionKey = 'kundenkarte_equipment_types_system_filter';
if (GETPOSTISSET('system')) {
	$systemFilter = GETPOSTINT('system');
	$_SESSION[$sessionKey] = $systemFilter;
} elseif (isset($_SESSION[$sessionKey])) {
	$systemFilter = $_SESSION[$sessionKey];
} else {
	$systemFilter = 0;
}

$form = new Form($db);
$equipmentType = new EquipmentType($db);

// Load systems
$systems = array();
$sql = "SELECT rowid, code, label FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system WHERE active = 1 ORDER BY position ASC";
$resql = $db->query($sql);
if ($resql) {
	while ($obj = $db->fetch_object($resql)) {
		$systems[$obj->rowid] = $obj;
	}
}

// Load products for dropdown
$products = array();
$sql = "SELECT rowid, ref, label FROM ".MAIN_DB_PREFIX."product WHERE tosell = 1 ORDER BY ref ASC";
$resql = $db->query($sql);
if ($resql) {
	while ($obj = $db->fetch_object($resql)) {
		$products[$obj->rowid] = $obj;
	}
}

/*
 * Actions
 */

if ($action == 'add') {
	$equipmentType->ref = GETPOST('ref', 'aZ09');
	$equipmentType->label = GETPOST('label', 'alphanohtml');
	$equipmentType->label_short = GETPOST('label_short', 'alphanohtml');
	$equipmentType->description = GETPOST('description', 'restricthtml');
	$equipmentType->fk_system = GETPOSTINT('fk_system');
	$equipmentType->width_te = GETPOSTINT('width_te');
	$equipmentType->color = GETPOST('color', 'alphanohtml');
	$equipmentType->fk_product = GETPOSTINT('fk_product');
	$equipmentType->terminals_config = GETPOST('terminals_config', 'nohtml');
	$equipmentType->flow_direction = GETPOST('flow_direction', 'alphanohtml');
	$equipmentType->terminal_position = GETPOST('terminal_position', 'alphanohtml') ?: 'both';
	$equipmentType->picto = GETPOST('picto', 'alphanohtml');
	$equipmentType->position = GETPOSTINT('position');
	$equipmentType->active = 1;

	if (empty($equipmentType->ref) || empty($equipmentType->label) || empty($equipmentType->fk_system)) {
		setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
		$action = 'create';
	} else {
		$result = $equipmentType->create($user);
		if ($result > 0) {
			// Create default fields for common equipment
			$defaultFields = array(
				array('code' => 'characteristic', 'label' => 'Charakteristik', 'type' => 'select', 'options' => 'B|C|D|K|Z', 'position' => 10, 'show_in_hover' => 1, 'show_on_block' => 1),
				array('code' => 'ampere', 'label' => 'Nennstrom (A)', 'type' => 'select', 'options' => '6|10|13|16|20|25|32|40|50|63', 'position' => 20, 'show_in_hover' => 1, 'show_on_block' => 1),
				array('code' => 'pole', 'label' => 'Polzahl', 'type' => 'select', 'options' => '1|2|3|3+N', 'position' => 30, 'show_in_hover' => 1, 'show_on_block' => 0),
				array('code' => 'circuit', 'label' => 'Stromkreis', 'type' => 'text', 'options' => '', 'position' => 40, 'show_in_hover' => 1, 'show_on_block' => 0),
			);

			foreach ($defaultFields as $field) {
				$sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field";
				$sql .= " (fk_equipment_type, field_code, field_label, field_type, field_options, show_in_hover, show_on_block, required, position, active)";
				$sql .= " VALUES (".((int) $equipmentType->id).", '".$db->escape($field['code'])."', '".$db->escape($field['label'])."',";
				$sql .= " '".$db->escape($field['type'])."', '".$db->escape($field['options'])."', ".((int) $field['show_in_hover']).", ".((int) $field['show_on_block']).", 0, ".((int) $field['position']).", 1)";
				$db->query($sql);
			}

			setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
			header('Location: '.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$equipmentType->id.'&system='.$equipmentType->fk_system);
			exit;
		} else {
			setEventMessages($equipmentType->error, $equipmentType->errors, 'errors');
			$action = 'create';
		}
	}
}

if ($action == 'update') {
	$equipmentType->fetch($typeId);
	$equipmentType->ref = GETPOST('ref', 'aZ09');
	$equipmentType->label = GETPOST('label', 'alphanohtml');
	$equipmentType->label_short = GETPOST('label_short', 'alphanohtml');
	$equipmentType->description = GETPOST('description', 'restricthtml');
	$equipmentType->fk_system = GETPOSTINT('fk_system');
	$equipmentType->width_te = GETPOSTINT('width_te');
	$equipmentType->color = GETPOST('color', 'alphanohtml');
	$equipmentType->fk_product = GETPOSTINT('fk_product');
	$equipmentType->terminals_config = GETPOST('terminals_config', 'nohtml');
	$equipmentType->flow_direction = GETPOST('flow_direction', 'alphanohtml');
	$equipmentType->terminal_position = GETPOST('terminal_position', 'alphanohtml') ?: 'both';
	$equipmentType->picto = GETPOST('picto', 'alphanohtml');
	$equipmentType->position = GETPOSTINT('position');

	$result = $equipmentType->update($user);
	if ($result > 0) {
		setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
		header('Location: '.$_SERVER['PHP_SELF'].'?system='.$equipmentType->fk_system);
		exit;
	} else {
		setEventMessages($equipmentType->error, $equipmentType->errors, 'errors');
		$action = 'edit';
	}
}

if ($action == 'confirm_delete' && $confirm == 'yes') {
	$equipmentType->fetch($typeId);
	$result = $equipmentType->delete($user);
	if ($result > 0) {
		setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
	} else {
		setEventMessages($equipmentType->error, $equipmentType->errors, 'errors');
	}
	$action = '';
}

if ($action == 'activate') {
	$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_equipment_type SET active = 1 WHERE rowid = ".((int) $typeId);
	$db->query($sql);
	$action = '';
}

if ($action == 'deactivate') {
	$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_equipment_type SET active = 0 WHERE rowid = ".((int) $typeId);
	$db->query($sql);
	$action = '';
}

// Copy type with all fields
if ($action == 'copy' && $typeId > 0) {
	$sourceType = new EquipmentType($db);
	if ($sourceType->fetch($typeId) > 0) {
		$newType = new EquipmentType($db);
		$newType->ref = $sourceType->ref.'_COPY';
		$newType->label = $sourceType->label.' (Kopie)';
		$newType->label_short = $sourceType->label_short;
		$newType->description = $sourceType->description;
		$newType->fk_system = $sourceType->fk_system;
		$newType->width_te = $sourceType->width_te;
		$newType->color = $sourceType->color;
		$newType->fk_product = $sourceType->fk_product;
		$newType->picto = $sourceType->picto;
		$newType->position = $sourceType->position + 1;
		$newType->active = 1;

		$result = $newType->create($user);
		if ($result > 0) {
			// Copy all fields from source type
			$sourceFields = $sourceType->fetchFields(0);
			foreach ($sourceFields as $field) {
				$sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field";
				$sql .= " (fk_equipment_type, field_code, field_label, field_type, field_options, show_in_hover, show_on_block, required, position, active)";
				$sql .= " VALUES (".((int) $newType->id).", '".$db->escape($field->field_code)."', '".$db->escape($field->field_label)."',";
				$sql .= " '".$db->escape($field->field_type)."', '".$db->escape($field->field_options)."', ".((int) $field->show_in_hover).",";
				$sql .= " ".((int) $field->show_on_block).", ".((int) $field->required).", ".((int) $field->position).", ".((int) $field->active).")";
				$db->query($sql);
			}

			setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
			header('Location: '.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$newType->id.'&system='.$newType->fk_system);
			exit;
		} else {
			setEventMessages($newType->error, $newType->errors, 'errors');
		}
	}
	$action = '';
}

// Field actions
$fieldId = GETPOSTINT('fieldid');

if ($action == 'add_field') {
	$fieldCode = GETPOST('field_code', 'aZ09');
	$fieldLabel = GETPOST('field_label', 'alphanohtml');
	$fieldType = GETPOST('field_type', 'aZ09');
	$fieldOptions = GETPOST('field_options', 'nohtml');
	$showInHover = GETPOSTINT('show_in_hover');
	$showOnBlock = GETPOSTINT('show_on_block');
	$isRequired = GETPOSTINT('is_required');
	$fieldPosition = GETPOSTINT('field_position');

	if (empty($fieldCode) || empty($fieldLabel) || empty($fieldType)) {
		setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
	} else {
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field";
		$sql .= " (fk_equipment_type, field_code, field_label, field_type, field_options, show_in_hover, show_on_block, required, position, active)";
		$sql .= " VALUES (".((int) $typeId).", '".$db->escape($fieldCode)."', '".$db->escape($fieldLabel)."',";
		$sql .= " '".$db->escape($fieldType)."', '".$db->escape($fieldOptions)."',";
		$sql .= " ".((int) $showInHover).", ".((int) $showOnBlock).", ".((int) $isRequired).", ".((int) $fieldPosition).", 1)";

		if ($db->query($sql)) {
			setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
		} else {
			setEventMessages($db->lasterror(), null, 'errors');
		}
	}
	$action = 'edit';
}

if ($action == 'update_field') {
	$fieldCode = GETPOST('field_code', 'aZ09');
	$fieldLabel = GETPOST('field_label', 'alphanohtml');
	$fieldType = GETPOST('field_type', 'aZ09');
	$fieldOptions = GETPOST('field_options', 'nohtml');
	$showInHover = GETPOSTINT('show_in_hover');
	$showOnBlock = GETPOSTINT('show_on_block');
	$isRequired = GETPOSTINT('is_required');
	$fieldPosition = GETPOSTINT('field_position');

	$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field SET";
	$sql .= " field_code = '".$db->escape($fieldCode)."',";
	$sql .= " field_label = '".$db->escape($fieldLabel)."',";
	$sql .= " field_type = '".$db->escape($fieldType)."',";
	$sql .= " field_options = '".$db->escape($fieldOptions)."',";
	$sql .= " show_in_hover = ".((int) $showInHover).",";
	$sql .= " show_on_block = ".((int) $showOnBlock).",";
	$sql .= " required = ".((int) $isRequired).",";
	$sql .= " position = ".((int) $fieldPosition);
	$sql .= " WHERE rowid = ".((int) $fieldId);

	if ($db->query($sql)) {
		setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
	} else {
		setEventMessages($db->lasterror(), null, 'errors');
	}
	$action = 'edit';
}

if ($action == 'confirm_delete_field' && $confirm == 'yes') {
	$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field WHERE rowid = ".((int) $fieldId);
	if ($db->query($sql)) {
		setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
	} else {
		setEventMessages($db->lasterror(), null, 'errors');
	}
	header('Location: '.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$typeId.'&system='.$systemFilter);
	exit;
}

if ($action == 'activate_field') {
	$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field SET active = 1 WHERE rowid = ".((int) $fieldId);
	$db->query($sql);
	$action = 'edit';
}

if ($action == 'deactivate_field') {
	$sql = "UPDATE ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field SET active = 0 WHERE rowid = ".((int) $fieldId);
	$db->query($sql);
	$action = 'edit';
}

/*
 * View
 */

$title = $langs->trans('EquipmentTypes');

$morejs = array('/kundenkarte/js/kundenkarte.js?v='.time());
$morecss = array('/kundenkarte/css/kundenkarte.css?v='.time());

llxHeader('', $title, '', '', 0, 0, $morejs, $morecss);

$head = kundenkarteAdminPrepareHead();
print dol_get_fiche_head($head, 'equipment_types', $langs->trans('ModuleKundenKarteName'), -1, 'fa-file');

// Confirmation for type deletion
if ($action == 'delete') {
	print $form->formconfirm(
		$_SERVER['PHP_SELF'].'?typeid='.$typeId.'&system='.$systemFilter,
		$langs->trans('Delete'),
		$langs->trans('ConfirmDeleteType'),
		'confirm_delete',
		'',
		'yes',
		1
	);
}

// Confirmation for field deletion
if ($action == 'delete_field') {
	print $form->formconfirm(
		$_SERVER['PHP_SELF'].'?typeid='.$typeId.'&fieldid='.$fieldId.'&system='.$systemFilter,
		$langs->trans('Delete'),
		$langs->trans('ConfirmDeleteField'),
		'confirm_delete_field',
		'',
		'yes',
		1
	);
	$action = 'edit';
}

// Add/Edit form
if (in_array($action, array('create', 'edit'))) {
	if ($action == 'edit' && $typeId > 0) {
		$equipmentType->fetch($typeId);
	}

	print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
	print '<input type="hidden" name="token" value="'.newToken().'">';
	print '<input type="hidden" name="action" value="'.($action == 'edit' ? 'update' : 'add').'">';
	if ($action == 'edit') {
		print '<input type="hidden" name="typeid" value="'.$typeId.'">';
	}

	print '<table class="border centpercent">';

	// System
	print '<tr><td class="titlefield fieldrequired">'.$langs->trans('System').'</td>';
	print '<td><select name="fk_system" class="flat minwidth200" required>';
	print '<option value="">'.$langs->trans('SelectSystem').'</option>';
	foreach ($systems as $sys) {
		$sel = ($equipmentType->fk_system == $sys->rowid) ? ' selected' : '';
		print '<option value="'.$sys->rowid.'"'.$sel.'>'.dol_escape_htmltag($sys->label).'</option>';
	}
	print '</select></td></tr>';

	// Reference
	print '<tr><td class="fieldrequired">'.$langs->trans('TypeRef').'</td>';
	print '<td><input type="text" name="ref" class="flat minwidth200" value="'.dol_escape_htmltag($equipmentType->ref).'" maxlength="64" required>';
	print ' <span class="opacitymedium">(UPPERCASE, no spaces)</span></td></tr>';

	// Label
	print '<tr><td class="fieldrequired">'.$langs->trans('TypeLabel').'</td>';
	print '<td><input type="text" name="label" class="flat minwidth300" value="'.dol_escape_htmltag($equipmentType->label).'" required></td></tr>';

	// Short label
	print '<tr><td>'.$langs->trans('TypeShortLabel').'</td>';
	print '<td><input type="text" name="label_short" class="flat" value="'.dol_escape_htmltag($equipmentType->label_short).'" maxlength="32">';
	print ' <span class="opacitymedium">(z.B. LS, FI, FI/LS)</span></td></tr>';

	// Description
	print '<tr><td>'.$langs->trans('Description').'</td>';
	print '<td><textarea name="description" class="flat minwidth300" rows="2">'.dol_escape_htmltag($equipmentType->description).'</textarea></td></tr>';

	// Width in TE
	print '<tr><td class="fieldrequired">'.$langs->trans('WidthTE').'</td>';
	print '<td><input type="number" name="width_te" class="flat" value="'.($equipmentType->width_te ?: 1).'" min="1" max="12" required>';
	print ' <span class="opacitymedium">'.$langs->trans('WidthTEHelp').'</span></td></tr>';

	// Color
	print '<tr><td>'.$langs->trans('Color').'</td>';
	print '<td><input type="color" name="color" value="'.($equipmentType->color ?: '#3498db').'" style="width:60px;height:30px;padding:0;border:1px solid #ccc;">';
	print ' <span class="opacitymedium">'.$langs->trans('ColorForSVG').'</span></td></tr>';

	// Product
	print '<tr><td>'.$langs->trans('LinkedProduct').'</td>';
	print '<td><select name="fk_product" class="flat minwidth300">';
	print '<option value="0">'.$langs->trans('None').'</option>';
	foreach ($products as $prod) {
		$sel = ($equipmentType->fk_product == $prod->rowid) ? ' selected' : '';
		print '<option value="'.$prod->rowid.'"'.$sel.'>'.dol_escape_htmltag($prod->ref.' - '.$prod->label).'</option>';
	}
	print '</select></td></tr>';

	// FontAwesome Icon (fallback)
	print '<tr><td>'.$langs->trans('SystemPicto').'</td>';
	print '<td><div class="kundenkarte-icon-picker-wrapper">';
	print '<span class="kundenkarte-icon-preview">';
	if ($equipmentType->picto) {
		print kundenkarte_render_icon($equipmentType->picto);
	}
	print '</span>';
	print '<input type="text" name="picto" class="flat minwidth200" value="'.dol_escape_htmltag($equipmentType->picto).'" placeholder="fa-bolt">';
	print '<button type="button" class="kundenkarte-icon-picker-btn" data-input="picto"><i class="fa fa-th"></i> '.$langs->trans('SelectIcon').'</button>';
	print '</div>';
	print '<span class="opacitymedium small">Fallback-Icon wenn kein Schaltzeichen hochgeladen</span>';
	print '</td></tr>';

	// Schaltzeichen Upload (SVG/PNG)
	print '<tr><td>'.$langs->trans('SchematicSymbol').'</td>';
	print '<td>';
	print '<div id="icon-upload-area" style="display:flex;align-items:center;gap:15px;">';

	// Preview area
	print '<div id="icon-preview" style="width:80px;height:80px;border:2px dashed #ccc;border-radius:8px;display:flex;align-items:center;justify-content:center;background:#f8f8f8;">';
	if ($action == 'edit' && $equipmentType->icon_file) {
		$iconUrl = DOL_URL_ROOT.'/document.php?modulepart=kundenkarte&file=equipment_icons/'.urlencode($equipmentType->icon_file);
		print '<img src="'.$iconUrl.'" style="max-width:70px;max-height:70px;" alt="Icon">';
	} else {
		print '<span style="color:#999;font-size:11px;text-align:center;">Kein<br>Symbol</span>';
	}
	print '</div>';

	// Upload controls
	print '<div>';
	print '<input type="file" id="icon-file-input" accept=".svg,.png" style="display:none;">';
	print '<button type="button" id="icon-upload-btn" class="button" onclick="document.getElementById(\'icon-file-input\').click();">';
	print '<i class="fa fa-upload"></i> SVG/PNG hochladen</button>';
	if ($action == 'edit' && $equipmentType->icon_file) {
		print ' <button type="button" id="icon-delete-btn" class="button" style="background:#e74c3c;border-color:#c0392b;color:#fff;">';
		print '<i class="fa fa-trash"></i> Löschen</button>';
	}
	print '<div class="opacitymedium small" style="margin-top:5px;">Normgerechte Symbole nach IEC 60617 / DIN EN 60617</div>';
	print '</div>';

	print '</div>';
	print '</td></tr>';

	// Block Image Upload (for SchematicEditor display)
	print '<tr><td>'.$langs->trans('BlockImage').'</td>';
	print '<td>';
	print '<div id="block-image-upload-area" style="display:flex;align-items:center;gap:15px;">';

	// Preview area
	print '<div id="block-image-preview" style="width:80px;height:80px;border:2px dashed #ccc;border-radius:8px;display:flex;align-items:center;justify-content:center;background:#f8f8f8;">';
	if ($action == 'edit' && $equipmentType->block_image) {
		$blockImageUrl = DOL_URL_ROOT.'/document.php?modulepart=kundenkarte&file=block_images/'.urlencode($equipmentType->block_image);
		print '<img src="'.$blockImageUrl.'" style="max-width:70px;max-height:70px;" alt="Block Image">';
	} else {
		print '<span style="color:#999;font-size:11px;text-align:center;">Kein<br>Bild</span>';
	}
	print '</div>';

	// Upload controls
	print '<div>';
	print '<input type="file" id="block-image-file-input" accept=".svg,.png,.jpg,.jpeg,.gif,.webp" style="display:none;">';
	print '<button type="button" id="block-image-upload-btn" class="button" onclick="document.getElementById(\'block-image-file-input\').click();">';
	print '<i class="fa fa-upload"></i> Bild hochladen</button>';
	if ($action == 'edit' && $equipmentType->block_image) {
		print ' <button type="button" id="block-image-delete-btn" class="button" style="background:#e74c3c;border-color:#c0392b;color:#fff;">';
		print '<i class="fa fa-trash"></i> Löschen</button>';
	}

	// Dropdown to select existing images
	$blockImagesDir = DOL_DATA_ROOT.'/kundenkarte/block_images/';
	$existingImages = array();
	if (is_dir($blockImagesDir)) {
		$files = scandir($blockImagesDir);
		foreach ($files as $file) {
			if ($file != '.' && $file != '..' && preg_match('/\.(png|jpg|jpeg|gif|svg|webp)$/i', $file)) {
				$existingImages[] = $file;
			}
		}
		sort($existingImages);
	}

	if (!empty($existingImages)) {
		print '<div style="margin-top:10px;">';
		print '<select id="block-image-select" class="flat minwidth200">';
		print '<option value="">-- Vorhandenes Bild wählen --</option>';
		foreach ($existingImages as $img) {
			$sel = ($equipmentType->block_image == $img) ? ' selected' : '';
			print '<option value="'.dol_escape_htmltag($img).'"'.$sel.'>'.dol_escape_htmltag($img).'</option>';
		}
		print '</select>';
		print ' <button type="button" id="block-image-select-btn" class="button">';
		print '<i class="fa fa-check"></i> Übernehmen</button>';
		print '</div>';
	}

	print '<div class="opacitymedium small" style="margin-top:5px;">Bild wird im SchematicEditor als Block-Hintergrund angezeigt</div>';
	print '</div>';

	print '</div>';
	print '</td></tr>';

	// Position
	print '<tr><td>'.$langs->trans('Position').'</td>';
	print '<td><input type="number" name="position" class="flat" value="'.($equipmentType->position ?: 0).'" min="0"></td></tr>';

	// Terminal configuration
	print '<tr><td>'.$langs->trans('TerminalConfig').'</td>';
	print '<td>';
	print '<div style="margin-bottom:10px;">';
	print '<label style="margin-right:15px;"><strong>Vorlagen:</strong></label>';
	print '<button type="button" class="button small" onclick="setTerminals(\'1P\')">1-polig</button> ';
	print '<button type="button" class="button small" onclick="setTerminals(\'2P\')">2-polig (L+N)</button> ';
	print '<button type="button" class="button small" onclick="setTerminals(\'3P\')">3-polig</button> ';
	print '<button type="button" class="button small" onclick="setTerminals(\'4P\')">4-polig (3P+N)</button>';
	print '</div>';
	print '<textarea name="terminals_config" id="terminals_config" class="flat" rows="4" style="width:100%;font-family:monospace;font-size:11px;" placeholder=\'{"terminals":[{"id":"t1","label":"L","pos":"top"},{"id":"t2","label":"L","pos":"bottom"}]}\'>';
	print dol_escape_htmltag($equipmentType->terminals_config);
	print '</textarea>';
	print '<div class="opacitymedium small" style="margin-top:5px;">';
	print 'JSON-Format: <code>{"terminals":[{"id":"t1","label":"L","pos":"top"},...]}</code> - pos: "top" oder "bottom"';
	print '</div>';
	print '</td></tr>';

	// Terminal Position
	print '<tr><td>Anschlusspunkt-Position</td>';
	print '<td>';
	print '<select name="terminal_position" class="flat minwidth200">';
	print '<option value="both"'.($equipmentType->terminal_position == 'both' ? ' selected' : '').'>Beidseitig (oben + unten)</option>';
	print '<option value="top_only"'.($equipmentType->terminal_position == 'top_only' ? ' selected' : '').'>Nur oben</option>';
	print '<option value="bottom_only"'.($equipmentType->terminal_position == 'bottom_only' ? ' selected' : '').'>Nur unten</option>';
	print '</select>';
	print '<div class="opacitymedium small" style="margin-top:5px;">Wo sollen die Anschlusspunkte angezeigt werden?</div>';
	print '</td></tr>';

	// Flow Direction
	print '<tr><td>Richtung (Pfeil)</td>';
	print '<td>';
	print '<select name="flow_direction" class="flat minwidth200">';
	print '<option value=""'.(!$equipmentType->flow_direction ? ' selected' : '').'>Keine</option>';
	print '<option value="top_to_bottom"'.($equipmentType->flow_direction == 'top_to_bottom' ? ' selected' : '').'>Von oben nach unten ↓</option>';
	print '<option value="bottom_to_top"'.($equipmentType->flow_direction == 'bottom_to_top' ? ' selected' : '').'>Von unten nach oben ↑</option>';
	print '</select>';
	print '<div class="opacitymedium small" style="margin-top:5px;">Zeigt einen Richtungspfeil im Block an (z.B. für Typ B FI-Schalter)</div>';
	print '</td></tr>';

	print '</table>';

	// JavaScript for terminal presets and icon upload
	$typeIdJs = $action == 'edit' ? $typeId : 0;
	print '<script>
	function setTerminals(type) {
		var configs = {
			"1P": {"terminals":[{"id":"t1","label":"●","pos":"top"},{"id":"t2","label":"●","pos":"bottom"}]},
			"2P": {"terminals":[{"id":"t1","label":"L","pos":"top"},{"id":"t2","label":"N","pos":"top"},{"id":"t3","label":"L","pos":"bottom"},{"id":"t4","label":"N","pos":"bottom"}]},
			"3P": {"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"L1","pos":"bottom"},{"id":"t5","label":"L2","pos":"bottom"},{"id":"t6","label":"L3","pos":"bottom"}]},
			"4P": {"terminals":[{"id":"t1","label":"L1","pos":"top"},{"id":"t2","label":"L2","pos":"top"},{"id":"t3","label":"L3","pos":"top"},{"id":"t4","label":"N","pos":"top"},{"id":"t5","label":"L1","pos":"bottom"},{"id":"t6","label":"L2","pos":"bottom"},{"id":"t7","label":"L3","pos":"bottom"},{"id":"t8","label":"N","pos":"bottom"}]}
		};
		if (configs[type]) {
			document.getElementById("terminals_config").value = JSON.stringify(configs[type], null, 2);
		}
	}

	// Icon upload handling
	var typeId = '.$typeIdJs.';

	document.getElementById("icon-file-input").addEventListener("change", function(e) {
		if (!typeId || typeId == 0) {
			alert("Bitte speichern Sie zuerst den Equipment-Typ bevor Sie ein Symbol hochladen.");
			return;
		}

		var file = e.target.files[0];
		if (!file) return;

		var formData = new FormData();
		formData.append("action", "upload");
		formData.append("type_id", typeId);
		formData.append("icon_file", file);
		formData.append("token", "'.newToken().'");

		fetch("'.DOL_URL_ROOT.'/custom/kundenkarte/ajax/equipment_type_icon.php", {
			method: "POST",
			body: formData
		})
		.then(function(response) { return response.json(); })
		.then(function(data) {
			if (data.success) {
				var preview = document.getElementById("icon-preview");
				preview.innerHTML = \'<img src="\' + data.icon_url + \'?t=\' + Date.now() + \'" style="max-width:70px;max-height:70px;" alt="Icon">\';

				// Add delete button if not present
				if (!document.getElementById("icon-delete-btn")) {
					var btn = document.createElement("button");
					btn.type = "button";
					btn.id = "icon-delete-btn";
					btn.className = "button";
					btn.style.cssText = "background:#e74c3c;border-color:#c0392b;color:#fff;margin-left:5px;";
					btn.innerHTML = \'<i class="fa fa-trash"></i> Löschen\';
					btn.onclick = deleteIcon;
					document.getElementById("icon-upload-btn").after(btn);
				}
			} else {
				alert("Fehler: " + data.error);
			}
		})
		.catch(function(err) {
			alert("Upload fehlgeschlagen: " + err);
		});

		e.target.value = "";
	});

	function deleteIcon() {
		if (!confirm("Symbol wirklich löschen?")) return;

		fetch("'.DOL_URL_ROOT.'/custom/kundenkarte/ajax/equipment_type_icon.php", {
			method: "POST",
			headers: {"Content-Type": "application/x-www-form-urlencoded"},
			body: "action=delete&type_id=" + typeId + "&token='.newToken().'"
		})
		.then(function(response) { return response.json(); })
		.then(function(data) {
			if (data.success) {
				var preview = document.getElementById("icon-preview");
				preview.innerHTML = \'<span style="color:#999;font-size:11px;text-align:center;">Kein<br>Symbol</span>\';
				var delBtn = document.getElementById("icon-delete-btn");
				if (delBtn) delBtn.remove();
			} else {
				alert("Fehler: " + data.error);
			}
		});
	}

	var delBtn = document.getElementById("icon-delete-btn");
	if (delBtn) delBtn.onclick = deleteIcon;

	// Block Image upload handling
	document.getElementById("block-image-file-input").addEventListener("change", function(e) {
		if (!typeId || typeId == 0) {
			alert("Bitte speichern Sie zuerst den Equipment-Typ bevor Sie ein Bild hochladen.");
			return;
		}

		var file = e.target.files[0];
		if (!file) return;

		var formData = new FormData();
		formData.append("action", "upload");
		formData.append("type_id", typeId);
		formData.append("block_image", file);
		formData.append("token", "'.newToken().'");

		fetch("'.DOL_URL_ROOT.'/custom/kundenkarte/ajax/equipment_type_block_image.php", {
			method: "POST",
			body: formData
		})
		.then(function(response) { return response.json(); })
		.then(function(data) {
			if (data.success) {
				var preview = document.getElementById("block-image-preview");
				preview.innerHTML = \'<img src="\' + data.block_image_url + \'&t=\' + Date.now() + \'" style="max-width:70px;max-height:70px;" alt="Block Image">\';

				// Add delete button if not present
				if (!document.getElementById("block-image-delete-btn")) {
					var btn = document.createElement("button");
					btn.type = "button";
					btn.id = "block-image-delete-btn";
					btn.className = "button";
					btn.style.cssText = "background:#e74c3c;border-color:#c0392b;color:#fff;margin-left:5px;";
					btn.innerHTML = \'<i class="fa fa-trash"></i> Löschen\';
					btn.onclick = deleteBlockImage;
					document.getElementById("block-image-upload-btn").after(btn);
				}
			} else {
				alert("Fehler: " + data.error);
			}
		})
		.catch(function(err) {
			alert("Upload fehlgeschlagen: " + err);
		});

		e.target.value = "";
	});

	function deleteBlockImage() {
		if (!confirm("Bild wirklich löschen?")) return;

		fetch("'.DOL_URL_ROOT.'/custom/kundenkarte/ajax/equipment_type_block_image.php", {
			method: "POST",
			headers: {"Content-Type": "application/x-www-form-urlencoded"},
			body: "action=delete&type_id=" + typeId + "&token='.newToken().'"
		})
		.then(function(response) { return response.json(); })
		.then(function(data) {
			if (data.success) {
				var preview = document.getElementById("block-image-preview");
				preview.innerHTML = \'<span style="color:#999;font-size:11px;text-align:center;">Kein<br>Bild</span>\';
				var delBtn = document.getElementById("block-image-delete-btn");
				if (delBtn) delBtn.remove();
			} else {
				alert("Fehler: " + data.error);
			}
		});
	}

	var blockImgDelBtn = document.getElementById("block-image-delete-btn");
	if (blockImgDelBtn) blockImgDelBtn.onclick = deleteBlockImage;

	// Select existing image
	var selectBtn = document.getElementById("block-image-select-btn");
	if (selectBtn) {
		selectBtn.onclick = function() {
			if (!typeId || typeId == 0) {
				alert("Bitte speichern Sie zuerst den Equipment-Typ.");
				return;
			}

			var select = document.getElementById("block-image-select");
			var selectedImage = select.value;
			if (!selectedImage) {
				alert("Bitte wählen Sie ein Bild aus.");
				return;
			}

			fetch("'.DOL_URL_ROOT.'/custom/kundenkarte/ajax/equipment_type_block_image.php", {
				method: "POST",
				headers: {"Content-Type": "application/x-www-form-urlencoded"},
				body: "action=select&type_id=" + typeId + "&image=" + encodeURIComponent(selectedImage) + "&token='.newToken().'"
			})
			.then(function(response) { return response.json(); })
			.then(function(data) {
				if (data.success) {
					var preview = document.getElementById("block-image-preview");
					preview.innerHTML = \'<img src="\' + data.block_image_url + \'&t=\' + Date.now() + \'" style="max-width:70px;max-height:70px;" alt="Block Image">\';

					// Add delete button if not present
					if (!document.getElementById("block-image-delete-btn")) {
						var btn = document.createElement("button");
						btn.type = "button";
						btn.id = "block-image-delete-btn";
						btn.className = "button";
						btn.style.cssText = "background:#e74c3c;border-color:#c0392b;color:#fff;margin-left:5px;";
						btn.innerHTML = \'<i class="fa fa-trash"></i> Löschen\';
						btn.onclick = deleteBlockImage;
						document.getElementById("block-image-upload-btn").after(btn);
					}
				} else {
					alert("Fehler: " + data.error);
				}
			})
			.catch(function(err) {
				alert("Fehler: " + err);
			});
		};
	}
	</script>';

	print '<div class="center" style="margin-top:20px;">';
	print '<button type="submit" class="button">'.$langs->trans('Save').'</button>';
	print ' <a class="button button-cancel" href="'.$_SERVER['PHP_SELF'].'?system='.$systemFilter.'">'.$langs->trans('Cancel').'</a>';
	print '</div>';

	print '</form>';

	// Fields management for existing type
	if ($action == 'edit' && $typeId > 0) {
		$editFieldId = GETPOSTINT('editfield');

		print '<br><br>';
		print '<h3>'.$langs->trans('EquipmentTypeFields').'</h3>';

		$fields = $equipmentType->fetchFields(0);

		// Field types available
		$fieldTypes = array(
			'text' => 'Textfeld (einzeilig)',
			'textarea' => 'Textfeld (mehrzeilig)',
			'number' => 'Zahlenfeld',
			'select' => 'Dropdown-Auswahl',
			'date' => 'Datumsfeld',
			'checkbox' => 'Checkbox (Ja/Nein)',
		);

		// Output edit forms BEFORE the table
		foreach ($fields as $field) {
			if ($editFieldId == $field->rowid) {
				$formId = 'editfield_'.$field->rowid;
				print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" id="'.$formId.'">';
				print '<input type="hidden" name="token" value="'.newToken().'">';
				print '<input type="hidden" name="action" value="update_field">';
				print '<input type="hidden" name="typeid" value="'.$typeId.'">';
				print '<input type="hidden" name="fieldid" value="'.$field->rowid.'">';
				print '<input type="hidden" name="system" value="'.$systemFilter.'">';
				print '</form>';
			}
		}

		print '<table class="noborder centpercent">';
		print '<tr class="liste_titre">';
		print '<th>'.$langs->trans('FieldCode').'</th>';
		print '<th>'.$langs->trans('FieldLabel').'</th>';
		print '<th>'.$langs->trans('FieldType').'</th>';
		print '<th>'.$langs->trans('FieldOptions').'</th>';
		print '<th class="center">'.$langs->trans('ShowInHover').'</th>';
		print '<th class="center">'.$langs->trans('ShowOnBlock').'</th>';
		print '<th class="center">'.$langs->trans('IsRequired').'</th>';
		print '<th class="center">'.$langs->trans('Position').'</th>';
		print '<th class="center">'.$langs->trans('Status').'</th>';
		print '<th class="center">'.$langs->trans('Actions').'</th>';
		print '</tr>';

		foreach ($fields as $field) {
			if ($editFieldId == $field->rowid) {
				$formId = 'editfield_'.$field->rowid;

				print '<tr class="oddeven">';
				print '<td><input type="text" name="field_code" form="'.$formId.'" class="flat minwidth100" value="'.dol_escape_htmltag($field->field_code).'" required></td>';
				print '<td><input type="text" name="field_label" form="'.$formId.'" class="flat minwidth150" value="'.dol_escape_htmltag($field->field_label).'" required></td>';
				print '<td><select name="field_type" form="'.$formId.'" class="flat">';
				foreach ($fieldTypes as $ftype => $flabel) {
					$sel = ($field->field_type == $ftype) ? ' selected' : '';
					print '<option value="'.$ftype.'"'.$sel.'>'.$flabel.'</option>';
				}
				print '</select></td>';
				print '<td><input type="text" name="field_options" form="'.$formId.'" class="flat minwidth100" value="'.dol_escape_htmltag($field->field_options).'" placeholder="opt1|opt2|opt3"></td>';
				print '<td class="center"><input type="checkbox" name="show_in_hover" form="'.$formId.'" value="1"'.($field->show_in_hover ? ' checked' : '').'></td>';
				print '<td class="center"><input type="checkbox" name="show_on_block" form="'.$formId.'" value="1"'.($field->show_on_block ? ' checked' : '').'></td>';
				print '<td class="center"><input type="checkbox" name="is_required" form="'.$formId.'" value="1"'.($field->required ? ' checked' : '').'></td>';
				print '<td class="center"><input type="number" name="field_position" form="'.$formId.'" class="flat" style="width:50px;" value="'.$field->position.'" min="0"></td>';
				print '<td></td>';
				print '<td class="center nowraponall">';
				print '<div style="display:inline-flex;gap:8px;">';
				print '<button type="submit" form="'.$formId.'" class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;background:#28a745;border-color:#28a745;color:#fff;" title="'.$langs->trans('Save').'"><i class="fas fa-save"></i></button>';
				print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;" href="'.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$typeId.'&system='.$systemFilter.'" title="'.$langs->trans('Cancel').'"><i class="fas fa-times"></i></a>';
				print '</div>';
				print '</td>';
				print '</tr>';
			} else {
				print '<tr class="oddeven">';
				print '<td>'.dol_escape_htmltag($field->field_code).'</td>';
				print '<td>'.dol_escape_htmltag($field->field_label).'</td>';
				print '<td>'.dol_escape_htmltag($fieldTypes[$field->field_type] ?? $field->field_type).'</td>';
				print '<td class="small opacitymedium">'.dol_escape_htmltag(dol_trunc($field->field_options, 20)).'</td>';
				print '<td class="center">'.($field->show_in_hover ? img_picto('', 'tick') : '').'</td>';
				print '<td class="center">'.($field->show_on_block ? img_picto('', 'tick') : '').'</td>';
				print '<td class="center">'.($field->required ? img_picto('', 'tick') : '').'</td>';
				print '<td class="center">'.$field->position.'</td>';
				print '<td class="center">';
				if ($field->active) {
					print '<a href="'.$_SERVER['PHP_SELF'].'?action=deactivate_field&typeid='.$typeId.'&fieldid='.$field->rowid.'&system='.$systemFilter.'&token='.newToken().'">'.img_picto($langs->trans('Enabled'), 'switch_on').'</a>';
				} else {
					print '<a href="'.$_SERVER['PHP_SELF'].'?action=activate_field&typeid='.$typeId.'&fieldid='.$field->rowid.'&system='.$systemFilter.'&token='.newToken().'">'.img_picto($langs->trans('Disabled'), 'switch_off').'</a>';
				}
				print '</td>';
				print '<td class="center nowraponall">';
				print '<div style="display:inline-flex;gap:8px;">';
				print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;" href="'.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$typeId.'&editfield='.$field->rowid.'&system='.$systemFilter.'" title="'.$langs->trans('Edit').'"><i class="fas fa-pen"></i></a>';
				print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;background:#dc3545;border-color:#dc3545;color:#fff;" href="'.$_SERVER['PHP_SELF'].'?action=delete_field&typeid='.$typeId.'&fieldid='.$field->rowid.'&system='.$systemFilter.'&token='.newToken().'" title="'.$langs->trans('Delete').'"><i class="fas fa-trash"></i></a>';
				print '</div>';
				print '</td>';
				print '</tr>';
			}
		}

		if (empty($fields)) {
			print '<tr class="oddeven"><td colspan="10" class="opacitymedium">'.$langs->trans('NoFieldsDefined').'</td></tr>';
		}

		print '</table>';

		// Add new field form
		print '<div class="margintoponlyshort">';
		print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
		print '<input type="hidden" name="token" value="'.newToken().'">';
		print '<input type="hidden" name="action" value="add_field">';
		print '<input type="hidden" name="typeid" value="'.$typeId.'">';
		print '<input type="hidden" name="system" value="'.$systemFilter.'">';
		print '<table class="noborder centpercent">';
		print '<tr class="liste_titre">';
		print '<th colspan="10">'.$langs->trans('Add').' '.$langs->trans('Field').'</th>';
		print '</tr>';
		print '<tr class="oddeven">';
		print '<td><input type="text" name="field_code" class="flat minwidth100" placeholder="CODE" required></td>';
		print '<td><input type="text" name="field_label" class="flat minwidth150" placeholder="'.$langs->trans('FieldLabel').'" required></td>';
		print '<td><select name="field_type" class="flat" required>';
		print '<option value="">'.$langs->trans('Select').'</option>';
		foreach ($fieldTypes as $ftype => $flabel) {
			print '<option value="'.$ftype.'">'.$flabel.'</option>';
		}
		print '</select></td>';
		print '<td><input type="text" name="field_options" class="flat minwidth100" placeholder="opt1|opt2"></td>';
		print '<td class="center"><input type="checkbox" name="show_in_hover" value="1" checked></td>';
		print '<td class="center"><input type="checkbox" name="show_on_block" value="1"></td>';
		print '<td class="center"><input type="checkbox" name="is_required" value="1"></td>';
		print '<td class="center"><input type="number" name="field_position" class="flat" style="width:50px;" value="0" min="0"></td>';
		print '<td></td>';
		print '<td class="center"><button type="submit" class="button buttongen"><i class="fa fa-plus"></i> '.$langs->trans('Add').'</button></td>';
		print '</tr>';
		print '</table>';
		print '</form>';
		print '</div>';

		// Help box
		print '<div class="info" style="margin-top:15px;">';
		print '<p><strong><i class="fa fa-info-circle"></i> Hilfe: Equipment-Felder</strong></p>';
		print '<table class="noborder" style="margin-top:10px;">';
		print '<tr><td style="width:200px;"><strong>Show in Hover</strong></td><td>Feld wird im Tooltip angezeigt</td></tr>';
		print '<tr><td><strong>Show on Block</strong></td><td>Feld wird direkt auf dem SVG-Block angezeigt (z.B. "B16")</td></tr>';
		print '<tr><td><strong>Dropdown-Auswahl</strong></td><td>Optionen mit <code>|</code> trennen, z.B.: <code>B|C|D</code> oder <code>6|10|16|20|25|32</code></td></tr>';
		print '</table>';
		print '</div>';
	}

} else {
	// System filter
	print '<form method="GET" action="'.$_SERVER['PHP_SELF'].'" style="margin-bottom:15px;">';
	print $langs->trans('FilterBySystem').': ';
	print '<select name="system" class="flat" onchange="this.form.submit();">';
	print '<option value="0">'.$langs->trans('All').'</option>';
	foreach ($systems as $sys) {
		$sel = ($systemFilter == $sys->rowid) ? ' selected' : '';
		print '<option value="'.$sys->rowid.'"'.$sel.'>'.dol_escape_htmltag($sys->label).'</option>';
	}
	print '</select>';
	print '</form>';

	// Add button
	print '<div style="margin-bottom:15px;">';
	print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=create&system='.$systemFilter.'">';
	print '<i class="fa fa-plus"></i> '.$langs->trans('AddEquipmentType');
	print '</a>';
	print '</div>';

	// List
	$types = $equipmentType->fetchAllBySystem($systemFilter, 0);

	print '<table class="noborder centpercent">';
	print '<tr class="liste_titre">';
	print '<th>'.$langs->trans('TypeRef').'</th>';
	print '<th>'.$langs->trans('TypeLabel').'</th>';
	print '<th>'.$langs->trans('System').'</th>';
	print '<th class="center">'.$langs->trans('WidthTE').'</th>';
	print '<th class="center">'.$langs->trans('Color').'</th>';
	print '<th class="center">'.$langs->trans('Position').'</th>';
	print '<th class="center">'.$langs->trans('Status').'</th>';
	print '<th class="center">'.$langs->trans('Actions').'</th>';
	print '</tr>';

	foreach ($types as $type) {
		print '<tr class="oddeven">';

		print '<td>';
		if ($type->picto) {
			print kundenkarte_render_icon($type->picto).' ';
		}
		print dol_escape_htmltag($type->ref).'</td>';

		print '<td>'.dol_escape_htmltag($type->label);
		if ($type->label_short) {
			print ' <span class="opacitymedium">('.$type->label_short.')</span>';
		}
		print '</td>';

		print '<td>'.dol_escape_htmltag($type->system_label).'</td>';
		print '<td class="center">'.$type->width_te.' TE</td>';
		print '<td class="center"><span style="display:inline-block;width:24px;height:24px;background:'.($type->color ?: '#3498db').';border-radius:3px;"></span></td>';
		print '<td class="center">'.$type->position.'</td>';

		print '<td class="center">';
		if ($type->active) {
			print '<a href="'.$_SERVER['PHP_SELF'].'?action=deactivate&typeid='.$type->id.'&system='.$systemFilter.'&token='.newToken().'">'.img_picto($langs->trans('Enabled'), 'switch_on').'</a>';
		} else {
			print '<a href="'.$_SERVER['PHP_SELF'].'?action=activate&typeid='.$type->id.'&system='.$systemFilter.'&token='.newToken().'">'.img_picto($langs->trans('Disabled'), 'switch_off').'</a>';
		}
		print '</td>';

		print '<td class="center nowraponall">';
		print '<div style="display:inline-flex;gap:8px;">';
		print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;" href="'.$_SERVER['PHP_SELF'].'?action=edit&typeid='.$type->id.'&system='.$systemFilter.'" title="'.$langs->trans('Edit').'"><i class="fas fa-pen"></i></a>';
		print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;" href="'.$_SERVER['PHP_SELF'].'?action=copy&typeid='.$type->id.'&system='.$systemFilter.'&token='.newToken().'" title="'.$langs->trans('Copy').'"><i class="fas fa-copy"></i></a>';
		if (!$type->is_system) {
			print '<a class="button buttongen" style="width:32px;height:32px;padding:0;display:inline-flex;align-items:center;justify-content:center;background:#dc3545;border-color:#dc3545;color:#fff;" href="'.$_SERVER['PHP_SELF'].'?action=delete&typeid='.$type->id.'&system='.$systemFilter.'" title="'.$langs->trans('Delete').'" onclick="return confirm(\''.$langs->trans('ConfirmDelete').'\');"><i class="fas fa-trash"></i></a>';
		}
		print '</div>';
		print '</td>';

		print '</tr>';
	}

	if (empty($types)) {
		print '<tr class="oddeven"><td colspan="8" class="opacitymedium">'.$langs->trans('NoRecords').'</td></tr>';
	}

	print '</table>';
}

print dol_get_fiche_end();

llxFooter();
$db->close();
