{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

{% block meta_title %}{{ 'Invoice Settings'|trans }}{% endblock %}

{% set active_menu = 'system' %}

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|url }}">
                <svg class="icon">
                    <use xlink:href="#home"/>
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'system'|url }}">{{ 'Settings'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Invoice Settings'|trans }}</li>
    </ol>
{% endblock %}

{% block content %}
    {% set params = admin.system_get_params %}
    <div class="card">
        <div class="card-header">
            <div>
                <h3 class="card-title">{{ 'Invoice Settings'|trans }}</h3>
                <p class="card-subtitle">{{ 'Configure invoice timing, numbering, document behavior, refunds, and add-funds limits.'|trans }}</p>
            </div>
        </div>
        <form method="post" action="{{ 'system/update_params'|api_url }}" {{ fb_api_form({message: 'Settings Updated'|trans}) }}>
            <div class="card-body">
                <div class="alert alert-info" role="alert">
                    <strong class="me-1">{{ 'Information:'|trans }}</strong>{{ 'These settings control when invoices are issued, how they are numbered, and how invoice documents behave after they are created.'|trans }}
                </div>

                <h4 class="card-title mb-2">{{ 'Invoice Timing'|trans }}</h4>
                <p class="text-secondary mb-3">{{ 'Control when renewal invoices are created and when they become due.'|trans }}</p>
                <div class="row g-3">
                    <div class="col-lg-6">
                        <label class="form-label" for="invoice_issue_days_before_expire">{{ 'Generate New Invoice Before Order Expiration'|trans }}</label>
                        <div class="input-group">
                            <input class="form-control" id="invoice_issue_days_before_expire" type="number" min="0" name="invoice_issue_days_before_expire" value="{{ params.invoice_issue_days_before_expire }}">
                            <span class="input-group-text">{{ 'days'|trans }}</span>
                        </div>
                    </div>
                    <div class="col-lg-6">
                        <label class="form-label" for="invoice_due_days">{{ 'Invoice Due Days'|trans }}</label>
                        <div class="input-group">
                            <input class="form-control" id="invoice_due_days" type="number" min="0" name="invoice_due_days" value="{{ params.invoice_due_days }}">
                            <span class="input-group-text">{{ 'days'|trans }}</span>
                        </div>
                    </div>
                </div>
                <div class="mt-3">
                    <label class="form-check form-switch form-switch-2">
                        <input class="form-check-input" name="invoice_auto_approval" type="checkbox" value="1" {% if params.invoice_auto_approval == '1' %}checked{% endif %}>
                        <span class="form-check-label">{{ 'Enable Auto Invoice Approval'|trans }}</span>
                    </label>
                </div>
                <div class="mt-3">
                    <label class="form-label" for="invoice_default_note">{{ 'Default Invoice Note'|trans }}</label>
                    <textarea class="form-control editor-textarea" id="invoice_default_note" name="invoice_default_note" rows="5">{{ params.invoice_default_note }}</textarea>
                    <small class="form-hint">{{ 'Supports markdown format.'|trans }}</small>
                </div>
            </div>
            <div class="card-body border-top">
                <h4 class="card-title mb-2">{{ 'Sequential Numbering'|trans }}</h4>
                <p class="text-secondary mb-3">{{ 'Control invoice cleanup rules and the numbering scheme used for generated documents.'|trans }}</p>
                <div class="row g-3">
                    <div class="col-lg-6">
                        <label class="form-label" for="remove_after_days">{{ 'Remove Unpaid Invoices After'|trans }}</label>
                        <div class="input-group">
                            <input class="form-control" id="remove_after_days" type="number" min="0" name="remove_after_days" value="{{ params.remove_after_days|default(0) }}">
                            <span class="input-group-text">{{ 'days'|trans }}</span>
                        </div>
                        <small class="form-hint">{{ 'Set 0 to keep invoices forever.'|trans }}</small>
                    </div>
                    <div class="col-lg-6">
                        <label class="form-label" for="invoice_number_padding">{{ 'Invoice Number Padding Length'|trans }}</label>
                        <input class="form-control" id="invoice_number_padding" type="number" name="invoice_number_padding" value="{{ params.invoice_number_padding|default(5) }}" min="0" max="20">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label" for="invoice_series">{{ 'Invoice Prefix / Series'|trans }}</label>
                        <input class="form-control" id="invoice_series" type="text" name="invoice_series" value="{{ params.invoice_series }}">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label" for="invoice_series_paid">{{ 'Paid Invoice Prefix / Series'|trans }}</label>
                        <input class="form-control" id="invoice_series_paid" type="text" name="invoice_series_paid" value="{{ params.invoice_series_paid }}">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label" for="invoice_starting_number">{{ 'Next Paid Invoice Number'|trans }}</label>
                        <input class="form-control" id="invoice_starting_number" type="text" name="invoice_starting_number" value="{{ params.invoice_starting_number }}">
                    </div>
                </div>
            </div>
            <div class="card-body border-top">
                <h4 class="card-title mb-2">{{ 'Document Settings'|trans }}</h4>
                <p class="text-secondary mb-3">{{ 'Choose the invoice page format and whether invoice hashes may be used for direct access.'|trans }}</p>
                <div class="row g-4">
                    <div class="col-lg-6">
                        <label class="form-label d-block">{{ 'Document Format'|trans }}</label>
                        <div class="form-check">
                            <input class="form-check-input" id="radioLetter" type="radio" name="invoice_document_format" value="Letter" {% if params.invoice_document_format == 'Letter' %}checked{% endif %}>
                            <label class="form-check-label" for="radioLetter">{{ 'Letter'|trans }}</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" id="radioA4" type="radio" name="invoice_document_format" value="A4" {% if params.invoice_document_format == 'A4' %}checked{% endif %}>
                            <label class="form-check-label" for="radioA4">{{ 'A4'|trans }}</label>
                        </div>
                    </div>
                    <div class="col-lg-6">
                        <label class="form-check form-switch form-switch-2">
                            <input class="form-check-input" type="checkbox" name="invoice_accessible_from_hash" value="1" {% if params.invoice_accessible_from_hash == '1' %}checked{% endif %}>
                            <span class="form-check-label">{{ 'Allow Access Without Authentication Using Invoice Hash'|trans }}</span>
                        </label>
                        <small class="form-hint">{{ "If enabled, invoices can be accessed by anyone who knows the invoice hash. The hash is a 30-60 character random hex string and is unfeasible to brute-force, but the link exposes the buyer's name, email, full address, phone, country and company/VAT. Use the lifetime setting below to automatically expire links."|trans }}</small>

                        <div class="mt-3">
                            <label class="form-label" for="invoice_hash_lifetime_days">{{ 'Invoice hash lifetime (days)'|trans }}</label>
                            <input class="form-control" type="number" min="0" id="invoice_hash_lifetime_days" name="invoice_hash_lifetime_days" value="{{ params.invoice_hash_lifetime_days|default('90') }}">
                            <small class="form-hint">{{ 'Number of days an invoice hash link remains valid after issue. Set to 0 to never expire. The lifetime is automatically extended when the invoice email is re-sent.'|trans }}</small>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body border-top">
                <h4 class="card-title mb-2">{{ 'Refunds Settings'|trans }}</h4>
                <p class="text-secondary mb-3">{{ 'Choose how refund documents are generated when a paid invoice is reversed.'|trans }}</p>
                <label class="form-label d-block">{{ 'Refunds Logic'|trans }}</label>
                <div class="d-flex flex-column gap-2">
                    <div class="form-check">
                        <input class="form-check-input" id="radioNegativeInvoice" type="radio" name="invoice_refund_logic" value="negative_invoice" {% if params.invoice_refund_logic == 'negative_invoice' %}checked{% endif %}>
                        <label class="form-check-label" for="radioNegativeInvoice">{{ 'Negative Invoice - Generate refund invoice continuing paid invoices numbering'|trans }}</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" id="radioCreditNote" type="radio" name="invoice_refund_logic" value="credit_note" {% if params.invoice_refund_logic == 'credit_note' %}checked{% endif %}>
                        <label class="form-check-label" for="radioCreditNote">{{ 'Credit Note - Generate credit note with unique numbering'|trans }}</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" id="radioInvoiceRefundLogic" type="radio" name="invoice_refund_logic" value="manual" {% if params.invoice_refund_logic == 'manual' %}checked{% endif %}>
                        <label class="form-check-label" for="radioInvoiceRefundLogic">{{ 'Manual - No actions performed'|trans }}</label>
                    </div>
                </div>
            </div>
            <div class="card-body border-top">
                <h4 class="card-title mb-2">{{ 'Add Funds Settings'|trans }}</h4>
                <p class="text-secondary mb-3">{{ 'Set optional minimum and maximum invoice amounts for client add-funds requests.'|trans }}</p>
                <div class="row g-3">
                    <div class="col-lg-6">
                        <label class="form-label" for="funds_min_amount">{{ 'Minimum Amount for Add Funds Invoice'|trans }}</label>
                        <input class="form-control" id="funds_min_amount" type="text" name="funds_min_amount" value="{{ params.funds_min_amount }}" placeholder="{{ 'Unlimited'|trans }}">
                    </div>
                    <div class="col-lg-6">
                        <label class="form-label" for="funds_max_amount">{{ 'Maximum Amount for Add Funds Invoice'|trans }}</label>
                        <input class="form-control" id="funds_max_amount" type="text" name="funds_max_amount" value="{{ params.funds_max_amount }}" placeholder="{{ 'Unlimited'|trans }}">
                    </div>
                </div>
            </div>
            <div class="card-footer text-end">
                <button class="btn btn-primary" type="submit">{{ 'Update Settings'|trans }}</button>
            </div>
        </form>
    </div>
{% endblock %}
