{{> header}}

<style>
  .my-courses-page {
    background: linear-gradient(135deg, #0f172a 0%, #020617 100%);
    min-height: calc(100vh - 70px);
    padding: 60px 40px;
  }
  .page-header {
    text-align: center;
    max-width: 700px;
    margin: 0 auto 3rem;
  }
  .page-header h1 {
    color: white;
    font-size: 2rem;
    margin-bottom: 0.5rem;
  }
  .page-header p {
    color: #64748b;
  }
  .courses-container {
    max-width: 1000px;
    margin: 0 auto;
  }
  .course-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 1rem;
    display: flex;
    gap: 1.5rem;
    align-items: center;
    transition: all 0.3s ease;
  }
  .course-item:hover {
    border-color: #10b981;
  }
  .course-thumb {
    width: 120px;
    height: 80px;
    background: linear-gradient(135deg, #10b981 0%, #6366f1 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    flex-shrink: 0;
  }
  .course-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 10px;
  }
  .course-info {
    flex: 1;
  }
  .course-info h3 {
    color: white;
    margin-bottom: 0.5rem;
  }
  .course-info p {
    color: #64748b;
    font-size: 0.9rem;
    margin-bottom: 0.75rem;
  }
  .progress-bar {
    height: 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    overflow: hidden;
  }
  .progress-fill {
    height: 100%;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    border-radius: 3px;
  }
  .course-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  .btn-continue {
    display: inline-block;
    padding: 10px 20px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    text-align: center;
    transition: all 0.3s ease;
  }
  .btn-continue:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
  }
  .empty-state {
    text-align: center;
    padding: 60px 20px;
  }
  .empty-state span {
    font-size: 4rem;
    display: block;
    margin-bottom: 1rem;
  }
  .empty-state h2 {
    color: white;
    margin-bottom: 1rem;
  }
  .empty-state p {
    color: #64748b;
    margin-bottom: 2rem;
  }
  .empty-state a {
    display: inline-block;
    padding: 14px 28px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    text-decoration: none;
    border-radius: 10px;
    font-weight: 600;
  }
  .guest-message {
    text-align: center;
    padding: 60px 20px;
  }
  .guest-message span {
    font-size: 4rem;
    display: block;
    margin-bottom: 1rem;
  }
  .guest-message h2 {
    color: white;
    margin-bottom: 1rem;
  }
  .guest-message p {
    color: #64748b;
    margin-bottom: 2rem;
  }
  .guest-message a {
    display: inline-block;
    padding: 14px 28px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    text-decoration: none;
    border-radius: 10px;
    font-weight: 600;
    margin: 0 0.5rem;
  }
  @media (max-width: 600px) {
    .course-item {
      flex-direction: column;
      text-align: center;
    }
    .course-thumb {
      width: 100%;
      height: 120px;
    }
  }
</style>

<section class="my-courses-page">
  {{#if user}}
  <div class="page-header">
    <h1>📚 My Courses</h1>
    <p>Continue your learning journey</p>
  </div>
  
  <div class="courses-container" id="coursesContainer">
    <div class="empty-state" id="loadingState">
      <span>⏳</span>
      <h2>Loading your courses...</h2>
    </div>
  </div>

  <script>
    (function() {
      var API = '/api';
      function getAuthHeaders() {
        var token = localStorage.getItem('token');
        return token ? { 'Authorization': 'Bearer ' + token } : {};
      }
      
      async function loadCourses() {
        try {
          var r = await fetch(API + '/lms/my-enrollments', { credentials: 'include', headers: getAuthHeaders() });
          if (!r.ok) throw new Error('Failed to load');
          var enrollments = await r.json();
          renderCourses(enrollments || []);
        } catch(e) {
          console.error('Load courses error:', e);
          document.getElementById('coursesContainer').innerHTML = '<div class="empty-state"><span>📭</span><h2>No courses yet</h2><p>Browse our catalog and start learning!</p><a href="/courses">Browse Courses</a></div>';
        }
      }
      
      function renderCourses(enrollments) {
        if (!enrollments.length) {
          document.getElementById('coursesContainer').innerHTML = '<div class="empty-state"><span>📭</span><h2>No courses yet</h2><p>Browse our catalog and start learning!</p><a href="/courses">Browse Courses</a></div>';
          return;
        }
        var html = enrollments.map(function(e) {
          var progress = e.progress && e.progress.percent ? e.progress.percent : 0;
          var c = e.course || {};
          var statusBadge = e.status === 'COMPLETED' ? '<span style="background:#10b981;color:white;padding:2px 8px;border-radius:4px;font-size:0.75rem;margin-left:8px">Completed</span>' : '';
          return '<div class="course-item"><div class="course-thumb">' + (c.featuredImage ? '<img src="'+c.featuredImage+'" alt="">' : '📚') + '</div><div class="course-info"><h3>'+c.title+statusBadge+'</h3><p>'+(c.shortDescription||'')+'</p><div class="progress-bar"><div class="progress-fill" style="width:'+progress+'%"></div></div><p style="margin-top:0.5rem;font-size:0.85rem">'+progress+'% complete</p></div><div class="course-actions"><a href="/admin/lms/learn/'+c.id+'" class="btn-continue">'+(e.status === 'COMPLETED' ? 'Review' : 'Continue')+'</a></div></div>';
        }).join('');
        document.getElementById('coursesContainer').innerHTML = html;
      }
      
      loadCourses();
    })();
  </script>
  {{else}}
  <div class="guest-message">
    <span>🔒</span>
    <h2>Please sign in</h2>
    <p>You need to be logged in to view your courses.</p>
    <a href="/login">Sign In</a>
    <a href="/register">Create Account</a>
  </div>
  {{/if}}
</section>

{{> footer}}

