<div class="box tabular">
  <p class="meeting-input"><%= f.text_field :name, :required => true %></p>
  <p>
    <label><%= l(:field_time) %></label>
    <span class="input-append inline">
      <%= text_field_tag 'easy_meeting[start_time][date]',
        User.current.user_time_in_zone(@easy_meeting.start_time).try(:to_date), :size => 13, :required => true %>
      <%= calendar_for('easy_meeting_start_time_date') %>
      <%= tag(:input, :type => 'time',
        :value => User.current.user_time_in_zone(@easy_meeting.start_time).try(:strftime, '%H:%M'),
        :name => 'easy_meeting[start_time][time]',
        :id => 'easy_meeting_start_time_time',
        :size => 3) %>
    </span>
    <span class="meeting-delimiter">-</span>
    <span class="input-append inline">
      <%= text_field_tag 'easy_meeting[end_time][date]',
        User.current.user_time_in_zone(@easy_meeting.end_time).try(:to_date), :size => 13 %>
      <%= calendar_for('easy_meeting_end_time_date') %>
      <%= tag(:input, :type => 'time',
        :value => User.current.user_time_in_zone(@easy_meeting.end_time).try(:strftime, '%H:%M'),
        :name => 'easy_meeting[end_time][time]',
        :id => 'easy_meeting_end_time_time',
        :size => 3) %>
    </span>
    <span class="all-day">
      <%= hidden_field_tag('easy_meeting[all_day]', '0', :id => nil) %>
      <% box = check_box_tag('easy_meeting[all_day]', '1', @easy_meeting.all_day) %>
      <%= label_tag('easy_meeting_all_day', box + l(:field_all_day), :class => 'checkbox inline') %>
    </span>
  </p>

  <p>
    <%= f.select :priority, easy_meeting_available_priorities %>
  </p>

  <p>
    <%= f.select :privacy, easy_meeting_available_privacy %>
    <em class="block"><%= l(:text_easy_meeting_privacy) %></em>
  </p>

  <p class="meeting-input">
    <%= f.text_field(:place_name) %>
  </p>
  <% if EasyRoom.any? %>
    <p class="meeting-room"><%= f.select :easy_room_id, EasyRoom.all.collect{|r| [r.name_with_capacity, r.id]}, :include_blank => true %> <%= link_to l(:button_check_availability), availability_easy_rooms_path, :target => '_blank', :class => 'button' %></p>
  <% elsif User.current.allowed_to?({:controller => :easy_rooms, :action => 'index'}, nil, :global => true) %>
    <p><label></label><%= link_to l(:label_manage_rooms), easy_rooms_path, :target => '_blank', :class => 'button' %> </p>
  <% end -%>
  <p>
    <label><%= l(:field_project) %></label><%= easy_select_tag(
      'easy_meeting[project_id]',
      {:name => @easy_meeting.project.try(:name), :id => @easy_meeting.project.try(:id)},
      nil,
      easy_autocomplete_path('visible_projects'),
      :include_blank => true, :root_element => 'projects', :html_options => {:id => 'easy_meeting_project_id'}
    ) -%>
  </p>
  <p class="meeting-description">
    <%= f.text_area :description %>
  </p>
  <div id="edit_meeting_repeat_options">
    <%= render(:partial => 'easy_repeating/simple_form', :locals => {:object => @easy_meeting}) %>
  </div>
  <p>
    <%= f.check_box :big_recurring %>
    <em class="block"><%= l(:text_easy_meeting_big_recurring) %></em>
  </p>
  <% if @easy_meeting.easy_is_repeating? || @easy_meeting.easy_repeat_parent.try(:easy_is_repeating?) %>
      <div id="delete_repeating_buttons">
        <p>
          <%= link_to l(:button_delete_easy_meeting), easy_meeting_path(@easy_meeting), :remote => true, :method => :delete, :data => {:confirm => l(:text_meeting_destroy_confirmation)}, :class => 'button easy-delete-meeting' %>
          <%= link_to l(:button_delete_all_repeating_easy_meetings), easy_meeting_path(@easy_meeting, :repeating => '1'), :remote => true, :method => :delete, :data => {:confirm => l(:text_meeting_destroy_confirmation)}, :class => 'button easy-delete-meeting' %>
        </p>
      </div>
  <% end %>

  <%= call_hook(:view_easy_meeting_form_attributes_bottom, easy_meeting: @easy_meeting, form: f) %>

  <fieldset>
    <legend><%= l(:label_invitations) %></legend>
    <p>
      <label>
        <%= l(:label_meeting_users_and_groups) %>
      </label><%= easy_select_tag('user_select', {}, nil, easy_autocomplete_path('users_in_meeting_calendar'),
        :include_blank => true, :root_element => 'users', :html_options => {:id => 'user_select'}, :onchange => 'userAdded();')
      %>
    </p>
    <p>
      <%= f.text_field(:mails) %>
    </p>
    <p>
      <%= hidden_field_tag('easy_meeting[include_author]', '0', :id => nil) %>
      <%= check_box_tag 'easy_meeting[include_author]', '1', @easy_meeting.include_author? %>
      <%= label_tag 'easy_meeting[include_author]', l(:field_include_author) %>
    </p>
    <p id="selected-users-entity-array">
    </p>
  </fieldset>
  <% if !request.xhr? %>
    <div class="form-actions">
      <%= f.submit l(:button_save), class: 'button-positive' %>
      <%= link_to l(:button_cancel), easy_calendar_path, class: 'button' %>
    </div>
  <% end %>
</div>
<script type="text/javascript">
  (function ($) {
    var allDay = $('#easy_meeting_all_day'),
        times = $('#easy_meeting_start_time_time, #easy_meeting_end_time_time'),
        usersEntityArray = $("#selected-users-entity-array"),
        p;

    var project_id = $("#header").attr("data-project-id");
    if (project_id) {
      var project_name = $("#header .self").text();
      $("#easy_meeting_project_id_autocomplete").val(project_name);
      $("#easy_meeting_project_id").val(project_id);
    }
    var room_id = $(".active-dialog").attr("data-room-id");
    if (room_id) {
      $("#easy_meeting_easy_room_id").val(room_id);
    }

    function toggleTimeOnAllDay() {
      if (allDay.is(':checked')) {
        times.hide();
      } else {
        times.show();
      }
    }
    toggleTimeOnAllDay();
    allDay.change(toggleTimeOnAllDay);

    usersEntityArray.entityArray({
      inputNames: 'easy_meeting[user_ids][]',
      entities: <%= @easy_meeting.easy_invitations.collect{|i| {:id => i.user.id, :name => i.user.name, :color => i.color_by_status}}.to_json.html_safe %>,
      afterRemove: function(user) {
        if (user.id == "<%= User.current.id %>") {
          $('#easy_meeting_include_author').prop('checked', false);
        }
      }
    });

    $('#easy_meeting_include_author').change(function(){
      if ($(this).is(':checked')) {
        usersEntityArray.entityArray('add', {id: "<%= User.current.id %>", name: "<%= t(:label_me) %>"});
      } else {
        usersEntityArray.entityArray('removeEntity', "<%= User.current.id %>");
      }
    });

    window.userAdded = function (event, ui) {
      var id = $('#user_select').val(),
          name = $('#user_select_autocomplete').val();

      if (!id.match(/\d+/)) {
        return false;
      }

      $('#user_select_autocomplete').val('');
      usersEntityArray.entityArray('add', {id: id, name: name});
      if (id == "<%= User.current.id %>") {
        $('#easy_meeting_include_author').prop('checked', true);
      }
    };

    function reloadIcons() {
      usersEntityArray.data('accepted', '<%= @easy_meeting.easy_invitations.where(:accepted => true).pluck(:user_id).to_json %>');
      usersEntityArray.data('declined', '<%= @easy_meeting.easy_invitations.where(:accepted => false).pluck(:user_id).to_json %>');
      var inputs = usersEntityArray.find('input');
      var accepted = JSON.parse(usersEntityArray.data('accepted'));
      var declined = JSON.parse(usersEntityArray.data('declined'));

      $.each(inputs, function( index, input ) {
        var value = parseInt($(input).attr('value'));
        if($.inArray(value, accepted)!==-1) { $(input).parent().addClass('icon icon-true'); }
        if($.inArray(value, declined)!==-1) { $(input).parent().addClass('icon icon-false'); }
      });
    };
    reloadIcons();

  }(jQuery));
</script>
