'use strict';
var Tutorials = {
render: function(params) {
var el = document.getElementById('content');
if (params[0]) { this.renderSingle(el, params[0]); return; }
el.textContent = 'Lade Tutorials...';
var self = this;
API.get('/tutorials').then(function(data) { self.renderList(el, data.tutorials); })
.catch(function(e) { el.textContent = e.message; });
},
renderList: function(el, tutorials) {
var done = tutorials.filter(function(t) { return t.completed; }).length;
var h = '
';
h += '';
tutorials.forEach(function(t, i) {
h += '
';
h += '
' + String(i + 1).padStart(2, '0') + '
';
h += '
' + Markdown.esc(t.title) + '
';
h += '
' + (t.duration_min ? 'ca. ' + t.duration_min + ' Min' : '');
if (t.description) h += ' — ' + Markdown.esc(t.description);
h += '
';
if (t.completed) h += '
✓
';
h += '
';
});
h += '
';
el.innerHTML = h;
el.querySelectorAll('.tutorial-item').forEach(function(item) {
item.addEventListener('click', function() { location.hash = '#/tutorials/' + item.dataset.slug; });
});
},
renderSingle: function(el, slug) {
el.textContent = 'Lade Tutorial...';
var self = this;
API.get('/tutorials/' + slug).then(function(data) {
var t = data.tutorial;
var h = '← Zurueck zu Tutorials';
h += '' + Markdown.render(t.content_md) + '
';
h += '';
if (t.completed) {
h += '
✓ Abgeschlossen
';
} else {
h += '
';
}
h += '
';
el.innerHTML = h;
var btn = document.getElementById('complete-btn');
if (btn) btn.addEventListener('click', function() { self.complete(slug); });
}).catch(function(e) { el.textContent = e.message; });
},
complete: function(slug) {
var self = this;
API.post('/tutorials/' + slug + '/complete').then(function() {
self.renderSingle(document.getElementById('content'), slug);
}).catch(function(e) { alert('Fehler: ' + e.message); });
}
};