<%!
  from django.core.urlresolvers import reverse
  from courseware.courses import course_image_url, get_course_about_section
  from courseware.access import has_access
%>
<%inherit file="main.html" />

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

<%block name="title"><title>Dashboard</title></%block>

<%block name="js_extra">
  <script type="text/javascript">
  (function() {

    $(".unenroll").click(function(event) {
      $("#unenroll_course_id").val( $(event.target).data("course-id") );
      $("#unenroll_course_number").text( $(event.target).data("course-number") );

    });

     $(document).delegate('#unenroll_form', 'ajax:success', function(data, json, xhr) {
       if(json.success) {
         location.href="${reverse('dashboard')}";
       } else {
         if($('#unenroll_error').length == 0) {
           $('#unenroll_form').prepend('<div id="unenroll_error" class="modal-form-error"></div>');
         }
         $('#unenroll_error').text(json.error).stop().css("display", "block");
       }
     });
  })(this)
  </script>
</%block>

<section class="container dashboard">

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

  <section class="profile-sidebar">
    <header class="profile">
      <h1 class="user-name">${ user.username }</h1>
    </header>
    <section class="user-info">
      <ul>
        <li>
          <span class="title"><div class="icon name-icon"></div>Full Name</span><span class="data">${ user.profile.name | h }</span>
        </li>
        <li>
          <span class="title"><div class="icon email-icon"></div>Email</span><span class="data">${ user.email | h }</span>
        </li>
      </ul>
    </section>
  </section>

  <section class="my-courses">
    <header>
      <h2>Current Courses</h2>
    </header>

    % if len(courses) > 0:
      % for course in courses:

        <article class="my-course">
          <%
          if has_access(user, course, 'load'):
            course_target = reverse('info', args=[course.id])
          else:
            course_target = reverse('about_course', args=[course.id])
          %>
          <a href="${course_target}" class="cover" style="background-image: url('${course_image_url(course)}')">
            <div class="shade"></div>
            <div class="arrow">&#10095;</div>
          </a>
          <section class="info">
            <hgroup>
              <a href="${reverse('university_profile', args=[course.org])}" class="university">${get_course_about_section(course, 'university')}</a>
              <h3><a href="${course_target}">${course.number} ${course.title}</a></h3>
            </hgroup>
            <section class="course-status">
              <p>Class Starts - <span>${course.start_date_text}</span></div>
            </section>
            <section class="meta">
              <div class="course-work-icon"></div>
              <div class="progress">
                <div class="meter">
                  <div class="meter-fill"></div>
                </div>
              </div>
              <div class="complete">
                ##<p><span class="completeness">60%</span> complete</p>
              </div>
            </section>
          </section>
        </article>
        <a href="#unenroll-modal" class="unenroll" rel="leanModal" data-course-id="${course.id}" data-course-number="${course.number}">Unregister</a>

      % endfor
    % else:
      <section class="empty-dashboard-message">
        <p>Looks like you haven't registered for any courses yet.</p>
        <a href="${reverse('courses')}">Find courses now!</a>
      </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
    % endif
  </section>
</section>



<section id="unenroll-modal" class="modal unenroll-modal">
  <div class="inner-wrapper">
    <header>
      <h2>Are you sure you want to unregister from <span id="unenroll_course_number"></span>?</h2>
      <hr>
    </header>

    <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="Unregister" />
      </div>
    </form>

    <div class="close-modal">
      <div class="inner">
        <p>&#10005;</p>
      </div>
    </div>
  </div>
</section>


