<?php
/**
 * Enterprise HRM ERP System - Employee Exit List
 * Resigned & Absconded
 */

require_once __DIR__ . '/../../includes/init.php';
requireLogin();

$pageTitle = 'Employee Exit Management';
$currentModule = 'exit';
$currentTab = 'resigned';

$siteId = getCurrentSiteId();

// Get exit records
$exits = [];

try {
    $db = getDB();
    
    // Get exits for this site
    $stmt = $db->prepare("
        SELECT ex.*, e.employee_code, e.first_name, e.last_name, e.middle_name,
               d.department_name, des.designation_name
        FROM employee_exits ex
        INNER JOIN employees e ON ex.employee_id = e.employee_id
        LEFT JOIN departments d ON e.department_id = d.department_id
        LEFT JOIN designations des ON e.designation_id = des.designation_id
        WHERE ex.site_id = ?
        ORDER BY ex.last_working_date DESC, ex.created_at DESC
    ");
    $stmt->execute([$siteId]);
    $exits = $stmt->fetchAll();
    
} catch (Exception $e) {
    error_log("Exit list error: " . $e->getMessage());
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Employee Exit Management</h2>
        <div style="display: flex; gap: 10px;">
            <a href="<?php echo BASE_URL; ?>/modules/exit/resigned_abscondee.php" class="btn btn-primary">Resigned & Abscondee Entry</a>
            <a href="<?php echo BASE_URL; ?>/modules/exit/resigned_abscondee_report.php" class="btn btn-secondary">Resigned & Abscondee Report</a>
        </div>
    </div>
    
    <?php if (count($exits) > 0): ?>
    <div style="margin-top: 20px; overflow-x: auto;">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Employee</th>
                    <th>Department</th>
                    <th>Exit Type</th>
                    <th>Last Working Date</th>
                    <th>Notice Served</th>
                    <th>Final Settlement</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($exits as $exit): ?>
                <tr>
                    <td>
                        <strong><?php echo htmlspecialchars($exit['employee_code']); ?></strong><br>
                        <small><?php echo htmlspecialchars(trim($exit['first_name'] . ' ' . ($exit['middle_name'] ?? '') . ' ' . $exit['last_name'])); ?></small>
                    </td>
                    <td><?php echo htmlspecialchars($exit['department_name'] ?? 'N/A'); ?></td>
                    <td>
                        <span class="status-badge" style="background: <?php 
                            echo $exit['exit_type'] === 'resigned' ? '#dbeafe' : 
                                ($exit['exit_type'] === 'absconded' ? '#fee2e2' : '#fef3c7'); 
                        ?>;">
                            <?php echo ucfirst($exit['exit_type']); ?>
                        </span>
                    </td>
                    <td><?php echo formatDate($exit['last_working_date']); ?></td>
                    <td><?php echo $exit['notice_served_date'] ? formatDate($exit['notice_served_date']) : 'N/A'; ?></td>
                    <td>
                        <?php if ($exit['final_settlement_amount'] > 0): ?>
                        <?php echo formatCurrency($exit['final_settlement_amount']); ?>
                        <?php if ($exit['final_settlement_date']): ?>
                        <br><small><?php echo formatDate($exit['final_settlement_date']); ?></small>
                        <?php endif; ?>
                        <?php else: ?>
                        <span style="color: var(--text-secondary);">Pending</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <span class="status-badge status-<?php echo $exit['exit_status']; ?>">
                            <?php echo ucfirst($exit['exit_status']); ?>
                        </span>
                    </td>
                    <td class="table-actions-cell">
                        <a href="<?php echo BASE_URL; ?>/modules/exit/view.php?id=<?php echo $exit['exit_id']; ?>" 
                           class="btn btn-sm btn-primary">View</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div class="table-empty" style="margin-top: 30px;">
        <div class="table-empty-icon">🚪</div>
        <div class="table-empty-message">No exit records found</div>
    </div>
    <?php endif; ?>
</div>

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

