@extends('layouts.admin')

@section('title', $user->exists ? 'Edit Administrator' : 'Add Administrator')

@section('content')
<div class="container mx-auto max-w-2xl">
    <div class="flex items-center gap-4 mb-6">
        <a href="{{ route('admin.users.index') }}" class="w-10 h-10 rounded-full bg-white border border-slate-200 flex items-center justify-center text-slate-500 hover:text-brand-primary hover:border-brand-primary transition shadow-sm">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
        </a>
        <h1 class="text-2xl font-bold text-slate-900">{{ $user->exists ? 'Edit' : 'Add' }} Administrator</h1>
    </div>

    <div class="bg-white shadow-sm rounded-2xl border border-slate-100 p-8">
        <form action="{{ $user->exists ? route('admin.users.update', $user) : route('admin.users.store') }}" method="POST">
            @csrf
            @if($user->exists)
                @method('PUT')
            @endif

            <div class="space-y-6">
                <!-- Username -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2" for="username">Username</label>
                    <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition @error('username') border-red-500 @enderror" 
                           id="username" name="username" type="text" placeholder="e.g. jdoe" required
                           value="{{ old('username', $user->username) }}">
                    @error('username')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- Role -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2" for="role">Role</label>
                    <select id="role" name="role" class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition @error('role') border-red-500 @enderror" required>
                        <option value="viewer" {{ old('role', $user->role) === 'viewer' ? 'selected' : '' }}>Viewer (Read-only)</option>
                        <option value="manager" {{ old('role', $user->role) === 'manager' ? 'selected' : '' }}>Manager (Messages, Blog, FAQs)</option>
                        <option value="hr" {{ old('role', $user->role) === 'hr' ? 'selected' : '' }}>HR Manager (Partners, Team)</option>
                        <option value="editor" {{ old('role', $user->role) === 'editor' ? 'selected' : '' }}>Editor (Manage Content)</option>
                        <option value="superadmin" {{ old('role', $user->role) === 'superadmin' ? 'selected' : '' }}>Superadmin (Full Access)</option>
                    </select>
                    @error('role')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                    <p class="text-[11px] text-slate-400 mt-2 italic">
                        <strong>Superadmin:</strong> Everything. <strong>Editor:</strong> Content management but no system settings. <strong>Viewer:</strong> Dashboard and lists only.
                    </p>
                </div>

                <hr class="border-slate-100">

                <div class="bg-brand-primary/5 p-6 rounded-2xl border border-brand-primary/10">
                    <h3 class="text-sm font-bold text-brand-primary mb-4 flex items-center gap-2">
                        <svg class="w-4 h-4" 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"></path></svg>
                        Password Settings
                    </h3>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <!-- Password -->
                        <div>
                            <label class="block text-slate-700 text-sm font-bold mb-2" for="password">{{ $user->exists ? 'Change Password' : 'Password' }}</label>
                            <input class="w-full px-4 py-3 bg-white border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition @error('password') border-red-500 @enderror" 
                                   id="password" name="password" type="password" {{ $user->exists ? '' : 'required' }}>
                        </div>

                        <!-- Confirm Password -->
                        <div>
                            <label class="block text-slate-700 text-sm font-bold mb-2" for="password_confirmation">Confirm Password</label>
                            <input class="w-full px-4 py-3 bg-white border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                                   id="password_confirmation" name="password_confirmation" type="password" {{ $user->exists ? '' : 'required' }}>
                        </div>
                    </div>
                    @error('password')
                        <p class="text-red-500 text-xs mt-2">{{ $message }}</p>
                    @enderror
                    @if($user->exists)
                        <p class="text-[11px] text-slate-400 mt-3">Leave password fields blank to keep current password.</p>
                    @endif
                </div>
            </div>

            <div class="mt-8 pt-6 border-t border-slate-100 flex items-center justify-end gap-3">
                <a href="{{ route('admin.users.index') }}" class="px-6 py-3 font-bold text-sm text-slate-500 hover:text-slate-800 hover:bg-slate-100 rounded-xl transition">Cancel</a>
                <button type="submit" class="bg-brand-primary hover:bg-brand-accent text-white font-bold py-3 px-8 rounded-xl shadow-lg shadow-brand-primary/30 transition flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                    {{ $user->exists ? 'Update Administrator' : 'Create Administrator' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
