<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h2 class="mb-4">
                <i class="fas fa-user-circle me-2" style="color: var(--nis-primary);"></i>
                My Profile
            </h2>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body text-center">
                    <div class="mb-3">
                        <i class="fas fa-user-circle fa-5x" style="color: var(--nis-primary);"></i>
                    </div>
                    <h4><?= htmlspecialchars($user['full_name']) ?></h4>
                    <p class="text-muted"><?= htmlspecialchars($user['username']) ?></p>
                    <span class="badge bg-<?= $user['is_active'] ? 'success' : 'danger' ?>">
                        <?= $user['is_active'] ? 'Active' : 'Inactive' ?>
                    </span>
                </div>
                <div class="card-footer text-muted">
                    <small>Member since: <?= date('d/m/Y', strtotime($user['created_at'])) ?></small><br>
                    <small>Last login: <?= $user['last_login'] ? date('d/m/Y H:i', strtotime($user['last_login'])) : 'Never' ?></small>
                </div>
            </div>
        </div>
        
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <i class="fas fa-edit me-2"></i> Edit Profile
                </div>
                <div class="card-body">
                    <form method="POST" action="<?= APP_URL ?>/profile/update">
                        <?= $this->csrf_field() ?>
                        
                        <div class="mb-3">
                            <label class="form-label">Full Name</label>
                            <input type="text" name="full_name" class="form-control" 
                                   value="<?= htmlspecialchars($user['full_name']) ?>" required>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Email Address</label>
                            <input type="email" name="email" class="form-control" 
                                   value="<?= htmlspecialchars($user['email']) ?>" required>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Username</label>
                            <input type="text" class="form-control" value="<?= htmlspecialchars($user['username']) ?>" readonly disabled>
                            <small class="text-muted">Username cannot be changed</small>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Role</label>
                            <input type="text" class="form-control" value="<?= $user['role'] ?>" readonly disabled>
                        </div>
                        
                        <button type="submit" class="btn btn-nis">
                            <i class="fas fa-save me-2"></i> Update Profile
                        </button>
                    </form>
                </div>
            </div>
            
            <div class="card mt-4">
                <div class="card-header">
                    <i class="fas fa-key me-2"></i> Change Password
                </div>
                <div class="card-body">
                    <form id="changePasswordForm">
                        <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
                        
                        <div class="mb-3">
                            <label class="form-label">Current Password</label>
                            <input type="password" name="current_password" class="form-control" required>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">New Password</label>
                            <input type="password" name="new_password" class="form-control" required minlength="8">
                            <small class="text-muted">Minimum 8 characters</small>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">Confirm New Password</label>
                            <input type="password" name="confirm_password" class="form-control" required>
                        </div>
                        
                        <button type="submit" class="btn btn-warning">
                            <i class="fas fa-key me-2"></i> Change Password
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$('#changePasswordForm').submit(function(e) {
    e.preventDefault();
    
    const current = $('input[name="current_password"]').val();
    const newPass = $('input[name="new_password"]').val();
    const confirm = $('input[name="confirm_password"]').val();
    
    if (newPass !== confirm) {
        alert('New passwords do not match');
        return;
    }
    
    if (newPass.length < 8) {
        alert('Password must be at least 8 characters');
        return;
    }
    
    $.post('<?= APP_URL ?>/profile/change-password', $(this).serialize(), function(response) {
        if (response.success) {
            alert('Password changed successfully');
            $('#changePasswordForm')[0].reset();
        } else {
            alert('Error: ' + response.error);
        }
    }).fail(function(xhr) {
        alert('Error: ' + xhr.responseJSON?.error || 'Failed to change password');
    });
});
</script>