@extends('layouts.app')

@section('content')
<div class="container-fluid" onload="initAutocomplete()">
    <div class="row justify-content-center d-flex align-item-center login-row">
        <div class="col-md-5 px-0 form-col">
        <div class="card border-0">
                <div class="card-body">
                <h2 style='font-weight:bold;'>Create An Account</h2>
                    <p style='font-size:14px;'>Enter Your Personal Information to create an account.</p>
                    <form method="POST" action="{{ route('register') }}">
                        @csrf
                    <div class='row'>
                        <div class='col-md-6'>
                            <div class="form-group mb-3">
                                        <input id="first_name" type="text" class="form-control @error('first_name') is-invalid @enderror" name="first_name" placeholder='First Name' value="{{ old('first_name') }}" autocomplete="first_name" autofocus>
                                        @error('first_name')
                                            <span class="invalid-feedback" role="alert">
                                                <strong>{{ $message }}</strong>
                                            </span>
                                        @enderror
                            </div>
                        </div>
                        <div class='col-md-6'>
                            <div class="form-group mb-3">
                                    <input id="last_name" type="text" class="form-control @error('last_name') is-invalid @enderror" name="last_name" placeholder='Last Name' value="{{ old('last_name') }}" autocomplete="last_name" autofocus>
                                    @error('last_name')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                            </div>
                        </div>
                    </div>
                        <div class="form-group mb-3">     
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" placeholder='Email' name="email" value="{{ old('email') }}" autocomplete="email">
                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>
                        <div class="form-group mb-3">
                                <!-- <input id="phone" type="tel" class="form-control @error('phone') is-invalid @enderror w-100" name="phone" placeholder='Phone' value="{{ old('phone') }}" autocomplete="phone" oninput="handleInput()" onkeypress="return validateInput()" autofocus> -->
                                <input id="phone" type="tel" class="form-control @error('phone') is-invalid @enderror w-100" name="phone" placeholder='Phone' value="{{ old('phone') }}" autocomplete="phone" autofocus>
                             
                                @error('phone')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>
                        <div class="form-group mb-3">
                                <input id="address" type="text" class="form-control @error('address') is-invalid @enderror" name="address" placeholder='Address' value="{{ old('address') }}" autocomplete="address" autofocus>
                                @error('address')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>
                        <div class="form-group mb-3 password-div">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" placeholder='Password' name="password" autocomplete="new-password">
                                <i id='showpswd' class="fa fa-eye-slash" onclick="showpwd()" aria-hidden="true"></i>

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                        </div>
                        <div class="form-group mb-3">
                                <input id="password-confirm" type="password" class="form-control" placeholder='Confirm Password' name="password_confirmation" autocomplete="new-password">
                        </div>
                        <div class="form-group mb-0">
                                <button type="submit" class="btn btn-primary w-100">
                                    {{ __('Register') }}
                                </button>
                                <a class="btn btn-link text-secondary text-end" style='width:100%;' href="{{ route('login') }}">
                                        {{ __('Back to Login?') }}
                                    </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class='col-md-7 px-0 login-img-col vh-100'>
              <img src="{{ url('/dashboardd.png') }}">
            </div>
    </div>
</div>

<script>

$('#registerform').one('submit', function() {
    $(this).find('input[type="submit"]').attr('disabled', 'disabled');
});

</script>
<!-- <script>

    // Automatically format the phone number as the user types
    var input = document.getElementById('phone');
    input.addEventListener('input', function() {
      this.value = formatPhoneNumber(this.value);
    });
    
    function formatPhoneNumber(input) {
      // Remove all non-digit characters
      var cleaned = ('' + input).replace(/\D/g, '');

      // Limit the input to 14 characters
      cleaned = cleaned.substring(0, 14);

      // Format the number as (XXX) XXX-XXXX
      var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
      if (match) {
        return '(' + match[1] + ') ' + match[2] + '-' + match[3];
      }
      return cleaned;
    }

    function handleInput() {
      var input = document.getElementById('phone').value;
    //   var formatted = formatPhoneNumber(input);
    //   document.getElementById('formattedPhone').innerText = formatted;
    }

    function validateInput() {
      var input = document.getElementById('phone').value;
      if (/^\d{0,14}$/.test(input)) {
        return true;
      } else {
        return false;
      }
    }
  
  </script> -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js"></script>

<script>
function showpwd() {
    var icon = document.getElementById("showpswd");
  var x = document.getElementById("password");

  if (x.type === "password") {
    x.type = "text";
    icon.classList.remove('fa-eye-slash');
    icon.classList.add('fa-eye');
  } else {
    x.type = "password";
    icon.classList.remove('fa-eye');
    icon.classList.add('fa-eye-slash');
  }
}
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        console.log('DOM fully loaded and parsed');
        var input = document.querySelector("#phone");
        if (input) {
            console.log('Phone input found', input);
            try {
                var iti = window.intlTelInput(input, {
                    initialCountry: "auto",
                    geoIpLookup: function(callback) {
                        $.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
                            var countryCode = (resp && resp.country) ? resp.country : "us";
                            callback(countryCode);
                        });
                    },
                    utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js"
                });

                function validateInput() {
                    if (iti.isValidNumber()) {
                        var countryCode = iti.getSelectedCountryData().dialCode;
                    var currentNumber = iti.getNumber(intlTelInputUtils.numberFormat.E164);
                    $('#phone').val(currentNumber);
                        console.log('Phone number is valid');
                    } else {
                        console.log('Phone number is invalid');
                    }
                }

                function handleInput() {
                    var countryCode = iti.getSelectedCountryData().dialCode;
                    var currentNumber = iti.getNumber(intlTelInputUtils.numberFormat.E164);
                    $('#phone').val(currentNumber);
                    validateInput();
                }

                $(document).ready(function() {
                    $('.iti__flag-container').click(handleInput);
                    $('#phone').on('blur', validateInput);
                });

            } catch (error) {
                console.error('Error initializing intlTelInput', error);
            }
        } else {
            console.error('Phone input element not found');
        }
    });
</script>

<script>
    function initAutocomplete() {
      var input = document.getElementById('address');
      var autocomplete = new google.maps.places.Autocomplete(input);

      autocomplete.addListener('place_changed', function() {
        var place = autocomplete.getPlace();
        // console.log('Selected place:', place);
        // Display the place details
      });
    }
  </script>
@endsection
