<?php
include '../../includes/header.php';
require_once __DIR__ . '/_module.php';

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!$payroll->tableExists('hr_departments')) {
        $error = 'Department tables are not installed yet.';
    } elseif (trim($_POST['department_name'] ?? '') !== '' && $payroll->createDepartment($_POST)) {
        header('Location: departments.php');
        exit();
    } else {
        $error = 'Failed to save department.';
    }
}

$departments = $payroll->getDepartments();

payrollModuleStyles();
?>

<div class="container-fluid px-4">
    <?php payrollModuleNav('departments'); ?>

    <div class="payroll-page-head">
        <div>
            <h1>Departments</h1>
            <p>Maintain organizational departments for reporting, approvals, and payroll grouping.</p>
        </div>
        <a href="employee_list.php" class="btn btn-outline-secondary no-print"><i class="fas fa-users"></i> Employees</a>
    </div>

    <?php payrollMigrationNotice($payroll, ['hr_departments'], 'Departments require the professional HR schema.'); ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <div class="row g-3">
        <div class="col-lg-4">
            <div class="payroll-panel">
                <div class="payroll-panel-title"><h2>Add Department</h2></div>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label" for="department_name">Department Name</label>
                        <input type="text" class="form-control" id="department_name" name="department_name" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="department_code">Department Code</label>
                        <input type="text" class="form-control" id="department_code" name="department_code">
                    </div>
                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" id="is_active" name="is_active" checked>
                        <label class="form-check-label" for="is_active">Active</label>
                    </div>
                    <button type="submit" class="btn btn-primary" <?php echo $payroll->tableExists('hr_departments') ? '' : 'disabled'; ?>><i class="fas fa-save"></i> Save Department</button>
                </form>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="payroll-panel">
                <div class="payroll-panel-title"><h2>Department Register</h2></div>
                <div class="table-responsive">
                    <table class="table table-bordered payroll-table">
                        <thead>
                            <tr>
                                <th>Department</th>
                                <th>Code</th>
                                <th>Status</th>
                                <th>Created</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($departments)): ?>
                                <?php foreach ($departments as $department): ?>
                                    <tr>
                                        <td><?php echo payrollText($department['department_name'] ?? ''); ?></td>
                                        <td><?php echo payrollText($department['department_code'] ?? ''); ?></td>
                                        <td><?php echo !empty($department['is_active']) ? '<span class="badge bg-success">Active</span>' : '<span class="badge bg-secondary">Inactive</span>'; ?></td>
                                        <td><?php echo payrollText($department['created_at'] ?? ''); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr><td colspan="4" class="text-center">No departments found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <?php payrollModuleEnd(); ?>
</div>

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