<?php
/**
 * Admin-View: Liste empfangener Rechnungen
 *
 * @var \stdClass[] $invoices
 * @var int         $total
 * @var string      $status
 * @var int         $invoiceId
 * @var string      $msg
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

use Itdatex\Rechnungseingang\Plugin;

// Status-Notice
$notices = [
    'uploaded'     => [ 'updated',  __( 'Rechnung wurde verarbeitet.', 'itdatex-e-invoice-intake' ) ],
    'duplicate'    => [ 'warning',  __( 'Diese Datei wurde bereits einmal hochgeladen.', 'itdatex-e-invoice-intake' ) ],
    'no_file'      => [ 'error',    __( 'Keine Datei ausgewaehlt.', 'itdatex-e-invoice-intake' ) ],
    'upload_error' => [ 'error',    __( 'Upload-Fehler. Bitte erneut versuchen.', 'itdatex-e-invoice-intake' ) ],
    'parse_error'  => [ 'error',    __( 'Datei konnte nicht verarbeitet werden: ', 'itdatex-e-invoice-intake' ) . esc_html( $msg ) ],
];

$upload_url = admin_url( 'admin.php?page=itdatex-e-invoice-intake-upload' );
?>
<div class="wrap reing-wrap">
  <h1>
    <?php esc_html_e( 'itdatex E-Invoice Intake', 'itdatex-e-invoice-intake' ); ?>
    <a href="<?php echo esc_url( $upload_url ); ?>" class="page-title-action"><?php esc_html_e( 'Hochladen', 'itdatex-e-invoice-intake' ); ?></a>
  </h1>

  <?php if ( $status && isset( $notices[ $status ] ) ) :
      [ $class, $text ] = $notices[ $status ]; ?>
    <div class="notice notice-<?php echo esc_attr( $class ); ?> is-dismissible">
      <p><?php echo esc_html( $text ); ?>
        <?php if ( $invoiceId ) : ?>
          <a href="<?php echo esc_url( admin_url( 'admin.php?page=itdatex-e-invoice-intake-invoice&id=' . $invoiceId ) ); ?>">
            <?php esc_html_e( 'Anzeigen', 'itdatex-e-invoice-intake' ); ?>
          </a>
        <?php endif; ?>
      </p>
    </div>
  <?php endif; ?>

  <p class="description">
    <?php
    /* translators: %d = Anzahl der Rechnungen */
    echo esc_html( sprintf( _n( '%d Rechnung im Archiv.', '%d Rechnungen im Archiv.', $total, 'itdatex-e-invoice-intake' ), $total ) );
    ?>
  </p>

  <table class="wp-list-table widefat striped reing-table">
    <thead>
      <tr>
        <th scope="col"><?php esc_html_e( 'Eingang', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Rechnungsnr.', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Lieferant', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Datum', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Faelligkeit', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col" style="text-align:right;"><?php esc_html_e( 'Brutto', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Format', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Validierung', 'itdatex-e-invoice-intake' ); ?></th>
        <th scope="col"><?php esc_html_e( 'Status', 'itdatex-e-invoice-intake' ); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php if ( empty( $invoices ) ) : ?>
        <tr><td colspan="9">
          <em><?php esc_html_e( 'Noch keine Rechnungen empfangen.', 'itdatex-e-invoice-intake' ); ?></em>
          &nbsp;<a href="<?php echo esc_url( $upload_url ); ?>"><?php esc_html_e( 'Erste Rechnung hochladen →', 'itdatex-e-invoice-intake' ); ?></a>
        </td></tr>
      <?php else : foreach ( $invoices as $row ) :
          $detail_url = admin_url( 'admin.php?page=itdatex-e-invoice-intake-invoice&id=' . (int) $row->id );
          $gross      = is_numeric( $row->total_gross ) ? number_format( (float) $row->total_gross, 2, ',', '.' ) : '—';
          $vs_class   = [ 'valid' => 'reing-badge-valid', 'warnings' => 'reing-badge-warn', 'invalid' => 'reing-badge-error' ][ $row->validation_status ] ?? '';
          ?>
        <tr>
          <td><?php echo esc_html( mysql2date( 'd.m.Y H:i', (string) $row->created_at ) ); ?></td>
          <td><a href="<?php echo esc_url( $detail_url ); ?>"><strong><?php echo esc_html( $row->invoice_number ?: '—' ); ?></strong></a></td>
          <td><?php echo esc_html( $row->seller_name ?: '—' ); ?></td>
          <td><?php echo $row->issue_date ? esc_html( mysql2date( 'd.m.Y', (string) $row->issue_date ) ) : '—'; ?></td>
          <td><?php echo $row->due_date   ? esc_html( mysql2date( 'd.m.Y', (string) $row->due_date ) )   : '—'; ?></td>
          <td style="text-align:right;font-variant-numeric:tabular-nums;"><?php echo esc_html( $gross . ' ' . $row->currency ); ?></td>
          <td><code><?php echo esc_html( $row->format ); ?></code></td>
          <td><span class="reing-badge <?php echo esc_attr( $vs_class ); ?>"><?php echo esc_html( $row->validation_status ); ?></span></td>
          <td><?php echo esc_html( $row->payment_status ); ?></td>
        </tr>
      <?php endforeach; endif; ?>
    </tbody>
  </table>
</div>
