<?php
if (!defined('ROOT_FOLDER')) {
	require_once(dirname(__DIR__, 3) . '/init.php');
}

// Güvenli session ve admin kontrolü
m_secure_session_start();
if (!m_admin_check()) { m_redirect(ADMIN_URL . '/login'); }
m_validate_session_security();

// Sadece süper admin erişsin
if (!function_exists('is_super_admin') || !is_super_admin()){
	m_set_session('formSuccess', false);
	m_set_session('error_message', lang('security_violation', [], 'Güvenlik ihlali tespit edildi.'));
	m_redirect(ADMIN_URL);
}

// Korunan modüller
$protected = ['modules','profile','dashboard'];

// Toggle işlemi
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	$csrf = $_POST['csrf_token'] ?? '';
	if (!isset($_SESSION['csrf_token']) || !hash_equals($_SESSION['csrf_token']['value'] ?? '', $csrf)) {
		m_set_session('formSuccess', false);
		m_set_session('error_message', lang('security.csrf_invalid', [], 'Güvenlik token\'ı geçersiz!'));
		m_redirect(ADMIN_URL . '/modules/lists');
	}

	$module = trim((string)($_POST['module'] ?? ''));
	$enable = isset($_POST['enable']) && $_POST['enable'] === '1';
	if ($module === '' || in_array($module, $protected, true)) {
		m_set_session('formSuccess', false);
		m_set_session('error_message', lang('modules.protected_module', [], 'Bu modül korunuyor ve devre dışı bırakılamaz.'));
		m_redirect(ADMIN_URL . '/modules/lists');
	}

	$ok = module_set_enabled($module, $enable);
	if ($ok) {
		m_set_session('formSuccess', true);
		m_set_session('success_message', lang('modules.toggle_success', ['name' => $module], 'Modül durumu güncellendi.'));
	} else {
		m_set_session('formSuccess', false);
		m_set_session('error_message', lang('modules.toggle_error', [], 'İşlem başarısız.'));
	}
	m_redirect(ADMIN_URL . '/modules/lists');
}

$modules = list_admin_modules();

?>
<div class="container-fluid">
	<h1 class="mb-4"><?= lang('modules.title', [], 'Modül Yönetimi') ?></h1>
	<div class="card">
		<div class="card-body table-responsive">
			<table class="table table-striped align-middle">
				<thead>
					<tr>
						<th style="width:50%"><?= lang('modules.name', [], 'Modül') ?></th>
						<th style="width:25%"><?= lang('modules.status', [], 'Durum') ?></th>
						<th style="width:25%"><?= lang('published', [], 'İşlemler') ?></th>
					</tr>
				</thead>
				<tbody>
                <?php foreach ($modules as $m):
					$isProtected = in_array($m, $protected, true);
					$enabled = module_is_enabled($m);
				?>
					<tr>
                        <td><?= htmlspecialchars(module_label($m), ENT_QUOTES, 'UTF-8') ?></td>
						<td>
							<span class="badge bg-<?= $enabled ? 'success' : 'secondary' ?>">
								<?= $enabled ? lang('modules.enabled', [], 'Açık') : lang('modules.disabled', [], 'Kapalı') ?>
							</span>
						</td>
						<td>
							<form method="post" class="d-inline">
								<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']['value'] ?? '', ENT_QUOTES, 'UTF-8') ?>" />
								<input type="hidden" name="module" value="<?= htmlspecialchars($m, ENT_QUOTES, 'UTF-8') ?>" />
								<input type="hidden" name="enable" value="<?= $enabled ? '0' : '1' ?>" />
								<button type="submit" class="btn btn-sm btn-<?= $enabled ? 'warning' : 'primary' ?>" <?= $isProtected ? 'disabled' : '' ?>>
									<?= $enabled ? lang('modules.disable', [], 'Kapat') : lang('modules.enable', [], 'Aç') ?>
								</button>
								<?php if ($isProtected): ?>
									<small class="text-muted ms-2"><?= lang('modules.protected_module', [], 'Korunan modül') ?></small>
								<?php endif; ?>
							</form>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>


