{% extends "layout.html" %}

{% block content %}
<div class="edit-user-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon"><i class="ti ti-user-edit"></i></span>
                    Edit User: {{ user.username }}
                </h1>
                <p class="page-subtitle">System Administrator - Edit user across companies</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('users.system_admin_users') }}" class="btn btn-secondary">
                    <i class="ti ti-arrow-left"></i>
                    Back to Users
                </a>
            </div>
        </div>
    </div>

    <!-- Main Form -->
    <form method="POST" class="user-edit-form">
        <div class="form-grid">
            <!-- Basic Information -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-user"></i></span>
                        Basic Information
                    </h2>
                </div>
                <div class="card-body">
                    
                    <div class="form-group">
                        <label for="username">Username</label>
                        <input type="text" id="username" name="username" 
                               value="{{ user.username }}" required 
                               class="form-control">
                    </div>

                    <div class="form-group">
                        <label for="email">Email</label>
                        <input type="email" id="email" name="email" 
                               value="{{ user.email }}" required 
                               class="form-control">
                    </div>
                </div>
            </div>

            <!-- Company & Team Assignment -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-building"></i></span>
                        Company & Team
                    </h2>
                </div>
                <div class="card-body">
                    
                    <div class="form-group">
                        <label for="company_id">Company</label>
                        <select id="company_id" name="company_id" required class="form-control">
                            {% for company in companies %}
                            <option value="{{ company.id }}" 
                                    {% if company.id == user.company_id %}selected{% endif %}>
                                {{ company.name }}
                                {% if company.is_personal %}(Personal){% endif %}
                            </option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="team_id">Team (Optional)</label>
                        <select id="team_id" name="team_id" class="form-control">
                            <option value="">No Team</option>
                            {% for team in teams %}
                            <option value="{{ team.id }}" 
                                    {% if team.id == user.team_id %}selected{% endif %}>
                                {{ team.name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>

            <!-- Role & Permissions -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-shield-check"></i></span>
                        Role & Permissions
                    </h2>
                </div>
                <div class="card-body">
                    
                    <div class="form-group">
                        <label for="role">Role</label>
                        <select id="role" name="role" required class="form-control">
                            {% for role in roles %}
                            <option value="{{ role.name }}" 
                                    {% if role == user.role %}selected{% endif %}>
                                {{ role.value }}
                            </option>
                            {% endfor %}
                        </select>
                        {% if user.role == Role.SYSTEM_ADMIN %}
                        <small class="form-text warning-text"><i class="ti ti-alert-triangle"></i> Warning: This user is a System Administrator</small>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Account Status -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-settings"></i></span>
                        Account Status
                    </h2>
                </div>
                <div class="card-body">
                    
                    <div class="form-group">
                        <label class="toggle-label">
                            <input type="checkbox" name="is_verified" 
                                   {% if user.is_verified %}checked{% endif %}>
                            <span class="toggle-slider"></span>
                            <span class="toggle-text">Email Verified</span>
                        </label>
                        <small class="form-text">Whether the user's email address has been verified</small>
                    </div>

                    <div class="form-group">
                        <label class="toggle-label">
                            <input type="checkbox" name="is_blocked" 
                                   {% if user.is_blocked %}checked{% endif %}>
                            <span class="toggle-slider"></span>
                            <span class="toggle-text">Account Blocked</span>
                        </label>
                        <small class="form-text">Blocked users cannot log in to the system</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- User Information Display -->
        <div class="card full-width">
            <div class="card-header">
                <h2 class="card-title">
                    <span class="icon"><i class="ti ti-info-circle"></i></span>
                    User Information
                </h2>
            </div>
            <div class="card-body">
                <div class="info-grid">
                    <div class="info-item">
                        <label>Account Type:</label>
                        <span class="badge {% if user.account_type == AccountType.FREELANCER %}badge-freelancer{% else %}badge-company{% endif %}">
                            {{ user.account_type.value }}
                        </span>
                    </div>
                    
                    <div class="info-item">
                        <label>Created:</label>
                        <span>{{ user.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
                    </div>
                    
                    {% if user.business_name %}
                    <div class="info-item">
                        <label>Business Name:</label>
                        <span>{{ user.business_name }}</span>
                    </div>
                    {% endif %}
                    
                    <div class="info-item">
                        <label>2FA Enabled:</label>
                        <span class="{% if user.two_factor_enabled %}text-success{% else %}text-muted{% endif %}">
                            {{ 'Yes' if user.two_factor_enabled else 'No' }}
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Form Actions -->
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">
                <i class="ti ti-device-floppy"></i>
                Save Changes
            </button>
            <a href="{{ url_for('users.system_admin_users') }}" class="btn btn-secondary">Cancel</a>
        </div>
    </form>

    <!-- Danger Zone -->
    {% if user.id != g.user.id and not (user.role == Role.SYSTEM_ADMIN and user.id == g.user.id) %}
    <div class="danger-zone">
        <div class="danger-header">
            <h2 class="danger-title">
                <i class="ti ti-alert-triangle"></i>
                Danger Zone
            </h2>
        </div>
        <div class="danger-content">
            <div class="danger-item">
                <div class="danger-info">
                    <h4>Delete User Account</h4>
                    <p>Permanently delete this user account. This will also delete all their time entries and cannot be undone.</p>
                </div>
                <div class="danger-actions">
                    <form method="POST" action="{{ url_for('users.system_admin_delete_user', user_id=user.id) }}" 
                          onsubmit="return confirm('Are you sure you want to delete user \'{{ user.username }}\'? This will also delete all their time entries and cannot be undone.')">
                        <button type="submit" class="btn btn-danger">
                            <i class="ti ti-trash"></i>
                            Delete User
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% endif %}
</div>

<script>
// Dynamic team loading when company changes
document.getElementById('company_id').addEventListener('change', function() {
    const companyId = this.value;
    const teamSelect = document.getElementById('team_id');
    
    // Clear current teams
    teamSelect.innerHTML = '<option value="">No Team</option>';
    
    if (companyId) {
        // Fetch teams for the selected company
        fetch(`/api/teams?company_id=${companyId}`)
            .then(response => response.json())
            .then(data => {
                if (data.success && data.teams) {
                    data.teams.forEach(team => {
                        const option = document.createElement('option');
                        option.value = team.id;
                        option.textContent = team.name;
                        teamSelect.appendChild(option);
                    });
                }
            })
            .catch(error => {
                console.error('Error fetching teams:', error);
            });
    }
});
</script>

<style>
/* Container */
.edit-user-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

/* Form Grid */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

/* Cards */
.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    overflow: hidden;
    transition: all 0.3s ease;
}

.card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

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

.card-header {
    background: #f8f9fa;
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1f2937;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-title .icon {
    font-size: 1.5rem;
}

.card-body {
    padding: 1.5rem;
}

/* Form Elements */
.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: #374151;
}

.form-control {
    width: 100%;
    padding: 0.625rem 1rem;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-size: 1rem;
    transition: all 0.2s ease;
}

.form-control:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-text {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.875rem;
    color: #6b7280;
}

.warning-text {
    color: #dc2626 !important;
    font-weight: 500;
}

/* Toggle Switches */
.toggle-label {
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
    margin-bottom: 0.5rem;
}

.toggle-label input[type="checkbox"] {
    display: none;
}

.toggle-slider {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 24px;
    background: #e5e7eb;
    border-radius: 24px;
    transition: background 0.3s;
    flex-shrink: 0;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: white;
    top: 2px;
    left: 2px;
    transition: transform 0.3s;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider {
    background: #667eea;
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider::before {
    transform: translateX(26px);
}

.toggle-text {
    font-weight: 500;
    color: #1f2937;
}

/* Info Grid */
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 1.5rem;
}

.info-item {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.info-item label {
    font-weight: 600;
    color: #6b7280;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Badges */
.badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    display: inline-block;
    width: fit-content;
}

.badge-company {
    background: #dbeafe;
    color: #1e40af;
}

.badge-freelancer {
    background: #d1fae5;
    color: #065f46;
}

.text-success {
    color: #10b981;
    font-weight: 500;
}

.text-muted {
    color: #6b7280;
}

/* Form Actions */
.form-actions {
    display: flex;
    gap: 1rem;
    padding: 2rem;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    margin-bottom: 2rem;
}

/* Danger Zone */
.danger-zone {
    background: #fef2f2;
    border: 2px solid #fecaca;
    border-radius: 12px;
    overflow: hidden;
}

.danger-header {
    background: #fee2e2;
    padding: 1.5rem;
    border-bottom: 1px solid #fecaca;
}

.danger-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #991b1b;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.danger-content {
    padding: 2rem;
}

.danger-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 2rem;
}

.danger-info h4 {
    margin: 0 0 0.5rem 0;
    color: #991b1b;
    font-weight: 600;
}

.danger-info p {
    margin: 0;
    color: #7f1d1d;
    font-size: 0.9rem;
    line-height: 1.5;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-secondary {
    background: white;
    color: #667eea;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
}

.btn-danger {
    background: #dc2626;
    color: white;
}

.btn-danger:hover {
    background: #b91c1c;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 38, 38, 0.3);
}

/* Responsive Design */
@media (max-width: 768px) {
    .edit-user-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .form-grid {
        grid-template-columns: 1fr;
    }
    
    .danger-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: slideIn 0.3s ease-out;
    animation-fill-mode: both;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
.card:nth-child(4) { animation-delay: 0.4s; }
</style>

<script>
// Update teams when company changes
document.getElementById('company_id').addEventListener('change', function() {
    const companyId = this.value;
    const teamSelect = document.getElementById('team_id');
    
    // Clear current options except "No Team"
    teamSelect.innerHTML = '<option value="">No Team</option>';
    
    // Fetch teams for the selected company
    if (companyId) {
        fetch(`/api/companies/${companyId}/teams`)
            .then(response => response.json())
            .then(teams => {
                teams.forEach(team => {
                    const option = document.createElement('option');
                    option.value = team.id;
                    option.textContent = team.name;
                    teamSelect.appendChild(option);
                });
            })
            .catch(error => {
                console.error('Error fetching teams:', error);
            });
    }
});
</script>
{% endblock %}