{% extends "base.html" %}

{% block content %}
<div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
    <div>
        <h1 class="text-2xl font-bold">{{ _('Generate from Time, Costs & Goods') }}</h1>
        <p class="text-text-muted-light dark:text-text-muted-dark">{{ _('Select unbilled time entries, project costs, and extra goods to add to this invoice') }}</p>
    </div>
    <a href="{{ url_for('invoices.edit_invoice', invoice_id=invoice.id) }}" class="btn btn-secondary shrink-0">{{ _('Back to Edit') }}</a>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <div class="lg:col-span-2">
        <div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm">
            <form method="POST" id="generateFromTimeForm">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

                <div class="mb-6">
                    <h2 class="text-lg font-semibold mb-3">{{ _('Unbilled Time Entries') }}</h2>
                    {% if grouped_time_entries %}
                    <div class="space-y-4">
                        {% for day in grouped_time_entries %}
                        <div class="rounded-lg border border-border-light dark:border-border-dark overflow-hidden">
                            <div class="flex items-center justify-between px-4 py-2 bg-gray-50 dark:bg-gray-800">
                                <div class="font-semibold">
                                    <i class="fas fa-calendar-day text-primary mr-2"></i>
                                    {{ day.date|format_date if day.date else '-' }}
                                </div>
                                <div class="text-xs text-text-muted-light dark:text-text-muted-dark">
                                    <span class="font-semibold text-text-light dark:text-text-dark">{{ '%.2f'|format(day.total_hours) }}</span> {{ _('h') }}
                                    · {{ day.entries|length }} {{ _('entries') }}
                                </div>
                            </div>
                            <div class="p-3 space-y-2">
                                {% for entry in day.entries %}
                                <label class="flex items-start gap-3 p-3 rounded border border-border-light dark:border-border-dark hover:bg-background-light dark:hover:bg-background-dark">
                                    <input type="checkbox" class="mt-1" name="time_entries[]" value="{{ entry.id }}">
                                    <div class="flex-1 text-sm">
                                        <div class="font-medium">{{ entry.task.name if entry.task else _('Time Entry') }} · {{ '%.2f'|format(entry.duration_hours) }} {{ _('h') }}</div>
                                        <div class="text-text-muted-light dark:text-text-muted-dark">
                                            {{ entry.start_time|user_time }} → {{ entry.end_time|user_time if entry.end_time else '-' }}
                                            {% if entry.notes %} · {{ entry.notes[:120] }}{% if entry.notes|length > 120 %}…{% endif %}{% endif %}
                                        </div>
                                    </div>
                                </label>
                                {% endfor %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% elif time_entries %}
                    <div class="space-y-2">
                        {% for entry in time_entries %}
                        <label class="flex items-start gap-3 p-3 rounded border border-border-light dark:border-border-dark hover:bg-background-light dark:hover:bg-background-dark">
                            <input type="checkbox" class="mt-1" name="time_entries[]" value="{{ entry.id }}">
                            <div class="flex-1 text-sm">
                                <div class="font-medium">{{ entry.task.name if entry.task else _('Time Entry') }} · {{ '%.2f'|format(entry.duration_hours) }} {{ _('h') }}</div>
                                <div class="text-text-muted-light dark:text-text-muted-dark">
                                    {{ entry.start_time|user_datetime }} → {{ entry.end_time|user_datetime if entry.end_time else '-' }}
                                    {% if entry.notes %} · {{ entry.notes[:80] }}{% if entry.notes|length > 80 %}…{% endif %}{% endif %}
                                </div>
                            </div>
                        </label>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('No unbilled time entries found for this project.') }}</div>
                    {% endif %}
                </div>

                <div class="mb-6">
                    <h2 class="text-lg font-semibold mb-3">{{ _('Uninvoiced Project Costs') }}</h2>
                    {% if project_costs %}
                    <div class="space-y-2">
                        {% for cost in project_costs %}
                        <label class="flex items-start gap-3 p-3 rounded border border-border-light dark:border-border-dark hover:bg-background-light dark:hover:bg-background-dark">
                            <input type="checkbox" class="mt-1" name="project_costs[]" value="{{ cost.id }}">
                            <div class="flex-1 text-sm">
                                <div class="font-medium">{{ cost.description }} ({{ cost.category|title }}) · {{ '%.2f'|format(cost.amount) }} {{ cost.currency_code }}</div>
                                <div class="text-text-muted-light dark:text-text-muted-dark">{{ cost.cost_date|format_date }}</div>
                            </div>
                        </label>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('No uninvoiced costs found for this project.') }}</div>
                    {% endif %}
                </div>

                <div class="mb-6">
                    <h2 class="text-lg font-semibold mb-3">{{ _('Uninvoiced Billable Expenses') }}</h2>
                    {% if expenses %}
                    <div class="space-y-2">
                        {% for expense in expenses %}
                        <label class="flex items-start gap-3 p-3 rounded border border-border-light dark:border-border-dark hover:bg-background-light dark:hover:bg-background-dark">
                            <input type="checkbox" class="mt-1" name="expenses[]" value="{{ expense.id }}">
                            <div class="flex-1 text-sm">
                                <div class="font-medium">{{ expense.title }} ({{ expense.category|title }}) · {{ '%.2f'|format(expense.total_amount) }} {{ expense.currency_code }}</div>
                                <div class="text-text-muted-light dark:text-text-muted-dark">
                                    {{ expense.expense_date|format_date }}
                                    {% if expense.vendor %} · {{ _('Vendor') }}: {{ expense.vendor }}{% endif %}
                                    {% if expense.description %} · {{ expense.description[:80] }}{% if expense.description|length > 80 %}…{% endif %}{% endif %}
                                </div>
                            </div>
                        </label>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('No uninvoiced billable expenses found for this project.') }}</div>
                    {% endif %}
                </div>

                <div class="mb-6">
                    <h2 class="text-lg font-semibold mb-3">{{ _('Project Extra Goods') }}</h2>
                    {% if extra_goods %}
                    <div class="space-y-2">
                        {% for good in extra_goods %}
                        <label class="flex items-start gap-3 p-3 rounded border border-border-light dark:border-border-dark hover:bg-background-light dark:hover:bg-background-dark">
                            <input type="checkbox" class="mt-1" name="extra_goods[]" value="{{ good.id }}">
                            <div class="flex-1 text-sm">
                                <div class="font-medium">{{ good.name }} ({{ good.category|title }}) · {{ '%.2f'|format(good.quantity) }} x {{ '%.2f'|format(good.unit_price) }} {{ good.currency_code }}</div>
                                <div class="text-text-muted-light dark:text-text-muted-dark">
                                    {{ _('Total') }}: {{ '%.2f'|format(good.total_amount) }} {{ good.currency_code }}
                                    {% if good.description %} · {{ good.description[:80] }}{% if good.description|length > 80 %}…{% endif %}{% endif %}
                                </div>
                            </div>
                        </label>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('No extra goods found for this project.') }}</div>
                    {% endif %}
                </div>

                <div class="mt-6 flex flex-wrap justify-end gap-3 border-t border-border-light dark:border-border-dark pt-4">
                    <a href="{{ url_for('invoices.edit_invoice', invoice_id=invoice.id) }}" class="btn btn-secondary">{{ _('Cancel') }}</a>
                    <button type="submit" class="btn btn-primary">{{ _('Add Selected to Invoice') }}</button>
                </div>
            </form>
        </div>
    </div>

    <div class="space-y-4">
        <div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm">
            <h3 class="text-lg font-semibold mb-3">{{ _('Selection Summary') }}</h3>
            <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('Total available hours') }}: <span class="font-semibold text-text-light dark:text-text-dark">{{ '%.2f'|format(total_available_hours) }}</span></div>
            <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('Total available costs') }}: <span class="font-semibold text-text-light dark:text-text-dark">{{ '%.2f'|format(total_available_costs) }} {{ currency }}</span></div>
            <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('Total available expenses') }}: <span class="font-semibold text-text-light dark:text-text-dark">{{ '%.2f'|format(total_available_expenses) }} {{ currency }}</span></div>
            <div class="text-sm text-text-muted-light dark:text-text-muted-dark">{{ _('Total available goods') }}: <span class="font-semibold text-text-light dark:text-text-dark">{{ '%.2f'|format(total_available_goods) }} {{ currency }}</span></div>
            {% if prepaid_plan_hours %}
            <div class="mt-4 pt-4 border-t border-border-light dark:border-border-dark">
                <h4 class="text-sm font-semibold text-text-light dark:text-text-dark mb-2">{{ _('Prepaid Hours Overview') }}</h4>
                <p class="text-xs text-text-muted-light dark:text-text-muted-dark mb-2">
                    {{ _('Plan includes %(hours)s hours per cycle (resets on day %(day)s).', hours='%.2f'|format(prepaid_plan_hours), day=prepaid_reset_day) }}
                </p>
                {% if prepaid_summary %}
                <ul class="space-y-1 text-xs text-text-muted-light dark:text-text-muted-dark">
                    {% for item in prepaid_summary %}
                    <li class="flex justify-between gap-4">
                        <span class="font-medium text-text-light dark:text-text-dark">{{ item.allocation_month_label }}</span>
                        <span>{{ _('Consumed: %(consumed)s h • Remaining: %(remaining)s h', consumed='%.2f'|format(item.consumed_hours), remaining='%.2f'|format(item.remaining_hours)) }}</span>
                    </li>
                    {% endfor %}
                </ul>
                {% else %}
                <p class="text-xs text-text-muted-light dark:text-text-muted-dark">{{ _('No prepaid usage recorded for selected period yet.') }}</p>
                {% endif %}
            </div>
            {% endif %}
        </div>

        <div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm">
            <h3 class="text-lg font-semibold mb-3">{{ _('Tips') }}</h3>
            <ul class="list-disc pl-5 text-sm text-text-muted-light dark:text-text-muted-dark space-y-2">
                <li>{{ _('You can select multiple time entries, costs, expenses, and extra goods.') }}</li>
                <li>{{ _('Time entries are grouped by task or project at item creation.') }}</li>
                <li>{{ _('Costs and extra goods are added as individual invoice items.') }}</li>
                <li>{{ _('Expenses are linked to the invoice and appear in a separate section.') }}</li>
            </ul>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts_extra %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('generateFromTimeForm');
    if (!form) return;
    form.addEventListener('submit', function(e) {
        const anyChecked = form.querySelector('input[name="time_entries[]"]:checked, input[name="project_costs[]"]:checked, input[name="expenses[]"]:checked, input[name="extra_goods[]"]:checked');
        if (!anyChecked) {
            e.preventDefault();
            try { window.toastManager && window.toastManager.warning('{{ _('Please select at least one time entry, cost, expense, or extra good') }}'); } catch(_) {}
        }
    });
});
</script>
{% endblock %}


