<?php
/**
 * Enterprise HRM ERP System - Departments & Designations Management
 */

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/check_admin_access.php';

$pageTitle = 'Departments & Designations';
$currentModule = 'settings';

$siteId = getCurrentSiteId();
$tab = $_GET['tab'] ?? 'departments'; // departments or designations

// Departments
$departments = [];
try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT d.*, COUNT(DISTINCT e.employee_id) as employee_count
        FROM departments d
        LEFT JOIN employees e ON d.department_id = e.department_id AND e.site_id = ?
        WHERE d.site_id = ?
        GROUP BY d.department_id
        ORDER BY d.department_name
    ");
    $stmt->execute([$siteId, $siteId]);
    $departments = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Departments error: " . $e->getMessage());
}

// Designations
$designations = [];
try {
    $stmt = $db->prepare("
        SELECT des.*, COUNT(DISTINCT e.employee_id) as employee_count
        FROM designations des
        LEFT JOIN employees e ON des.designation_id = e.designation_id AND e.site_id = ?
        WHERE des.site_id = ?
        GROUP BY des.designation_id
        ORDER BY des.designation_name
    ");
    $stmt->execute([$siteId, $siteId]);
    $designations = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Designations error: " . $e->getMessage());
}

include __DIR__ . '/../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Departments & Designations</h2>
        <a href="<?php echo BASE_URL; ?>/modules/settings/index.php" class="btn btn-secondary">Back to Settings</a>
    </div>
    
    <!-- Tabs -->
    <div style="display: flex; gap: 10px; margin-top: 20px; border-bottom: 2px solid var(--border-color);">
        <button onclick="switchTab('departments')" 
                style="padding: 10px 20px; background: none; border: none; border-bottom: 2px solid <?php echo $tab === 'departments' ? 'var(--primary-color)' : 'transparent'; ?>; 
                       color: <?php echo $tab === 'departments' ? 'var(--primary-color)' : 'var(--text-secondary)'; ?>; 
                       font-weight: <?php echo $tab === 'departments' ? '600' : '400'; ?>; cursor: pointer;">
            🏛️ Departments
        </button>
        <button onclick="switchTab('designations')" 
                style="padding: 10px 20px; background: none; border: none; border-bottom: 2px solid <?php echo $tab === 'designations' ? 'var(--primary-color)' : 'transparent'; ?>; 
                       color: <?php echo $tab === 'designations' ? 'var(--primary-color)' : 'var(--text-secondary)'; ?>; 
                       font-weight: <?php echo $tab === 'designations' ? '600' : '400'; ?>; cursor: pointer;">
            💼 Designations
        </button>
    </div>
    
    <!-- Departments Tab -->
    <?php if ($tab === 'departments'): ?>
    <div style="margin-top: 20px;">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
            <h3>Departments</h3>
            <a href="<?php echo BASE_URL; ?>/admin/departments_create.php" class="btn btn-primary">Add Department</a>
        </div>
        
        <?php if (count($departments) > 0): ?>
        <table class="data-table">
            <thead>
                <tr>
                    <th>Department Code</th>
                    <th>Department Name</th>
                    <th>Description</th>
                    <th>Employees</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($departments as $dept): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($dept['department_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($dept['department_name']); ?></td>
                    <td><?php echo htmlspecialchars($dept['department_description'] ?? 'N/A'); ?></td>
                    <td><?php echo number_format($dept['employee_count']); ?></td>
                    <td>
                        <span class="status-badge status-<?php echo htmlspecialchars($dept['department_status']); ?>">
                            <?php echo ucfirst($dept['department_status']); ?>
                        </span>
                    </td>
                    <td class="table-actions-cell">
                        <a href="<?php echo BASE_URL; ?>/admin/departments_edit.php?id=<?php echo $dept['department_id']; ?>" 
                           class="btn btn-sm btn-primary">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div class="table-empty">
            <div class="table-empty-icon">🏛️</div>
            <div class="table-empty-message">No departments found</div>
            <div class="table-empty-submessage">
                <a href="<?php echo BASE_URL; ?>/admin/departments_create.php" class="btn btn-primary">Add First Department</a>
            </div>
        </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    
    <!-- Designations Tab -->
    <?php if ($tab === 'designations'): ?>
    <div style="margin-top: 20px;">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
            <h3>Designations</h3>
            <a href="<?php echo BASE_URL; ?>/admin/designations_create.php" class="btn btn-primary">Add Designation</a>
        </div>
        
        <?php if (count($designations) > 0): ?>
        <table class="data-table">
            <thead>
                <tr>
                    <th>Designation Code</th>
                    <th>Designation Name</th>
                    <th>Description</th>
                    <th>Employees</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($designations as $desg): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($desg['designation_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($desg['designation_name']); ?></td>
                    <td><?php echo htmlspecialchars($desg['designation_description'] ?? 'N/A'); ?></td>
                    <td><?php echo number_format($desg['employee_count']); ?></td>
                    <td>
                        <span class="status-badge status-<?php echo htmlspecialchars($desg['designation_status']); ?>">
                            <?php echo ucfirst($desg['designation_status']); ?>
                        </span>
                    </td>
                    <td class="table-actions-cell">
                        <a href="<?php echo BASE_URL; ?>/admin/designations_edit.php?id=<?php echo $desg['designation_id']; ?>" 
                           class="btn btn-sm btn-primary">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div class="table-empty">
            <div class="table-empty-icon">💼</div>
            <div class="table-empty-message">No designations found</div>
            <div class="table-empty-submessage">
                <a href="<?php echo BASE_URL; ?>/admin/designations_create.php" class="btn btn-primary">Add First Designation</a>
            </div>
        </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>
</div>

<style>
.status-badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    text-transform: uppercase;
}

.status-active {
    background-color: #d1fae5;
    color: #065f46;
}

.status-inactive {
    background-color: #e5e7eb;
    color: #374151;
}
</style>

<script>
function switchTab(tab) {
    window.location.href = '?tab=' + tab;
}
</script>

<?php include __DIR__ . '/../includes/footer.php'; ?>

