@extends('user.layouts.app')
@section('title', 'Atur Ulang Kata Sandi | ' . website_config('main')->website_name)
@section('description', 'Atur ulang kata sandi akun Anda dengan aman di ' . website_config('main')->website_name . '.')
@section('keywords', 'atur ulang kata sandi, reset password, ' . website_config('main')->meta_keywords)
@section('robots', 'noindex,nofollow')
@section('breadcrumb-first', 'Atur Ulang Kata Sandi')
@section('breadcrumb-second', website_config('main')->website_name)
@section('page-title', 'Atur Ulang Kata Sandi')
@section('content')
<div class="row">
	<div class="col-lg-6 offset-lg-3">
		<div class="card card-custom">
			<div class="card-body">
                <form method="post" action="{{ request()->url() }}" id="input_form">
                    @csrf
                    <div class="form-group mb-3">
                        <label class="form-label">Email <text class="text-danger">*</text></label>
                        <input class="form-control" type="email" name="email" id="email" value="{{ $target->email }}" readonly>
                        <small id="email"></small>
                    </div>
                    <div class="form-group mb-3">
                        <label class="form-label" for="password">Password <text class="text-danger">*</text></label>
                        <input type="password" class="form-control" name="password" id="password">
                        <small id="password"></small>
                    </div>
                    <div class="form-group mb-3">
                        <label class="form-label" for="confirm_password">Konfirmasi Password <text class="text-danger">*</text></label>
                        <input type="password" class="form-control" name="confirm_password" id="confirm_password">
                        <small id="confirm_password"></small>
                    </div>
                    <div class="button-items d-grid gap-2">
                        <button class="btn btn-custom btn btn-block waves-effect waves-light" type="submit">Atur Ulang Kata Sandi</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    $("#input_form").on('submit', function(element) {
        element.preventDefault();
        $.ajax({
            url: $(this).attr('action'),
            method: $(this).attr('method'),
            data: new FormData(this),
            processData: false,
            dataType: 'json',
            contentType: false,
            beforeSend: function() {
				$('#preloader').fadeIn("slow");
                $('button:submit').attr('disabled', 'true');
                $('button:submit').text('');
                $('button:submit').append('Mohon tunggu...');
                $(document).find('input, select').removeClass('is-invalid').removeClass('is-valid');
            },
            success: function(response) {
				$('#preloader').fadeOut("slow");
                $('button:submit').removeAttr('disabled');
                $('button:submit').text('Atur Ulang Kata Sandi');
                if (response.status == false) {
                    if (response.type == 'validation') {
                        $.each(response.message, function(key, value) {
							$('input#'+key+', select#'+key+'').addClass('is-invalid');
							$('small#'+key+'').addClass('invalid-feedback').html(value[0]);
                        });
                        showAlert('danger', 'Periksa Input', 3000, 'col-lg-6 offset-lg-3');
                    } 
                    if (response.type == 'alert') {
                        showAlert(
                            response.hasOwnProperty('alert') ? response.alert : 'danger', 
                            response.message, 
                            response.hasOwnProperty('timeout') ? response.timeout : 5000, 
                            response.hasOwnProperty('size') ? response.size : 'col-lg-6 offset-lg-3'
                        );
                    }
                } else {
                    $('button:submit').attr('disabled', 'true');
                    $('button:submit').text('');
                    $('button:submit').append('Mohon tunggu...');
                    showAlert(
                        'success', 
                        response.message, 
                        1000, 
                        'col-lg-6 offset-lg-3', 
                        '<?= url('auth/login') ?>'
                    );
                }
                $("input, select").each(function() {
                    if (this.classList.contains('is-invalid') == false) {
                        this.classList.add('is-valid');
                    }
                });
            },
            error: function() {
				$('#preloader').fadeOut("slow");
                $('button:submit').removeAttr('disabled');
                $('button:submit').text('Atur Ulang Kata Sandi');
                showAlert(
                    'danger', 
                    'Terjadi kesalahan.', 
                    5000, 
                    'col-lg-6 offset-lg-3'
                );
            },
        });
    });
</script>
@endsection

