<template>
    <div class="row">
        <div class="col p-r-0">
            <div class="row no-margin">
                <div class="col">
                    <div class="row m-b-20 animated fadeInUpBig fast" v-if="error">
                        <div class="col p-l-50">
                            <small class="bold fs-10 text-danger">{{error}}</small>
                        </div>
                    </div>
                    <div class="row animated fadeInDownBig">
                        <div class="col b-t b-b b-grey animated" :class="[{'shake': $v.parameters.email.$error}]" :style="[$v.parameters.email.$error ? {'border-bottom': 'solid 2px red !important'} : {}]">
                            <div class="row align-items-center">
                                <div class="col pl-md-0 pr-md-0 bg-white">
                                    <div class="input-group transparent b-grey b-t">
                                        <span class="input-group-addon no-border">
                                            <i class="fa fa-user-o fa-fw p-l-15 p-t-20" style="padding-right: 18px;"></i>
                                        </span>
                                        <div class="form-control no-border bg-white p-t-25 p-b-35 p-l-0 p-r-0 text-muted fs-13 text-lowercase" style="cursor: default;">{{parameters.email}}</div>
                                        <validation-error-component :validator="$v.parameters.email" class="absolute fs-12" style=" top: 10px; right: 15px; z-index: 99;"></validation-error-component>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row animated fadeInUpBig">
                        <div class="col bg-white b-b b-grey animated" :class="[{'shake': $v.parameters.password.$error}]" :style="[$v.parameters.password.$error ? {'border-bottom': 'solid 2px red !important'} : {}]">
                            <div class="row align-items-center">
                                <div class="col pl-md-0 pr-md-0 bg-white">
                                    <div class="input-group transparent b-grey b-b">
                                        <span class="input-group-addon no-border">
                                            <i class="fa fa-lock fa-fw p-l-15 p-t-20" style="padding-right: 18px;"></i>
                                        </span>
                                        <input type="password" v-model="parameters.password" placeholder="Password" class="form-control no-border bg-white p-t-35 p-b-35 p-l-0 p-r-0">
                                        <validation-error-component :validator="$v.parameters.password" class="absolute fs-12" style=" top: 10px; right: 15px; z-index: 99;"></validation-error-component>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row m-t-30 align-items-center">
                <div class="col-auto animated fadeInLeftBig slow">
                    <div class="b-t b-b b-r b-primary bg-transparent all-caps text-primary p-t-15 p-b-15 p-l-50 p-r-50 bold font-lato pointer" style="letter-spacing: 8px;" @click="submit(route('api.account.authentication.authenticate.attempt'), 'post', section, false, false)">Login</div>
                </div>
                <div class="col animated fadeInRightBig slow">
                    <small class="font-lato muted fs-10 pointer" @click="$store.dispatch('toggleSection', {name: 'forgetPassword', status: true})" style=" letter-spacing: 2px; ">Forgot Password ?</small>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import { required, email } from "vuelidate/lib/validators";
    import request from '../../../mixins/requestMixin'

    export default {
        props: {
            section:{
                type: String,
                required: true
            },
            email: {
                type: String,
                required: true
            }
        },
        data() {
            return {
                error: '',
                parameters: {
                    email: this.email,
                    password: ''
                }
            };
        },
        validations: {
            parameters: {
                email: { required, email },
                password: { required }
            }
        },
        watch: {
            'email': function() {
                this.parameters.email = this.email;
            }
        },
        methods:{
            successHandler(response){
                localStorage.setItem('user-token', response.payload.access_token);
                this.$store.dispatch('userAuthentication', {access_token: response.payload.access_token});

                this.error = '';
                this.$store.dispatch('toggleSection', {name: 'loginSuccess', status: true})
                setTimeout(function(){
                    window.location.href = response.payload.redirect_url;
                }, 2000);
            },
            errorHandler(error){
                this.$store.dispatch('userAuthentication', {access_token: ''});
                localStorage.removeItem('user-token');

                this.error = error.message;
            }
        },
        mixins: [request]
    }

</script>