@extends('layouts/blankLayout')

@section('title', 'Register Basic - Pages')

@section('page-style')
<!-- Page -->
<link rel="stylesheet" href="{{asset('assets/vendor/css/pages/page-auth.css')}}">
@endsection

@section('content')
<div class="container-xxl">
  <div class="authentication-wrapper authentication-basic container-p-y">
    <div class="authentication-inner">

      <!-- Register Card -->
      <div class="card">
        <div class="card-body">
          <!-- Logo -->
          <div class="app-brand justify-content-center">
            <a href="{{ url('/') }}" class="app-brand-link gap-2">
              <span class="app-brand-logo demo">
                <img src="{{ asset('assets/img/logo/seiza.png') }}" alt="Logo" width="100">
              </span>
              <!-- <span class="app-brand-text demo text-body fw-bolder">{{ config('variables.templateName') }}</span> -->
            </a>
          </div>

          <!-- /Logo -->
          <h4 class="mb-2">Welcome to LKP Seiza 👋</h4>
          <p class="mb-4">Create your account buddy!</p>

          <form id="formAuthentication" class="mb-3" action="{{url('/api/register')}}" method="POST"
            onsubmit="return validateForm()">
            <div class="mb-3">
              <label for="nama" class="form-label">Nama</label>
              <input type="text" class="form-control" id="nama" name="nama" placeholder="Enter your name" autofocus>
              <div class="invalid-feedback" id="nameError">Please enter a valid name.</div>
            </div>
            <div class="mb-3">
              <label for="username" class="form-label">Username</label>
              <input type="text" class="form-control" id="username" name="username" placeholder="Enter your username"
                autofocus>
              <div class="invalid-feedback" id="usernameError">Please enter a valid username.</div>
            </div>
            <div class="mb-3">
              <label for="email" class="form-label">Email</label>
              <input type="text" class="form-control" id="email" name="email" placeholder="Enter your email">
              <div class="invalid-feedback" id="emailError">Please enter a valid email.</div>
            </div>
            <div class="mb-3 form-password-toggle">
              <label class="form-label" for="password">Password</label>
              <div class="input-group input-group-merge">
                <input type="password" id="password" class="form-control" name="password"
                  placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
                  aria-describedby="password" />
                <span class="input-group-text cursor-pointer"><i class="bx bx-hide"></i></span>
              </div>
              <div class="invalid-feedback" id="passwordError">Please enter a valid password.</div>
            </div>
            <div class="mb-3 form-password-toggle">
              <label class="form-label" for="confirm_password">Confirm Password</label>
              <div class="input-group input-group-merge">
                <input type="password" id="confirm_password" class="form-control" name="confirm_password"
                  placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
                  aria-describedby="password" />
                <span class="input-group-text cursor-pointer"><i class="bx bx-hide"></i></span>
              </div>
              <div class="invalid-feedback" id="confirmPasswordError">Password does not match.</div>
            </div>

            <div>
              @if($errors->any())
          <div class="alert alert-danger" role="alert">
          {{$errors->first()}}
          </div>
        @endif
            </div>

            <!-- <div class="mb-3">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" id="terms-conditions" name="terms">
                <label class="form-check-label" for="terms-conditions">
                  I agree to
                  <a href="javascript:void(0);">privacy policy & terms</a>
                </label>
              </div>
            </div> -->
            <button class="btn btn-primary d-grid w-100" type="submit">
              Sign up
            </button>
          </form>

          <p class="text-center">
            <span>Already have an account?</span>
            <a href="{{url('auth/login-basic')}}">
              <span>Sign in instead</span>
            </a>
          </p>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

<script>
  function validateForm() {
    let isValid = true;
    const name = document.getElementById('nama').value;
    const nameError = document.getElementById('nameError');
    const username = document.getElementById('username').value;
    const usernameError = document.getElementById('usernameError');
    const email = document.getElementById('email').value;
    const emailError = document.getElementById('emailError');
    const allowedDomains = ['gmail.com', 'yahoo.co.id'];
    const password = document.getElementById('password').value;
    const passwordError = document.getElementById('passwordError');
    const confirmPassword = document.getElementById('confirm_password').value;
    const confirmPasswordError = document.getElementById('confirmPasswordError');

    if (name?.length > 0) {
      nameError.style.display = 'none';
    } else {
      nameError.style.display = 'block';
      isValid = false;
    }

    if (username?.length > 0) {
      usernameError.style.display = 'none';
    } else {
      usernameError.style.display = 'block';
      isValid = false;
    }

    const emailDomain = email.substring(email.lastIndexOf('@') + 1);
    if (!allowedDomains.includes(emailDomain)) {
      emailError.style.display = 'block';
      isValid = false;
    } else {
      emailError.style.display = 'none';
    }

    if (password?.length > 0) {
      passwordError.style.display = 'none';
    } else {
      passwordError.style.display = 'block';
      isValid = false;
    }

    if (password && (password == confirmPassword)) {
      confirmPasswordError.style.display = 'none';
    } else {
      confirmPasswordError.style.display = 'block';
      isValid = false;
    }

    return isValid;
  }
</script>
@endsection