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

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$employees = $payroll->getAllEmployees();
$summary = $payroll->getPayrollSummary();

payrollModuleStyles();
?>
<?php if (isset($_GET['updated'])): ?>
<div class="alert alert-success alert-dismissible fade show mx-4 mt-3" role="alert">
    Employee record updated successfully.
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

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

    <div class="payroll-page-head">
        <div>
            <h1>Employees</h1>
            <p>Manage HR employee records, contracts, contacts, and payroll assignments.</p>
        </div>
        <div class="no-print">
            <a href="../dashboard/admin.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Admin Dashboard</a>
            <a href="add_employee_form.php" class="btn btn-primary"><i class="fas fa-user-plus"></i> Add Employee</a>
        </div>
    </div>

    <div class="payroll-metrics">
        <div class="payroll-metric"><strong>Total Employees</strong><span><?php echo number_format($summary['employees']); ?></span></div>
        <div class="payroll-metric"><strong>Monthly Payroll</strong><span><?php echo payrollMoney($summary['monthly_payroll']); ?></span></div>
        <div class="payroll-metric"><strong>Active Loans</strong><span><?php echo number_format($summary['active_loans']); ?></span></div>
    </div>

    <div class="payroll-panel">
        <div class="payroll-panel-title">
            <h2>Employee Register</h2>
            <button type="button" class="btn btn-outline-secondary btn-sm no-print" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
        </div>

        <div class="table-responsive">
            <table class="table table-bordered table-hover payroll-table">
                <thead>
                    <tr>
                        <th>Employee</th>
                        <th>Phone</th>
                        <th>Branch</th>
                        <th>Department</th>
                        <th>Position</th>
                        <th>Status</th>
                        <th>Basic Salary</th>
                        <th>Contract</th>
                        <th class="no-print">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($employees)): ?>
                        <?php foreach ($employees as $employee): ?>
                            <tr>
                                <td>
                                    <strong><?php echo payrollText($employee['employee_fullname'] ?? ''); ?></strong><br>
                                    <small class="text-muted"><?php echo payrollText($employee['employee_code'] ?? ('EMP-' . ($employee['employee_id'] ?? ''))); ?></small><br>
                                    <small class="text-muted"><?php echo payrollText($employee['email'] ?? ''); ?></small>
                                </td>
                                <td><?php echo payrollText($employee['employee_phone'] ?? ''); ?></td>
                                <td><?php echo payrollText($employee['branch_name'] ?? $employee['branch'] ?? ''); ?></td>
                                <td><?php echo payrollText($employee['department_name'] ?? ''); ?></td>
                                <td><?php echo payrollText($employee['employee_position'] ?? ''); ?></td>
                                <td><?php echo payrollStatusBadge($employee['employment_status'] ?? 'active'); ?></td>
                                <td>
                                    <?php echo payrollMoney($employee['basic_salary'] ?? 0); ?>
                                    <?php if (!empty($employee['grade_title'])): ?>
                                        <br><small class="text-muted"><i class="fas fa-layer-group me-1"></i><?php echo payrollText($employee['grade_title']); ?></small>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php echo payrollText($employee['contract_start'] ?? ''); ?>
                                    <?php if (!empty($employee['contract_ends'])): ?>
                                        <br><small class="text-muted">Ends: <?php echo payrollText($employee['contract_ends']); ?></small>
                                    <?php endif; ?>
                                </td>
                                <td class="no-print">
                                    <a href="view_employee.php?id=<?php echo (int)$employee['employee_id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i> View</a>
                                    <a href="edit_employee_form.php?id=<?php echo (int)$employee['employee_id']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i> Edit</a>
                                    <a href="delete_employee.php?id=<?php echo (int)$employee['employee_id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this employee record?');"><i class="fas fa-trash"></i> Delete</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="9" class="text-center">No employees found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

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

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