<script setup lang="js">
import { ref, toRaw } from 'vue'
import { RouterLink, useRouter } from 'vue-router'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'

const toast = useToast({
  position: 'top-right'
})

const router = useRouter()

const isLoading = ref(false)
const username = ref('')
const email = ref('')
const password = ref('')
const passwordConfirm = ref('')

const register = async (e) => {
  e.preventDefault()
  isLoading.value = true

  if (toRaw(password.value) !== toRaw(passwordConfirm.value)) {
    toast.error('Password and confirm password do not match')
    return
  }

  try {
    const res = await axios.post('/auth/signup', {
      username: toRaw(username.value),
      email: toRaw(email.value),
      password: toRaw(password.value)
    })

    toast.default('Successfully created a new account')
    sessionStorage.setItem('email', res.data.data.Email)
    router.push({ path: `/login` })
  } catch (err) {
    const statusCode = err.request.status

    switch (statusCode) {
      // TODO: handle all potential errors
      case 409:
        toast.error(err.response.data.error)
        break
      default:
        toast.error('An unhandled exception occoured. Please check logs')
        console.error(err)
    }
  }

  isLoading.value = false
}
</script>

<template>
  <div id="signin-form" class="mx-auto mt-5">
    <ul class="nav nav-pills nav-justified mb-3" id="ex1" role="tablist">
      <li class="nav-item" role="presentation">
        <RouterLink
          class="nav-link"
          id="tab-login"
          data-mdb-toggle="pill"
          to="/login"
          role="tab"
          aria-controls="pills-login"
          aria-selected="true"
          >Login</RouterLink
        >
      </li>
      <li class="nav-item" role="presentation">
        <RouterLink
          class="nav-link active"
          id="tab-register"
          data-mdb-toggle="pill"
          to="/register"
          role="tab"
          aria-controls="pills-register"
          aria-selected="false"
          >Register</RouterLink
        >
      </li>
    </ul>

    <div>
      <form v-on:submit="register">
        <div class="form-floating mb-4">
          <input
            id="register-username-input"
            type="text"
            class="form-control"
            placeholder=""
            v-model="username"
          />
          <label for="register-username-input">Username</label>
        </div>

        <div class="form-floating mb-4">
          <input
            id="register-email-input"
            type="text"
            class="form-control"
            placeholder=""
            v-model="email"
          />
          <label for="register-email-input">E-Mail</label>
        </div>

        <div class="form-floating mb-4">
          <input
            id="register-password-input"
            type="password"
            class="form-control"
            placeholder=""
            v-model="password"
          />
          <label for="register-password-input">Password</label>
        </div>

        <div class="form-floating mb-4">
          <input
            id="register-confirmpassword-input"
            type="password"
            class="form-control"
            placeholder=""
            v-model="passwordConfirm"
          />
          <label for="register-confirmpassword-input">Confirm Password</label>
        </div>

        <button
          type="submit"
          class="btn btn-primary btn-block mb-4"
          :class="{ disabled: isLoading }"
        >
          Create Account
          <div v-if="isLoading" class="spinner-border spinner-border-sm ms-1" role="status">
            <span class="sr-only"></span>
          </div>
        </button>

        <div class="text-center">
          <p>Already have an account? <RouterLink to="/login">Sign In</RouterLink></p>
        </div>
      </form>
    </div>
  </div>
</template>

<style>
#signin-form {
  max-width: 30rem;
  width: 100%;
}
</style>
