<script setup lang="ts">
import MainLayout from "@/Layouts/MainLayout.vue";
import GlassBox from "@/Components/FlightsGoneBy/GlassBox.vue";
import { Head, Link, useForm } from "@inertiajs/vue3";

defineOptions({ layout: MainLayout });

defineProps<{
    canResetPassword: boolean;
    status?: string;
}>();

const isStandalone = (): boolean => {
    if (typeof window === 'undefined') return false;

    return window.matchMedia('(display-mode: standalone)').matches
        || (window.navigator as any).standalone === true;
};

const form = useForm({
    email: '',
    password: '',
    remember: isStandalone(),
});

const submit = () => {
    form.post(route('login'), {
        onFinish: () => form.reset('password'),
    });
};
</script>

<template>
    <Head title="Log in" />
    <GlassBox title="Welcome Back" blurb="Log in to continue logging your flights.">

        <v-alert v-if="status" type="success" class="mb-2">{{ status }}</v-alert>

        <v-form style="width: 100%" @submit.prevent="submit">
            <v-container>
                <v-row>
                    <v-col cols="12">
                        <v-text-field
                            v-model="form.email"
                            label="Email"
                            type="email"
                            autocomplete="username"
                            autofocus
                            :error-messages="form.errors.email"
                        />
                    </v-col>
                </v-row>

                <v-row>
                    <v-col cols="12">
                        <v-text-field
                            v-model="form.password"
                            label="Password"
                            type="password"
                            autocomplete="current-password"
                            :error-messages="form.errors.password"
                        />
                    </v-col>
                </v-row>

                <v-row>
                    <v-col cols="12">
                        <v-checkbox
                            v-model="form.remember"
                            label="Remember me"
                            hide-details
                        />
                    </v-col>
                </v-row>

                <v-row>
                    <v-col cols="12" class="d-flex align-center justify-end" style="gap: 1rem">
                        <v-btn
                            block
                            variant="elevated"
                            size="large"
                            type="submit"
                            :loading="form.processing"
                            :disabled="form.processing"
                        >
                            Log in
                        </v-btn>
                    </v-col>
                </v-row>
                <v-row>
                    <v-col cols="12" class="d-flex align-center justify-end" style="gap: 1rem">
                        <Link
                            v-if="canResetPassword"
                            :href="route('password.request')"
                            style="opacity: 0.7; font-size: 0.9rem"
                        >
                            Forgot your password?
                        </Link>
                    </v-col>
                </v-row>
            </v-container>
        </v-form>
    </GlassBox>
</template>

<style scoped>
h2 {
    font-size: 2rem;
    text-align: center;
}

p {
    text-align: center;
    width: 100%;
    opacity: 0.7;
    margin-bottom: 1rem;
}
</style>
