<template lang="pug">
main.view--login.w-full.f-col
    header
        h2 Login

    article
        form(@submit.prevent='onSubmit')
            label profile id:
            input(required='' type='profileId' v-model='form.profileId')
            button(type='submit') submit
</template>

<script>
import { currentProfile } from '../services'

export default {
    data: () => ({
        form: {
            profileId: null
        },
    }),
    methods: {
        async onSubmit() {
            if(!this.form.profileId) console.error('No profile in form')
            
            /**
             * Default to the HomeView and alter as needed (side-effects!)
             */
            const toRoute = { name: 'HomeView' }
            
            /**
             * Profile needs a complete survey and
             * alters the url if it's incomplete
             */
            const alreadyLoggedIn = await currentProfile.isLoggedIn()
            if(!alreadyLoggedIn) {
                await currentProfile.login(this.form.profileId)
            }

            this.$router.push(toRoute)
        },
    },
}
</script>

<style lang="postcss">

</style>
