{% put scripts %}

<script>
    $('#loader_form').hide();
    function login() {

        var phone = document.getElementsByName("phone")[0].value;
        var pass = document.getElementsByName("password")[0].value;
        var deviceName = 'from Web';

        var fd = new FormData();
        fd.append('phone', phone);
        fd.append('password', pass);
        fd.append('device_name', deviceName);

        $.ajax({
            url: 'https://nurgul.com.tm/app/api/customer/login',
            data: fd,
            processData: false,
            contentType: false,
            type: 'POST',
            beforeSend: function () {
                $('#loader_form').show();
            },
            success: function (data) {
                $('#loader_form').hide();
                console.log(data);
                console.log(data.data.name);
                console.log(data.token);

                oc.flashMsg({
                    message: "{{'register.success'|_}}",
                    type: 'success',
                    interval: 3
                });

                $('input[name=token]').val(data.token);
                $('input[name=name]').val(data.data.name);
                $('input[name=phone]').val(data.data.phone);

                var link = document.querySelector('#testq');
                if(link) {
                    link.click();
                }


            },
            error: function (data) {
                $('#loader_form').hide();
                dataq = JSON.parse(data.responseText);
                // console.log(dataq);
                for (var error of dataq.errors) {
                    $('#errors').append(`<a href="#" style="padding-bottom: 15px;"><small style="color: darkred;font-weight: bold;background: #ff8888;padding: 10px;border-radius: 5px;"> * ` + error + `</small></a>`);

                }
                // console.log(dataq.errors);
            }
        });
    }

</script>

{% endput %}
