@extends('layouts.admin')

@section('title', $member->exists ? 'Edit Team Member' : 'Add Team Member')

@section('content')
<div class="container mx-auto max-w-3xl">
    <div class="flex items-center gap-4 mb-6">
        <a href="{{ route('admin.team.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">{{ $member->exists ? 'Edit' : 'Add' }} Member</h1>
    </div>

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

            <div class="space-y-6">
                <!-- Name & Role -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-slate-700 text-sm font-bold mb-2">Name <span class="text-red-500">*</span></label>
                        <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                               name="name" type="text" placeholder="John Doe" required
                               value="{{ old('name', $member->name) }}">
                    </div>
                    <div>
                        <label class="block text-slate-700 text-sm font-bold mb-2">Role <span class="text-red-500">*</span></label>
                        <input class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                               name="role" type="text" placeholder="e.g. Senior Developer" required
                               value="{{ old('role', $member->role) }}">
                    </div>
                </div>

                <!-- Bio -->
                <div>
                    <label class="block text-slate-700 text-sm font-bold mb-2">Bio (Optional)</label>
                    <textarea class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-lg focus:ring-2 focus:ring-brand-primary focus:border-transparent outline-none transition" 
                              name="bio" rows="4" placeholder="Brief biography...">{{ old('bio', $member->bio) }}</textarea>
                </div>

                <!-- Team Photo -->
                <x-media-picker 
                    name="image_path" 
                    label="Team Photo" 
                    :value="$member->image_path" 
                    :preview="$member->image_path ? asset($member->image_path) : null" 
                />
                <div class="p-4 bg-slate-50 rounded-2xl border border-slate-100 mt-4 italic">
                    <p class="text-[11px] text-slate-500 leading-relaxed">
                        <strong>Direct Upload:</strong> You can also upload a photo directly below.
                    </p>
                    <input class="mt-2 w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-bold file:bg-brand-primary/10 file:text-brand-primary hover:file:bg-brand-primary/20 transition cursor-pointer" 
                           name="image" type="file" accept="image/*">
                </div>
                @error('image')
                    <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="mt-8 pt-6 border-t border-slate-100 flex items-center justify-end gap-3">
                <a href="{{ route('admin.team.index') }}" class="px-6 py-3 font-bold text-sm text-slate-500 hover:text-slate-800 hover:bg-slate-100 rounded-lg transition">Cancel</a>
                <button type="submit" class="bg-brand-primary hover:bg-brand-accent text-white font-bold py-3 px-8 rounded-lg 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>
                    {{ $member->exists ? 'Update Member' : 'Add Member' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
