<?php
/**
 * @var string              $action_url
 * @var string              $notice
 * @var array<string,string> $seller
 * @var array<string,string> $payment
 * @var array<string,string> $missing    Schluessel = Feldname, Wert = Fehlertext
 * @var array{pattern:string,counter:int,yearly_reset:bool,next_preview:string} $numbering
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

use Itdatex\Ausgangsrechnung\Admin\Pages\SettingsPage;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// Rationale: view files are lexically included from Admin\Pages\* renderers,
// so every $var below is a file-local template variable, not a real global.

$miss    = static fn( string $key ): bool   => isset( $missing[ $key ] );
$errCls  = static fn( string $key ): string => isset( $missing[ $key ] ) ? ' itdatex-required-missing' : '';
$errAria = static fn( string $key ): string => isset( $missing[ $key ] ) ? 'true' : 'false';
?>
<div class="wrap">
    <h1><?php esc_html_e( 'Ausgangsrechnung — Einstellungen', 'itdatex-e-invoice-output' ); ?></h1>

    <?php if ( $notice !== '' ) : ?>
        <div class="notice notice-success is-dismissible"><p><?php echo esc_html( $notice ); ?></p></div>
    <?php endif; ?>

    <?php if ( $missing !== [] ) : ?>
        <div class="notice notice-error">
            <p><strong><?php esc_html_e( 'KoSIT-Pflichtfelder fehlen:', 'itdatex-e-invoice-output' ); ?></strong>
               <?php esc_html_e( 'Ohne diese Felder schlaegt die XRechnung-3.0-Tiefenvalidierung fehl.', 'itdatex-e-invoice-output' ); ?></p>
            <ul style="margin:0 1.5em 0.5em; list-style:disc;">
                <?php foreach ( $missing as $err ) : ?>
                    <li><?php echo esc_html( $err ); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php else : ?>
        <div class="notice notice-success inline" style="margin:1em 0">
            <p><?php esc_html_e(
                'Alle KoSIT-/XRechnung-Pflichtfelder gepflegt — Rechnungen sollten die Tiefenvalidierung bestehen.',
                'itdatex-e-invoice-output'
            ); ?></p>
        </div>
    <?php endif; ?>

    <p><?php esc_html_e(
        'Diese Verkaeufer-Daten landen auf jeder ausgehenden Rechnung. Felder mit * sind KoSIT-/XRechnung-Pflicht.',
        'itdatex-e-invoice-output'
    ); ?></p>

    <form method="post" action="<?php echo esc_url( $action_url ); ?>">
        <?php wp_nonce_field( SettingsPage::ACTION, SettingsPage::NONCE ); ?>

        <h2><?php esc_html_e( 'Verkaeufer (Absender)', 'itdatex-e-invoice-output' ); ?></h2>
        <table class="form-table" role="presentation">
            <tr>
                <th><label for="seller_name"><?php esc_html_e( 'Firmenname', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[name]" id="seller_name" type="text" class="regular-text<?php echo esc_attr( $errCls( 'name' ) ); ?>"
                           value="<?php echo esc_attr( $seller['name'] ?? '' ); ?>" required aria-invalid="<?php echo esc_attr( $errAria( 'name' ) ); ?>"></td>
            </tr>
            <tr>
                <th><label for="seller_vat"><?php esc_html_e( 'USt-IdNr.', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[vat_number]" id="seller_vat" type="text" class="regular-text<?php echo esc_attr( $errCls( 'vat_number' ) ); ?>"
                           value="<?php echo esc_attr( $seller['vat_number'] ?? '' ); ?>"
                           placeholder="DE123456789"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'vat_number' ) ); ?>">
                    <p class="description"><?php esc_html_e(
                        'BR-DE-16: Bei VAT-Kategorien S/Z/E/AE/K/G/L/M muss BT-31 (USt-IdNr.), BT-32 (Steuernummer) oder BT-30 (Handelsregister) gesetzt sein.',
                        'itdatex-e-invoice-output'
                    ); ?></p></td>
            </tr>
            <tr>
                <th><label for="seller_address"><?php esc_html_e( 'Adresse', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><textarea name="seller[address]" id="seller_address" rows="3" class="regular-text<?php echo esc_attr( $errCls( 'address' ) ); ?>"
                              placeholder="Strasse + Hausnummer&#10;Adresszusatz"
                              required aria-invalid="<?php echo esc_attr( $errAria( 'address' ) ); ?>"><?php echo esc_textarea( $seller['address'] ?? '' ); ?></textarea>
                    <p class="description"><?php esc_html_e( 'Eine Zeile pro Adress-Bestandteil.', 'itdatex-e-invoice-output' ); ?></p></td>
            </tr>
            <tr>
                <th><label for="seller_postal"><?php esc_html_e( 'PLZ / Stadt', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td>
                    <input name="seller[postal_code]" id="seller_postal" type="text" class="small-text<?php echo esc_attr( $errCls( 'postal_code' ) ); ?>"
                           value="<?php echo esc_attr( $seller['postal_code'] ?? '' ); ?>" placeholder="10115"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'postal_code' ) ); ?>">
                    <input name="seller[city]" type="text" class="regular-text<?php echo esc_attr( $errCls( 'city' ) ); ?>"
                           value="<?php echo esc_attr( $seller['city'] ?? '' ); ?>" placeholder="Berlin"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'city' ) ); ?>">
                </td>
            </tr>
            <tr>
                <th><label for="seller_country"><?php esc_html_e( 'Land (ISO-2)', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[country_code]" id="seller_country" type="text" class="small-text<?php echo esc_attr( $errCls( 'country_code' ) ); ?>"
                           maxlength="2" value="<?php echo esc_attr( $seller['country_code'] ?? 'DE' ); ?>"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'country_code' ) ); ?>"></td>
            </tr>
            <tr>
                <th><label for="seller_contact_name"><?php esc_html_e( 'Kontakt-Person', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[contact_name]" id="seller_contact_name" type="text" class="regular-text<?php echo esc_attr( $errCls( 'contact_name' ) ); ?>"
                           value="<?php echo esc_attr( $seller['contact_name'] ?? '' ); ?>"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'contact_name' ) ); ?>">
                    <p class="description"><?php esc_html_e( 'BR-DE-5: Verkaeufer-Kontaktperson (BT-41).', 'itdatex-e-invoice-output' ); ?></p></td>
            </tr>
            <tr>
                <th><label for="seller_contact_email"><?php esc_html_e( 'Kontakt-E-Mail', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[contact_email]" id="seller_contact_email" type="email" class="regular-text<?php echo esc_attr( $errCls( 'contact_email' ) ); ?>"
                           value="<?php echo esc_attr( $seller['contact_email'] ?? '' ); ?>"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'contact_email' ) ); ?>">
                    <p class="description"><?php esc_html_e(
                        'BR-DE-7: Verkaeufer-E-Mail (BT-43) — BR-DE-28: muss gueltiges Format haben.',
                        'itdatex-e-invoice-output'
                    ); ?></p></td>
            </tr>
            <tr>
                <th><label for="seller_contact_phone"><?php esc_html_e( 'Kontakt-Telefon', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td><input name="seller[contact_phone]" id="seller_contact_phone" type="text" class="regular-text<?php echo esc_attr( $errCls( 'contact_phone' ) ); ?>"
                           value="<?php echo esc_attr( $seller['contact_phone'] ?? '' ); ?>"
                           placeholder="+49 30 123456"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'contact_phone' ) ); ?>">
                    <p class="description"><?php esc_html_e( 'BR-DE-6: Verkaeufer-Telefon (BT-42), mindestens 3 Ziffern (BR-DE-27).', 'itdatex-e-invoice-output' ); ?></p></td>
            </tr>
            <tr>
                <th><label for="seller_eas_scheme"><?php esc_html_e( 'Elektronische Adresse', 'itdatex-e-invoice-output' ); ?> *</label></th>
                <td>
                    <select name="seller[electronic_address_scheme]" id="seller_eas_scheme" class="<?php echo esc_attr( trim( $errCls( 'electronic_address_scheme' ) ) ); ?>" required aria-invalid="<?php echo esc_attr( $errAria( 'electronic_address_scheme' ) ); ?>">
                        <?php
                        $cur = strtoupper( $seller['electronic_address_scheme'] ?? '' );
                        $eas_codes = [
                            'EM'   => 'EM — E-Mail (Peppol BIS)',
                            '0204' => '0204 — Leitweg-ID (DE B2G)',
                            '0088' => '0088 — GLN (Global Location Number)',
                            '9930' => '9930 — DE: USt-IdNr.',
                            '9931' => '9931 — DE: Steuernummer',
                        ];
                        if ( $cur !== '' && ! isset( $eas_codes[ $cur ] ) ) {
                            $eas_codes[ $cur ] = $cur . ' — ' . __( '(eigener Code)', 'itdatex-e-invoice-output' );
                        }
                        if ( $cur === '' ) {
                            echo '<option value="">' . esc_html__( '— bitte waehlen —', 'itdatex-e-invoice-output' ) . '</option>';
                        }
                        foreach ( $eas_codes as $code => $label ) :
                        ?>
                            <option value="<?php echo esc_attr( $code ); ?>" <?php selected( $cur, $code ); ?>><?php echo esc_html( $label ); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <input name="seller[electronic_address_value]" type="text" class="regular-text<?php echo esc_attr( $errCls( 'electronic_address_value' ) ); ?>"
                           value="<?php echo esc_attr( $seller['electronic_address_value'] ?? '' ); ?>"
                           placeholder="<?php esc_attr_e( 'Wert (z.B. rechnungen@firma.de oder 04011000-1234512345-06)', 'itdatex-e-invoice-output' ); ?>"
                           required aria-invalid="<?php echo esc_attr( $errAria( 'electronic_address_value' ) ); ?>">
                    <p class="description">
                        <?php esc_html_e(
                            'PEPPOL-EN16931-R020: jeder Verkaeufer braucht eine eindeutige elektronische Adresse. Codes nach CEF/EAS-Liste.',
                            'itdatex-e-invoice-output'
                        ); ?>
                    </p>
                </td>
            </tr>
        </table>

        <h2><?php esc_html_e( 'Default-Zahlungsdaten', 'itdatex-e-invoice-output' ); ?></h2>
        <table class="form-table" role="presentation">
            <tr>
                <th><label for="payment_iban">IBAN</label></th>
                <td><input name="payment[iban]" id="payment_iban" type="text" class="regular-text"
                           value="<?php echo esc_attr( $payment['iban'] ?? '' ); ?>"
                           placeholder="DE89370400440532013000"></td>
            </tr>
            <tr>
                <th><label for="payment_bic">BIC</label></th>
                <td><input name="payment[bic]" id="payment_bic" type="text" class="regular-text"
                           value="<?php echo esc_attr( $payment['bic'] ?? '' ); ?>"
                           placeholder="COBADEFFXXX"></td>
            </tr>
            <tr>
                <th><label for="payment_terms"><?php esc_html_e( 'Zahlungsbedingungen', 'itdatex-e-invoice-output' ); ?></label></th>
                <td><textarea name="payment[terms]" id="payment_terms" rows="2" class="large-text"
                              placeholder="<?php esc_attr_e( 'Zahlbar innerhalb von 14 Tagen ohne Abzug.', 'itdatex-e-invoice-output' ); ?>"><?php echo esc_textarea( $payment['terms'] ?? '' ); ?></textarea></td>
            </tr>
        </table>

        <h2><?php esc_html_e( 'Rechnungsnummern', 'itdatex-e-invoice-output' ); ?></h2>
        <table class="form-table" role="presentation">
            <tr>
                <th><label for="numbering_pattern"><?php esc_html_e( 'Muster', 'itdatex-e-invoice-output' ); ?></label></th>
                <td>
                    <input name="numbering[pattern]" id="numbering_pattern" type="text" class="regular-text"
                           value="<?php echo esc_attr( $numbering['pattern'] ); ?>"
                           placeholder="{YYYY}-{####}">
                    <p class="description">
                        <?php esc_html_e(
                            'Platzhalter: {YYYY} 4-stelliges Jahr, {YY} 2-stelliges Jahr, {MM} Monat, {DD} Tag, {####} laufender Zaehler (Padding = Anzahl # Zeichen, z.B. {####} → 0001).',
                            'itdatex-e-invoice-output'
                        ); ?><br>
                        <?php
                        /* translators: %s: rendered next-number preview like "2026-0001" */
                        echo esc_html( sprintf( __( 'Naechster Vorschlag: %s', 'itdatex-e-invoice-output' ), $numbering['next_preview'] ) );
                        ?>
                    </p>
                </td>
            </tr>
            <tr>
                <th><label for="numbering_counter"><?php esc_html_e( 'Aktueller Zaehler', 'itdatex-e-invoice-output' ); ?></label></th>
                <td>
                    <input name="numbering[counter]" id="numbering_counter" type="number" min="1" step="1" class="small-text"
                           value="<?php echo esc_attr( (string) $numbering['counter'] ); ?>">
                    <p class="description">
                        <?php esc_html_e(
                            'Zaehlerstand der naechsten auto-generierten Nummer. Nur anpassen beim Umstieg aus einem bestehenden Rechnungssystem, damit die Nummern nahtlos weiterlaufen.',
                            'itdatex-e-invoice-output'
                        ); ?>
                    </p>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Jahres-Reset', 'itdatex-e-invoice-output' ); ?></th>
                <td>
                    <label>
                        <input name="numbering[yearly_reset]" type="checkbox" value="1" <?php checked( $numbering['yearly_reset'] ); ?>>
                        <?php esc_html_e( 'Zaehler bei Jahreswechsel automatisch zurueck auf 1 setzen', 'itdatex-e-invoice-output' ); ?>
                    </label>
                </td>
            </tr>
        </table>

        <?php submit_button( __( 'Speichern', 'itdatex-e-invoice-output' ) ); ?>
    </form>
</div>
