<?php
/**
 * Enterprise HRM ERP System - View Leave Application
 */

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

$pageTitle = 'View Leave Application';
$currentModule = 'leave';

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

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

try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT 
            la.*,
            e.employee_code,
            CONCAT(e.first_name, ' ', COALESCE(e.middle_name, ''), ' ', e.last_name) as employee_name,
            lt.leave_type_name,
            u.full_name as approved_by_name
        FROM leave_applications la
        INNER JOIN employees e ON la.employee_id = e.employee_id
        LEFT JOIN leave_types lt ON la.leave_type_id = lt.leave_type_id
        LEFT JOIN users u ON la.approved_by = u.user_id
        WHERE la.leave_application_id = ? AND la.site_id = ?
    ");
    $stmt->execute([$leaveId, $siteId]);
    $leave = $stmt->fetch();
    
    if (!$leave) {
        redirect(BASE_URL . '/modules/leave/list.php', 'Leave application not found.', 'error');
    }
    
} catch (Exception $e) {
    error_log("Error loading leave: " . $e->getMessage());
    redirect(BASE_URL . '/modules/leave/list.php', 'Error loading leave application.', 'error');
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Leave Application Details</h2>
        <a href="<?php echo BASE_URL; ?>/modules/leave/list.php" class="btn btn-secondary">Back to List</a>
    </div>
    
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px;">
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Application Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Employee:</td>
                    <td style="padding: 8px 0;">
                        <strong><?php echo htmlspecialchars($leave['employee_code']); ?></strong><br>
                        <small><?php echo htmlspecialchars($leave['employee_name']); ?></small>
                    </td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Leave Type:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($leave['leave_type_name'] ?? 'N/A'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Start Date:</td>
                    <td style="padding: 8px 0;"><?php echo formatDate($leave['start_date']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">End Date:</td>
                    <td style="padding: 8px 0;"><?php echo formatDate($leave['end_date']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Total Days:</td>
                    <td style="padding: 8px 0;"><?php echo number_format($leave['total_days'], 1); ?> days</td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Status:</td>
                    <td style="padding: 8px 0;">
                        <span class="status-badge status-<?php echo htmlspecialchars($leave['application_status']); ?>">
                            <?php echo ucfirst($leave['application_status']); ?>
                        </span>
                    </td>
                </tr>
            </table>
        </div>
        
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Additional Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Applied Date:</td>
                    <td style="padding: 8px 0;"><?php echo formatDateTime($leave['applied_at']); ?></td>
                </tr>
                <?php if ($leave['approved_by']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Approved By:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($leave['approved_by_name']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Approved Date:</td>
                    <td style="padding: 8px 0;"><?php echo formatDateTime($leave['approved_at']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($leave['reason']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; vertical-align: top;">Reason:</td>
                    <td style="padding: 8px 0;"><?php echo nl2br(htmlspecialchars($leave['reason'])); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($leave['rejection_reason']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; vertical-align: top;">Rejection Reason:</td>
                    <td style="padding: 8px 0; color: var(--error-color);"><?php echo nl2br(htmlspecialchars($leave['rejection_reason'])); ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

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

