$(document).on("click", "#add_sub_admin_form_btn", function (e) {
    $.validator.addMethod("lettersOnly", function (value, element) {
        return this.optional(element) || /^[a-zA-Z\s]+$/.test(value);
    }, "Please enter alphabetic characters only");
    $('#add_sub_admin_form').validate({
        ignore: [],
        debug: false,

        rules: {
            sub_admin_name: {
                required: true,
                lettersOnly: true

            },
            sub_admin_lname: {
                required: true,
                lettersOnly: true

            },
            sub_admin_email: {
                required: true,
                email: true

            },
            password: {
                required: true
            }
            ,"module_id[]":{
                required: true
            }
        },
        messages: {
            sub_admin_name: {
                required: 'Please enter this field',

            },
            sub_admin_lname: {
                required: 'Please enter this field',

            },
            sub_admin_email: {
                required: 'Please enter this field',
                email: 'Please enter a valid email address'
            },
            password: {
                required: 'Please enter this field'
            },
            "module_id[]":{
                required: 'Please enter this field'
            }
        },
        errorClass: 'error-message',
        submitHandler: function (form) {
            var formData = new FormData(form);
            let base_url = url_path;
            console.log(formData);
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                url: base_url + '/insert_sub_admin',
                type: 'POST',
                data: formData,
                beforeSend: function () {
                    $('#add_sub_admin_form_btn').html('Please wait...');
                    $('#add_sub_admin_form_btn').attr('disabled', true);
                },
                processData: false,
                contentType: false,
                success: function (result) {
                    if (result.status_code == 200) {
                        toastr.success('Data Added Sucessfully');
                        setTimeout(function () {
                            window.location.href = base_url + "/manage-sub-admin";
                        }, 2000);
                    } else {
                        toastr.error('Something Went Wrong');
                        setTimeout(function () {
                            window.location.href = base_url + "/manage-sub-admin";
                        }, 2000);
                    }
                    $('#add_sub_admin_form_btn').attr('disabled', false);
                    $('#add_sub_admin_form_btn').text('Submit');
                },

            });

            // $('#add_sub_admin_form_btn').attr('disabled', false);
            // $('#add_sub_admin_form_btn').text('Submit');
        }
    });
});
