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

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

/**
 * @return void
 */
function elementsell_landing_leads_register_menu() {
    if ( ! function_exists( 'elementsell_is_landing_enabled' ) || ! elementsell_is_landing_enabled() ) {
        return;
    }

    add_submenu_page(
        null,
        'Landing poptávky',
        'Landing poptávky',
        'manage_options',
        'elementsell-landing-leads',
        'elementsell_render_landing_leads_page'
    );
}

/**
 * @return string
 */
function elementsell_get_landing_leads_admin_url() {
    return admin_url( 'admin.php?page=elementsell-landing-leads' );
}

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

    if ( ! function_exists( 'elementsell_is_landing_enabled' ) || ! elementsell_is_landing_enabled() ) {
        wp_die( esc_html__( 'Tato stránka je dostupná jen na elementsell.cz.', 'platebni-formular' ) );
    }

    elementsell_render_admin_styles();

    $leads = function_exists( 'elementsell_get_landing_leads' ) ? elementsell_get_landing_leads( 200 ) : [];
    $count = function_exists( 'elementsell_get_landing_leads_count' ) ? elementsell_get_landing_leads_count() : 0;
    ?>
    <div class="wrap elementsell-admin">
        <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>Landing poptávky</h1>
        <p class="description">Poptávky z kontaktního formuláře na landing stránce. Celkem: <strong><?php echo esc_html( number_format_i18n( $count ) ); ?></strong></p>

        <div class="elementsell-admin-card">
            <?php if ( empty( $leads ) ) : ?>
                <p class="elementsell-admin-empty">Zatím žádné poptávky.</p>
            <?php else : ?>
                <table class="widefat striped">
                    <thead>
                        <tr>
                            <th>Datum</th>
                            <th>Jméno</th>
                            <th>Příjmení</th>
                            <th>Web</th>
                            <th>Co bude prodávat</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $leads as $lead ) : ?>
                            <tr>
                                <td><?php echo esc_html( mysql2date( 'j. n. Y H:i', (string) $lead->created_at ) ); ?></td>
                                <td><?php echo esc_html( (string) $lead->first_name ); ?></td>
                                <td><?php echo esc_html( (string) $lead->surname ); ?></td>
                                <td>
                                    <?php
                                    $website = (string) $lead->website;
                                    if ( $website !== '' ) :
                                        ?>
                                        <a href="<?php echo esc_url( $website ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $website ); ?></a>
                                    <?php else : ?>
                                        —
                                    <?php endif; ?>
                                </td>
                                <td><?php echo esc_html( (string) $lead->product_intent ); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
    <?php
}
