@extends('admin.layouts.app')

@section('title', __('admin/admin-management.create_title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/admin-management.create_title')"
        icon="fas fa-user-plus"
        :buttons="[
            ['label' => __('admin/admin-management.buttons_cancel'), 'route' => route('admin.admin-management.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.alert />

    @if($errors->any())
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="fas fa-exclamation-triangle me-2"></i>
            <strong>{{ __('admin/common.validation_errors') }}</strong>
            <ul class="mb-0 mt-2">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <form id="adminForm" action="{{ route('admin.admin-management.store') }}" method="POST">
        @csrf

        <x-admin.card>
            <div class="row">
                <div class="col-md-6">
                    <!-- Name Field -->
                    <div class="mb-3">
                        <label for="create_name" class="form-label">
                            {{ __('admin/admin-management.fields_name') }}
                            <span class="text-danger">*</span>
                        </label>
                        <input type="text" class="form-control @error('name') is-invalid @enderror"
                               id="create_name" name="name" value="{{ old('name') }}" required
                               placeholder="{{ __('admin/admin-management.placeholders_name') }}">
                        @error('name')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <!-- Email Field -->
                    <div class="mb-3">
                        <label for="create_email" class="form-label">
                            {{ __('admin/admin-management.fields_email') }}
                            <span class="text-danger">*</span>
                        </label>
                        <input type="email" class="form-control @error('email') is-invalid @enderror"
                               id="create_email" name="email" value="{{ old('email') }}" required
                               placeholder="{{ __('admin/admin-management.placeholders_email') }}">
                        @error('email')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <!-- Password Field -->
                    <div class="mb-3">
                        <label for="create_password" class="form-label">
                            {{ __('admin/admin-management.fields_password') }}
                            <span class="text-danger">*</span>
                        </label>
                        <div class="input-group">
                            <input type="password" class="form-control @error('password') is-invalid @enderror"
                                   id="create_password" name="password" required
                                   placeholder="{{ __('admin/admin-management.placeholders_password') }}">
                            <button class="btn btn-outline-secondary js-password-toggle" type="button" tabindex="-1"
                                    aria-label="{{ __('auth.password_show') }}"
                                    data-show-label="{{ __('auth.password_show') }}"
                                    data-hide-label="{{ __('auth.password_hide') }}">
                                <i class="fas fa-eye"></i>
                            </button>
                            <button class="btn btn-outline-secondary" type="button" id="generatePassword">
                                {{ __('admin/admin-management.buttons_generate_password') }}
                            </button>
                        </div>
                        <div class="form-text">{{ __('admin/admin-management.help_texts_password') }}</div>
                        @error('password')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <!-- Password Confirmation Field -->
                    <div class="mb-3">
                        <label for="create_password_confirmation" class="form-label">
                            {{ __('admin/admin-management.fields_password_confirmation') }}
                            <span class="text-danger">*</span>
                        </label>
                        <div class="input-group">
                            <input type="password" class="form-control @error('password_confirmation') is-invalid @enderror"
                                   id="create_password_confirmation" name="password_confirmation" required
                                   placeholder="{{ __('admin/admin-management.placeholders_password_confirmation') }}">
                            <button class="btn btn-outline-secondary js-password-toggle" type="button" tabindex="-1"
                                    aria-label="{{ __('auth.password_show') }}"
                                    data-show-label="{{ __('auth.password_show') }}"
                                    data-hide-label="{{ __('auth.password_hide') }}">
                                <i class="fas fa-eye"></i>
                            </button>
                            @error('password_confirmation') <div class="invalid-feedback">{{ $message }}</div> @enderror
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <!-- Role Assignment -->
                    <div class="mb-3">
                        <label class="form-label">{{ __('admin/admin-management.fields_roles') }}</label>
                        <div class="card">
                            <div class="card-body">
                                @foreach($roles as $role)
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox"
                                               id="role_{{ $role->id }}" name="roles[]" value="{{ $role->name }}"
                                               {{ in_array($role->name, old('roles', [])) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="role_{{ $role->id }}">
                                            {{ $role->name }}
                                        </label>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                        <div class="form-text">{{ __('admin/admin-management.help_texts_roles') }}</div>
                    </div>

                    <!-- Status Field -->
                    <div class="mb-3">
                        <label for="create_status" class="form-label">
                            {{ __('admin/admin-management.fields_status') }}
                        </label>
                        <select class="form-select @error('status') is-invalid @enderror"
                                id="create_status" name="status">
                            <option value="active" @selected(old('status', 'active') === 'active')>
                                {{ __('admin/admin-management.status_active') }}
                            </option>
                            <option value="inactive" @selected(old('status') === 'inactive')>
                                {{ __('admin/admin-management.status_inactive') }}
                            </option>
                            <option value="suspended" @selected(old('status') === 'suspended')>
                                {{ __('admin/admin-management.status_suspended') }}
                            </option>
                        </select>
                        @error('status')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>

            <x-slot:footer>
                <x-admin.form-actions :cancel-url="route('admin.admin-management.index')" />
            </x-slot:footer>
        </x-admin.card>
    </form>
</div>
@endsection

@push('scripts')
<script>
    // Route bilgilerini JavaScript'e aktar
    window.routes = {
        adminManagement: {
            store: '{{ route("admin.admin-management.store") }}'
        }
    };

    // Çevirileri JavaScript'e aktar
    window.translations = {
        validation: {
            required: '{{ __("validation.required") }}',
            email: '{{ __("validation.email") }}',
            confirmed: '{{ __("validation.confirmed") }}',
            min: '{{ __("validation.min_string") }}'
        },
        messages: {
            success: '{{ __("admin/admin-management.messages_created") }}',
            error: '{{ __("admin/admin-management.messages_create_error") }}'
        }
    };
</script>
@vite('resources/js/admin/admin-management/create.js')
@endpush