<% locals = { owner: @owner,
              show_user_selection: @activate_users,
              show_project_selection: @activate_projects,
              projects: @projects,
              users: @users,
              user_limit: @user_limit,
              project_limit: @project_limit,
              show_archived: @show_archived,
              form_path: @form_path} %>

$modal = $('#ajax-modal');
$modal.html("<%= j(render partial: 'easy_trial_expiration/entity_selection_form', locals: locals, formats: [:html]) %>");

var el = $modal;
//  if (el.length === 0 || el.is(':visible')) {return;}
var mh = ERUI.topMenu.outerHeight();
var wh = window.innerHeight;
maxHeight = wh - mh - 40;

el.dialog({
  width: '650px',
  modal: true,
  resizable: false,
  dialogClass: 'modal easy-trial-expiration',
  maxHeight: maxHeight,
  maxWidth: '100%',
//    title: title,
  closeOnEscape: false,
  open: function (event, ui) {
    ERUI.body.addClass('modal-opened');
    initEasyAutocomplete();
    EasyToggler.ensureToggle();

    var buttons = [];
    <% if @show_activate_all_users_button %>
      buttons.push({text: '<%= l('easy_trial_expiration.label_activate_all') %>', 'id': 'modal_button_activate_all_users', 'class': 'button-positive', click: function(){
        $('#checkbox_all_users_selected').prop('checked', true);
        <% if @activate_projects %>
          EASY.trialExpiration.showProjectSelection();
        <% else %>
          $('#ajax-modal').find('form').submit();
        <% end %>
      }});
    <% end %>
    <% if @show_activate_all_projects_button %>
      buttons.push({text: '<%= l('easy_trial_expiration.label_activate_all') %>', 'id': 'modal_button_activate_all_projects', 'class': 'button-positive', click: function(){$('#checkbox_all_projects_selected').prop('checked', true); $('#ajax-modal').find('form').submit();}});
    <% end %>

    buttons.push({text: '<%=j l(:button_save) %>', 'id': 'modal_button_save', 'class': 'button-positive', click: function(){$('#ajax-modal').find('form').submit();}});
    <% if @activate_users && @activate_projects %>
      buttons.push({text: '<%= l(:button_xml_data_continue) %>', 'id': 'modal_button_next', 'class': 'button-positive', click: function() {EASY.trialExpiration.showProjectSelection()}});
    <% end %>
    buttons.push({
      text: '<%=j l('easy_trial_expiration.label_button_later') %>', click: function () {
        $(this).dialog('close');
        EASY.trialExpiration.postponeActivation();
        showFlashMessage('notice', '<%=j l('easy_trial_expiration.text_activation_postponed') %>').delay(2500).fadeOut();
      }, 'class': 'button'
    });
    $modal.siblings('.ui-dialog-titlebar').html("<%= j(render partial: 'easy_trial_expiration/activate_entities/titlebar', locals: {show_title_for_user_selection: @activate_users, show_title_for_project_selection: @activate_projects}, formats: [:html]) %>");
    $modal.dialog("option", {
      buttons: buttons
    });

    <% if @activate_users && @activate_projects %>
      $('#modal_button_save').hide();
    <% end %>
    <% if @show_activate_all_users_button && @show_activate_all_projects_button %>
      $('#modal_button_activate_all_projects').hide();
    <% end %>

  },
  close: function (event, ui) {
    return false;
    ERUI.body.removeClass('modal-opened');
    $(this).dialog("destroy");
  },
  position: {
    my: "center top-" + maxHeight / 2
  }
});

ERUI.document.trigger("erui_interface_change_modal");
showModal('ajax-modal', 650);
