@extends('layouts.loginTemplate')

@section('content')

    <form class="form w-100" novalidate="novalidate" id="frm-reset-password" method="post"
        action="{{ url('post-reset-password') }}">

        <input type="hidden" name="userId" value="{{ $userIdView }}">
        <input type="hidden" name="_method" value="POST">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">

        <!--begin::Heading-->
        <div class="text-center mb-11">
            <!--begin::Title-->
            <h1 class="text-dark fw-bolder mb-3">Reset Password</h1>
            <!--end::Title-->
        </div>
        <!--begin::Heading-->


        <!--begin::Input group=-->
        <div class="fv-row mb-8">
            <!--begin::Email-->
            <input type="password" placeholder="Password" name="password" autocomplete="off"
                class="form-control bg-transparent" />
            <!--end::Email-->
        </div>

        <!--end::Input group=-->
        <!--begin::Wrapper-->
        <div class="d-flex flex-stack flex-wrap gap-3 fs-base fw-semibold mb-8">
            <div></div>
            <!--begin::Link-->
            <a href="{{ url('login') }}" class="link-primary">Login ?</a>
            <!--end::Link-->
        </div>
        <!--end::Wrapper-->

        <!--begin::Submit button-->
        <div class="d-grid mb-10">
            <button type="button" id="btn-submit" class="btn btn-primary">
                <!--begin::Indicator label-->
                <span class="indicator-label">Submit</span>
                <!--end::Indicator label-->
                <!--begin::Indicator progress-->
                <span class="indicator-progress">Please wait...
                    <span class="spinner-border spinner-border-sm align-middle ms-2"></span></span>
                <!--end::Indicator progress-->
            </button>
        </div>
        <!--end::Submit button-->

    </form>

@stop

@section('script')

    <script type="text/javascript">
        $(function() {

            $(document).on("click", "#btn-submit", function(e) {

                // handle bomb click
                e.preventDefault();

                alertLoading("Loading")

                formSubmit()
            })

            var inputPassword = document.getElementById("input-password");

            // Execute a function when the user presses a key on the keyboard
            inputPassword.addEventListener("keypress", function(event) {
                // If the user presses the "Enter" key on the keyboard
                if (event.key === "Enter") {
                    // Cancel the default action, if needed
                    event.preventDefault();
                    alertLoading("Loading")
                    // Trigger the button element with a click
                    document.getElementById("btn-submit").click();
                }
            });


        });

        function formSubmit() {
            // form data
            const form = $('#frm-reset-password')[0];
            const formData = new FormData(form);

            // set up ajax
            initAjaxSetupToken();

            const ajaxProp = {
                url: "{{ url('post-reset-password') }}",
                dataType: 'json',
                method: 'post',
                data: formData,
                processData: false,
                contentType: false,
            };

            $.ajax(ajaxProp).done(function(res) {
                if (isAjaxStatusSuccess(res.status)) {
                    alertSuccess('Notice', 'Please check your email address')
                    window.location.href = '{{ url('') }}';
                    hideLoading();
                } else {
                    alertFail('Notice', res.message ?? "Reset password unsuccessfully");
                }
            }).fail(function(xhr, status, error) {
                logAjaxError(xhr, status, error);
                alertFail('Notice', error.message ?? "Reset password unsuccessfully");
                hideLoading();
            });
        }
    </script>

@endsection
