<%
  modal ||= false
  time_entry_selector = '#main'
  comment_field_class = 'time_entry_comment'
%>
<%= form_for @time_entry, :url => {:action => 'save', :time_entry_id => params[:time_entry_id]}, :html => {:class => 'tabular form-disabled-on-ajax form-box', :id => 'bulk-time-entry-form', :remote => modal} do |f| %>
  <%= hidden_field_tag(:page_module_uuid, params[:page_module_uuid]) if params[:page_module_uuid] -%>
  <%= hidden_field_tag(:modal, true) if modal -%>
  <%= hidden_field_tag(:next, params[:next]) if params[:next] -%>
  <%= back_url_hidden_field_tag %>
  <%= error_messages_for 'time_entry' %>

  <div <%='class="splitcontent"'.html_safe unless in_mobile_view? || modal %>>
    <% unless in_mobile_view? || modal %>
        <div class="bulk-time-entry-topprojects splitcontentright size-4" id="top_last_entries">
          <%= render :partial => 'top_last_entries', :locals => {:top_last_entries => @top_last_entries} -%>
        </div>
        <div id="easy_worker_gauge_meter_container" class="splitcontentright size-2">
          <%= render :partial => 'common/charts/gauge_meter', :locals => {:modul_uniq_id => '', :value => @gauge_meter_spent_time, :max_value => @gauge_meter_max_value, :chart_label => @gauge_meter_label, :chart_height => 120} %>
        </div>
    <% end -%>

    <div <%='class="splitcontentleft size-4"'.html_safe unless in_mobile_view? || modal %>>
      <% unless modal %>
        <div class="combo-container no-label-required-by-css" id="container_users">
          <%= render :partial => 'container_users' %>
        </div>

        <div class="timeentry-projects combo-container no-label-required-by-css" id="container_projects">
          <%= render :partial => 'container_projects' %>
        </div>

        <% if [nil, 'Project', 'Issue'].include?(@time_entry.entity_type) %>
          <div class="timeentry-issues combo-container no-label-required-by-css" id="container_issues">
            <%= render :partial => 'container_issues' %>
          </div>
        <% end %>
      <% else %>
        <%= hidden_field_tag(:user_id, @time_entry.user_id) %>
        <%= hidden_field_tag(:project_id, @time_entry.project_id) %>
        <%= hidden_field_tag(:issue_id, @time_entry.issue_id) %>
      <% end %>
    </div>
  </div>
    <div id="container_time_entry">
      <%= render :partial => 'container_time_entry', :locals => {:modal => modal} %>
    </div>
    <%= call_hook(:view_timelog_edit_form_bottom, { :time_entry => @time_entry, :form => f }) %>
  <% unless modal %>
    <div class="form-actions">
      <%= submit_tag l(:button_create_and_continue), :class => 'button-positive', :name => 'continue', :title => l(:button_create_and_continue) -%>
      <%= submit_tag(l(:button_create_and_back), :class => 'button-positive', :name => 'back', :title => l(:button_create_and_back)) if back_url.present? -%>
      <% if params[:next].to_s == 'logout' %>
        <%= link_to l(:label_logout), force_user_logout_path, :class => 'button', :title => l(:label_logout) %>
      <% elsif back_url.present? %>
        <%= link_to l(:button_back), back_url, :class => 'button', :title => l(:button_back) -%>
      <% end %>
    </div>
  <% end %>

  <% unless modal %>
    <script type="text/javascript">
      $(function() {
        $(document).on('change', '<%= time_entry_selector %> #project_id', function(event){
          fillFormTextAreaFromCKEditor('<%= comment_field_class %>');
          if (event.target.value !== '') {
            $.ajax({url: '<%= j bulk_time_entries_path(:project_changed => '1', :format => 'js').html_safe %>', data: $('#bulk-time-entry-form').serialize()});
          }
        });
        $(document).on('change', '<%= time_entry_selector %> #issue_id', function(){
          fillFormTextAreaFromCKEditor('<%= comment_field_class %>');
          $.ajax({url: '<%= j bulk_time_entries_path(:issue_changed => '1', :format => 'js', :project_id => @project).html_safe %>', data: $('#bulk-time-entry-form').serialize()});
        });
        $(document).on('change', '<%= time_entry_selector %> .timeentry-activity', function(){
          fillFormTextAreaFromCKEditor('<%= comment_field_class %>');
          $.ajax({url: '<%= j bulk_time_entries_path(:activity_changed => '1', :format => 'js', :project_id => @project).html_safe %>', data: $('#bulk-time-entry-form').serialize()});
        });
      });
    </script>
  <% end %>
<% end %>

