<?php
require_once "../config/database.php";
require_once "../includes/auth.php";
require_once "../includes/rbac.php";
require_once "../includes/lang.php";
require_once "../includes/csrf.php";
require_once "../includes/avatar.php";

requirePageAuth();
if (!hasPermission("view_admin")) {
    http_response_code(403);
    echo "Forbidden";
    exit;
}

$user = getAuthUser();
$csrf = csrfToken();
$avatarUrl = resolveUserAvatarUrl($pdo, (int)($user['id'] ?? 0));
?>
<!DOCTYPE HTML>
<html lang="<?= htmlspecialchars(currentLang()) ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= htmlspecialchars(t("admin_profile")) ?> - Konzona Hub</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/style.css" type="text/css">
<link rel="stylesheet" href="assets/css/dashboard.css" type="text/css">
<link href="assets/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>
<div class="dashboard_container">
<header id="header">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<div class="logo"><a href="admin.php"><img src="assets/images/logo.png" alt="logo"/></a></div>
</div>
<div class="collapse navbar-collapse">
<div class="user_nav">
<div class="dropdown">
<span id="dLabel" type="button" data-toggle="dropdown"><img src="<?= htmlspecialchars($avatarUrl, ENT_QUOTES, 'UTF-8') ?>" alt="img" onerror="this.onerror=null;this.src='assets/images/100x100.jpg';"></span>
<ul class="dropdown-menu">
<li><a href="admin.php"><i class="fa fa-cogs"></i> <?= htmlspecialchars(t("dashboard")) ?></a></li>
<li class="active"><a href="admin_profile.php"><i class="fa fa-user-o"></i> <?= htmlspecialchars(t("admin_profile")) ?></a></li>
<li><a href="nalog.php"><i class="fa fa-plus"></i> <?= htmlspecialchars(t("add_user")) ?></a></li>
<li><a href="logout.php"><i class="fa fa-power-off"></i> <?= htmlspecialchars(t("logout")) ?></a></li>
</ul>
</div>
</div>
</div>
</div>
</nav>
</header>

<div id="dashboard">
<div id="dashboard-nav" class="dashboard-nav">
<ul>
<li><a href="admin.php"><i class="fa fa-cogs"></i> <?= htmlspecialchars(t("dashboard")) ?></a></li>
<li><a href="dashboard.php"><i class="fa fa-th-list"></i> <?= htmlspecialchars(t("requests")) ?></a></li>
<li><a href="match_history.php"><i class="fa fa-link"></i> Matched Workers</a></li>
<li><a href="agency_documents.php"><i class="fa fa-file-text-o"></i> Worker Documentation</a></li>
<li><a href="worker_documents_review.php"><i class="fa fa-folder-open-o"></i> Documentation Review</a></li>
<li>
  <a href="#adminActionsMenu" data-toggle="collapse" aria-expanded="true">
    <i class="fa fa-plus-circle"></i> Admin Actions <span class="caret"></span>
  </a>
  <ul id="adminActionsMenu" class="collapse in list-unstyled" style="padding-left:15px;">
    <li><a href="nalog.php"><i class="fa fa-user-plus"></i> <?= htmlspecialchars(t("add_user")) ?></a></li>
    <li><a href="add_skills.php"><i class="fa fa-tags"></i> <?= htmlspecialchars(t("add_skill")) ?></a></li>
    <li><a href="add_positions.php"><i class="fa fa-briefcase"></i> Positions</a></li>
    <li><a href="manage_permissions.php"><i class="fa fa-lock"></i> <?= htmlspecialchars(t("manage_permissions")) ?></a></li>
    <li class="active"><a href="admin_profile.php"><i class="fa fa-user-o"></i> <?= htmlspecialchars(t("admin_profile")) ?></a></li>
  </ul>
</li>
<li><a href="chat.php"><i class="fa fa-comments"></i> <?= htmlspecialchars(t("support")) ?></a></li>
<li><a href="profile.php"><i class="fa fa-user-o"></i> <?= htmlspecialchars(t("my_profile")) ?></a></li>
<li><a href="terms.php"><i class="fa fa-file-text-o"></i> <?= htmlspecialchars(t("terms_of_use")) ?></a></li>
<li><a href="logout.php"><i class="fa fa-power-off"></i> <?= htmlspecialchars(t("logout")) ?></a></li>
</ul>
</div>

<div class="dashboard-content">
<div id="titlebar">
<h2><?= htmlspecialchars(t("admin_profile")) ?></h2>
</div>

<div class="row">
<div class="col-lg-8 col-md-8">
<div class="dashboard-list-box-static">
<div class="featured-image"><img src="assets/images/400x400.jpg" alt="admin" onerror="this.src='assets/images/400x400.jpg'"></div>
<h4>Admin Control Center</h4>
<p>Manage users, permissions, skills, view stats and audit log.</p>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="dashboard-list-box-static">
<h4>Quick Actions</h4>
<ul>
<li><a href="nalog.php">Add User</a></li>
<li><a href="#" onclick="showSkillsPrompt()">Add Skill</a></li>
<li><a href="#" onclick="showPermissionsPrompt()">Manage Permissions</a></li>
<li><a href="admin.php">Stats & Charts</a></li>
<li><a href="admin.php" onclick="setAdminTab('payments')">Payments</a></li>
<li><a href="admin.php" onclick="setAdminTab('invoices')">Invoices</a></li>
<li><a href="admin.php" onclick="setAdminTab('workers')">Workers</a></li>
<li><a href="../api/search.php" target="_blank">Global Search</a></li>
</ul>
</div>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<div class="dashboard-list-box">
<h4>Admin Tools</h4>
<table class="table">
<thead>
<tr><th>Action</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>Add User</td><td>nalog.php - CRUD users</td></tr>
<tr><td>Skills</td><td>Checkbox list management</td></tr>
<tr><td>Permissions</td><td>Granular RBAC control</td></tr>
<tr><td>Agencies/Radnici/Companies</td><td>Filters, search, view profiles</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

<script>
window.APP = { csrf: "<?= htmlspecialchars($csrf) ?>", userId: <?= (int)$user["id"] ?> };
function setAdminTab(tab) {
  try { localStorage.setItem("adminActiveTab", tab); } catch (e) {}
}
function showSkillsPrompt() {
  const name = prompt('New skill name:');
  if (name) {
    fetch('../api/skills.php', {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({action: 'add', name: name.trim()})
    }).then(r => r.json()).then(j => alert(j.success ? 'Added' : j.error));
  }
}
function showPermissionsPrompt() {
  const name = prompt('New permission name (comma sep):');
  if (name) {
    fetch('../api/permissions.php', {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({action: 'add', name: name.split(',').map(n => n.trim())})
    }).then(r => r.json()).then(j => alert(j.success ? 'Added' : j.error));
  }
}
</script>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>

