<x-app-layout>
    <x-slot name="header">
        {{ __('Add User') }}
    </x-slot>

    <div class="p-6">
        <div class="max-w-3xl">
            <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-6 space-y-6">
                <div>
                    <h1 class="text-xl font-semibold text-gray-900">Add User</h1>
                    <p class="mt-1 text-sm text-gray-600">
                        Create a new user for the current dealer and assign roles and department.
                    </p>
                </div>

                <form method="POST" action="{{ route('settings.users.store') }}" class="space-y-4">
                    @csrf

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="name" class="block text-sm font-medium text-gray-700">Name</label>
                            <input
                                id="name"
                                name="name"
                                type="text"
                                value="{{ old('name') }}"
                                required
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                            >
                            @error('name')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="surname" class="block text-sm font-medium text-gray-700">Surname</label>
                            <input
                                id="surname"
                                name="surname"
                                type="text"
                                value="{{ old('surname') }}"
                                required
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                            >
                            @error('surname')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="email" class="block text-sm font-medium text-gray-700">Email</label>
                            <input
                                id="email"
                                name="email"
                                type="email"
                                value="{{ old('email') }}"
                                required
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                            >
                            @error('email')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="phone" class="block text-sm font-medium text-gray-700">Phone (optional)</label>
                            <input
                                id="phone"
                                name="phone"
                                type="text"
                                value="{{ old('phone') }}"
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                            >
                            @error('phone')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="department_id" class="block text-sm font-medium text-gray-700">Department</label>
                            <select
                                id="department_id"
                                name="department_id"
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-red-500 focus:ring-red-500 text-sm"
                            >
                                <option value="">None</option>
                                @foreach ($departments as $department)
                                    <option value="{{ $department->id }}" @selected(old('department_id') == $department->id)>
                                        {{ $department->name }}
                                    </option>
                                @endforeach
                            </select>
                            @error('department_id')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700">Roles</label>
                            <div class="mt-1 rounded-md border border-gray-200 p-2 max-h-40 overflow-y-auto text-sm space-y-1">
                                @foreach ($roles as $role)
                                    <label class="flex items-center gap-2">
                                        <input
                                            type="checkbox"
                                            name="roles[]"
                                            value="{{ $role }}"
                                            @checked(in_array($role, old('roles', [])))
                                            class="h-4 w-4 rounded border-gray-300 text-red-600 focus:ring-red-500"
                                        >
                                        <span>{{ $role }}</span>
                                    </label>
                                @endforeach
                            </div>
                            @error('roles')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>

                    <p class="mt-2 text-xs text-gray-500">
                        A strong temporary password will be generated automatically. The user will be forced to change it on first login.
                    </p>

                    <div class="flex items-center justify-end gap-3 pt-4">
                        <a
                            href="{{ route('settings.users.index') }}"
                            class="inline-flex items-center rounded-md border border-gray-200 px-4 py-2 text-sm text-gray-700 hover:bg-gray-50"
                        >
                            Cancel
                        </a>
                        <button
                            type="submit"
                            class="inline-flex items-center rounded-md bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-700"
                        >
                            Save User
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</x-app-layout>

