<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/functions.php';
startSession(); requireLogin();
$db = Database::getInstance();
$page = 'Organization Chart';

$depts = $db->fetchAll("SELECT d.*,CONCAT(e.first_name,' ',e.last_name) AS head_name FROM departments d LEFT JOIN employees e ON e.id=d.head_employee_id WHERE d.is_active=1 ORDER BY d.name");
$empsByDept = [];
// FIX (07 Jul 2026): scope to current org so this doesn't show another
// organization's employees. (Departments themselves are still shared
// master data across all organizations — a separate, lower-severity
// item flagged for a later pass.)
[$orgClause, $orgParams] = orgScopeClause($db, 'e.company_id');
$allEmps = $db->fetchAll("SELECT e.id,e.first_name,e.last_name,e.employee_id AS emp_code,e.department_id,e.reporting_manager_id,des.name AS designation FROM employees e LEFT JOIN designations des ON des.id=e.designation_id WHERE e.status='active' AND $orgClause ORDER BY e.first_name", $orgParams);
foreach($allEmps as $e) $empsByDept[$e['department_id']][] = $e;

include __DIR__ . '/../../includes/header.php';
?>
<div class="pc-card mb-3">
  <div class="d-flex align-items-center gap-3">
    <div class="pc-stat-icon brand" style="width:48px;height:48px;font-size:22px"><i class="ti ti-sitemap"></i></div>
    <div><h6 class="mb-0 fw-bold">Organization Chart</h6><p class="mb-0 text-muted small">Department-wise employee hierarchy</p></div>
    <div class="ms-auto">
      <span class="badge" style="background:var(--pc-brand-light);color:var(--pc-brand)"><?=count($allEmps)?> Active Employees</span>
      <span class="badge ms-1" style="background:var(--pc-green-bg);color:var(--pc-green)"><?=count($depts)?> Departments</span>
    </div>
  </div>
</div>

<div class="row g-3">
<?php foreach($depts as $dept): $emps = $empsByDept[$dept['id']] ?? []; ?>
<div class="col-md-6 col-xl-4">
  <div class="pc-card">
    <div class="d-flex align-items-center gap-2 mb-3 pb-2 border-bottom">
      <div class="pc-stat-icon brand" style="width:36px;height:36px;font-size:15px"><i class="ti ti-building"></i></div>
      <div>
        <div class="fw-bold" style="font-size:13px"><?=clean($dept['name'])?></div>
        <?php if($dept['head_name']): ?><div class="text-muted" style="font-size:11px">Head: <?=clean($dept['head_name'])?></div><?php endif; ?>
      </div>
      <span class="badge ms-auto" style="background:var(--pc-brand-light);color:var(--pc-brand)"><?=count($emps)?></span>
    </div>
    <?php if($emps): ?>
    <?php foreach($emps as $e): ?>
    <div class="d-flex align-items-center gap-2 mb-2 p-2 rounded" style="background:#fafbfd">
      <div style="width:30px;height:30px;background:var(--pc-brand-light);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:700;color:var(--pc-brand);flex-shrink:0">
        <?=strtoupper(substr($e['first_name'],0,1).substr($e['last_name'],0,1))?>
      </div>
      <div>
        <div style="font-size:12.5px;font-weight:600"><?=clean($e['first_name'].' '.$e['last_name'])?></div>
        <div class="text-muted" style="font-size:11px"><?=clean($e['designation']??'—')?> · <?=clean($e['emp_code'])?></div>
      </div>
    </div>
    <?php endforeach; ?>
    <?php else: ?><div class="text-muted text-center small py-2">No employees</div><?php endif; ?>
  </div>
</div>
<?php endforeach; ?>
</div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>