<script setup>
  useHead({
    title: 'Sign in | crbtc.org',
    meta: [
      { 
        name: 'description', 
        content: `` 
      },      
    ],
  })

  import { Form, Field, ErrorMessage } from 'vee-validate';

  function required(value) {
    return value ? true : 'This field is required';
  }

  const goodEmail = (email) => {
    if (!email) {
      return 'This field is required'
    }
  }


  const { login } = useDirectusAuth()

  const onSubmit = async (values) => {
    try {
      await login({ 
        email: values.email,
        password: values.password,
        role: process.env.DIRECTUS_ROLE
      })

      // Redirect on success
      navigateTo('/')

    } catch (e) {
      console.log(e)
    }
  }

</script>

<template>
  <div class="login">
    <h3>Sign in to your account</h3>
    <h4 class="policy">By continuing, you agree to our <NuxtLink href="terms">Terms of 
      use</NuxtLink> and <NuxtLink href="privacy">Privacy policy</NuxtLink>.</h4>

    <Form v-slot="{ errors }" @submit="onSubmit" >
      <div class="input">
        <label>Email</label>
        <Field name="email" :rules="required" placeholder="Enter your email" />
        <ErrorMessage name="email" />
      </div>

      <div class="input">
        <label>Password</label>
        <Field name="password" :rules="required" placeholder="********" type="password" />
        <ErrorMessage name="password" />
      </div>

      <button>Sign in</button>
    </Form>

    <p class="newbie">Don't have an account? <NuxtLink href="/register">Create account</NuxtLink></p>
    <p class="reset">Forgot password? <NuxtLink href="/reset">Reset password</NuxtLink></p>

  </div>
</template>