@extends('layouts.app')

@section('title', 'Edit User')

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Users', 'url' => route('admin.users.index')],
        ['label' => $user->name, 'url' => route('admin.users.show', $user)],
        ['label' => 'Edit'],
    ]" />

    <x-page-header title="Edit User" :subtitle="$user->email" />

    <x-card>
        <form method="POST" action="{{ route('admin.users.update', $user) }}">
            @csrf
            @method('PUT')

            <div class="row">
                <div class="col-md-6">
                    <x-form.input label="Full Name" name="name" :value="$user->name" required />
                    <x-form.input label="Email Address" name="email" type="email" :value="$user->email" required />
                    <x-form.input label="Phone" name="phone" :value="$user->phone" />
                </div>
                <div class="col-md-6">
                    <x-form.input label="New Password" name="password" type="password" help="Leave blank to keep current password" />
                    <x-form.input label="Confirm Password" name="password_confirmation" type="password" />
                    <x-form.checkbox label="Active account" name="is_active" :checked="$user->is_active" value="1" />
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label">Roles</label>
                <div class="row g-2">
                    @foreach ($roles as $role)
                        <div class="col-md-4">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="roles[]"
                                       value="{{ $role->id }}" id="role_{{ $role->id }}"
                                       @checked(in_array($role->id, old('roles', $user->roles->pluck('id')->all())))>
                                <label class="form-check-label" for="role_{{ $role->id }}">{{ $role->name }}</label>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">Save Changes</button>
                <a href="{{ route('admin.users.show', $user) }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </x-card>
@endsection
