{% extends "base.html" %}
{% from 'components/ui.html' import page_header %}

{% block content %}
{% set actions %}
<a href="{{ url_for('invoices.list_invoices') }}" class="btn btn-secondary">{{ _('Back to Invoices') }}</a>
{% endset %}
{{ page_header('fas fa-file-invoice', _('Create Invoice'), subtitle_text=_('Generate a new invoice for a project and client'), actions_html=actions, breadcrumbs=[{'text': _('Invoices'), 'url': url_for('invoices.list_invoices')}, {'text': _('Create Invoice')}]) }}

<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="createInvoiceForm" novalidate data-validate-form>
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="project_id" class="form-label">{{ _('Project') }} *</label>
                        <select name="project_id" id="project_id" required class="form-input">
                            <option value="">{{ _('Select a project') }}</option>
                            {% for project in projects %}
                            <option value="{{ project.id }}" data-client-name="{{ project.client_obj.name if project.client_obj else '' }}" data-client-email="{{ project.client_obj.email if project.client_obj else '' }}" data-client-address="{{ project.client_obj.address if project.client_obj else '' }}">{{ project.name }} ({{ project.client }})</option>
                            {% endfor %}
                        </select>
                        <p class="text-xs text-text-muted-light dark:text-text-muted-dark mt-1">{{ _('Selecting a project will auto-fill client details') }}</p>
                    </div>
                    <div>
                        <label for="client_name" class="form-label">{{ _('Client Name') }} *</label>
                        <input type="text" name="client_name" id="client_name" value="{{ request.form.get('client_name','') }}" required class="form-input">
                    </div>
                    <div>
                        <label for="client_email" class="form-label">{{ _('Client Email') }}</label>
                        <input type="email" name="client_email" id="client_email" value="{{ request.form.get('client_email','') }}" class="form-input">
                    </div>
                    <div>
                        <label for="due_date" class="form-label">{{ _('Due Date') }} *</label>
                        <input type="date" name="due_date" id="due_date" value="{{ default_due_date }}" required class="form-input">
                    </div>
                    <div class="md:col-span-2">
                        <label for="client_address" class="form-label">{{ _('Client Address') }}</label>
                        <textarea name="client_address" id="client_address" rows="3" class="form-input">{{ request.form.get('client_address','') }}</textarea>
                    </div>
                    <div class="md:col-span-2">
                        <label for="buyer_reference" class="form-label">{{ _('Buyer reference (PEPPOL BT-10)') }}</label>
                        <input type="text" name="buyer_reference" id="buyer_reference" value="{{ request.form.get('buyer_reference','') }}" class="form-input" placeholder="{{ _('Optional; used in PEPPOL UBL') }}">
                    </div>
                    <div>
                        <label for="tax_rate" class="form-label">{{ _('Tax Rate (%)') }}</label>
                        <input type="number" name="tax_rate" id="tax_rate" value="{{ settings.tax_rate or 0 }}" step="0.01" class="form-input">
                    </div>
                    <div class="md:col-span-2">
                        <label for="notes" class="form-label">{{ _('Notes') }}</label>
                        <textarea name="notes" id="notes" rows="3" class="form-input">{{ request.form.get('notes','') }}</textarea>
                    </div>
                    <div class="md:col-span-2">
                        <label for="terms" class="form-label">{{ _('Terms') }}</label>
                        <textarea name="terms" id="terms" rows="3" class="form-input">{{ request.form.get('terms','') }}</textarea>
                    </div>
                </div>
                <div class="mt-6">
                    <button type="submit" class="btn btn-primary">{{ _('Create Invoice') }}</button>
                </div>
            </form>
        </div>
    </div>
    <div>
        <div class="bg-card-light dark:bg-card-dark p-6 rounded-xl border border-border-light dark:border-border-dark shadow-sm">
            <h2 class="text-lg font-semibold mb-4">{{ _('Tips') }}</h2>
            <ul class="list-disc pl-5 text-sm text-text-muted-light dark:text-text-muted-dark space-y-2">
                <li>{{ _('Choose the correct project to auto-fill client details.') }}</li>
                <li>{{ _('You can customize notes and terms before sending the invoice.') }}</li>
            </ul>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts_extra %}
<style>
@keyframes field-flash {
    0% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.5); }
    50% { box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.3); }
    100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
}
.field-autofilled {
    animation: field-flash 0.6s ease-out;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var projectSelect = document.getElementById('project_id');
    var clientName = document.getElementById('client_name');
    var clientEmail = document.getElementById('client_email');
    var clientAddress = document.getElementById('client_address');

    function flashField(el) {
        if (!el) return;
        el.classList.remove('field-autofilled');
        void el.offsetWidth;
        el.classList.add('field-autofilled');
    }

    function applyFromSelected(isUserAction) {
        var opt = projectSelect.options[projectSelect.selectedIndex];
        if (!opt) return;
        var name = opt.getAttribute('data-client-name') || '';
        var email = opt.getAttribute('data-client-email') || '';
        var address = opt.getAttribute('data-client-address') || '';
        clientName.value = name || clientName.value;
        clientEmail.value = email || '';
        clientAddress.value = address || '';
        if (isUserAction) {
            flashField(clientName);
            flashField(clientEmail);
            flashField(clientAddress);
        }
    }

    projectSelect.addEventListener('change', function() { applyFromSelected(true); });
    applyFromSelected(false);
});
</script>
{% endblock %}
