@extends('layouts.landing_page.master')

@section('title', e(__('Reset Password')))

@push('styles')
<meta http-equiv="cache-control" content="private, max-age=0, no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="0">
<link href="{{ asset('frontend/styles/css/page-css/login-register-page.css') }}" rel="stylesheet">
@endpush

@section('content')
    <section id="signin" class="h-100vh">
        <div class="container bgm-shadows">
            <div class="row">
                <div class="col-lg-7 col-md-12 col-sm-12 col-12">
                    <img class="d-block" src="{{ asset('frontend/images/logo.png') }}" alt="..." width="95px"
                         height="95px" />
                    <h2 class="lgm-title mt-2">{{ __('Reset Password') }}</h2>
                </div>
                <div class="col-lg-1 col-md-12 col-sm-12 col-1"></div>
                <div class="col-lg-4 col-md-12 col-sm-12 col-12">
                    <form action="{{ route('member.password.update') }}" method="POST" id="resetForm">
                        @csrf @method('PATCH')
                        <input type="hidden" name="token" value="{{ $token }}">
                        <div id="form3" class="grid md:grid-cols-1 gap-1">
                            <div class="relative z-0 w-full mb-1 group">
                                <input type="text" name="email"
                                       id="email" value="{{ $email }}"
                                       class="form-control @error('email') invalid-input @else border-clr @enderror email-validation block p-2.5 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-700 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer"
                                       placeholder="" aria-describedby="inputGroupPrependchangeemail" autocomplete="off" readonly/>
                                <label for="email" class="ml-2 p-2 py-0 peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:start-0 rtl:peer-focus:translate-x-1/4 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6 bg-white">{{ __('Email') }}</label>
                            </div>
                            <span id="email_error" class="custom-error m-0 p-0">@error('email'){{ __($message) }}@enderror</span>

                            <div class="relative z-0 w-full mb-1 group">
                                <input  type="password" name="password" id="password" value="{{ old('password') }}"
                                        class="block @error('password') invalid-input @else border-clr @enderror password-validation p-2.5 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-700 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer"
                                        placeholder="" aria-describedby="inputGroupPrependpassword"  />
                                <label for="password" class="ml-2 p-2 py-0 peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:start-0 rtl:peer-focus:translate-x-1/4 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6 bg-white">{{ __('Password') }}</label>
                                <div class="input-group-prepend rounded-right"> <i class="cursor-pointer bi bi-eye" onclick="passwordToText($(this))"></i> </div>
                                <span id="password_error" class="custom-error m-0 p-0">@error('password'){{ __($message) }}@enderror</span>
                            </div>

                            <div class="relative z-0 w-full mb-1 group">
                                <input  type="password" name="password_confirmation" id="password_confirmation"
                                        class="block @error('password_confirmation') invalid-input @else border-clr @enderror p-2.5 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-700 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer"
                                        placeholder="" aria-describedby="inputGroupPrependconfirmemail" />
                                <label for="password_confirmation" class="ml-2 p-2 py-0 peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:start-0 rtl:peer-focus:translate-x-1/4 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6 bg-white">{{ __('Confirm Password') }}</label>
                                <div class="input-group-prepend rounded-right"> <i class="cursor-pointer bi bi-eye" onclick="passwordToText($(this))"></i> </div>
                                <span id="password_confirmation_error" class="custom-error m-0 p-0">@error('password_confirmation'){{ __($message) }}@enderror</span>
                            </div>

                        </div>
                        <button type="submit" id="reset-btn" class="w-full primarym-btn text-center mt23">{!! renderIconHtml('save') !!}{{ __('Reset password') }}</button>.
                    </form>
                </div>
            </div>
        </div>
    </section>
@endsection

@push('scripts')
    <script>

        $(document).on("submit", "#resetForm", function (event) {
            event.preventDefault();
            $(".custom-error").html('');
            let url = $(this).attr("action");
            let formData = $(this)[0];
            let data = new FormData(formData);
            let formId = $(this).attr("id");
            let resetBtn = $("#resetForm #reset-btn");
            let resetBtnText = resetBtn.html();
            $.ajax({
                url: url,
                type: "POST",
                data: data,
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
                },
                contentType: false,
                processData: false,
                beforeSend: function () {
                    resetBtn.attr("disabled", true);
                    resetBtn.html('<i class="fa fa-spinner fa-spin"></i>&nbsp;&nbsp;Loading...');
                },
                success: function (response) {
                    resetBtn.prop("disabled", false);
                    resetBtn.html(resetBtnText);
                    $(".error").text(""); // remove old errors
                    if (response.success) {
                        if(typeof response.redirectTo !== "undefined"){
                            window.location.href = response.redirectTo;
                        } else {
                            swalMsg(response.message,'success');
                        }
                    } else {
                        swalMsg(response.message,response.status || 'success');
                    }
                },
                error: function (jqXHR) {
                    resetBtn.html(resetBtnText);
                    resetBtn.attr("disabled", false);

                    if (globalErrorHandler(jqXHR.responseJSON)) return false;
                    if (jqXHR.responseJSON && jqXHR.responseJSON.errors) {
                        $("#" + formId + " .error").text("");
                        for (const key in jqXHR.responseJSON.errors) {
                            if ($("#resetForm #" + key + "_error")) {
                                $("#resetForm #" + key).removeClass('invalid-input').addClass('border-clr');
                                $("#resetForm #" + key + "_error").text(
                                    jqXHR.responseJSON.errors[key]
                                );
                            }
                        }
                    }
                },
            });
        });
        $(document).ready(function () {
            $(document).on('keyup change', '#resetForm input,#resetForm select', function () {
                var input = $(this);
                if (input.parent().find('.validation-error,.custom-error').length) {
                    if (input.parent().find("#passwordCriteria").length != 1) {
                        input.parent().find('.validation-error,.custom-error').html('');
                    }
                } else {
                    var inputName = input.attr('name');
                    if ($("#" + inputName + "_error").find("#passwordCriteria").length != 1) {
                        $("#" + inputName + "_error").html('');
                    }
                }
                input.removeClass('invalid-input').addClass('border-clr');
            });
        });
    </script>
@endpush
