{% extends "page.html" %}
{% block page_content %}
<div class="card">
    <div class="card-header"><h2>Система идей</h2>
        <button class="btn btn-primary btn-sm" onclick="document.getElementById('modal-idea').classList.add('open')">+ Новая идея</button>
    </div>
    <div class="table-wrap"><table><thead><tr><th>ID</th><th>Название</th><th>Описание</th><th>Статус</th><th>Дата</th><th class="actions"></th></tr></thead>
        <tbody>{% for idea in ideas %}<tr>
            <td>{{ idea.id }}</td>
            <td><strong>{{ idea.title }}</strong></td>
            <td>{{ (idea.description or '')[:100] }}{% if (idea.description or '')|length > 100 %}…{% endif %}</td>
            <td><span class="badge badge-{{ 'done' if idea.status == 'completed' else 'progress' if idea.status == 'in_progress' else 'default' }}">{{ {'normal':'Обычная','in_progress':'В работе','completed':'Выполнено'}.get(idea.status, idea.status) }}</span></td>
            <td><small>{{ idea.updated_at.strftime('%d.%m.%Y') if idea.updated_at else '' }}</small></td>
            <td class="actions">
                <form method="post" action="/service/ideas/status" style="display:inline">
                    <input type="hidden" name="id" value="{{ idea.id }}">
                    <button class="btn btn-secondary btn-sm" title="Переключить статус">↻</button>
                </form>
                <button class="btn btn-secondary btn-sm" onclick="editIdea({{ idea.id }},{{ idea.title|tojson }},{{ idea.description|tojson }})">✎</button>
                <form method="post" action="/service/ideas/delete" style="display:inline" onsubmit="return confirm('Удалить идею?')">
                    <input type="hidden" name="id" value="{{ idea.id }}">
                    <button class="btn btn-danger btn-sm">✕</button>
                </form>
            </td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>

<div class="modal-overlay" id="modal-idea"><div class="modal">
    <button type="button" class="modal-close" onclick="this.closest('.modal-overlay').classList.remove('open')">✕</button>
    <h2 id="modal-idea-title">Новая идея</h2>
    <form method="post" id="modal-idea-form">
        <input type="hidden" name="id" id="idea-id">
        <div class="form-group"><label>Название</label><input type="text" name="title" id="idea-title" required></div>
        <div class="form-group"><label>Описание</label><textarea name="description" id="idea-description" rows="4"></textarea></div>
        <div class="form-actions"><button type="submit" class="btn btn-primary">Сохранить</button>
        <button type="button" class="btn btn-secondary" onclick="this.closest('.modal-overlay').classList.remove('open')">Отмена</button></div>
    </form>
</div></div>

<script>
function editIdea(id,title,description){
    document.getElementById('modal-idea').classList.add('open');
    document.getElementById('modal-idea-title').textContent = 'Редактировать идею';
    document.getElementById('modal-idea-form').action = '/service/ideas/edit';
    document.getElementById('idea-id').value = id;
    document.getElementById('idea-title').value = title;
    document.getElementById('idea-description').value = description;
}
document.addEventListener('DOMContentLoaded', function(){
    var form = document.getElementById('modal-idea-form');
    if(form) form.action = '/service/ideas/create';
});
</script>
{% endblock %}
