<div class="col-md-8">
    <div class="bgc-white bd bdrs-3 p-20 mB-20">

        @include('admin.layouts.errors')

        <div class="mb-3">
            <label for="first_name" class="mb-1">First name:</label>
            <input type="text" name="first_name" class="form-control" placeholder="First name" value="{{ old('first_name', $user->first_name ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="last_name" class="mb-1">Last name:</label>
            <input type="text" name="last_name" class="form-control" placeholder="Last name" value="{{ old('last_name', $user->last_name ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="email" class="mb-1">Email:</label>
            <input type="email" name="email" class="form-control" placeholder="Email" value="{{ old('email', $user->email ?? '') }}">
        </div>

        @if(!isset($user))
            <div class="mb-3">
                <input type="checkbox" name="welcome_email" id="welcome_email" value="1" checked>
                <label for="welcome_email">Send Welcome Email</label>
            </div>
        @endif

        <div class="mb-3">
            <label for="password" class="mb-1">Password:</label>
            <input type="password" name="password" class="form-control" placeholder="Password">
            @if(Route::currentRouteName() == 'ch-admin.user.edit')
                <small class="help-block">Leave blank to keep the existing one.</small>
            @endif
        </div>

        <div class="mb-3">
            <label for="roles" class="mb-1">Role:</label>
            <select name="roles" class="form-control">
                <option value="" disabled>Please select</option>
                @foreach($roles as $id => $display_name)
                    <option value="{{ $id }}" {{ old('roles') == $id ? 'selected' : '' }}>{{ $display_name }}</option>
                @endforeach
            </select>
        </div>

        <hr>

        <h3>Billing Details:</h3>

        <div class="mb-3">
            <label for="phone_number" class="mb-1">Phone Number (optional):</label>
            <input type="text" name="phone_number" class="form-control" placeholder="Phone Number" value="{{ old('phone_number', $user->billingAddress->phone_number ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="company_name" class="mb-1">Company Name (optional):</label>
            <input type="text" name="company_name" class="form-control" placeholder="Company Name" value="{{ old('company_name', $user->billingAddress->company_name ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="billing_address" class="mb-1">Billing Address (optional):</label>
            <input type="text" name="billing_address" class="form-control" placeholder="Billing Address" value="{{ old('billing_address', $user->billingAddress->address ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="billing_city" class="mb-1">Billing City (optional):</label>
            <input type="text" name="billing_city" class="form-control" placeholder="City" value="{{ old('billing_city', $user->billingAddress->city ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="billing_zip" class="mb-1">Billing Zip/Postal Code (optional):</label>
            <input type="text" name="billing_zip" class="form-control" placeholder="Zip/Postal Code" value="{{ old('billing_zip', $user->billingAddress->zip ?? '') }}">
        </div>

        <div class="mb-3">
            <label for="billing_country" class="mb-1">Billing Country (optional):</label>
            <select name="billing_country" class="form-control user-country">
                <option value="">Please select</option>
                @foreach($countries as $id => $country)
                    <option value="{{ $id }}" {{ old('billing_country', $user->billingAddress->country_id ?? '') == $id ? 'selected' : '' }}>
                        {{ $country }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="mb-3 user-state">
            <label for="billing_state" class="mb-1">State (optional):</label>
            <div class="state-container">
                @if(Route::currentRouteName() === 'ch-admin.service.edit')
                    <input type="text" name="billing_state" class="form-control" placeholder="State" value="{{ old('billing_state') }}">
                @else
                    <select name="billing_state" class="form-control user-state">
                        <option value="">Please select</option>
                        @foreach($states as $id => $state)
                            <option value="{{ $id }}" {{ old('billing_state', $user->billingAddress->state_id ?? '') == $id ? 'selected' : '' }}>{{ $state }}</option>
                        @endforeach
                    </select>
                @endif
            </div>
        </div>

        @if(!empty($registrationFields) && $registrationFields->isNotEmpty())
        <hr>
        <h3>Custom Fields:</h3>

        @foreach($registrationFields as $regField)
            @php($metaValue = isset($user) ? ($user->meta->firstWhere('key', $regField->name)?->value ?? '') : '')
            <div class="mb-3">
                <label class="mb-1">
                    {{ $regField->label }}
                    @if($regField->is_required) <span class="text-danger">*</span> @endif
                    @if(!$regField->is_active) <span class="badge badge-warning">Inactive</span> @endif
                </label>

                @if($regField->type === 'textarea')
                    <textarea name="reg_fields[{{ $regField->name }}]" class="form-control"
                        placeholder="{{ $regField->placeholder }}">{{ old("reg_fields.{$regField->name}", $metaValue) }}</textarea>

                @elseif($regField->type === 'select')
                    <select name="reg_fields[{{ $regField->name }}]" class="form-control">
                        <option value="">{{ $regField->placeholder ?: 'Please select' }}</option>
                        @foreach($regField->options_array as $opt)
                            <option value="{{ $opt }}" {{ old("reg_fields.{$regField->name}", $metaValue) == $opt ? 'selected' : '' }}>{{ $opt }}</option>
                        @endforeach
                    </select>

                @elseif($regField->type === 'checkbox')
                    <div>
                        <input type="hidden" name="reg_fields[{{ $regField->name }}]" value="0">
                        <input type="checkbox" name="reg_fields[{{ $regField->name }}]"
                            id="cf_admin_{{ $regField->name }}" value="1"
                            {{ old("reg_fields.{$regField->name}", $metaValue) == '1' ? 'checked' : '' }}>
                        <label for="cf_admin_{{ $regField->name }}">{{ $regField->placeholder ?: $regField->label }}</label>
                    </div>

                @else
                    <input type="{{ $regField->type }}" name="reg_fields[{{ $regField->name }}]"
                        class="form-control" placeholder="{{ $regField->placeholder }}"
                        value="{{ old("reg_fields.{$regField->name}", $metaValue) }}">
                @endif
            </div>
        @endforeach
        @endif

        <a href="{{route('ch-admin.user.index')}}" class="btn btn-secondary">Cancel</a>
        <button type="submit" class="btn btn-primary">{{ $buttonLabel }}</button>
    </div>
</div>

<script id="states-template" type="text/x-handlebars-template">
    <select class="form-control" name="billing_state">
        <option value="">Select State/Province</option>
        @{{#each this}}
        <option value="@{{ @key }}">@{{this}}</option>
        @{{/each}}
    </select>
</script>
