<%! from django.utils.translation import ugettext as _ %>
<%! from django.template import RequestContext %>
<%! import third_party_auth %>
<%! from third_party_auth import pipeline %>
<%! from microsite_configuration import microsite %>

<%!
  from django.core.urlresolvers import reverse
%>

<%
  cert_name_short = settings.CERT_NAME_SHORT
  cert_name_long = settings.CERT_NAME_LONG
%>

<%inherit file="main.html" />

<%namespace name='static' file='static_content.html'/>

<%block name="pagetitle">${_("Dashboard")}</%block>
<%block name="bodyclass">view-dashboard is-authenticated</%block>
<%block name="nav_skip">#my-courses</%block>

<%block name="header_extras">
% for template_name in ["donation"]:
<script type="text/template" id="${template_name}-tpl">
  <%static:include path="dashboard/${template_name}.underscore" />
</script>
% endfor

% for template_name in ["dashboard_search_item", "dashboard_search_results", "search_loading", "search_error"]:
<script type="text/template" id="${template_name}-tpl">
    <%static:include path="search/${template_name}.underscore" />
</script>
% endfor
</%block>

<%block name="js_extra">
  <%static:js group='dashboard'/>
  <script type="text/javascript">
    $(document).ready(function() {
      edx.dashboard.legacy.init({
        dashboard: "${reverse('dashboard')}",
        signInUser: "${reverse('signin_user')}",
        changeEmailSettings: "${reverse('change_email_settings')}",
        verifyToggleBannerFailedOff: "${reverse('verify_student_toggle_failed_banner_off')}",
      });
    });
  </script>
</%block>

<div class="dashboard-notifications" tabindex="-1">
    % if reverifications["must_reverify"] or reverifications["denied"]:
        ## Section Element must be outside of the re-verify template. The template is re-used for courseware, and has separate styling.
        <section class="dashboard-banner">
            <%include file='dashboard/_dashboard_prompt_midcourse_reverify.html' />
        </section>
    % endif

    %if message:
        <section class="dashboard-banner">
            ${message}
        </section>
    %endif

    %if enrollment_message:
        <section class="dashboard-banner">
            ${enrollment_message}
        </section>
    %endif
</div>

<section class="container dashboard" id="dashboard-main">
  <section class="my-courses" id="my-courses" role="main" aria-label="Content">
    <header class="wrapper-header-courses">
      <h2 class="header-courses">${_("Current Courses")}</h2>
    </header>


    % if len(course_enrollment_pairs) > 0:
      <ul class="listing-courses">
      <% share_settings = settings.FEATURES.get('DASHBOARD_SHARE_SETTINGS', {}) %>
      % for dashboard_index, (course, enrollment) in enumerate(course_enrollment_pairs):
        <% show_courseware_link = (course.id in show_courseware_links_for) %>
        <% cert_status = cert_statuses.get(course.id) %>
        <% show_email_settings = (course.id in show_email_settings_for) %>
        <% course_mode_info = all_course_modes.get(course.id) %>
        <% show_refund_option = (course.id in show_refund_option_for) %>
        <% is_paid_course = (course.id in enrolled_courses_either_paid) %>
        <% is_course_blocked = (course.id in block_courses) %>
        <% course_verification_status = verification_status_by_course.get(course.id, {}) %>
        <% course_requirements = courses_requirements_not_met.get(course.id) %>
        <%include file='dashboard/_dashboard_course_listing.html' args="course=course, enrollment=enrollment, show_courseware_link=show_courseware_link, cert_status=cert_status, show_email_settings=show_email_settings, course_mode_info=course_mode_info, show_refund_option = show_refund_option, is_paid_course = is_paid_course, is_course_blocked = is_course_blocked, verification_status=course_verification_status, course_requirements=course_requirements, dashboard_index=dashboard_index, share_settings=share_settings" />
      % endfor

      % if settings.FEATURES.get('CUSTOM_COURSES_EDX', False):
        % for ccx, membership, course in ccx_membership_triplets:
          <% show_courseware_link = ccx.has_started() %>
          <% is_course_blocked = False %>
          <%include file='ccx/_dashboard_ccx_listing.html' args="ccx=ccx, membership=membership, course=course, show_courseware_link=show_courseware_link, is_course_blocked=is_course_blocked" />
        % endfor
      % endif

      </ul>
    % else:
      <section class="empty-dashboard-message">
        <p>${_("Looks like you haven't enrolled in any courses yet.")}</p>

        % if settings.FEATURES.get('COURSES_ARE_BROWSABLE'):
          <a href="${marketing_link('COURSES')}">
            ${_("Find courses now!")}
          </a>
        %endif
      </section>
    % endif

    % if staff_access and len(errored_courses) > 0:
      <div id="course-errors">
        <h2>${_("Course-loading errors")}</h2>

      % for course_dir, errors in errored_courses.items():
         <h3>${course_dir | h}</h3>
             <ul>
           % for (msg, err) in errors:
               <li>${msg}
                 <ul><li><pre>${err}</pre></li></ul>
               </li>
           % endfor
             </ul>
      % endfor
      </div>
    % endif
  </section>

  % if settings.FEATURES.get('ENABLE_DASHBOARD_SEARCH'):
    <div id="dashboard-search-bar" class="search-bar dashboard-search-bar" role="search" aria-label="Dashboard">
      <form>
        <label for="dashboard-search-input">${_('Search Your Courses')}</label>
        <div class="search-field-wrapper">
          <input id="dashboard-search-input" type="text" class="search-field"/>
          <button type="submit" class="search-button" aria-label="${_('Search')}">
            <i class="icon fa fa-search" aria-hidden="true"></i>
          </button>
          <button type="button" class="cancel-button" aria-label="${_('Clear search')}">
            <i class="icon fa fa-remove" aria-hidden="true"></i>
          </button>
        </div>
      </form>
    </div>
  % endif

  % if settings.FEATURES.get('ENABLE_DASHBOARD_SEARCH'):
    <section id="dashboard-search-results" class="search-results dashboard-search-results"></section>
  % endif

  <section class="profile-sidebar" id="profile-sidebar" role="region" aria-label="User info">
    <header class="profile">
      <h2 class="username-header"><span class="sr">${_("Username")}: </span></h2>
    </header>
    <section class="user-info">
      <ul>
        <li class="heads-up">
          <span class="title">${_("Want to change your account settings?")}</span>
          <span class="copy">${_("Click the arrow next to your username above.")}</span>
        </li>

        % if len(order_history_list):
        <li class="order-history">
          <span class="title">${_("Order History")}</span>
          % for order_history_item in order_history_list:
            <span><a href="${order_history_item['receipt_url']}" target="_blank" class="edit-name">${order_history_item['order_date']}</a></span>
          % endfor
        </li>
        % endif

        <%include file='dashboard/_dashboard_status_verification.html' />

        <%include file='dashboard/_dashboard_reverification_sidebar.html' />

      </ul>
    </section>
  </section>
</section>

<section id="email-settings-modal" class="modal" aria-hidden="true">
  <div class="inner-wrapper" role="dialog" aria-labelledby="email-settings-title">
    <button class="close-modal">
      <i class="icon fa fa-remove"></i>
      <span class="sr">
        ## Translators: this is a control to allow users to exit out of this modal interface (a menu or piece of UI that takes the full focus of the screen)
        ${_("Close")}
      </span>
    </button>

    <header>
      <h2 id="email-settings-title">
        ${_("Email Settings for {course_number}").format(course_number='<span id="email_settings_course_number"></span>')}
        <span class="sr">,
          ## Translators: this text gives status on if the modal interface (a menu or piece of UI that takes the full focus of the screen) is open or not
          ${_("window open")}
        </span>
      </h2>
      <hr/>
    </header>

    <form id="email_settings_form" method="post">
      <input name="course_id" id="email_settings_course_id" type="hidden" />
      <label>${_("Receive course emails")} <input type="checkbox" id="receive_emails" name="receive_emails" /></label>
      <div class="submit">
        <input type="submit" id="submit" value="${_("Save Settings")}" />
      </div>
    </form>
  </div>
</section>

<section id="unenroll-modal" class="modal unenroll-modal" aria-hidden="true">
  <div class="inner-wrapper" role="dialog" aria-labelledby="unenrollment-modal-title">
    <button class="close-modal">
      <i class="icon fa fa-remove"></i>
      <span class="sr">
        ## Translators: this is a control to allow users to exit out of this modal interface (a menu or piece of UI that takes the full focus of the screen)
        ${_("Close")}
      </span>
    </button>

    <header>
      <h2 id="unenrollment-modal-title">
        <span id='track-info'></span>
        <span id='refund-info'></span>
        <span class="sr">,
          ## Translators: this text gives status on if the modal interface (a menu or piece of UI that takes the full focus of the screen) is open or not
          ${_("window open")}
        </span>
      </h2>
      <hr/>
    </header>
    <div id="unenroll_error" class="modal-form-error"></div>
    <form id="unenroll_form" method="post" data-remote="true" action="${reverse('change_enrollment')}">
      <input name="course_id" id="unenroll_course_id" type="hidden" />
      <input name="enrollment_action" type="hidden" value="unenroll" />
      <div class="submit">
        <input name="submit" type="submit" value="${_("Unenroll")}" />
      </div>
    </form>
  </div>
</section>
