<?php
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Illuminate\Validation\Rules\Password;
use Illuminate\Support\Facades\Hash;
?>

<div>
    <header class="flex justify-between items-center mb-8">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Create User</h1>
            <p class="text-sm text-gray-500 mt-1">Add a new admin panel user and assign a role.</p>
        </div>
        <a href="{{ route('admin.users') }}" wire:navigate class="btn-secondary">← Back</a>
    </header>

    <form wire:submit="save" class="max-content space-y-6">

        <!-- Avatar -->
        <div class="card p-6">
            <h2 class="text-sm font-semibold text-gray-700 mb-4">Profile Photo</h2>
            <div class="flex items-center gap-5">
                <div class="w-16 h-16 rounded-full bg-gray-100 border border-gray-200 flex items-center justify-center overflow-hidden flex-shrink-0">
                    @if($avatar)
                        <img src="{{ $avatar->temporaryUrl() }}" class="w-full h-full object-cover">
                    @else
                        <span class="text-gray-400 text-xl font-bold">{{ $name ? strtoupper(substr($name, 0, 1)) : '?' }}</span>
                    @endif
                </div>
                <div>
                    <input wire:model="avatar" type="file" accept="image/*" class="text-sm text-gray-600">
                    <p class="text-xs text-gray-400 mt-1">PNG, JPG up to 2 MB. Optional.</p>
                </div>
            </div>
            @error('avatar') <p class="text-red-500 text-xs mt-2">{{ $message }}</p> @enderror
        </div>

        <!-- Account Details -->
        <div class="card p-6 space-y-4">
            <h2 class="text-sm font-semibold text-gray-700 mb-2">Account Details</h2>

            <div>
                <label class="label">Full Name</label>
                <input wire:model="name" type="text" class="input-field w-full" placeholder="John Doe">
                @error('name') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
            </div>

            <div>
                <label class="label">Email Address</label>
                <input wire:model="email" type="email" class="input-field w-full" placeholder="john@example.com">
                @error('email') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
            </div>

            <div>
                <label class="label">Role</label>
                <select wire:model="role" class="input-field w-full">
                    @foreach($roles as $r)
                        <option value="{{ $r }}">{{ ucfirst($r) }}</option>
                    @endforeach
                </select>
                @error('role') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
            </div>
        </div>

        <!-- Password -->
        <div class="card p-6 space-y-4">
            <h2 class="text-sm font-semibold text-gray-700 mb-2">Set Password</h2>

            <div>
                <label class="label">Password</label>
                <input wire:model="password" type="password" class="input-field w-full" placeholder="Min. 8 characters">
                @error('password') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
            </div>

            <div>
                <label class="label">Confirm Password</label>
                <input wire:model="password_confirmation" type="password" class="input-field w-full">
            </div>
        </div>

        <div class="flex gap-3">
            <button type="submit" wire:loading.attr="disabled" class="btn-primary">
                <span wire:loading.remove wire:target="save">Create User</span>
                <span wire:loading wire:target="save">Creating...</span>
            </button>
            <a href="{{ route('admin.users') }}" wire:navigate class="btn-secondary">Cancel</a>
        </div>
    </form>
</div>