- content_for :javascript do
  :javascript
    $(function() {
      $('#message_recipient_tokens').tokenInput("#{users_path(:format => :json)}", {
        crossDomain: false,
        prePopulate: $('#message_recipient_tokens').data('pre'),
        hintText: '#{t '.search_user'}',
        noResultText: '#{t '.no_user_found'}',
        searchingText: '#{t '.search'}',
        theme: 'facebook'
      });

      function slideRecipients() {
        if ($('#message_send_to_all').is(':checked')) {
          $('#recipients').slideDown();
        } else {
          $('#recipients').slideUp();
        }
      }

      function updateSlideState() {
        switch($('input[name="message[send_method]"]:checked').val()) {
        case 'all':
          $('#workgroup').slideUp();
          $('#ordergroup').slideUp();
          $('#messagegroup').slideUp();
          $('#order').slideUp();
          $('#recipients').slideUp();
          break;

        case 'workgroup':
          $('#workgroup').slideDown();
          $('#ordergroup').slideUp();
          $('#messagegroup').slideUp();
          $('#order').slideUp();
          $('#recipients').slideDown();
          break;

        case 'ordergroup':
          $('#workgroup').slideUp();
          $('#ordergroup').slideDown();
          $('#messagegroup').slideUp();
          $('#order').slideUp();
          $('#recipients').slideDown();
          break;

        case 'messagegroup':
          $('#workgroup').slideUp();
          $('#ordergroup').slideUp();
          $('#messagegroup').slideDown();
          $('#order').slideUp();
          $('#recipients').slideDown();
          break;

        case 'order':
          $('#workgroup').slideUp();
          $('#ordergroup').slideUp();
          $('#messagegroup').slideUp();
          $('#order').slideDown();
          $('#recipients').slideDown();
          break;

        case 'recipients':
          $('#workgroup').slideUp();
          $('#ordergroup').slideUp();
          $('#messagegroup').slideUp();
          $('#order').slideUp();
          $('#recipients').slideDown();
          break;
        }
      }

      $('input[name="message[send_method]"]').on('click', updateSlideState);
      updateSlideState();
    });

- title t('.title')

= simple_form_for @message do |f|
  = f.hidden_field :reply_to

  - if @message.reply_to
    %p= t('.reply_to', link: link_to(t('.message'), message_path(@message.reply_to))).html_safe

  - unless FoodsoftConfig[:mailing_list].blank?
    %b= t('.list.desc', list: mail_to(FoodsoftConfig[:mailing_list])).html_safe
    %br/
    %small{:style => "color:grey"}
      = t '.list.subscribe_msg'
      %br/
      - if FoodsoftConfig[:mailing_list_subscribe].blank?
        = t('.list.subscribe', link: link_to(t('.list.wiki'), wiki_page_path('MailingListe'))).html_safe
      - else
        = t('.list.mail', email: mail_to(FoodsoftConfig[:mailing_list_subscribe])).html_safe

  - send_method_collection = [:workgroup, :ordergroup, :messagegroup, :order, :recipients]
  - send_method_collection.unshift :all if FoodsoftConfig[:mailing_list].blank?
  = f.input :send_method, :as => :radio_buttons, :collection => send_method_collection, :label => false, :label_method => ->(obj){ t("activerecord.attributes.message.send_method.#{obj}") }

  #workgroup
    = f.input :workgroup_id, :as => :select, include_blank: false, :collection => Workgroup.undeleted.order(:name).includes(:memberships).reject { |g| g.memberships.empty? }
  #ordergroup
    = f.input :ordergroup_id, :as => :select, include_blank: false, :collection => Ordergroup.undeleted.order(:name).includes(:memberships).reject { |g| g.memberships.empty? }
  #messagegroup
    = f.input :messagegroup_id, :as => :select, include_blank: false, :collection => Messagegroup.undeleted.order(:name)
  #order
    = f.input :order_id, :as => :select, include_blank: false, :collection => Order.finished_not_closed.order('pickup DESC').includes(:supplier).limit(25),
        :label_method => ->(obj){ t('.order_item', supplier_name: obj.name, pickup: format_date(obj.pickup)) }, :input_html => {class: 'input-xxlarge'}
  #recipients
    = f.input :recipient_tokens, :input_html => { 'data-pre' => User.where(id: @message.recipients_ids).map(&:token_attributes).to_json }
  = f.input :private, inline_label: t('.hint_private')
  = f.input :subject, input_html: {class: 'input-xxlarge'}
  = f.rich_text_area :body, input_html: {class: 'input-xxlarge', rows: 13}
  .form-actions
    = f.submit class: 'btn btn-primary'
    = link_to t('ui.or_cancel'), :back
