(function ($) {
    "use strick";
    jQuery(document).ready(function ($) {
        $(".lifeline-forms-builder").on('submit', 'form', function (e) {
            e.preventDefault();
            var $form_id, $data, $button_text,$encrap, $this = $(this), validate;
            
            $encrap = new Encryption();
            $form_id = $this.context.dataset.form;
            $data = $(this).serialize();
            $data = $encrap.encrypt($data, '20f54bc473f7921351d9db94b4101f02');
            $button_text = $this.find("button").text();
            
            validate = $this.find("input[name='form_validate']").val();

            switch (validate) {
                case "login":
                    Ajax.formLogin($this, $data, $button_text);
                  break;
                  case "signup":
                    Ajax.formSignup($this, $data, $button_text);
                  break;
                case 1:
                    Ajax.formBuilder($this, $data, $button_text);
                  break;
                default:
                    confirm("Sorry! We didn't find the form type in the system.");
              }
            
        });
    });

    let Ajax = {
        formLogin: function($this, $data, $button_text){
            $.ajax({
                url: lifeline_data.ajaxurl,
                type: "POST",
                data: {
                    data: $data,
                    action: "lifeline_form_login",
                    nonce: lifeline_data.nonce
                },
                beforeSend: function () {
                    $this.find("button").append("<i class='fa-spin fa fa-cog'></i>");
                },
                success: function (response) {
                    $this.find("button").html($button_text);
                    if(response.success == true){
                        $this.find("button").text(response.data.message);
                        window.location = $this.find("input[name='redirect_url']").val();
                    }else{
                        alert(response.data.message);
                    }
                }
            });
        },
        formSignup: function($this, $data, $button_text){
            $.ajax({
                url: lifeline_data.ajaxurl,
                type: "POST",
                data: {
                    data: $data,
                    action: "lifeline_form_signup",
                    nonce: lifeline_data.nonce
                },
                beforeSend: function () {
                    $this.find("button").append("<i class='fa-spin fa fa-cog'></i>");
                },
                success: function (response) {
                    $this.find("button").html($button_text);
                    if(response.success == true){
                        $this.find("button").text(response.data.message);
                    }else{
                        alert(response.data.message);
                    }
                }
            });
        },
        formBuilder:function($this, $data, $button_text){
            $.ajax({
                url: lifeline_data.ajaxurl,
                type: "POST",
                data: {
                    data: $data,
                    action: "lifeline_form",
                    nonce: lifeline_data.nonce
                },
                beforeSend: function () {
                    $this.find("button").append("<i class='fa-spin fa fa-cog'></i>");
                },
                success: function (response) {
                    $this.find("button").html($button_text);
                }
            });
        }
    }

})(jQuery);
