<template>
    <div id="login_form">
        <div class="holder">
            <div class="logo">
                <img src="assets/images/cms_logo_white.png" alt="">
            </div>

            <div class="content">
                <p class="intro_text">Bitte melde dich mit deinen Benutzerdaten an.</p>
                <my-form :elements="elements" :buttons="buttons" :submitHandler="login"></my-form>
                <transition name="component-fade">
                    <div class="loginerror" v-show="loginError">{{ loginError }}</div>
                </transition>
            </div>
        </div>
    </div>
</template>

<script>
import MyForm from "components/my-form";
import MyInput from "components/my-input";

export default {
    name: "LoginForm",
    components: {
        MyForm,
        MyInput
    },
    data() {
        return {
            username: "",
            password: "",
            elements: {
                username: {
                    element: MyInput,
                    icon: "icon-user",
                    required: true,
                    requiredMessage: "Der Benutzername wird benötigt!",
                    props: {
                        type: "text",
                        placeholder: "Benutzername"
                    }
                },
                password: {
                    element: MyInput,
                    icon: "icon-key",
                    required: true,
                    requiredMessage: "Das Passwort wird benötigt!",
                    props: {
                        type: "password",
                        placeholder: "Passwort"
                    }
                }
            },
            buttons: [{
                label: "login",
                type: "submit"
            }],
            loginError: ""
        }
    },
    methods: {
        login(formData) {
            this.$store.dispatch("login", formData)
                .then(user => {
                    console.log("---- user login --------");
                    console.log(user);
                    this.$router.go(-1);
                })
                .catch(error => {
                    console.log("---- login error -------");
                    console.log(error);
                    this.loginError = "Loginfehler: " + error;
                });
        }
    }
}
</script>

<style scoped>
    .loginerror {
        color: black;
        font-size: 1em;
        text-align: center;
        background-color: #faa;
        padding: 10px;
        margin-top: 10px;
        border-radius: 30px;
    }
</style>

<style lang="less">
@import "~mixins";

/*--------------------------------------------------------------
# LOGINBOX
--------------------------------------------------------------*/
#login_form{
    /* Disply on top */
    position: fixed;
    width: 100%;
    height: 100%;
    z-index: 3000;
    top:0px; left:0px; bottom:0px; right:0px;

    /* Center vertically */
    display: flex;
    justify-content: center;
    align-items: center;


    background: @cms_brand_primary;

    &>.holder {
        position: relative;
        display: block;
        max-width: 380px;
        width: 90%;
        height: auto;
        margin: 0px;
        border-radius: 2px;
        padding: 20px;
    }

    .logo{
        line-height:0; margin-bottom:15px; text-align:center;
        img {width:100%; max-width:72px;}
    }

    .content{
        .clearfix();
        p {text-align:center; color: white; font-size:15px; line-height:1.2;}
    }

    .intro_text{
        font-size:14px!important; margin-bottom:20px; line-height:1.5;
    }

    form{
        text-align: center;
    }

    input{
        height: 40px;
        padding: 0px 15px;
        border-color: transparent;

        &:focus {
            border: solid 1px @gray_dark;
            background: white;
        }
    }
}
</style>

