<?php
/**
 * @var array  $rows
 * @var int    $total
 * @var string $status
 * @var int    $invoiceId
 * @var string $msg
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

use Itdatex\Ausgangsrechnung\Admin\Menu;

// 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.
?>
<div class="wrap">
    <h1 class="wp-heading-inline">
        <?php esc_html_e( 'Ausgegangene Rechnungen', 'itdatex-e-invoice-output' ); ?>
        <span class="title-count"><?php echo esc_html( sprintf( '(%d)', $total ) ); ?></span>
    </h1>
    <a href="<?php echo esc_url( admin_url( 'admin.php?page=' . Menu::SLUG_CREATE ) ); ?>" class="page-title-action">
        <?php esc_html_e( 'Neue Rechnung', 'itdatex-e-invoice-output' ); ?>
    </a>
    <hr class="wp-header-end">

    <?php if ( $status === 'issued' && $invoiceId > 0 ) : ?>
        <div class="notice notice-success is-dismissible"><p>
            <?php echo esc_html( sprintf(
                /* translators: %d: numerische Rechnungs-ID in der gemeinsamen wp_reing_invoices-Tabelle */
                __( 'Rechnung #%d erfolgreich ausgestellt.', 'itdatex-e-invoice-output' ),
                (int) $invoiceId
            ) ); ?>
        </p></div>
    <?php elseif ( $status === 'failed' ) : ?>
        <div class="notice notice-error is-dismissible"><p>
            <?php echo esc_html(
                $msg !== ''
                    /* translators: %s: technischer Fehlertext vom Issue-Service (z.B. unsupported_format:xyz) */
                    ? sprintf( __( 'Erstellung fehlgeschlagen: %s', 'itdatex-e-invoice-output' ), $msg )
                    : __( 'Erstellung fehlgeschlagen.', 'itdatex-e-invoice-output' )
            ); ?>
        </p></div>
    <?php elseif ( $status === 'duplicate' ) : ?>
        <div class="notice notice-warning is-dismissible"><p>
            <?php esc_html_e( 'Duplikat erkannt (gleicher Inhalt wie eine bestehende Rechnung).', 'itdatex-e-invoice-output' ); ?>
        </p></div>
    <?php endif; ?>

    <?php if ( $rows === [] ) : ?>
        <p><?php esc_html_e( 'Noch keine ausgehenden Rechnungen vorhanden.', 'itdatex-e-invoice-output' ); ?></p>
    <?php else : ?>
        <table class="wp-list-table widefat striped">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Rechnungs-Nr.', 'itdatex-e-invoice-output' ); ?></th>
                    <th><?php esc_html_e( 'Datum', 'itdatex-e-invoice-output' ); ?></th>
                    <th><?php esc_html_e( 'Faellig', 'itdatex-e-invoice-output' ); ?></th>
                    <th><?php esc_html_e( 'Kaeufer', 'itdatex-e-invoice-output' ); ?></th>
                    <th style="text-align:right"><?php esc_html_e( 'Brutto', 'itdatex-e-invoice-output' ); ?></th>
                    <th><?php esc_html_e( 'Validierung', 'itdatex-e-invoice-output' ); ?></th>
                    <th><?php esc_html_e( 'Zahlung', 'itdatex-e-invoice-output' ); ?></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ( $rows as $r ) :
                $url = add_query_arg(
                    [ 'page' => Menu::SLUG_DETAIL, 'id' => (int) $r->id ],
                    admin_url( 'admin.php' )
                );
                $statusBadge = match ( (string) $r->validation_status ) {
                    'valid'     => '<span style="color:#2271b1">valid</span>',
                    'warnings'  => '<span style="color:#dba617">warnings</span>',
                    'invalid'   => '<span style="color:#d63638">invalid</span>',
                    default     => '<span>—</span>',
                };
                ?>
                <tr>
                    <td><a href="<?php echo esc_url( $url ); ?>"><strong><?php echo esc_html( (string) ( $r->invoice_number ?: '#' . $r->id ) ); ?></strong></a></td>
                    <td><?php echo esc_html( (string) ( $r->issue_date ?: '—' ) ); ?></td>
                    <td><?php echo esc_html( (string) ( $r->due_date ?: '—' ) ); ?></td>
                    <td><?php echo esc_html( (string) ( $r->buyer_name ?: '—' ) ); ?></td>
                    <td style="text-align:right; white-space:nowrap;">
                        <?php echo esc_html( number_format( (float) $r->total_gross, 2, ',', '.' ) ); ?>
                        <?php echo esc_html( (string) $r->currency ); ?>
                    </td>
                    <td><?php echo wp_kses_post( $statusBadge ); ?></td>
                    <td><?php echo esc_html( (string) $r->payment_status ); ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php if ( $total > count( $rows ) ) : ?>
            <p class="description">
                <?php echo esc_html( sprintf(
                    /* translators: 1: Anzahl angezeigter Zeilen, 2: Gesamtanzahl in der Datenbank */
                    __( 'Zeige %1$d von %2$d. Aelter via direkte DB-Abfrage.', 'itdatex-e-invoice-output' ),
                    count( $rows ), (int) $total
                ) ); ?>
            </p>
        <?php endif; ?>
    <?php endif; ?>
</div>
