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

function elementsell_render_email_log() {

    global $wpdb;

    $table = $wpdb->prefix . 'elementsell_email_log';

    echo '<div class="elementsell-admin-card" id="elementsell-email-log">';
    echo '<h2 class="elementsell-admin-section-title">Záznam odeslaných e-mailů</h2>';

    $notice = function_exists( 'elementsell_email_get_deliverability_notice' )
        ? elementsell_email_get_deliverability_notice()
        : '';

    if ( $notice !== '' ) {
        echo '<div class="notice notice-warning inline" style="margin:0 0 16px;"><p>' . esc_html( $notice ) . '</p></div>';
    }

    $table_exists = $wpdb->get_var(
        $wpdb->prepare( 'SHOW TABLES LIKE %s', $table )
    );

    if ( $table_exists !== $table ) {
        echo '<p>Tabulka záznamu e-mailů zatím neexistuje.</p>';
        echo '</div>';
        return;
    }

    $emails = $wpdb->get_results(
        "SELECT *
         FROM {$table}
         ORDER BY id DESC
         LIMIT 100"
    );

    echo '<div class="elementsell-table-scroll elementsell-table-scroll--compact">';
    echo '<table class="widefat striped">';
    echo '<thead>';
    echo '<tr>';
    echo '<th>Datum</th>';
    echo '<th>Stav</th>';
    echo '<th>From</th>';
    echo '<th>E-mailová adresa</th>';
    echo '<th>Předmět</th>';
    echo '<th width="120">Akce</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';

    if ( empty( $emails ) ) {
        echo '<tr><td colspan="6">Zatím nebyl odeslán žádný e-mail.</td></tr>';
    } else {
        foreach ( $emails as $mail ) {
            $mail_id   = (int) $mail->id;
            $status    = isset( $mail->status ) ? (string) $mail->status : 'sent';
            $is_failed = $status === 'failed';
            $from      = isset( $mail->from_email ) ? (string) $mail->from_email : '';

            echo '<tr>';
            echo '<td>' . esc_html(
                mysql2date(
                    'j. n. Y, H:i',
                    $mail->created_at ?? $mail->sent_at ?? ''
                )
            ) . '</td>';
            echo '<td>';

            if ( $is_failed ) {
                echo '<span style="color:#b32d2e;font-weight:600;">Selhalo</span>';
            } else {
                echo '<span style="color:#1d7a46;font-weight:600;">Odesláno</span>';
            }

            echo '</td>';
            echo '<td>';

            if ( $from !== '' ) {
                echo esc_html( $from );
            } else {
                echo '<span class="description">—</span>';
            }

            echo '</td>';
            echo '<td>' . esc_html( $mail->email ?? '' ) . '</td>';
            echo '<td>' . esc_html( $mail->subject ?? '' ) . '</td>';
            echo '<td>';
            echo '<button class="button button-small elementsell-toggle-email" data-email="' . esc_attr( (string) $mail_id ) . '">';
            echo 'Zobrazit';
            echo '</button>';
            echo '</td>';
            echo '</tr>';

            echo '<tr id="elementsell-email-' . esc_attr( (string) $mail_id ) . '" style="display:none;">';
            echo '<td colspan="6" style="background:#fff;padding:20px 25px;">';
            echo wp_kses_post( $mail->body ?? '' );
            echo '</td>';
            echo '</tr>';
        }
    }

    echo '</tbody>';
    echo '</table>';
    echo '</div>';
    echo '</div>';

    ?>

    <script>
    document.querySelectorAll('.elementsell-toggle-email').forEach(function(button){

        button.addEventListener('click', function(){

            const id = this.dataset.email;
            const row = document.getElementById('elementsell-email-' + id);

            if (row.style.display === 'none') {
                row.style.display = 'table-row';
                this.textContent = 'Skrýt';
            } else {
                row.style.display = 'none';
                this.textContent = 'Zobrazit';
            }

        });

    });
    </script>

    <?php
}
