<template>
  <div class="account-page">
    <!-- Main Wrapper -->
    <div class="main-wrapper">
      <div class="account-content">
        <div class="w-100 overflow-hidden position-relative flex-wrap d-block vh-100">
          <div class="row">
            <div class="col-lg-6">
              <div
                class="d-lg-flex align-items-center justify-content-center bg-light-300 d-lg-block d-none flex-wrap vh-100 overflowy-auto bg-01"
              >
                <div>
                  <img
                    src="@/assets/img/authentication/authentication-06.svg"
                    alt="Img"
                  />
                </div>
              </div>
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12">
              <div
                class="row justify-content-center align-items-center vh-100 overflow-auto flex-wrap"
              >
                <div class="col-md-8 mx-auto p-4">
                  <form
                    method="get"
                    class="digit-group login-form-control"
                    data-group-name="digits"
                    data-autosubmit="false"
                    autocomplete="off"
                    action="/"
                  >
                    <div class="card">
                      <div class="card-body">
                        <div class="mb-4">
                          <h2 class="mb-2">Login with your Email Address</h2>
                          <p class="mb-0">
                            We sent a verification code to your email. Enter the code from
                            the email in the field below
                          </p>
                        </div>
                        <div class="text-center">
                          <div class="d-flex align-items-center mb-3">
                            <input
                              v-for="(digit, index) in otpDigits"
                              :key="index"
                              :ref="'digit' + index"
                              type="text"
                              class="border rounded w-100 py-sm-3 py-2 text-center fs-26 fw-bold me-3"
                              maxlength="1"
                              inputmode="numeric"
                              pattern="[0-9]*"
                              v-model="otp[index]"
                              @input="onInput(index, $event)"
                              @keydown.backspace="onBackspace(index, $event)"
                            />
                          </div>
                          <div>
                            <div class="badge bg-soft-danger mb-3">
                              <p>Otp will expire in 09:59</p>
                            </div>
                            <div class="form-wrap mb-0">
                              <button type="submit" class="btn btn-primary w-100">
                                Verify My Account
                              </button>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="mt-5 text-center">
                      <p class="mb-0">Copyright &copy; {{ new Date().getFullYear() }} - Preskool</p>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- /Main Wrapper -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      otp: ["", "", "", ""],
      otpDigits: [0, 1, 2, 3],
    };
  },
  methods: {
    onInput(index, event) {
      const value = event.target.value;

      // Allow only digits
      if (!/^\d$/.test(value)) {
        this.otp[index] = "";
        return;
      }

      // Move to next input
      if (index < this.otp.length - 1) {
        const nextInput = this.$refs[`digit${index + 1}`][0];
        nextInput.focus();
      }
    },
    onBackspace(index, event) {
      if (!this.otp[index] && index > 0) {
        const prevInput = this.$refs[`digit${index - 1}`][0];
        prevInput.focus();
      }
    },
    submitForm() {
      this.$router.push("/reset-password");
    },
  },
};
</script>
