$( document ).ready(function() {
    $(document).on('change', 'select[name="user_type"]', function(event) {
        var userTypeId = $(this).val().trim();
        // Reset disabled fields
        $('input[name="email_id"]').attr('disabled', false).addClass('required');
        $('input[name="phone_no"]').attr('disabled', false).addClass('required');
        $('select[name="role_id"]').select2("enable", true);
        $('input[name="profile_pic"]').attr('disabled', false)

        switch (userTypeId) {
            case "2": // Super Admin
                $('input[name="email_id"]').attr('disabled', true).removeClass('required'); // disable email field
                $('input[name="phone_no"]').attr('disabled', true).removeClass('required'); // disable phone number field
                $('select[name="role_id"]').select2("enable", false); // disable role id field
                break;
            case "3": // Company
                $('input[name="profile_pic"]').attr('disabled', true).removeClass('required'); // disable image upload
                $('select[name="role_id"]').val('1').select2().trigger('change').select2("enable", false); // select company role and disable role id field
                break;
            case "4": // App User
                $('input[name="email_id"]').attr('disabled', true).removeClass('required'); // disable email field
                $('input[name="phone_no"]').attr('disabled', true).removeClass('required'); // disable phone number field
                $('select[name="role_id"]').val('2').select2().trigger('change').select2("enable", false);
                break;
            default:
                break;
        }
    })
});