<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for equipment panels (Schaltschrankfelder)
 */

if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');

$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.'/custom/kundenkarte/class/equipmentpanel.class.php';
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentcarrier.class.php';

header('Content-Type: application/json; charset=UTF-8');

$action = GETPOST('action', 'aZ09');
$panelId = GETPOSTINT('panel_id');
$anlageId = GETPOSTINT('anlage_id');

$panel = new EquipmentPanel($db);

$response = array('success' => false, 'error' => '');

// Security check
if (!$user->hasRight('kundenkarte', 'read')) {
	$response['error'] = 'Permission denied';
	echo json_encode($response);
	exit;
}

switch ($action) {
	case 'get':
		// Get single panel data
		if ($panelId > 0 && $panel->fetch($panelId) > 0) {
			$response['success'] = true;
			$response['panel'] = array(
				'id' => $panel->id,
				'fk_anlage' => $panel->fk_anlage,
				'label' => $panel->label,
				'position' => $panel->position,
				'note_private' => $panel->note_private,
				'status' => $panel->status
			);
		} else {
			$response['error'] = 'Panel not found';
		}
		break;

	case 'list':
		// List all panels for an Anlage
		if ($anlageId > 0) {
			$panels = $panel->fetchByAnlage($anlageId);
			$result = array();
			foreach ($panels as $p) {
				$result[] = array(
					'id' => $p->id,
					'fk_anlage' => $p->fk_anlage,
					'label' => $p->label,
					'position' => $p->position,
					'status' => $p->status
				);
			}
			$response['success'] = true;
			$response['panels'] = $result;
		} else {
			$response['error'] = 'Missing anlage_id';
		}
		break;

	case 'create':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$panel->fk_anlage = $anlageId;
		$panel->label = GETPOST('label', 'alphanohtml');
		$panel->position = GETPOSTINT('position');
		$panel->note_private = GETPOST('note_private', 'restricthtml');

		$result = $panel->create($user);
		if ($result > 0) {
			$response['success'] = true;
			$response['panel_id'] = $result;
			$response['label'] = $panel->label;
		} else {
			$response['error'] = $panel->error;
		}
		break;

	case 'update':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($panel->fetch($panelId) > 0) {
			$panel->label = GETPOST('label', 'alphanohtml') ?: $panel->label;
			$panel->position = GETPOSTINT('position') ?: $panel->position;
			$panel->note_private = GETPOST('note_private', 'restricthtml');

			$result = $panel->update($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $panel->error;
			}
		} else {
			$response['error'] = 'Panel not found';
		}
		break;

	case 'delete':
		if (!$user->hasRight('kundenkarte', 'delete')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($panel->fetch($panelId) > 0) {
			$result = $panel->delete($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $panel->error;
			}
		} else {
			$response['error'] = 'Panel not found';
		}
		break;

	case 'list_with_carriers':
		// List all panels with their carriers for an Anlage
		if ($anlageId > 0) {
			$panels = $panel->fetchByAnlage($anlageId);
			$result = array();

			foreach ($panels as $p) {
				$panelData = array(
					'id' => $p->id,
					'fk_anlage' => $p->fk_anlage,
					'label' => $p->label,
					'position' => $p->position,
					'status' => $p->status,
					'carriers' => array()
				);

				// Fetch carriers for this panel
				$p->fetchCarriers();
				foreach ($p->carriers as $c) {
					$panelData['carriers'][] = array(
						'id' => $c->id,
						'label' => $c->label,
						'total_te' => $c->total_te,
						'position' => $c->position
					);
				}

				$result[] = $panelData;
			}

			$response['success'] = true;
			$response['panels'] = $result;
		} else {
			$response['error'] = 'Missing anlage_id';
		}
		break;

	case 'duplicate':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($panel->fetch($panelId) > 0) {
			// Create a copy of the panel
			$newPanel = new EquipmentPanel($db);
			$newPanel->fk_anlage = $panel->fk_anlage;
			$newPanel->label = $panel->label.' (Kopie)';
			$newPanel->note_private = $panel->note_private;

			$result = $newPanel->create($user);
			if ($result > 0) {
				$response['success'] = true;
				$response['panel_id'] = $result;
			} else {
				$response['error'] = $newPanel->error;
			}
		} else {
			$response['error'] = 'Panel not found';
		}
		break;

	default:
		$response['error'] = 'Unknown action';
}

echo json_encode($response);
$db->close();
