{% extends "base.html" %}

{% block title %}События - Домовой Бот{% endblock %}

{% block nav_events %}active{% endblock %}

{% block content %}
<h2 class="mb-4"><i class="bi bi-calendar-event"></i> События дома</h2>

<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0"><i class="bi bi-plus-lg"></i> Добавить событие</h5>
    </div>
    <div class="card-body">
        <form id="addEventForm" class="row g-3">
            <div class="col-md-4">
                <input type="text" class="form-control" id="eventTitle" placeholder="Название события" required>
            </div>
            <div class="col-md-3">
                <select class="form-select" id="eventType">
                    <option value="meeting">👥 Собрание</option>
                    <option value="cleanup">🧹 Субботник</option>
                    <option value="holiday">🎉 Праздник</option>
                    <option value="other">📅 Другое</option>
                </select>
            </div>
            <div class="col-md-3">
                <input type="datetime-local" class="form-control" id="eventDate" required>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">Добавить</button>
            </div>
            <div class="col-md-12">
                <textarea class="form-control" id="eventDesc" placeholder="Описание события" rows="2"></textarea>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <h5>Предстоящие события:</h5>
        <table class="table table-hover">
            <thead>
                <tr><th>Название</th><th>Тип</th><th>Дата и время</th><th>Описание</th></tr>
            </thead>
            <tbody>
                {% for event in events %}
                <tr>
                    <td>{{ event.title }}</td>
                    <td>{{ event.event_type or '—' }}</td>
                    <td>{{ event.event_date.strftime('%d.%m.%Y %H:%M') if event.event_date else '—' }}</td>
                    <td>{{ event.description or '—' }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}

{% block extra_script %}
<script>
    document.getElementById('addEventForm').addEventListener('submit', async (e) => {
        e.preventDefault();
        const data = {
            title: document.getElementById('eventTitle').value,
            event_type: document.getElementById('eventType').value,
            event_date: new Date(document.getElementById('eventDate').value).toISOString(),
            description: document.getElementById('eventDesc').value
        };
        const r = await fetch('/api/event/add', {
            method: 'POST', headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(data)
        });
        const result = await r.json();
        if (result.success) { alert('✅ Событие добавлено'); location.reload(); }
    });
</script>
{% endblock %}
