jQuery(function () {
    var mreeir_modal_container = document.getElementById('mreeir_ucr-container');
    var mreeir_modal_closer = document.getElementsByClassName("mreeir_close_container")[0];
    var mreeir_modal_canceler = document.getElementsByClassName("mreeir_cancel_create_user")[0];

    jQuery("#mreeir_create_customer").on('click', function (e) {
        e.preventDefault();
        mreeir_modal_container.style.display = "block";
        return false;
    });

    mreeir_modal_canceler.onclick = function () {
        mreeir_modal_container.style.display = "none";
    };
    mreeir_modal_closer.onclick = function () {
        mreeir_modal_container.style.display = "none";
    };
    window.onclick = function (event) {
        if (event.target == mreeir_modal_container) {
            mreeir_modal_container.style.display = "none";
        }
    };
    jQuery('#mreeir_user_firstname,#mreeir_user_lastname,#mreeir_user_mobile,#mreeir_user_email').keypress(function (e) {
        var key = e.which;
        if(key == 13)  // the enter key code
        {
            jQuery('.mreeir_createcustomer_btn').click();
            return false;  
        }
    });
    jQuery(".mreeir_createcustomer_btn").on('click', function (e) {
        
        e.preventDefault();
        var firstname    = jQuery("#mreeir_user_firstname").val();
        var lastname     = jQuery("#mreeir_user_lastname").val();
        var mobilenumber = jQuery("#mreeir_user_mobile").val();
        var email        = jQuery("#mreeir_user_email").val();
        
        mobilenumber = mreeir_correct_mobile_number(mobilenumber);

        if ( firstname == "" || lastname == "" ||  mobilenumber == "" ) {
            alert(mreeir_create_user_options.enterallfields);
            return;
        }
        if ( !mreeir_is_mobile(mobilenumber) ) {
            alert(mreeir_create_user_options.invalidmobile);
            return;
        }
        if ( !mreeir_is_email(email) && email != "" ) {
            alert(mreeir_create_user_options.invalidemail);
            return;
        }

        jQuery.ajax({
            type: 'post',
            url: mreeir_create_user_options.ajax_url,
            data: {
                action: 'mreeir_create_user_order',
                firstname: firstname,
                lastname: lastname,
                mobilenumber: mobilenumber,
                email: email,
                csrf: mreeir_create_user_options.csrf
            },
            beforeSend: function() {
                jQuery('.mreeir-element-wrapper.mreeir-loading').removeClass('mreeir-hide');
            },
            success: function (res) {
                
                if (res == "error") {
                    alert(mreeir_create_user_options.error);
                } else if (res == "mobile-not-valid") {
                    alert(mreeir_create_user_options.invalidmobile);  
                } if (res == "email-not-valid") {
                    alert(mreeir_create_user_options.invalidemail);  
                } else if (res == "user-exists") {
                    alert(mreeir_create_user_options.EmailMobileNumberAlreadyRegistered);
                } else {
                    var userdata = jQuery.parseJSON(res);
                    if ( jQuery('#customer_user').length > 0 ) {
                        jQuery('#customer_user')
                        .select2({
                            data: [{id: userdata.ID, text: firstname + ' ' + lastname + ' ' + mobilenumber}]
                        }).val(userdata.ID).trigger("change");

                        setTimeout(
                            function() 
                            {
                                jQuery('.edit_address .form-field._billing_first_name_field input#_billing_first_name').val(firstname);
                                jQuery('.edit_address .form-field._billing_last_name_field input#_billing_last_name').val(lastname);
                                jQuery('.edit_address .form-field._billing_email_field input#_billing_email').val(email);
                                jQuery('.edit_address .form-field._billing_phone_field input#_billing_phone').val(mobilenumber);
                            },
                            3000
                        );
                        jQuery("#mreeir_user_firstname").val('');
                        jQuery("#mreeir_user_lastname").val('');
                        jQuery("#mreeir_user_mobile").val('');
                        jQuery("#mreeir_user_email").val('');
                    } else {
                        alert(mreeir_create_user_options.userregisteredsuccessfully);
                        jQuery("#mreeir_user_firstname").val('');
                        jQuery("#mreeir_user_lastname").val('');
                        jQuery("#mreeir_user_mobile").val('');
                        jQuery("#mreeir_user_email").val('');
                    }
                    
                    mreeir_modal_container.style.display = "none";
                }

            },
            complete: function() {
                jQuery('.mreeir-element-wrapper.mreeir-loading').addClass('mreeir-hide');
            }
        });


    });


    function mreeir_is_email (email) {
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
    }

    function mreeir_is_mobile(mobileNumber) {
        var regex = /^09\d{9}$/;
        return regex.test(mobileNumber);
    }

    function mreeir_correct_mobile_number( $mobile ) {
        var inputs = "" + $mobile;
        new_val = inputs.replace(/۰/g, '0').replace(/۱/g, '1').replace(/۲/g, '2').replace(/۳/g, '3').replace(/۴/g, '4').replace(/۵/g, '5').replace(/۶/g, '6').replace(/۷/g, '7').replace(/۸/g, '8').replace(/۹/g, '9').replace(/٤/g, '4').replace(/٥/g, '5').replace(/٦/g, '6');
        
        new_val = new_val.replace(/٠/g, '0').replace(/١/g, '1').replace(/٢/g, '2').replace(/٣/g, '3').replace(/٤/g, '4').replace(/۵/g, '5').replace(/٦/g, '6').replace(/٧/g, '7').replace(/٨/g, '8').replace(/٩/g, '9').replace(/٤/g, '4').replace(/٥/g, '5').replace(/٦/g, '6');
        return new_val;   
    }
});