<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
    show_google_docs = @assignment.allow_google_docs_submission? &&
                       @real_current_user.blank? &&
                       @domain_root_account &&
                       feature_and_service_enabled?(:google_drive)
-%>
<%
  js_bundle :submit_assignment
  css_bundle :tinymce
  js_env({
    :SUBMIT_ASSIGNMENT => {
      :ALLOWED_EXTENSIONS => (@assignment.allowed_extensions || []),
      :ID => @assignment.id,
      :GROUP_ID_FOR_USER => @assignment.group_category.try(:group_for, @current_user).try(:id)
    }
  })
%>
<div style="display: none;" id="submit_assignment" data-context_code="<%= @assignment.context_code %>" data-asset_string="<%= @assignment.asset_string %>">
  <div class="content" id="submit_assignment_tabs">
    <ul>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_upload/) %>
        <li><a href="#submit_online_upload_form" class="submit_online_upload_option"><%= t 'links.submit.file_upload', "File Upload" %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_text_entry/) %>
        <li><a href="#submit_online_text_entry_form" class="submit_online_text_entry_option"><%= t 'links.submit.text_entry', "Text Entry" %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_url/) %>
        <li><a href="#submit_online_url_form" class="submit_online_url_option"><%= t 'links.submit.website', "Website URL" %></a></li>
      <% end %>
      <% if show_google_docs %>
        <li><a href="#submit_google_doc_form" class="submit_online_url_option"><%= t 'links.submit.google_doc', 'Google Doc' %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/media_recording/) %>
        <li><a href="#submit_media_recording_form" class="submit_media_recording_option"><%= t 'links.submit.media', "Media" %></a></li>
      <% end %>
      <% (@external_tools[0,3] || []).each do |external_tool| %>
        <li><a href="#submit_from_external_tool_form_<%= external_tool.id %>"
          class="external-tool"
          data-id="<%= external_tool.id %>"
          data-name="<%= external_tool.name %>"><%= external_tool.label_for(:homework_submission, I18n.locale) %></a></li>
      <% end %>
      <% if @external_tools.length > 3 %>
        <li><a href="#submit_from_external_tool_form" class="submit_from_external_tool_option"><%= t 'links.submit.external_tool', "More" %></a></li>
      <% end %>
    </ul>
    <% if @assignment.submission_types && @assignment.submission_types.match(/online_upload/) %>
      <%= render :partial => 'assignments/submit_online_upload', locals: { eula_url: eula_url } %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/online_text_entry/) %>
      <%= render :partial => 'assignments/submit_online_text_entry', locals: { eula_url: eula_url } %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/online_url/) %>
    <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_online_url_form", :class => "submit_assignment_form"}) do %>
    <%= hidden_field :submission, :submission_type, :value => "online_url" %>
    <table class="formtable" style="width: 100%;">
      <tr>
        <td colspan="2">
          <%= t 'instructions.online_url', "Copy and paste the link to the web site you'd like to submit for this assignment." %>
            <%= render :partial => "assignments/group_submission_reminder" if @assignment.has_group_category? %>
          </td>
      </tr><tr>
        <td colspan="2">
          <%= blabel :submission, :url, :en => "Website URL" %>
          &nbsp;&nbsp;
          <%= text_field :submission, :url, :style => "min-width: 250px;" %>
        </td>
      </tr><tr>
        <td colspan="2" style="text-align: center;">
          <div style="text-align: <%= direction('left') %>;">
            <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
          </div>
        </td>
      </tr>
      <%= render :partial => "group_comment" %>
      <% if @assignment.turnitin_enabled? %>
        <%= render :partial => "turnitin" %>
      <% end %>
      <% if @assignment.vericite_enabled? %>
        <%= render :partial => "vericite" %>
      <% end %>
      <tr>
        <td colspan="2" class='button-container'>
          <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
          <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
        </td>
      </tr>
    </table>
    <% end %>
    <% end %>

    <style>
    #google_docs_tree li.folder {
      cursor: pointer;
    }
    #google_docs_tree li.file {
      cursor: pointer;
      -moz-border-radius: 3px;
      width: 80%;
      padding-<%= direction('right') %>: 20px;
    }
    #google_docs_tree li.file:hover {
      background-color: #eee;
    }
    ul.instTree li span.active {
      background: none;
    }
    #google_docs_tree li.file.leaf.active {
      background-color: #ddd;
    }
    #google_docs_tree li.file .filename {
      float: <%= direction('left') %>;
      max-width: 98%;
    }
    #google_docs_tree li.file .popout {
      float: <%= direction('right') %>;
    }
    </style>
    <% if show_google_docs %>
      <% if @user_has_google_drive %>
        <% if @domain_root_account.feature_enabled?(:google_docs_domain_restriction) &&
              @domain_root_account.settings[:google_docs_domain] &&
             !@current_user.gmail.match(%r{@#{@domain_root_account.settings[:google_docs_domain]}$}) %>
          <div id="submit_google_doc_form">
            <p class="alert alert-error">
              <i class="icon-warning" aria-hidden="true"></i>
              <%= t(:invalid_google_docs_domain, 'Invalid domain') %>
            </p>

           <p>
            <%= t(:gmail_restriction_description, <<-END, domain: @domain_root_account.settings[:google_docs_domain])
              Your account has restricted Google Doc submissions to Google accounts
              on the %{domain} domain. To submit this assignment with a Google Doc,
              you will need to reconfigure the Google Docs integration on your
              user settings page.
            END
            %>
           </p>
            </div>
         <% else %>
          <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_google_doc_form", :class => "submit_assignment_form"}) do %>
            <%= hidden_field :submission, :submission_type, :value => "google_doc" %>
            <%= hidden_field :google_doc, :document_id, :value => "", :class => "google_doc_id" %>
            <table class="formtable" style="width: 100%;">
              <tr>
                <td style="padding-bottom: 10px;" colspan="2">
                  <%= t 'instructions.google_docs', "Select the file from the list below." %>
                  <%= render :partial => "assignments/group_submission_reminder" if @assignment.has_group_category? %>
                  </td>
              </tr><tr>
                <td colspan="2">
                  <div id="google_docs_container" style="height: 200px; overflow: auto;">
                    <div style="text-align: center; margin: 10px;">
                      <%= image_tag "ajax-loader-bar.gif", :alt => '' %>
                    </div>
                  </div>
                </td>
              </tr><tr>
                <td colspan="2" style="text-align: center;">
                  <div style="text-align: <%= direction('left') %>;">
                    <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
                  </div>
                </td>
              </tr>
              <%= render :partial => "group_comment" %>
              <% if @assignment.turnitin_enabled? %>
                <%= render :partial => "turnitin" %>
              <% end %>
              <% if @assignment.vericite_enabled? %>
                <%= render :partial => "vericite" %>
              <% end %>
              <tr>
                <td colspan="2" class='button-container'>
                  <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
                  <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
                </td>
              </tr>
            </table>
            <div id="uploading_google_doc_message" style="display: none;">
              <%= t 'messages.uploading', "Retrieving a copy of your Google Doc to submit for this assignment.  This may take a little while, depending on the size of the file..." %>
              <div style="text-align: center; margin: 10px;">
                <%= image_tag "ajax-loader-bar.gif", :alt => '' %>
              </div>
            </div>
          <% end %>
        <% end %>
      <% else %>
        <div id="submit_google_doc_form">
          <%= t 'messages.google_drives_auth_required', "Before you can submit assignments directly from Google Drive you need to authorize Canvas to access your Google Drive account:" %>
          <div style="font-size: 1.1em; text-align: center; margin: 10px;">
            <a class="btn" href="<%= oauth_url(:service => :google_drive, :return_to => (request.url + "#submit_google_doc_form")) %>"><%= t 'links.authorize_google_drive', "Authorize Google Drive Access" %></a>
          </div>
        </div>
      <% end %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/media_recording/) %>
      <% if !feature_enabled?(:kaltura) %>
        <div id="submit_media_recording_form">
          <h3><%= t 'titles.media_recordings_disabled', "Media recordings are not enabled for this Canvas site" %></h3>
          <p><%= t 'messages.media_recordings_disabled', "Media recordings are not enabled for this Canvas site, and you will not be able to submit these types of assignments.  Please contact your instructor about changing the settings for this assignment, or your system administrator about enabling media recordings." %></p>
        </div>
      <% else %>
        <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_media_recording_form", :class => "submit_assignment_form", :multipart => true }) do %>
        <%= hidden_field :submission, :submission_type, :value => "media_recording" %>
        <%= hidden_field :submission, :media_comment_id, :value => "", :class => "media_comment_id" %>
        <%= hidden_field :submission, :media_comment_type, :value => "video" %>
        <table class="formtable" style="width: 100%;">
          <tr>
            <td colspan="2" style="padding-bottom: 10px;">
              <%= t 'instructions.media_recording', "Use the link below to record your submission.  Click \"Save\" when you're finished and ready to submit." %>
                <%= render :partial => "assignments/group_submission_reminder" if @assignment.has_group_category? %>
            </td>
          </tr><tr>
            <td>
              <div id="media_media_recording_submission_holder" style="font-size: 1.2em; font-weight: bold;">
                <div id="media_media_recording_ready" style="display: none; font-size: 1.5em;">
                  <%= t 'messages.media_recording_ready', "Ready to Submit!  Click 'Submit Assignment' button to finish."%>
                  <div id="media_media_recording_thumbnail">
                    <%= image_tag "media_comment.png", :alt => '' %>
                  </div>
                </div>
                <a href="#" class="record_media_comment_link btn" style="font-size: 1.1em;"><%= image_tag "record.gif", :alt => '' %> <%= t 'buttons.record_media', "Record / Upload Media" %></a>
                <div id="media_media_recording_submission" class="pull-left" style="display: none; width: auto; border: 2px solid #444;">
                </div>
                <div class="clear"></div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2" style="text-align: center;">
              <div style="text-align: <%= direction('left') %>;">
                <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
              </div>
            </td>
          </tr>
          <%= render :partial => "group_comment" %>
          <tr>
            <td colspan="2" class='button-container'>
              <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
              <button type="submit" class="btn btn-primary" disabled="disabled" id="media_comment_submit_button"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
            </td>
          </tr><tr style="display: none;">
            <td colspan="2" style="text-align: center;">
              <%= t 'messages.submitting', "Submitting..." %> <img src=""/>
            </td>
          </tr>
        </table>
       <% end %>
      <% end %>
    <% end %>

    <% (@external_tools || []).each do |external_tool| %>
      <div id="submit_from_external_tool_form_<%= external_tool.id %>" style="padding: 5px 5px 0 5px;"></div>
    <% end %>

    <% if @external_tools.length > 3
      user_group = @assignment.group_category.try(:group_for, @current_user)
      file_upload_path = user_group ? api_v1_group_files_path(user_group) : "/api/v1/courses/#{@context.id}/assignments/#{@assignment.id}/submissions/#{@current_user.id}/files"
    %>
      <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_from_external_tool_form", :class => "submit_assignment_form" }) do %>
        <%= hidden_field :submission, :submission_type, :value => "online_url", :id => 'external_tool_submission_type' %>
        <%= hidden_field :submission, :filename, :id => 'external_tool_filename' %>
        <%= hidden_field :submission, :attachment_ids, :id => 'external_tool_file_id' %>
        <%= hidden_field :submission, :url, :id => 'external_tool_url' %>
        <%= hidden_field :submission, :content_type, :id => 'external_tool_content_type' %>
        <a href="<%= context_url(@context, :homework_submissions_context_external_tools_url) %>" id="external_tools_url"></a>
        <a href="<%= file_upload_path %>" id="homework_file_url"></a>
        <table role="presentation" class="formtable" style="width: 100%;">
          <tr>
            <td colspan="2">
              <%= t 'instructions.from_external_tool', "Select a tool from the list below to load and submit a resource provided by that tool." %>
              <%= render :partial => "assignments/group_submission_reminder" if @assignment.has_group_category? %>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <ul class='tools'>
                <li><%= t 'loading_tools', "Loading tools..." %></li>
              </ul>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <%= t('submission_url', "Submission:") %>
              <span id="external_tool_submission_details" class='not_selected'>
                <%= t('no_submission', 'No Submission Selected') %>
              </span>
              <% if @assignment.allowed_extensions.present? -%>
                <div class='bad_ext_msg error_message' style="display: none">
                  <%= t 'errors.unacceptable_file_type', "This file type is not allowed. Accepted file types are: %{allowed_extensions_list}", :allowed_extensions_list => @assignment.allowed_extensions.to_sentence %>
                </div>
              <% end -%>
            </td>
          </tr><tr>
            <td colspan="2" style="text-align: center;">
              <div style="text-align: <%= direction('left') %>;">
                <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :style => "height: 16px;", :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
              </div>
            </td>
          </tr>
          <%= render :partial => "group_comment" %>
          <% if @assignment.turnitin_enabled? %>
            <%= render :partial => "turnitin" %>
          <% end %>
          <% if @assignment.vericite_enabled? %>
            <%= render :partial => "vericite" %>
          <% end %>
          <tr>
            <td colspan="2" class='button-container'>
              <button type="button" class="cancel_button btn"><%= t '#buttons.cancel', "Cancel" %></button>
              <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
            </td>
          </tr>
        </table>
      <% end %>
    <% end %>
  </div>
</div>
