'use client'

import { FormEvent, useState } from 'react'
import { useRouter } from 'next/navigation'
import { Loader2 } from 'lucide-react'

export default function LoginPage () {
  const [username, setUsername] = useState('')
  const [password, setPassword] = useState('')
  const [loading, setLoading] = useState<boolean>(false)

  const router = useRouter()
  async function handleSubmit (event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    setLoading(true)
    console.log('username', username, 'pass', password)
    const response = await fetch('/api/auth/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username, password })
    })

    if (response.ok) {
      router.push('/dashboard')
    } else {
      console.log(response)
    }
    setLoading(false)
  }

  return (
    <form
      onSubmit={handleSubmit}
      className='min-h-screen w-full flex flex-col items-center bg-dot-black/[0.2] dark:bg-dot-white/[0.2] justify-center relative'
    >      
      <div className='absolute pointer-events-none inset-0 flex bg-background items-center justify-center [mask-image:radial-gradient(ellipse_at_center,transparent_10%,black)]'></div>
      <div className='relative z-30 p-10 w-full md:max-w-sm flex flex-grow justify-center flex-col gap-4 items-center'>
        <svg
          width='30'
          height='30'
          viewBox='0 0 1000 1000'
          fill='none'
          xmlns='http://www.w3.org/2000/svg'
          className='w-10 h-10'
        >
          <path
            d='M500.268 179.189L726.13 665.598L499.732 820.811L273.594 665.221L500.268 179.189Z'
            stroke='currentColor'
            strokeWidth={89}
          />
        </svg>
        <input
          type='text'
          className='p-[16px] h-fit text-base rounded-md border font-mono border-border shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:border-border disabled:cursor-not-allowed disabled:opacity-50 w-full '
          name='username'
          autoComplete='off'
          placeholder='username'
          value={username}
          onChange={e => setUsername(e.target.value)}
          required
        />
        <input
          type='password'
          name='password'
          placeholder='password'
          autoComplete='off'
          className='p-[16px] h-fit text-base rounded-md border font-mono border-border shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:border-border disabled:cursor-not-allowed disabled:opacity-50 w-full '
          value={password}
          onChange={e => setPassword(e.target.value)}
          required
        />
        <button
          type='submit'
          disabled={loading}
          className='w-full bg-primary-foreground dark:bg-white flex items-center justify-center gap-2 h-[50px] text-background dark:text-black p-[10px] rounded-md'
        >
          {loading ? <Loader2 className='animate-spin' /> : 'Login'}
        </button>
      </div>
      <p className='font-mono text-sm text-muted-foreground mb-4'>v1.2.4-r</p>

    </form>
  )
}
