<?php

defined( 'ABSPATH' ) or exit;

use Automattic\WooCommerce\Admin\API\Reports\Customers\Query as CustomersQuery;

if ( isset( $search ) && !empty( $search ) ) {
    $members = pwlpar_get_members( array( 'search' => $search ) );
} else {
    $members = pwlpar_get_members( array( 'include_stats' => true ) );
}

?>
<table id="pwlpara-members-table" class="pwlpara-table">
    <thead>
        <tr>
            <th><?php esc_html_e( 'Member', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Point Balance', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Total orders', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Total revenue', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Average order value', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Actions', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if ( empty( $members ) ) : ?>
            <tr>
                <td colspan="99"><?php esc_html_e( 'No members have joined. Yet!', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></td>
            </tr>
        <?php endif; ?>
        <?php foreach ( $members as $member ) : ?>
            <tr class="pwlpara-members-table-member" data-user-id="<?php echo esc_attr( $member->get_id() ); ?>" data-user-login="<?php echo esc_attr( $member->get_login() ); ?>">
                <td>
                    <a href="<?php echo esc_attr( pwlpara_admin_url( 'members', array( 'id' => $member->get_id() ) ) ); ?>"><?php
                        $name = !empty( $member->get_display_name() ) ? $member->get_display_name() : $member->get_login();
                        echo esc_html( $name );
                    ?></a>
                </td>
                <td class="pwlpara-members-table-balance"><?php echo wp_kses_post( $member->get_balance() ); ?></td>
                <td><?php echo wp_kses_post( $member->get_total_orders() ); ?></td>
                <td><?php echo wp_kses_post( $member->get_total_revenue() ); ?></td>
                <td><?php echo wp_kses_post( $member->get_average_order_value() ); ?></td>
                <td>
                    <div>
                        <a href="<?php echo esc_attr( pwlpara_admin_url( 'members', array( 'id' => $member->get_id() ) ) ); ?>" class="button button-primary"><?php esc_html_e( 'View details', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                        <a href="#" class="pwlpara-members-table-add-points-button button button-secondary"><?php esc_html_e( 'Add points', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                        <a href="#" class="pwlpara-members-table-subtract-points-button button button-secondary"><?php esc_html_e( 'Subtract points', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php
