<?php
/**
 * Enterprise HRM ERP System - View Employee Exit
 */

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

$pageTitle = 'View Exit Record';
$currentModule = 'exit';

$siteId = getCurrentSiteId();
$exitId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($exitId <= 0) {
    redirect(BASE_URL . '/modules/exit/list.php', 'Invalid exit ID.', 'error');
}

try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT ex.*, e.employee_code, e.first_name, e.last_name, e.middle_name,
               d.department_name, des.designation_name,
               u.full_name as created_by_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
        LEFT JOIN users u ON ex.created_by = u.user_id
        WHERE ex.exit_id = ? AND ex.site_id = ?
    ");
    $stmt->execute([$exitId, $siteId]);
    $exit = $stmt->fetch();
    
    if (!$exit) {
        redirect(BASE_URL . '/modules/exit/list.php', 'Exit record not found.', 'error');
    }
    
} catch (Exception $e) {
    error_log("Error loading exit: " . $e->getMessage());
    redirect(BASE_URL . '/modules/exit/list.php', 'Error loading exit data.', 'error');
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Employee Exit Details</h2>
        <a href="<?php echo BASE_URL; ?>/modules/exit/list.php" class="btn btn-secondary">Back to Exits</a>
    </div>
    
    <div style="margin-top: 20px;">
        <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px;">
            <div>
                <h3 style="margin-bottom: 15px;">Employee Information</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Employee Code</strong></td>
                        <td><?php echo htmlspecialchars($exit['employee_code']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Employee Name</strong></td>
                        <td><?php echo htmlspecialchars(trim($exit['first_name'] . ' ' . ($exit['middle_name'] ?? '') . ' ' . $exit['last_name'])); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Department</strong></td>
                        <td><?php echo htmlspecialchars($exit['department_name'] ?? 'N/A'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Designation</strong></td>
                        <td><?php echo htmlspecialchars($exit['designation_name'] ?? 'N/A'); ?></td>
                    </tr>
                </table>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px;">Exit Details</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Exit Type</strong></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>
                    </tr>
                    <tr>
                        <td><strong>Last Working Date</strong></td>
                        <td><?php echo formatDate($exit['last_working_date']); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Notice Served Date</strong></td>
                        <td><?php echo $exit['notice_served_date'] ? formatDate($exit['notice_served_date']) : 'N/A'; ?></td>
                    </tr>
                    <tr>
                        <td><strong>Notice Period</strong></td>
                        <td><?php echo $exit['notice_period_days']; ?> days</td>
                    </tr>
                    <tr>
                        <td><strong>Status</strong></td>
                        <td>
                            <span class="status-badge status-<?php echo $exit['exit_status']; ?>">
                                <?php echo ucfirst($exit['exit_status']); ?>
                            </span>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div>
                <h3 style="margin-bottom: 15px;">Final Settlement</h3>
                <table class="data-table">
                    <tr>
                        <td><strong>Settlement Amount</strong></td>
                        <td style="text-align: right;">
                            <strong><?php echo formatCurrency($exit['final_settlement_amount']); ?></strong>
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Settlement Date</strong></td>
                        <td><?php echo $exit['final_settlement_date'] ? formatDate($exit['final_settlement_date']) : 'Pending'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
        
        <?php if ($exit['exit_reason']): ?>
        <div style="margin-top: 30px;">
            <h3 style="margin-bottom: 15px;">Exit Reason</h3>
            <p style="padding: 15px; background: #f9fafb; border-radius: 6px;">
                <?php echo nl2br(htmlspecialchars($exit['exit_reason'])); ?>
            </p>
        </div>
        <?php endif; ?>
    </div>
</div>

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

