<?php
/**
 * Enterprise HRM ERP System - View User Details
 */

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/check_admin_access.php';

$pageTitle = 'User Details';
$currentModule = 'settings';

$userId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($userId <= 0) {
    redirect(BASE_URL . '/admin/users.php', 'Invalid user ID.', 'error');
}

try {
    $db = getDB();
    
    // Get user details
    $stmt = $db->prepare("
        SELECT u.*, r.role_name, r.role_code, r.role_description
        FROM users u
        INNER JOIN roles r ON u.role_id = r.role_id
        WHERE u.user_id = ?
    ");
    $stmt->execute([$userId]);
    $user = $stmt->fetch();
    
    if (!$user) {
        redirect(BASE_URL . '/admin/users.php', 'User not found.', 'error');
    }
    
    // Get assigned sites
    $stmt = $db->prepare("
        SELECT us.*, s.site_name, s.site_code, s.site_status
        FROM user_sites us
        INNER JOIN sites s ON us.site_id = s.site_id
        WHERE us.user_id = ?
        ORDER BY us.is_primary DESC, s.site_name
    ");
    $stmt->execute([$userId]);
    $userSites = $stmt->fetchAll();
    
    // Get user activity stats
    $stmt = $db->prepare("
        SELECT 
            COUNT(DISTINCT DATE(created_at)) as login_days,
            MAX(last_login) as last_login_time
        FROM user_sessions
        WHERE user_id = ?
    ");
    $stmt->execute([$userId]);
    $activity = $stmt->fetch();
    
} catch (Exception $e) {
    error_log("Error loading user: " . $e->getMessage());
    redirect(BASE_URL . '/admin/users.php', 'Error loading user data.', 'error');
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">User Details</h2>
        <div style="display: flex; gap: 10px;">
            <a href="<?php echo BASE_URL; ?>/admin/users.php" class="btn btn-secondary">Back to Users</a>
            <a href="<?php echo BASE_URL; ?>/admin/users_edit.php?id=<?php echo $userId; ?>" class="btn btn-primary">Edit User</a>
        </div>
    </div>
    
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px; margin-top: 20px;">
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Basic Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Username:</td>
                    <td style="padding: 8px 0;"><strong><?php echo htmlspecialchars($user['username']); ?></strong></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Full Name:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($user['full_name']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Email:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($user['email']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Phone:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($user['phone'] ?? 'N/A'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Role:</td>
                    <td style="padding: 8px 0;">
                        <span class="status-badge" style="background: #e0e7ff; color: #3730a3;">
                            <?php echo htmlspecialchars($user['role_name']); ?> (<?php echo htmlspecialchars($user['role_code']); ?>)
                        </span>
                    </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($user['user_status']); ?>">
                            <?php echo ucfirst($user['user_status']); ?>
                        </span>
                    </td>
                </tr>
            </table>
        </div>
        
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Activity Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Last Login:</td>
                    <td style="padding: 8px 0;"><?php echo $user['last_login'] ? formatDateTime($user['last_login']) : 'Never'; ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Login Days:</td>
                    <td style="padding: 8px 0;"><?php echo number_format($activity['login_days'] ?? 0); ?> days</td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Failed Attempts:</td>
                    <td style="padding: 8px 0;"><?php echo number_format($user['failed_login_attempts']); ?></td>
                </tr>
                <?php if ($user['locked_until']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Locked Until:</td>
                    <td style="padding: 8px 0; color: var(--error-color);"><?php echo formatDateTime($user['locked_until']); ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Created At:</td>
                    <td style="padding: 8px 0;"><?php echo formatDateTime($user['created_at']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Updated At:</td>
                    <td style="padding: 8px 0;"><?php echo formatDateTime($user['updated_at']); ?></td>
                </tr>
            </table>
        </div>
    </div>
    
    <!-- Assigned Sites -->
    <?php if (count($userSites) > 0): ?>
    <div style="margin-top: 30px;">
        <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Assigned Sites</h3>
        <table class="data-table">
            <thead>
                <tr>
                    <th>Site Code</th>
                    <th>Site Name</th>
                    <th>Status</th>
                    <th>Primary</th>
                    <th>Assigned At</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($userSites as $us): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($us['site_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($us['site_name']); ?></td>
                    <td>
                        <span class="status-badge status-<?php echo htmlspecialchars($us['site_status']); ?>">
                            <?php echo ucfirst($us['site_status']); ?>
                        </span>
                    </td>
                    <td>
                        <?php if ($us['is_primary']): ?>
                        <span style="color: var(--primary-color); font-weight: 600;">✓ Primary</span>
                        <?php else: ?>
                        <span style="color: var(--text-secondary);">-</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo formatDateTime($us['assigned_at'] ?? $us['created_at']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<style>
.status-badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    text-transform: uppercase;
}

.status-active {
    background-color: #d1fae5;
    color: #065f46;
}

.status-inactive {
    background-color: #e5e7eb;
    color: #374151;
}

.status-locked {
    background-color: #fee2e2;
    color: #991b1b;
}
</style>

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

