<template>
    <v-card flat>
        <v-form ref="form" v-model="valid" lazy-validation>
            <v-card-text>
                <v-text-field
                    v-model="form.username"
                    :rules="usernameRules"
                    label="Username"
                    required
                    prepend-icon="mdi-account"
                ></v-text-field>

                <v-text-field
                    v-model="form.password"
                    :rules="passwordRules"
                    label="Password"
                    required
                    prepend-icon="mdi-lock"
                    type="password"
                ></v-text-field>
            </v-card-text>
            <v-card-actions>
                <v-spacer />
                <v-btn
                    :disabled="!valid"
                    color="success"
                    type="submit"
                    @click.prevent="userLogin"
                >
                    Login
                </v-btn>
            </v-card-actions>
        </v-form>
    </v-card>
</template>

<script>
export default {
    name: 'LoginForm',
    data: () => ({
        valid: false,
        form: {
            username: '',
            password: '',
        },
        usernameRules: [(v) => !!v || 'Name is required'],
        passwordRules: [(v) => !!v || 'Password is required'],
    }),
    methods: {
        userLogin() {
            try {
                this.$auth.loginWith('local', {
                    data: this.form,
                })
            } catch (err) {
                console.log(err)
            }
        },
    },
}
</script>
