@extends('admin.layout')

@section('header-title', 'Kullanıcı Düzenle - ' . $user->name)

@push('styles')
<style>
    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 32px;
        flex-wrap: wrap;
        gap: 16px;
    }
    .page-title {
        font-size: 24px;
        font-weight: 800;
        color: #1f2937;
    }
    .page-subtitle {
        font-size: 14px;
        color: #6b7280;
        margin-top: 4px;
    }

    .card {
        background: white;
        border: 1px solid #e5e7eb;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    }
    .card-body {
        padding: 20px;
    }

    .alert-error {
        background: #fef2f2;
        border: 1px solid #fecaca;
        color: #991b1b;
        padding: 12px 16px;
        border-radius: 8px;
        margin-bottom: 20px;
        font-size: 13px;
        font-weight: 500;
    }
    .alert-error ul {
        margin: 8px 0 0 0;
        padding-left: 20px;
    }

    .form-group {
        margin-bottom: 20px;
    }
    .form-label {
        display: block;
        font-weight: 600;
        color: #1f2937;
        margin-bottom: 8px;
        font-size: 14px;
    }
    .form-input,
    .form-select,
    .form-textarea {
        width: 100%;
        padding: 10px 14px;
        border: 1px solid #d1d5db;
        border-radius: 8px;
        font-size: 14px;
        font-family: inherit;
        background: white;
        color: #1f2937;
        transition: all 0.2s ease;
    }
    .form-input:focus,
    .form-select:focus,
    .form-textarea:focus {
        outline: none;
        border-color: #1e40af;
        box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.1);
    }
    .form-textarea {
        resize: vertical;
        min-height: 100px;
    }

    .form-error {
        margin-top: 4px;
        font-size: 12px;
        color: #dc2626;
        display: flex;
        align-items: center;
        gap: 4px;
    }

    .form-hint {
        margin-top: 4px;
        font-size: 12px;
        color: #6b7280;
    }

    .password-section {
        background: #f9fafb;
        border: 1px solid #e5e7eb;
        border-radius: 8px;
        padding: 16px;
        margin-top: 20px;
    }
    .password-section h3 {
        font-size: 14px;
        font-weight: 600;
        color: #1f2937;
        margin-bottom: 12px;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .checkbox-group {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 12px;
    }
    .checkbox-group input[type="checkbox"] {
        cursor: pointer;
    }
    .checkbox-group label {
        cursor: pointer;
        font-size: 14px;
        color: #1f2937;
    }

    .avatar-preview {
        width: 100px;
        height: 100px;
        border-radius: 8px;
        border: 2px dashed #d1d5db;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f9fafb;
        margin-bottom: 12px;
        overflow: hidden;
        object-fit: cover;
    }

    .form-actions {
        display: flex;
        gap: 12px;
        margin-top: 28px;
        padding-top: 20px;
        border-top: 1px solid #e5e7eb;
    }
    .btn {
        padding: 11px 24px;
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    .btn-primary {
        background: #1e40af;
        color: white;
    }
    .btn-primary:hover {
        background: #1e3a8a;
    }
    .btn-secondary {
        background: #f3f4f6;
        color: #374151;
        border: 1px solid #d1d5db;
    }
    .btn-secondary:hover {
        background: #e5e7eb;
    }

    .two-columns {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 20px;
    }
    @media (max-width: 768px) {
        .two-columns {
            grid-template-columns: 1fr;
        }
    }
</style>
@endpush

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">✏️ Kullanıcı Düzenle</h1>
        <p class="page-subtitle">{{ $user->name }} kullanıcısının bilgilerini güncelleyin</p>
    </div>
</div>

@if($errors->any())
    <div class="alert-error">
        <strong>✗ Hata!</strong>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form method="POST" action="{{ route('admin.users.update', $user) }}" enctype="multipart/form-data" class="card">
    <div class="card-body">
    @csrf
    @method('PUT')

    <div class="two-columns">
        {{-- Name --}}
        <div class="form-group">
            <label class="form-label">Ad Soyad *</label>
            <input type="text" name="name" class="form-input @error('name') is-invalid @enderror" 
                   value="{{ old('name', $user->name) }}" required>
            @error('name')
                <div class="form-error">
                    <svg style="width:14px;height:14px;" fill="currentColor" viewBox="0 0 24 24">
                        <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
                    </svg>
                    {{ $message }}
                </div>
            @enderror
        </div>

        {{-- Email --}}
        <div class="form-group">
            <label class="form-label">E-posta *</label>
            <input type="email" name="email" class="form-input @error('email') is-invalid @enderror" 
                   value="{{ old('email', $user->email) }}" required>
            @error('email')
                <div class="form-error">
                    <svg style="width:14px;height:14px;" fill="currentColor" viewBox="0 0 24 24">
                        <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
                    </svg>
                    {{ $message }}
                </div>
            @enderror
        </div>
    </div>

    <div class="two-columns">
        {{-- Role --}}
        <div class="form-group">
            <label class="form-label">Rol *</label>
            <select name="role" class="form-select @error('role') is-invalid @enderror" required>
                <option value="user" {{ old('role', $user->role) === 'user' ? 'selected' : '' }}>Kullanıcı</option>
                <option value="editor" {{ old('role', $user->role) === 'editor' ? 'selected' : '' }}>Editör</option>
                <option value="moderator" {{ old('role', $user->role) === 'moderator' ? 'selected' : '' }}>Moderatör</option>
                @if(auth()->user()->role === 'admin')
                    <option value="admin" {{ old('role', $user->role) === 'admin' ? 'selected' : '' }}>Admin</option>
                @endif
            </select>
            @error('role')
                <div class="form-error">{{ $message }}</div>
            @enderror
        </div>

        {{-- Status --}}
        <div class="form-group">
            <label class="form-label">Durum *</label>
            <select name="status" class="form-select @error('status') is-invalid @enderror" required>
                <option value="active" {{ old('status', $user->status) === 'active' ? 'selected' : '' }}>Aktif</option>
                <option value="inactive" {{ old('status', $user->status) === 'inactive' ? 'selected' : '' }}>Pasif</option>
                <option value="banned" {{ old('status', $user->status) === 'banned' ? 'selected' : '' }}>Yasaklı</option>
                <option value="suspended" {{ old('status', $user->status) === 'suspended' ? 'selected' : '' }}>Askıya Alınmış</option>
            </select>
            @error('status')
                <div class="form-error">{{ $message }}</div>
            @enderror
        </div>
    </div>

    <div class="two-columns">
        {{-- Points --}}
        <div class="form-group">
            <label class="form-label">Puan</label>
            <input type="number" name="points" class="form-input @error('points') is-invalid @enderror" 
                   value="{{ old('points', $user->points ?? 0) }}" min="0">
            @error('points')
                <div class="form-error">{{ $message }}</div>
            @enderror
        </div>

        {{-- Phone --}}
        <div class="form-group">
            <label class="form-label">Telefon</label>
            <input type="tel" name="phone" class="form-input @error('phone') is-invalid @enderror" 
                   value="{{ old('phone', $user->phone) }}" placeholder="+90 5XX XXX XXXX">
            @error('phone')
                <div class="form-error">{{ $message }}</div>
            @enderror
        </div>
    </div>

    {{-- Bio --}}
    <div class="form-group">
        <label class="form-label">Biyografi</label>
        <textarea name="bio" class="form-textarea @error('bio') is-invalid @enderror" 
                  placeholder="Kullanıcı hakkında kısa bilgi...">{{ old('bio', $user->bio) }}</textarea>
        @error('bio')
            <div class="form-error">{{ $message }}</div>
        @enderror
    </div>

    {{-- Avatar --}}
    <div class="form-group">
        <label class="form-label">Avatar Resmi</label>
        @if($user->avatar)
            <div>
                <img src="{{ asset('storage/' . $user->avatar) }}" class="avatar-preview" alt="{{ $user->name }}">
                <div class="form-hint">Mevcut resim görülüyor. Yeni bir resim yüklemek için dosya seçin.</div>
            </div>
        @else
            <div class="avatar-preview" id="avatarPreview" style="display:none;"></div>
            <div class="form-hint">PNG, JPG veya GIF formatında, maksimum 2MB</div>
        @endif
        <input type="file" name="avatar" class="form-input @error('avatar') is-invalid @enderror" 
               accept="image/*" onchange="previewAvatar(this)">
        @error('avatar')
            <div class="form-error">{{ $message }}</div>
        @enderror
    </div>

    {{-- Password Reset Section --}}
    <div class="password-section">
        <h3>
            <svg style="width:18px;height:18px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
            </svg>
            Şifre Sıfırla
        </h3>

        <div class="checkbox-group">
            <input type="checkbox" id="resetPassword" name="reset_password" value="1" onchange="togglePasswordFields()">
            <label for="resetPassword">Kullanıcının şifresini sıfırla</label>
        </div>

        <div id="passwordFields" style="display:none;">
            <div class="form-group">
                <label class="form-label">Yeni Şifre *</label>
                <input type="password" name="new_password" class="form-input @error('new_password') is-invalid @enderror" 
                       placeholder="Minimum 8 karakter">
                @error('new_password')
                    <div class="form-error">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label class="form-label">Şifreyi Onayla *</label>
                <input type="password" name="new_password_confirmation" class="form-input" 
                       placeholder="Şifreyi tekrar girin">
            </div>
        </div>
    </div>

    {{-- Actions --}}
    <div class="form-actions">
        <button type="submit" class="btn btn-primary">
            <svg style="width:16px;height:16px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
            </svg>
            Kaydet
        </button>
        <a href="{{ route('admin.users.index') }}" class="btn btn-secondary">
            İptal
        </a>
    </div>
    </div>
</form>

@push('scripts')
<script>
function togglePasswordFields() {
    const checkbox = document.getElementById('resetPassword');
    const fields = document.getElementById('passwordFields');
    fields.style.display = checkbox.checked ? 'block' : 'none';
    
    if (!checkbox.checked) {
        document.querySelector('input[name="new_password"]').value = '';
        document.querySelector('input[name="new_password_confirmation"]').value = '';
    }
}

function previewAvatar(input) {
    const preview = document.getElementById('avatarPreview');
    
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        
        reader.onload = function(e) {
            preview.style.backgroundImage = 'url(' + e.target.result + ')';
            preview.style.backgroundSize = 'cover';
            preview.style.backgroundPosition = 'center';
            preview.style.display = 'block';
        };
        
        reader.readAsDataURL(input.files[0]);
    }
}
</script>
@endpush
@endsection
