<?php
$title = 'User Details';
$active = 'users';
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-user-circle"></i>
                User Details: <?php echo Security::escape($user['username']); ?>
            </h1>
            <p>View complete user information</p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/users" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to Users
            </a>
            <?php if (Auth::can('users.manage')): ?>
            <a href="<?php echo BASE_URL; ?>/users/edit/<?php echo $user['id']; ?>" class="btn btn-success">
                <i class="fas fa-edit"></i> Edit User
            </a>
            <?php endif; ?>
            <button class="btn btn-outline" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- User Information -->
    <div class="user-details-grid">
        <!-- Profile Card -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-id-badge"></i> Profile Information</h3>
            </div>
            <div class="card-body">
                <div class="profile-image-container text-center mb-4">
                    <?php if ($user['profile_image']): ?>
                        <img src="<?php echo BASE_URL; ?>/<?php echo $user['profile_image']; ?>" 
                             alt="Profile" class="profile-image img-circle" width="120">
                    <?php else: ?>
                        <div class="profile-image-placeholder">
                            <i class="fas fa-user-circle fa-5x text-muted"></i>
                        </div>
                    <?php endif; ?>
                </div>
                
                <div class="detail-row">
                    <div class="detail-label">Username:</div>
                    <div class="detail-value"><?php echo Security::escape($user['username']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Full Name:</div>
                    <div class="detail-value"><?php echo Security::escape($user['full_name']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">NIS Number:</div>
                    <div class="detail-value"><?php echo Security::escape($user['nis_number']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Rank:</div>
                    <div class="detail-value"><?php echo Security::escape($user['rank']); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Status:</div>
                    <div class="detail-value">
                        <?php if ($user['is_active']): ?>
                            <span class="status-badge status-active">Active</span>
                        <?php else: ?>
                            <span class="status-badge status-inactive">Inactive</span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Contact Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-address-card"></i> Contact Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Email:</div>
                    <div class="detail-value">
                        <a href="mailto:<?php echo Security::escape($user['email']); ?>">
                            <?php echo Security::escape($user['email']); ?>
                        </a>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Phone:</div>
                    <div class="detail-value">
                        <a href="tel:<?php echo Security::escape($user['phone']); ?>">
                            <?php echo Security::escape($user['phone']); ?>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Role Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-user-tag"></i> Role Assignment</h3>
            </div>
            <div class="card-body">
                <?php 
                $roleNames = explode(', ', $user['role_names'] ?? '');
                if (empty($roleNames) || $roleNames[0] === ''): 
                ?>
                    <p class="text-muted">No roles assigned</p>
                <?php else: ?>
                    <?php foreach ($roleNames as $role): ?>
                        <div class="role-item">
                            <i class="fas fa-check-circle text-success"></i>
                            <span><?php echo Security::escape($role); ?></span>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- System Information -->
        <div class="detail-card">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> System Information</h3>
            </div>
            <div class="card-body">
                <div class="detail-row">
                    <div class="detail-label">Last Login:</div>
                    <div class="detail-value">
                        <?php echo $user['last_login'] ? date('d/m/Y H:i:s', strtotime($user['last_login'])) : 'Never'; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Last IP:</div>
                    <div class="detail-value"><?php echo Security::escape($user['last_ip'] ?? 'N/A'); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Created:</div>
                    <div class="detail-value"><?php echo date('d/m/Y H:i:s', strtotime($user['created_at'])); ?></div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">Last Updated:</div>
                    <div class="detail-value"><?php echo date('d/m/Y H:i:s', strtotime($user['updated_at'])); ?></div>
                </div>
            </div>
        </div>

        <!-- Recent Activity -->
        <div class="detail-card full-width">
            <div class="card-header">
                <h3><i class="fas fa-history"></i> Recent Activity</h3>
            </div>
            <div class="card-body">
                <?php if (empty($activities)): ?>
                    <div class="empty-state">
                        <i class="fas fa-history"></i>
                        <p>No activity recorded</p>
                    </div>
                <?php else: ?>
                    <div class="activity-list">
                        <?php foreach ($activities as $activity): ?>
                            <div class="activity-item">
                                <div class="activity-icon">
                                    <?php
                                    $icon = 'fa-info-circle';
                                    if (strpos($activity['action'], 'CREATE') !== false) $icon = 'fa-plus-circle';
                                    elseif (strpos($activity['action'], 'UPDATE') !== false) $icon = 'fa-edit';
                                    elseif (strpos($activity['action'], 'DELETE') !== false) $icon = 'fa-trash';
                                    elseif (strpos($activity['action'], 'LOGIN') !== false) $icon = 'fa-sign-in-alt';
                                    ?>
                                    <i class="fas <?php echo $icon; ?>"></i>
                                </div>
                                <div class="activity-details">
                                    <div class="activity-title">
                                        <?php echo Security::escape($activity['action']); ?>
                                    </div>
                                    <div class="activity-description">
                                        <?php echo Security::escape($activity['description'] ?? ''); ?>
                                        on <?php echo date('d/m/Y H:i:s', strtotime($activity['created_at'])); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Action Buttons -->
    <div class="action-buttons-group">
        <?php if (Auth::can('users.manage') && $user['id'] != Auth::id()): ?>
            <?php if ($user['is_active']): ?>
                <a href="<?php echo BASE_URL; ?>/users/toggle-status/<?php echo $user['id']; ?>" 
                   class="btn btn-warning"
                   onclick="return confirm('Are you sure you want to deactivate this user?')">
                    <i class="fas fa-ban"></i> Deactivate User
                </a>
            <?php else: ?>
                <a href="<?php echo BASE_URL; ?>/users/toggle-status/<?php echo $user['id']; ?>" 
                   class="btn btn-success"
                   onclick="return confirm('Are you sure you want to activate this user?')">
                    <i class="fas fa-check-circle"></i> Activate User
                </a>
            <?php endif; ?>
            
            <?php if ($user['id'] != Auth::id()): ?>
                <a href="<?php echo BASE_URL; ?>/users/delete/<?php echo $user['id']; ?>" 
                   class="btn btn-danger"
                   onclick="return confirm('Are you sure you want to delete this user? This action cannot be undone.')">
                    <i class="fas fa-trash"></i> Delete User
                </a>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

<style>
.user-details-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-bottom: 30px;
}

.detail-card {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    overflow: hidden;
}

.detail-card.full-width {
    grid-column: 1 / -1;
}

.card-header {
    padding: 15px 20px;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
    color: white;
}

.card-header h3 {
    margin: 0;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-body {
    padding: 20px;
}

.detail-row {
    display: flex;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.detail-row:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.detail-label {
    flex: 0 0 120px;
    font-weight: 600;
    color: var(--text-secondary);
}

.detail-value {
    flex: 1;
    color: var(--text-primary);
}

.role-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border-color);
}

.role-item:last-child {
    border-bottom: none;
}

.role-item i {
    color: var(--success-color);
}

.activity-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.activity-item {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 10px;
    background: var(--light-bg);
    border-radius: 8px;
}

.activity-icon {
    width: 40px;
    height: 40px;
    background: white;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-color);
}

.activity-details {
    flex: 1;
}

.activity-title {
    font-weight: 600;
    color: var(--text-primary);
}

.activity-description {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.profile-image-container {
    text-align: center;
    margin-bottom: 20px;
}

.profile-image {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid var(--primary-light);
}

.profile-image-placeholder {
    color: var(--text-light);
}

.action-buttons-group {
    display: flex;
    gap: 15px;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

@media (max-width: 768px) {
    .user-details-grid {
        grid-template-columns: 1fr;
    }
    
    .detail-row {
        flex-direction: column;
    }
    
    .detail-label {
        margin-bottom: 5px;
    }
    
    .action-buttons-group {
        flex-direction: column;
    }
    
    .action-buttons-group .btn {
        width: 100%;
    }
}
</style>

