<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'admin_menu', 'elementsell_billing_invoices_register_menu', 21 );

/**
 * @return void
 */
function elementsell_billing_invoices_register_menu() {
    add_submenu_page(
        null,
        'Faktury za ElementSell',
        'Faktury za ElementSell',
        'manage_options',
        'elementsell-billing-invoices',
        'elementsell_render_billing_invoices_page'
    );
}

/**
 * @return void
 */
function elementsell_render_billing_invoices_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    elementsell_billing_ensure_tables();
    elementsell_render_admin_styles();

    $year  = isset( $_GET['billing_year'] ) ? (int) $_GET['billing_year'] : 0;
    $month = isset( $_GET['billing_month'] ) ? (int) $_GET['billing_month'] : 0;
    $kind  = isset( $_GET['billing_kind'] ) ? sanitize_key( wp_unslash( (string) $_GET['billing_kind'] ) ) : '';

    $years    = elementsell_billing_get_invoice_years();
    $invoices = elementsell_billing_get_invoices_filtered(
        [
            'year'         => $year,
            'month'        => $month,
            'invoice_kind' => $kind,
            'limit'        => 200,
        ]
    );

    $month_labels = [
        1  => 'Leden',
        2  => 'Únor',
        3  => 'Březen',
        4  => 'Duben',
        5  => 'Květen',
        6  => 'Červen',
        7  => 'Červenec',
        8  => 'Srpen',
        9  => 'Září',
        10 => 'Říjen',
        11 => 'Listopad',
        12 => 'Prosinec',
    ];

    ?>
    <div class="wrap elementsell-admin elementsell-billing-invoices-page">
        <p style="margin-bottom:16px;">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=elementsell' ) ); ?>">&larr; Zpět na ElementSell</a>
        </p>

        <h1>Faktury za ElementSell</h1>
        <p class="description">Faktury vystavené po stržení měsíčního poplatku za službu ElementSell.</p>

        <div class="elementsell-invoices-toolbar">
        <form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" class="elementsell-billing-invoices-filter">
            <input type="hidden" name="page" value="elementsell-billing-invoices">

            <label for="elementsell-billing-year">Rok</label>
            <select id="elementsell-billing-year" name="billing_year">
                <option value="0">Vše</option>
                <?php foreach ( $years as $invoice_year ) : ?>
                    <option value="<?php echo esc_attr( (string) $invoice_year ); ?>"<?php selected( $year, $invoice_year ); ?>>
                        <?php echo esc_html( (string) $invoice_year ); ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <label for="elementsell-billing-month">Měsíc</label>
            <select id="elementsell-billing-month" name="billing_month">
                <option value="0">Vše</option>
                <?php foreach ( $month_labels as $month_number => $month_label ) : ?>
                    <option value="<?php echo esc_attr( (string) $month_number ); ?>"<?php selected( $month, $month_number ); ?>>
                        <?php echo esc_html( $month_label ); ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <label for="elementsell-billing-kind">Typ</label>
            <select id="elementsell-billing-kind" name="billing_kind">
                <option value="">Vše</option>
                <option value="fee"<?php selected( $kind, 'fee' ); ?>>Měsíční poplatek</option>
                <option value="card_verification"<?php selected( $kind, 'card_verification' ); ?>>Ověření karty</option>
            </select>

            <button type="submit" class="button">Filtrovat</button>
            <a class="button button-link" href="<?php echo esc_url( elementsell_get_billing_invoices_admin_url() ); ?>">Zrušit filtr</a>
        </form>
        <?php
        elementsell_render_invoices_zip_download_button(
            'elementsell_download_billing_invoices_zip',
            [
                'billing_year'  => $year,
                'billing_month' => $month,
                'billing_kind'  => $kind,
            ],
            empty( $invoices )
        );
        ?>
        </div>

        <div class="elementsell-admin-card" style="margin-top:20px;">
            <?php if ( empty( $invoices ) ) : ?>
                <p class="elementsell-admin-empty">Žádné faktury pro zvolený filtr.</p>
            <?php else : ?>
                <table class="widefat striped">
                    <thead>
                        <tr>
                            <th>Číslo</th>
                            <th>Období</th>
                            <th>Popis</th>
                            <th>Částka</th>
                            <th>Zaplaceno</th>
                            <th>PDF</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $invoices as $invoice ) : ?>
                            <?php
                            $period_label = '—';

                            if ( ! empty( $invoice->billing_year ) && ! empty( $invoice->billing_month ) ) {
                                $period_label = sprintf( '%02d/%04d', (int) $invoice->billing_month, (int) $invoice->billing_year );
                            } elseif ( (string) $invoice->invoice_kind === 'card_verification' ) {
                                $period_label = 'Ověření karty';
                            }
                            ?>
                            <tr>
                                <td><?php echo esc_html( $invoice->invoice_number ); ?></td>
                                <td><?php echo esc_html( $period_label ); ?></td>
                                <td><?php echo esc_html( $invoice->description ); ?></td>
                                <td><?php echo esc_html( elementsell_format_invoice_amount( (float) $invoice->amount ) ); ?></td>
                                <td><?php echo esc_html( mysql2date( 'j. n. Y', $invoice->paid_date, false ) ); ?></td>
                                <td>
                                    <?php if ( ! empty( $invoice->pdf_path ) && file_exists( $invoice->pdf_path ) ) : ?>
                                        <a href="<?php echo esc_url( elementsell_billing_get_invoice_download_url( (int) $invoice->id ) ); ?>">Stáhnout</a>
                                    <?php else : ?>
                                        —
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
    <?php
}
