<?php
$page_title = 'Struktur Organisasi';
require_once('includes/load.php');
// Menampilkan semua eror PHP untuk debugging. Hapus baris ini setelah selesai.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Anda mungkin ingin menyesuaikan level otorisasi jika diperlukan
// page_require_level(4);

/**
 * Mendapatkan dan memproses data struktur organisasi dari database.
 * JOIN antara tabel `employees` dan `departments` serta `employees` itu sendiri.
 * Query ini telah disesuaikan agar sesuai dengan nama kolom `department_id` di tabel Anda.
 */
$sql = "
    SELECT 
        e.id,
        CONCAT(e.first_name, ' ', e.last_name) AS employee_name,
        e.job_title,
        d.name AS department_name,
        e.manager_id,
        CONCAT(m.first_name, ' ', m.last_name) AS manager_name
    FROM employees e
    LEFT JOIN departments d ON e.department_id = d.department_id
    LEFT JOIN employees m ON e.manager_id = m.id
    ORDER BY d.name, e.manager_id, e.job_title;
";

$employees = find_by_sql($sql);

/**
 * Mengelompokkan data karyawan ke dalam struktur hierarki.
 * Karyawan yang tidak memiliki manajer (manager_id = NULL) ditempatkan di level teratas (kunci 0).
 */
$org_structure = [];
foreach ($employees as $employee) {
    $manager_id = $employee['manager_id'] ?? 0;
    
    if (!isset($org_structure[$manager_id])) {
        $org_structure[$manager_id] = [
            'manager_name' => $employee['manager_name'] ?? 'CEO / Direktur Utama',
            'employees' => []
        ];
    }
    
    $org_structure[$manager_id]['employees'][] = $employee;
}

?>

<?php include_once('layouts/header.php'); ?>

<div class="dashboard-container">
    <div class="dashboard-header">
        <h1><i class="fas fa-sitemap"></i> Struktur Organisasi</h1>
    </div>

    <div class="org-chart-container">
        <?php
        /**
         * Fungsi rekursif untuk menampilkan hierarki organisasi.
         */
        function displayOrgChart($manager_id, $org_structure) {
            if (!isset($org_structure[$manager_id])) {
                return;
            }

            echo '<ul class="org-list">';
            foreach ($org_structure[$manager_id]['employees'] as $employee) {
                echo '<li class="org-item">';
                echo '<div class="card employee-card">';
                echo '<div class="employee-info">';
                echo '<h4>' . htmlspecialchars($employee['employee_name']) . '</h4>';
                echo '<p class="job-title">' . htmlspecialchars($employee['job_title'] ?? 'N/A') . '</p>';
                echo '<p class="department"><i class="fas fa-building"></i> ' . htmlspecialchars($employee['department_name'] ?? 'N/A') . '</p>';
                echo '</div>';
                echo '</div>';

                displayOrgChart($employee['id'], $org_structure);
                
                echo '</li>';
            }
            echo '</ul>';
        }

        if (isset($org_structure[0])) {
            echo '<div class="top-level">';
            echo '<h2>' . htmlspecialchars($org_structure[0]['manager_name']) . '</h2>';
            displayOrgChart(0, $org_structure);
            echo '</div>';
        } else {
            echo '<div class="no-data"><i class="fas fa-info-circle"></i> Tidak ada data struktur organisasi yang ditemukan.</div>';
        }
        ?>
    </div>
</div>

<style>
/* CSS untuk Tampilan Modern dan Responsif */
:root {
    --primary: #4361ee;
    --secondary: #3f37c9;
    --success: #4cc9f0;
    --danger: #f72585;
    --warning: #f8961e;
    --info: #4895ef;
    --light: #f8f9fa;
    --dark: #212529;
    --gray: #6c757d;
    --light-gray: #e9ecef;
}

body {
    background-color: #f0f2f5;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #333;
}
.dashboard-container {
    padding: 20px;
}
.dashboard-header {
    margin-bottom: 25px;
    text-align: center;
}
.dashboard-header h1 {
    font-size: 28px;
    font-weight: 600;
    color: var(--dark);
    margin: 0;
}
.dashboard-header h1 i {
    margin-right: 10px;
    color: var(--primary);
}

.org-chart-container {
    background: #fff;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    overflow-x: auto;
}
.org-chart-container h2 {
    text-align: center;
    color: var(--primary);
    margin-bottom: 20px;
}

.org-list, .org-item {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.org-list {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    gap: 20px;
}
.org-item {
    position: relative;
    text-align: center;
    flex-shrink: 0;
    padding-top: 50px;
}

.org-item::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 2px;
    height: 20px;
    background-color: var(--info);
}
.org-item ul {
    margin: 0;
    padding: 20px 0 0 0;
    position: relative;
}
.org-item ul::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background-color: var(--info);
}

.employee-card {
    background: #fff;
    border: 1px solid var(--light-gray);
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease-in-out;
    text-align: center;
    position: relative;
    z-index: 10;
    min-width: 250px;
}
.employee-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
}
.employee-card h4 {
    margin: 0 0 5px 0;
    font-size: 18px;
    font-weight: 600;
}
.employee-card .job-title {
    color: var(--gray);
    font-size: 14px;
    margin: 0 0 10px 0;
}
.employee-card .department {
    color: var(--gray);
    font-size: 13px;
    margin: 0;
}
.department i {
    margin-right: 5px;
    color: var(--primary);
}

.no-data {
    text-align: center;
    color: var(--gray);
    padding: 50px;
}
.no-data i {
    font-size: 40px;
    margin-bottom: 10px;
    color: var(--light-gray);
}
</style>

<?php include_once('layouts/footer.php'); ?>