<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * Admin page to manage installation system categories
 */

$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');

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

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

$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
$systemId = GETPOSTINT('systemid');

$form = new Form($db);

/*
 * Actions
 */

if ($action == 'add') {
	$code = GETPOST('code', 'aZ09');
	$label = GETPOST('label', 'alphanohtml');
	$picto = GETPOST('picto', 'alphanohtml');
	$color = GETPOST('color', 'alphanohtml');
	$position = GETPOSTINT('position');

	// Tree display config
	$treeConfig = array(
		'show_ref' => GETPOSTINT('show_ref'),
		'show_label' => GETPOSTINT('show_label'),
		'show_type' => GETPOSTINT('show_type'),
		'show_icon' => GETPOSTINT('show_icon'),
		'show_status' => GETPOSTINT('show_status'),
		'show_fields' => GETPOSTINT('show_fields'),
		'expand_default' => GETPOSTINT('expand_default'),
		'indent_style' => GETPOST('indent_style', 'alpha') ?: 'lines'
	);
	$treeConfigJson = json_encode($treeConfig);

	if (empty($code) || empty($label)) {
		setEventMessages($langs->trans('ErrorFieldRequired'), null, 'errors');
	} else {
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system";
		$sql .= " (code, label, picto, color, position, active, entity, tree_display_config)";
		$sql .= " VALUES ('".$db->escape(strtoupper($code))."', '".$db->escape($label)."',";
		$sql .= " ".($picto ? "'".$db->escape($picto)."'" : "NULL").",";
		$sql .= " ".($color ? "'".$db->escape($color)."'" : "NULL").",";
		$sql .= " ".((int) $position).", 1, 0,";
		$sql .= " '".$db->escape($treeConfigJson)."')";

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

if ($action == 'update') {
	$code = GETPOST('code', 'aZ09');
	$label = GETPOST('label', 'alphanohtml');
	$picto = GETPOST('picto', 'alphanohtml');
	$color = GETPOST('color', 'alphanohtml');
	$position = GETPOSTINT('position');

	// Tree display config
	$treeConfig = array(
		'show_ref' => GETPOSTINT('show_ref'),
		'show_label' => GETPOSTINT('show_label'),
		'show_type' => GETPOSTINT('show_type'),
		'show_icon' => GETPOSTINT('show_icon'),
		'show_status' => GETPOSTINT('show_status'),
		'show_fields' => GETPOSTINT('show_fields'),
		'expand_default' => GETPOSTINT('expand_default'),
		'indent_style' => GETPOST('indent_style', 'alpha') ?: 'lines'
	);
	$treeConfigJson = json_encode($treeConfig);

	$sql = "UPDATE ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system SET";
	$sql .= " code = '".$db->escape(strtoupper($code))."'";
	$sql .= ", label = '".$db->escape($label)."'";
	$sql .= ", picto = ".($picto ? "'".$db->escape($picto)."'" : "NULL");
	$sql .= ", color = ".($color ? "'".$db->escape($color)."'" : "NULL");
	$sql .= ", position = ".((int) $position);
	$sql .= ", tree_display_config = '".$db->escape($treeConfigJson)."'";
	$sql .= " WHERE rowid = ".((int) $systemId);

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

if ($action == 'confirm_delete' && $confirm == 'yes') {
	// Check if in use
	$sql = "SELECT COUNT(*) as cnt FROM ".MAIN_DB_PREFIX."kundenkarte_anlage WHERE fk_system = ".((int) $systemId);
	$resql = $db->query($sql);
	$obj = $db->fetch_object($resql);

	if ($obj->cnt > 0) {
		setEventMessages($langs->trans('ErrorSystemInUse'), null, 'errors');
	} else {
		// Also delete types for this system
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_anlage_type WHERE fk_system = ".((int) $systemId);
		$db->query($sql);

		$sql = "DELETE FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system WHERE rowid = ".((int) $systemId);
		$result = $db->query($sql);
		if ($result) {
			setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
		}
	}
	$action = '';
}

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

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

/*
 * View
 */

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

// Include CSS and JS
$morejs = array('/kundenkarte/js/kundenkarte.js?v=1769962608');
$morecss = array('/kundenkarte/css/kundenkarte.css?v=1769962608');

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

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

// Confirmation
if ($action == 'delete') {
	print $form->formconfirm(
		$_SERVER['PHP_SELF'].'?systemid='.$systemId,
		$langs->trans('Delete'),
		$langs->trans('ConfirmDeleteSystem'),
		'confirm_delete',
		'',
		'yes',
		1
	);
}

// Add form
if ($action == 'create' || $action == 'edit') {
	$system = null;
	if ($action == 'edit' && $systemId > 0) {
		$sql = "SELECT * FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system WHERE rowid = ".((int) $systemId);
		$resql = $db->query($sql);
		$system = $db->fetch_object($resql);
	}

	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="systemid" value="'.$systemId.'">';
	}

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

	print '<tr><td class="titlefield fieldrequired">'.$langs->trans('SystemCode').'</td>';
	print '<td><input type="text" name="code" class="flat minwidth200" value="'.dol_escape_htmltag($system ? $system->code : '').'" maxlength="32" required></td></tr>';

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

	print '<tr><td>'.$langs->trans('SystemPicto').'</td>';
	print '<td><div class="kundenkarte-icon-picker-wrapper">';
	print '<span class="kundenkarte-icon-preview">';
	if ($system && $system->picto) {
		print kundenkarte_render_icon($system->picto);
	}
	print '</span>';
	print '<input type="text" name="picto" class="flat minwidth200" value="'.dol_escape_htmltag($system ? $system->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></td></tr>';

	print '<tr><td>'.$langs->trans('SystemColor').'</td>';
	print '<td><input type="color" name="color" value="'.dol_escape_htmltag($system ? $system->color : '#3498db').'"></td></tr>';

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

	print '</table>';

	// Tree display configuration
	print '<br><h3>'.$langs->trans('TreeDisplayConfig').'</h3>';

	// Parse existing config
	$treeConfig = array(
		'show_ref' => 1,
		'show_label' => 1,
		'show_type' => 1,
		'show_icon' => 1,
		'show_status' => 1,
		'show_fields' => 0,
		'expand_default' => 1,
		'indent_style' => 'lines'
	);
	if ($system && !empty($system->tree_display_config)) {
		$savedConfig = json_decode($system->tree_display_config, true);
		if (is_array($savedConfig)) {
			$treeConfig = array_merge($treeConfig, $savedConfig);
		}
	}

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

	print '<tr><td class="titlefield">'.$langs->trans('TreeShowRef').'</td>';
	print '<td><input type="checkbox" name="show_ref" value="1"'.($treeConfig['show_ref'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowRefHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeShowLabel').'</td>';
	print '<td><input type="checkbox" name="show_label" value="1"'.($treeConfig['show_label'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowLabelHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeShowType').'</td>';
	print '<td><input type="checkbox" name="show_type" value="1"'.($treeConfig['show_type'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowTypeHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeShowIcon').'</td>';
	print '<td><input type="checkbox" name="show_icon" value="1"'.($treeConfig['show_icon'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowIconHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeShowStatus').'</td>';
	print '<td><input type="checkbox" name="show_status" value="1"'.($treeConfig['show_status'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowStatusHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeShowFields').'</td>';
	print '<td><input type="checkbox" name="show_fields" value="1"'.($treeConfig['show_fields'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeShowFieldsHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeExpandDefault').'</td>';
	print '<td><input type="checkbox" name="expand_default" value="1"'.($treeConfig['expand_default'] ? ' checked' : '').'> ';
	print '<span class="opacitymedium">'.$langs->trans('TreeExpandDefaultHelp').'</span></td></tr>';

	print '<tr><td>'.$langs->trans('TreeIndentStyle').'</td>';
	print '<td><select name="indent_style" class="flat">';
	$styles = array(
		'lines' => $langs->trans('TreeIndentLines'),
		'dots' => $langs->trans('TreeIndentDots'),
		'arrows' => $langs->trans('TreeIndentArrows'),
		'simple' => $langs->trans('TreeIndentSimple')
	);
	foreach ($styles as $code => $label) {
		$selected = ($treeConfig['indent_style'] == $code) ? ' selected' : '';
		print '<option value="'.$code.'"'.$selected.'>'.$label.'</option>';
	}
	print '</select></td></tr>';

	print '</table>';

	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'].'">'.$langs->trans('Cancel').'</a>';
	print '</div>';

	print '</form>';

} else {
	// List
	print '<div style="margin-bottom:15px;">';
	print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=create">';
	print '<i class="fa fa-plus"></i> '.$langs->trans('AddSystem');
	print '</a>';
	print '</div>';

	$sql = "SELECT * FROM ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system ORDER BY position ASC, label ASC";
	$resql = $db->query($sql);

	print '<table class="noborder centpercent">';
	print '<tr class="liste_titre">';
	print '<th>'.$langs->trans('SystemCode').'</th>';
	print '<th>'.$langs->trans('SystemLabel').'</th>';
	print '<th>'.$langs->trans('SystemPicto').'</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>';

	if ($resql) {
		while ($obj = $db->fetch_object($resql)) {
			print '<tr class="oddeven">';

			print '<td>'.dol_escape_htmltag($obj->code).'</td>';
			print '<td>'.dol_escape_htmltag($obj->label).'</td>';
			print '<td>';
			if ($obj->picto) {
				print kundenkarte_render_icon($obj->picto, '', 'color:'.$obj->color.';').' ';
				print dol_escape_htmltag($obj->picto);
			}
			print '</td>';
			print '<td class="center">'.$obj->position.'</td>';

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

			print '<td class="center nowraponall">';
			print '<a href="'.$_SERVER['PHP_SELF'].'?action=edit&systemid='.$obj->rowid.'">'.img_edit().'</a>';
			print ' <a href="'.$_SERVER['PHP_SELF'].'?action=delete&systemid='.$obj->rowid.'" class="deletelink">'.img_delete().'</a>';
			print '</td>';

			print '</tr>';
		}
	}

	print '</table>';
}

print dol_get_fiche_end();

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