<template>
  <div
    class="grid min-h-screen place-items-center bg-[#faf9f5] px-4 py-12 font-sans text-[#141413]"
  >
    <main
      class="grid w-full max-w-[1040px] grid-cols-1 items-center gap-6 md:grid-cols-[minmax(0,1fr)_minmax(340px,420px)] md:gap-12"
      aria-labelledby="create-account-title"
    >
      <section class="relative pt-2 md:py-8">
        <p class="m-0 text-xs font-medium uppercase leading-[1.4] tracking-[1.5px] text-[#6c6a64]">
          Nova conta
        </p>
        <h1
          id="create-account-title"
          class="my-4 max-w-[560px] font-serif text-[42px] font-normal leading-[1.04] tracking-[-0.8px] text-[#141413] md:my-5 md:text-[clamp(42px,7vw,68px)] md:tracking-[-1.2px]"
        >
          Crie sua conta
        </h1>
        <p class="m-0 max-w-[510px] text-base font-normal leading-[1.55] text-[#3d3d3a] md:text-lg">
          Um acesso simples para entrar no sistema com email, senha e uma experiencia mais calma
          desde o primeiro clique.
        </p>
        <div
          class="mt-7 w-full max-w-[440px] rounded-2xl border border-[#e6dfd8] bg-[#f5f0e8] p-5 md:mt-10 md:p-6"
          aria-hidden="true"
        >
          <span class="block h-3 w-3/4 rounded-full bg-[#181715]"></span>
          <span class="my-4 block h-3 w-3 rounded-full bg-[#5db8a6]"></span>
          <span class="block h-3 w-[48%] rounded-full bg-[#cc785c]"></span>
        </div>
      </section>

      <section
        class="grid gap-[18px] rounded-xl border border-[#e6dfd8] bg-[#efe9de] p-6 md:p-8"
        aria-label="Formulario de criacao de conta"
      >
        <div class="mb-1.5">
          <span
            class="inline-flex min-h-[26px] items-center rounded-full bg-[#cc785c] px-3 py-1 text-xs font-medium uppercase leading-none tracking-[1.5px] text-white"
          >
            Cadastro
          </span>
          <h2
            class="mt-4 font-serif text-[32px] font-normal leading-[1.15] tracking-[-0.5px] text-[#141413]"
          >
            Dados de entrada
          </h2>
        </div>

        <div class="grid gap-2">
          <label
            class="m-0 text-xs font-medium uppercase leading-[1.4] tracking-[1.5px] text-[#6c6a64]"
            for="email"
          >
            Email
          </label>
          <input
            v-model="formData.email"
            class="h-11 w-full rounded-lg border border-[#e6dfd8] bg-[#faf9f5] px-3.5 py-2.5 text-base leading-[1.55] text-[#141413] outline-none transition focus:border-[#cc785c] focus:shadow-[0_0_0_3px_rgba(204,120,92,0.15)]"
            type="text"
            id="email"
            name="email"
          />
        </div>

        <div class="grid gap-2">
          <label
            class="m-0 text-xs font-medium uppercase leading-[1.4] tracking-[1.5px] text-[#6c6a64]"
            for="senha"
          >
            Senha
          </label>
          <input
            v-model="formData.password"
            class="h-11 w-full rounded-lg border border-[#e6dfd8] bg-[#faf9f5] px-3.5 py-2.5 text-base leading-[1.55] text-[#141413] outline-none transition focus:border-[#cc785c] focus:shadow-[0_0_0_3px_rgba(204,120,92,0.15)]"
            type="password"
            id="senha"
            name="senha"
          />
        </div>

        <button
          class="mt-1.5 h-11 rounded-lg border border-[#cc785c] bg-[#cc785c] px-5 text-sm font-medium leading-none text-white transition hover:border-[#a9583e] hover:bg-[#a9583e] active:border-[#a9583e] active:bg-[#a9583e]"
          @click="criarConta"
        >
          Criar Conta
        </button>
      </section>
    </main>
  </div>
</template>

<script setup>
const formData = reactive({
  email: '',
  password: ''
})

async function criarConta() {
  try {
    ;(
      await fetch('/auth/register', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          email: formData.email,
          password: formData.password
        })
      })
    )()
  } catch (error) {
    window.alert('Ocorreu um erro ao criar a conta. Tente novamente mais tarde.', error.statusText)
  }
}
</script>
