<x-app-layout>
    <div class="app-main flex-column flex-row-fluid">
        <div class="d-flex flex-column flex-column-fluid">
            <div class="app-toolbar py-3 py-lg-6">
                <div class="app-container container-xxl d-flex flex-stack">
                    <div class="page-title d-flex flex-column justify-content-center flex-wrap me-3">
                        <h1 class="page-heading d-flex text-dark fw-bold fs-3 flex-column justify-content-center my-0">
                            Create User</h1>
                        {{ Breadcrumbs::render('user-management.create') }}
                    </div>
                    <div class="d-flex align-items-center ml-auto gap-2 gap-lg-3">
                        @if ($url = Session::get('backUrl'))
                            <x-add-back-button onclick="window.location='{{ $url }}'" >
                                <i class="ki-duotone ki-left fs-3"></i>
                                {{ __('Back') }}
                            </x-add-back-button>
                        @else
                            <x-add-back-button onclick="window.location='{{ url()->previous() }}'" >
                            <i class="ki-duotone ki-left fs-3"></i>
                            {{ __('Back') }}
                            </x-add-back-button>
                        @endif
                    </div>
                </div>
            </div>
            <div class="app-content flex-column-fluid">
                <div class="app-container  container-xxl ">
                    <div class="app-content flex-column-fluid p-12 sm:p-8 bg-white shadow sm:rounded-lg">
                        <div class="max-w-8xl">
                            <section>
                                <header class="flex justify-between items-center mb-5">
                                    <h2 class="text-2xl font-medium text-gray-900">
                                        {{ __('Create Account') }}
                                        <p class="mt-1 mb-6 text-base text-gray-600">
                                            {{ __('Create a new account for user.') }}
                                        </p>
                                    </h2>
                                </header>
                                <form id="kt_modal_add_user_form" class="form" method="POST"
                                    action="{{ route('register') }}">
                                    @csrf
                                    <div class="grid gap-6 mb-5 md:grid-cols-2">
                                        <div class="fv-row mb-5">
                                            <x-input-label for="name" :value="__('Name')" />
                                            <x-text-input id="name" class="form-control form-control-solid"
                                                type="text" name="name" :value="old('name')" required
                                                autocomplete="name" />
                                            <x-input-error :messages="$errors->get('name')" class="mt-3" />
                                        </div>
                                        <div class="fv-row mb-5">
                                            <x-input-label for="email" :value="__('Email')" />
                                            <x-text-input id="email" class="form-control form-control-solid"
                                                type="email" name="email" :value="old('email')" required
                                                autocomplete="username" />
                                            <x-input-error :messages="$errors->get('email')" class="mt-3" />
                                        </div>
                                        <div class="fv-row mb-5">
                                            <x-input-label for="phone" :value="__('Phone Number')" />
                                            <x-text-input id="phone" class="form-control form-control-solid"
                                                type="number" name="phone" :value="old('phone')" autocomplete="phone" placeholder="Ex: 0123456789" />
                                            <x-input-error :messages="$errors->get('phone')" class="mt-3" />
                                        </div>
                                        <div class="fv-row mb-5">
                                            <x-input-label for="department" :value="__('Department')" />
                                            <x-text-input id="department" class="form-control form-control-solid"
                                                type="text" name="department" :value="old('department')"
                                                autocomplete="department" />
                                            <x-input-error :messages="$errors->get('department')" class="mt-3" />
                                        </div>
                                    </div>
                                    <div class="fv-row mb-10">
                                        <x-input-label for="password" :value="__('Password')" />

                                        <x-text-input id="password" class="form-control form-control-solid"
                                            type="password" name="password" required autocomplete="new-password" />
                                        <x-input-error :messages="$errors->get('password')" class="mt-3" />
                                    </div>
                                    <div class="fv-row mb-10">
                                        <x-input-label for="password_confirmation" :value="__('Confirm Password')" />

                                        <x-text-input id="password_confirmation" class="form-control form-control-solid"
                                            type="password" name="password_confirmation" required
                                            autocomplete="new-password" />
                                        <x-input-error :messages="$errors->get('password_confirmation')" class="mt-3" />
                                    </div>
                                    <header>
                                        <h2 class="text-2xl font-medium text-gray-900">
                                            {{ __('Role') }}
                                        </h2>

                                        <p class="mt-1 text-base text-gray-600">
                                            {{ __('Assign a role to user') }}
                                        </p>
                                    </header>
                                    <div class="fv-row mb-10">
                                        @foreach ($roles as $role)
                                            @if (auth()->user()->hasRole('super-admin'))
                                                <div class='separator separator-dashed my-5'></div>
                                                <div class="d-flex fv-row">
                                                    <div class="form-check form-check-custom form-check-solid">
                                                        <input class="form-check-input me-3" name="roles[]"
                                                            type="checkbox" value="{{ $role->id }}"
                                                            {{ in_array($role->id, old('roles', [])) ? 'checked' : ''}}
                                                            id="role_{{ $role->id }}" />
                                                        <label class="form-check-label" for="role_{{ $role->id }}">
                                                            <div class="fw-bold text-gray-800">{{ $role->name }}
                                                            </div>
                                                        </label>
                                                    </div>
                                                </div>
                                            @else
                                                @if ($role->name !== 'super-admin')
                                                    <div class='separator separator-dashed my-5'></div>
                                                    <div class="d-flex fv-row">
                                                        <div class="form-check form-check-custom form-check-solid">
                                                            <input class="form-check-input me-3" name="roles[]"
                                                                type="checkbox" value="{{ $role->id }}"
                                                                {{ in_array($role->id, old('roles', [])) ? 'checked' : ''}}
                                                                id="role_{{ $role->id }}" />
                                                            <label class="form-check-label" for="role_{{ $role->id }}">
                                                                <div class="fw-bold text-gray-800">{{ $role->name }}
                                                                </div>
                                                            </label>
                                                        </div>
                                                    </div>
                                                @endif
                                            @endif
                                        @endforeach
                                    </div>
                                    <div class="pt-10">
                                        <x-discard-button class="btn btn-warning me-3"
                                            data-kt-users-modal-action="cancel">
                                            <i class="ki-duotone ki-archive-tick fs-3">
                                                <i class="path1"></i>
                                                <i class="path2"></i>
                                            </i>
                                            <span class="indicator-label">{{ __('Discard') }}</span>
                                        </x-discard-button>
                                        <x-primary-button class="btn btn-primary"
                                            data-kt-users-modal-action="submit">
                                            <i class="ki-duotone ki-triangle fs-3"><span
                                                class="path1"></span><span class="path2"></span>
                                                <span class="path3"></span>
                                            </i>
                                            <span class="indicator-label">{{ __('Submit') }}</span>
                                        </x-primary-button>
                                    </div>
                                </form>
                            </section>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
