@extends('layouts.app')

@section('content')
    <div class="container-fluid" id="list">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">登录</div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 text-md-right">
                                <img src="{{asset('images/login/bg1.jpg')}}" alt="" class="img-fluid">
                            </div>
                            <div class="col-md-6"><form method="POST" action="{{ route('login') .'?rand='.microtime(true)}}">
                                    @csrf
                                    <label for="name" class="col-form-label text-md-right">用户名</label>
                                    <div class="form-group row">

                                        <div class="col-md-8">
                                            <input id="name" type="text" class="form-control" :class="[errors.name?'is-invalid':'']"
                                                   oninput="setTimeout(function(){vue.errors.name=''},10)"
                                                   name="name" v-model="inputting.name" required autocomplete="name" autofocus>

                                            <span class="invalid-feedback" v-if="errors.name">
                                                <strong v-for="msg in errors.name">@{{ msg }}</strong>
                                            </span>
                                        </div>
                                    </div>

                                    <label for="password" class="col-form-label text-md-right">密码</label>
                                    <div class="form-group row">

                                        <div class="col-md-8">
                                            <input id="password" type="password" class="form-control " :class="[errors.password?'is-invalid':'']"
                                                   @keypress="passwordInput($event)"
                                                   v-model="inputting.password" name="password" required autocomplete="current-password">

                                            <span class="invalid-feedback" v-if="errors.password">
                                                <strong v-for="msg in errors.password">@{{ msg }}</strong>
                                            </span>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <div class="col-md-6">
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                                <label class="form-check-label" for="remember">
                                                    记住我
                                                </label>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group row mb-0">
                                        <div class="col-md-8">
                                            <button type="button" class="btn btn-primary form-control" onclick="loginSubmit()">
                                                登录
                                            </button>

                                            {{--                                @if (Route::has('password.request'))--}}
                                            {{--                                    <a class="btn btn-link" href="{{ route('password.request') }}">--}}
                                            {{--                                        {{ __('Forgot Your Password?') }}--}}
                                            {{--                                    </a>--}}
                                            {{--                                @endif--}}
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('lastScript')
    <script>
        let vue=new Vue({
                el:"#list",
                mounted:function(){
                    $('.invalid-feedback').addClass('d-block')
                },
                data:{
                    inputting:{name:'',password:'',is_json:true},
                    errors: {name:'',password:'',},
                },
                methods:{
                    passwordInput:function(e){
                        setTimeout(function(){vue.errors.password=''},10);
                        if(e.key==='Enter'){
                            loginSubmit()
                        }
                    },
                }
            }
        )
        function loginSubmit() {
            tempTip.setDuration(99999);
            tempTip.waitingTip('提交中');
            axios.post('{{ route('login') .'?rand='.microtime(true)}}',vue.inputting).then(function (response) {
                tempTip.setDuration(2000);
                tempTip.cancelWaitingTip();
                if(response.data.success!==true){
                    if(response.data.errors){
                        vue.errors=response.data.errors;
                        return;
                    }
                    if(response.data.message){
                        tempTip.show(response.data.message)
                        return;
                    }
                }else{
                    tempTip.setDuration(99999);
                    tempTip.waitingTip('页面跳转中');
                    location=response.data.url;
                }
            }).catch(function(response){
                tempTip.setDuration(6000);
                tempTip.waitingTip('错误');
                console.log(response)
                alert('页面过期或网络异常，请在刷新后重新登录');
                window.location.reload();
            })
        }
    </script>
@endsection
