<script setup>
import Checkbox from "../../Components/Checkbox.vue";
import GuestLayout from "../../Layouts/GuestLayout.vue";
import InputError from "../../Components/InputError.vue";
import InputLabel from "../../Components/InputLabel.vue";
import TextInput from "../../Components/TextInput.vue";
import { Head, Link, useForm, router } from "@inertiajs/vue3";

defineProps({
    canResetPassword: {
        type: Boolean,
    },
    status: {
        type: String,
    },
});

const form = useForm({
    email: "",
    password: "",
    remember: false,
});

// if log in is a success, reroute to user files
const submit = () => {
    form.post(route("login"), {
        onFinish: () => form.reset("password"),
        onSuccess: () => router.visit(route("login")),
    });
};
</script>

<template>
    <GuestLayout>
        <Head title="Log in" />

        <div v-if="status" class="mb-4 font-medium text-sm text-green-600">
            {{ status }}
        </div>

        <form @submit.prevent="submit">
            <div>
                <InputLabel for="email" value="Email" />

                <TextInput
                    id="email"
                    type="email"
                    class="mt-1 block w-full"
                    v-model="form.email"
                    required
                    autofocus
                    autocomplete="username"
                />

                <InputError class="mt-2" :message="form.errors.email" />
            </div>

            <div class="mt-4">
                <InputLabel for="password" value="Password" />

                <TextInput
                    id="password"
                    type="password"
                    class="mt-1 block w-full"
                    v-model="form.password"
                    required
                    autocomplete="current-password"
                />

                <InputError class="mt-2" :message="form.errors.password" />
            </div>

            <div class="block mt-4">
                <label class="flex items-center">
                    <Checkbox name="remember" v-model:checked="form.remember" />
                    <span class="ml-2 text-sm text-gray-400">Remember me</span>
                </label>
            </div>

            <div class="flex items-center justify-end mt-4 gap-6">
                <Link
                    :href="route('register')"
                    class="underline text-sm text-gray-400 hover:text-gray-100 rounded-md"
                >
                    Register
                </Link>

                <Link
                    :href="route('password.request')"
                    class="underline text-sm text-gray-400 hover:text-gray-100 rounded-md"
                >
                    Forgot your password?
                </Link>

                <button
                    class="px-6 py-3 border-sky-600 border rounded-lg hover:bg-sky-600 loginbutton"
                    :class="{ 'opacity-25': form.processing }"
                    :disabled="form.processing"
                >
                    Log in
                </button>
            </div>
        </form>
    </GuestLayout>
</template>
