- @pagetitle = 'Notifications'

.card
  .card-header.d-flex.justify-content-between
    %h5 Notifications
  - unless @groups_users.empty?
    .card-body
      %h4.card-title Groups
      .card-text#notifications-groups
        %p Choose groups and channels for which you want to get notifications.
        .d-flex.flex-wrap
          - @groups_users.each do |group_user|
            .card.p-2.m-2
              - group = group_user.group
              = link_to(group.title, group_path(group), class: 'pe-4')
              .row.row-cols-auto
                .col.my-2
                  .form-check
                    = check_box_tag "groups[#{group_user.group.title}][email]", '1', group_user.email, class: 'form-check-input',
                      data: { url: update_my_subscriptions_path, method: :put, remote: true },
                      onchange: "$(this).data('params', 'groups[#{group_user.group.title}][email]=' + this.checked)"
                    = label_tag nil, 'email', class: 'form-check-label', for: "groups_#{group_user.group.title}_email"
                    %i.fas.fa-spinner.invisible
                .col.my-2
                  .form-check
                    = check_box_tag "groups[#{group_user.group.title}][web]", '1', group_user.web, class: 'form-check-input',
                      data: { url: update_my_subscriptions_path, method: :put, remote: true },
                      onchange: "$(this).data('params', 'groups[#{group_user.group.title}][web]=' + this.checked)"
                    = label_tag nil, 'web', class: 'form-check-label', for: "groups_#{group_user.group.title}_web"
                    %i.fas.fa-spinner.invisible
  .card-body
    %h4.card-title Events
    %p Choose events you want to get notified about and the corresponding channels.
    #subscriptions-form
      = render partial: 'webui/subscriptions/subscriptions_form', locals: { path: update_my_subscriptions_path,
                                                                            subscriptions_form: @subscriptions_form }

.card.mt-3
  .card-body
    %h4.card-title RSS Feed
    = form_tag(rss_secret_user_path(@user), id: 'generate_rss_token_form', method: :post) do
      %p
      - if @user.rss_secret.present?
        To access your RSS feeds, use the following url:
        = link_to(user_rss_notifications_url(secret: @user.rss_secret, format: 'rss'),
        user_rss_notifications_url(secret: @user.rss_secret, format: 'rss'), target: '_blank', rel: 'noopener')
      - else
        No feed url exists for your notifications.
      .card-body
        = submit_tag "#{@user.rss_secret ? 'Regenerate Url' : 'Generate Url'}", class: 'btn btn-primary'

- content_for :ready_function do
  :plain
    $('#notifications-groups, #subscriptions-form').on('ajax:before ajax:complete', '.form-check-input', function() {
      $(this).siblings('i.fa-spinner').toggleClass('fa-spin invisible');
    });
