<?php

declare(strict_types=1);

ob_start();

?>

<div class="d-flex justify-content-between align-items-center mb-4">

    <div>
        <h2 class="mb-1">
            Employee Management
        </h2>

        <p class="text-muted mb-0">
            Manage personnel records and biometric registration status.
        </p>
    </div>

    <a
        href="/public/employees/create"
        class="btn btn-primary">

        <i class="bi bi-person-plus me-1"></i>
        Add Employee

    </a>

</div>

<?php if (!empty($created)): ?>

    <div class="alert alert-success">
        Employee created successfully.
    </div>

<?php endif; ?>

<?php if (!empty($updated)): ?>

    <div class="alert alert-success">
        Employee updated successfully.
    </div>

<?php endif; ?>

<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <?= htmlspecialchars(
            (string) $error,
            ENT_QUOTES,
            'UTF-8'
        ) ?>

    </div>

<?php endif; ?>

<div class="card shadow-sm border-0">

    <div class="card-body">

        <div class="table-responsive">

            <table
                class="table table-hover align-middle"
                id="employeesTable">

                <thead>

                    <tr>
                        <th>Employee ID</th>
                        <th>Name</th>
                        <th>Gender</th>
                        <th>Rank</th>
                        <th>Department</th>
                        <th>Position</th>
                        <th>Employment</th>
                        <th>Biometric</th>
                        <th class="text-end">
                            Actions
                        </th>
                    </tr>

                </thead>

                <tbody>

                <?php if (empty($employees)): ?>

                    <tr>

                        <td
                            colspan="9"
                            class="text-center text-muted py-5">

                            No employee records found.

                        </td>

                    </tr>

                <?php else: ?>

                    <?php foreach (
                        $employees as $employee
                    ): ?>

                        <tr>

                            <td>

                                <strong>

                                    <?= htmlspecialchars(
                                        (string) $employee['employee_id'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </strong>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    trim(
                                        (string) $employee['first_name']
                                        . ' '
                                        . (string) (
                                            $employee['middle_name']
                                            ?? ''
                                        )
                                        . ' '
                                        . (string) $employee['last_name']
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    ucfirst(
                                        strtolower(
                                            (string) $employee['gender']
                                        )
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    (string) (
                                        $employee['rank_name']
                                        ?? '—'
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    (string) (
                                        $employee['department_name']
                                        ?? '—'
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    (string) (
                                        $employee['position_name']
                                        ?? '—'
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <?php

                                $employmentStatus =
                                    (string) (
                                        $employee[
                                            'employment_status'
                                        ]
                                        ?? 'INACTIVE'
                                    );

                                ?>

                                <span class="badge <?= $employmentStatus === 'ACTIVE'
                                    ? 'text-bg-success'
                                    : 'text-bg-secondary' ?>">

                                    <?= htmlspecialchars(
                                        ucwords(
                                            strtolower(
                                                str_replace(
                                                    '_',
                                                    ' ',
                                                    $employmentStatus
                                                )
                                            )
                                        ),
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </span>

                            </td>

                            <td>

                                <?php

                                $biometricStatus =
                                    (string) (
                                        $employee[
                                            'biometric_status'
                                        ]
                                        ?? 'NOT_REGISTERED'
                                    );

                                ?>

                                <span class="badge <?= in_array(
                                    $biometricStatus,
                                    [
                                        'REGISTERED',
                                        'VERIFIED'
                                    ],
                                    true
                                )
                                    ? 'text-bg-success'
                                    : 'text-bg-warning' ?>">

                                    <?= htmlspecialchars(
                                        ucwords(
                                            strtolower(
                                                str_replace(
                                                    '_',
                                                    ' ',
                                                    $biometricStatus
                                                )
                                            )
                                        ),
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </span>

                            </td>

                            <td class="text-end">

                                <div class="btn-group">

                                    <a
                                        href="/public/employees/show?id=<?= (int) $employee['id'] ?>"
                                        class="btn btn-sm btn-outline-secondary"
                                        title="View Profile">

                                        <i class="bi bi-eye"></i>

                                    </a>

                                    <a
                                        href="/public/employees/edit?id=<?= (int) $employee['id'] ?>"
                                        class="btn btn-sm btn-outline-primary"
                                        title="Edit Employee">

                                        <i class="bi bi-pencil"></i>

                                    </a>

                                </div>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'Employee Management';

include APP_PATH
    . '/Modules/Administration/Views/layouts/admin.php';