@extends('theme::layouts.app', ['title' => trans('auth.title|Register')])

@section('content')
    <div class="flex-grow flex items-center justify-center py-12 px-6 lg:px-10">
        <div class="w-full max-w-lg">
            <div
                class="bg-white dark:bg-surface-dark p-8 md:p-10 rounded-2xl border border-gray-100 dark:border-gray-800 shadow-lg">
                <div class="text-center mb-10">
                    <h1 class="text-3xl font-bold text-text-main dark:text-white mb-2">@lang('auth.heading|Register')</h1>
                    <p class="text-text-muted dark:text-gray-400">@lang('auth.Join our community of digital creators.')</p>
                </div>
                
                <form action="{{ route('register') }}" class="space-y-5" method="POST" id="register-form">
                    @csrf
                    
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5"
                                for="first_name">@lang('auth.First Name')</label>
                            <div class="relative">
                                <span class="absolute left-3 top-1/2 -translate-y-1/2 text-text-muted material-symbols-outlined text-xl">person</span>
                                <input
                                    class="w-full pl-10 pr-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                    id="first_name" name="first_name" placeholder="John" required="" type="text" value="{{ old('first_name') }}" />
                            </div>
                            @if ($errors->has('first_name'))
                                <p class="mt-2 text-sm text-red-600">{{ $errors->first('first_name') }}</p>
                            @endif
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5"
                                for="last_name">@lang('auth.Last Name')</label>
                            <div class="relative">
                                <input
                                    class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                    id="last_name" name="last_name" placeholder="Doe" required="" type="text" value="{{ old('last_name') }}" />
                            </div>
                            @if ($errors->has('last_name'))
                                <p class="mt-2 text-sm text-red-600">{{ $errors->first('last_name') }}</p>
                            @endif
                        </div>
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5"
                            for="email">@lang('auth.Email Address')</label>
                        <div class="relative">
                            <span class="absolute left-3 top-1/2 -translate-y-1/2 text-text-muted material-symbols-outlined text-xl">mail</span>
                            <input
                                class="w-full pl-10 pr-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                id="email" name="email" placeholder="name@company.com" required="" type="email" value="{{ old('email') }}" />
                        </div>
                        @if ($errors->has('email'))
                            <p class="mt-2 text-sm text-red-600">{{ $errors->first('email') }}</p>
                        @endif
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5"
                            for="password">@lang('auth.Password')</label>
                        <div class="relative">
                            <span class="absolute left-3 top-1/2 -translate-y-1/2 text-text-muted material-symbols-outlined text-xl">lock</span>
                            <input
                                class="w-full pl-10 pr-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                id="password" name="password" placeholder="••••••••" required="" type="password" />
                        </div>
                        @if ($errors->has('password'))
                            <p class="mt-2 text-sm text-red-600">{{ $errors->first('password') }}</p>
                        @endif
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5"
                            for="password-confirm">@lang('auth.Confirm Password')</label>
                        <div class="relative">
                            <span class="absolute left-3 top-1/2 -translate-y-1/2 text-text-muted material-symbols-outlined text-xl">lock_reset</span>
                            <input
                                class="w-full pl-10 pr-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                id="password-confirm" name="password_confirmation" placeholder="••••••••" required="" type="password" />
                        </div>
                    </div>
                    
                    @if(!empty($registrationFields) && $registrationFields->isNotEmpty())
                        @foreach($registrationFields as $regField)
                            <div>
                                <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5">
                                    {{ $regField->label }}
                                    @if($regField->is_required) <span class="text-red-500">*</span> @endif
                                </label>

                                @if($regField->type === 'textarea')
                                    <textarea name="reg_fields[{{ $regField->name }}]"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="{{ $regField->placeholder }}"
                                        {{ $regField->is_required ? 'required' : '' }}>{{ old("reg_fields.{$regField->name}") }}</textarea>

                                @elseif($regField->type === 'select')
                                    <select name="reg_fields[{{ $regField->name }}]"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        {{ $regField->is_required ? 'required' : '' }}>
                                        <option value="">{{ $regField->placeholder ?: 'Please select' }}</option>
                                        @foreach($regField->options_array as $opt)
                                            <option value="{{ $opt }}" {{ old("reg_fields.{$regField->name}") == $opt ? 'selected' : '' }}>{{ $opt }}</option>
                                        @endforeach
                                    </select>

                                @elseif($regField->type === 'checkbox')
                                    <div class="flex items-center gap-2">
                                        <input type="hidden" name="reg_fields[{{ $regField->name }}]" value="0">
                                        <input type="checkbox" name="reg_fields[{{ $regField->name }}]"
                                            id="cf_{{ $regField->name }}" value="1"
                                            class="rounded border-gray-300 text-primary focus:ring-primary"
                                            {{ old("reg_fields.{$regField->name}") == '1' ? 'checked' : '' }}
                                            {{ $regField->is_required ? 'required' : '' }}>
                                        <label for="cf_{{ $regField->name }}" class="text-sm text-text-muted dark:text-gray-400">
                                            {{ $regField->placeholder ?: $regField->label }}
                                        </label>
                                    </div>

                                @else
                                    <input type="{{ $regField->type }}" name="reg_fields[{{ $regField->name }}]"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="{{ $regField->placeholder }}"
                                        value="{{ old("reg_fields.{$regField->name}") }}"
                                        {{ $regField->is_required ? 'required' : '' }}>
                                @endif

                                @if($errors->has("reg_fields.{$regField->name}"))
                                    <p class="mt-1 text-sm text-red-600">{{ $errors->first("reg_fields.{$regField->name}") }}</p>
                                @endif
                            </div>
                        @endforeach
                    @endif

                    @if($billingOnRegistration)
                    <div class="border-t border-gray-200 dark:border-gray-700 pt-5">
                        <p class="text-sm font-semibold text-text-main dark:text-white mb-4">Billing Information</p>

                        <div class="space-y-4">
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="phone_number">Phone Number</label>
                                    <input type="tel" id="phone_number" name="phone_number"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="+1 (555) 000-0000" value="{{ old('phone_number') }}">
                                    @error('phone_number') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                                </div>
                                <div>
                                    <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="company_name">Company Name</label>
                                    <input type="text" id="company_name" name="company_name"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="Acme Inc." value="{{ old('company_name') }}">
                                    @error('company_name') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                                </div>
                            </div>

                            <div>
                                <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="billing_address">Address</label>
                                <input type="text" id="billing_address" name="billing_address"
                                    class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                    placeholder="123 Main St" value="{{ old('billing_address') }}">
                                @error('billing_address') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                            </div>

                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="billing_city">City</label>
                                    <input type="text" id="billing_city" name="billing_city"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="San Francisco" value="{{ old('billing_city') }}">
                                    @error('billing_city') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                                </div>
                                <div>
                                    <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="billing_zip">Zip / Postal Code</label>
                                    <input type="text" id="billing_zip" name="billing_zip"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all"
                                        placeholder="94103" value="{{ old('billing_zip') }}">
                                    @error('billing_zip') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                                </div>
                            </div>

                            <div>
                                <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="billing_country">Country</label>
                                <select id="billing_country" name="billing_country"
                                    class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all appearance-none user-country">
                                    <option value="">Select Country</option>
                                    @foreach($countries as $id => $country)
                                        <option value="{{ $id }}" {{ old('billing_country') == $id ? 'selected' : '' }}>{{ $country }}</option>
                                    @endforeach
                                </select>
                                @error('billing_country') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                            </div>

                            <div class="user-state">
                                <label class="block text-sm font-semibold text-text-main dark:text-gray-200 mb-1.5" for="billing_state">State / Province</label>
                                <div class="state-container">
                                    <select id="billing_state" name="billing_state"
                                        class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all appearance-none">
                                        <option value="">Select State / Province</option>
                                    </select>
                                </div>
                                @error('billing_state') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                            </div>
                        </div>
                    </div>

                    <script id="states-template" type="text/x-handlebars-template">
                        <select class="w-full px-4 py-2.5 bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary transition-all appearance-none" name="billing_state">
                            <option value="">Select State / Province</option>
                            @{{#each this}}
                            <option value="@{{ @key }}">@{{this}}</option>
                            @{{/each}}
                        </select>
                    </script>
                    @endif

                    @include('theme::partials.recaptcha', ['formId' => 'register-form', 'action' => 'register'])

                    <div class="flex items-start">
                         <p class="text-xs text-text-muted dark:text-gray-400">
                            @lang('auth.By registering you agree to the <a href=":url" class="text-primary font-medium hover:underline">terms and conditions</a>.', ['url' => url('page/terms-of-service')])
                        </p>
                    </div>

                    <button
                        class="w-full py-3 bg-primary hover:bg-primary-dark text-white rounded-lg font-semibold transition-all shadow-lg shadow-primary/20 flex items-center justify-center gap-2 mt-4"
                        type="submit">
                        @lang('auth.button|Register')
                    </button>
                </form>

                @include('theme::pages.auth.social')

                <div class="flex items-center justify-center gap-6 mt-8">
                     <p class="text-center text-sm text-text-muted dark:text-gray-400">
                        @lang('auth.Already have an account?')
                        <a class="text-primary font-bold hover:underline" href="{{ route('login') }}">@lang('auth.Log in')</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
@endsection
