<?php

defined( 'ABSPATH' ) or exit;

?>
<div class="pwlpara-points-metabox order-attribution-metabox">
<?php
    if ( pwlpar_user_is_rewards_member( $order->get_user_id( 'edit' ) ) ) {
        ?>
        <h4>
            <?php esc_html_e( 'Order points', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
        </h4>
        <?php
            $transactions_by_status = array();
            $transactions = pwlpar_get_transactions( array( 'earned_order_id' => $order->get_id() ) );
            foreach ( $transactions as $transaction ) {
                if ( !isset( $transactions_by_status[ $transaction->get_status( 'edit' ) ] ) ) {
                    $transactions_by_status[ $transaction->get_status( 'edit' ) ] = 0;
                }

                $transactions_by_status[ $transaction->get_status( 'edit' ) ] += $transaction->get_points( 'edit' );
            }
            if ( empty( $transactions_by_status ) ) {
                $transactions_by_status['active'] = 0;
            }

            foreach ( $transactions_by_status as $status => $points ) {
                ?>
                <span class="pwlpara-points-earned">
                    <?php
                        echo esc_html( pwlpar_format_points( $points ) );

                        if ( $status != 'active' ) {
                            echo esc_html( " ($status)" );
                        }
                    ?>
                </span>
                <?php
            }
        ?>
        <h4><?php esc_html_e( 'Current point balance', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></h4>
        <span class="pwlpara-points-balance">
            <?php echo esc_html( pwlpar_get_member_point_balance( $order->get_user_id( 'edit' ), true ) ); ?>
        </span>

        <div style="margin-top: 1em;">
            <?php
                $args = array(
                    'user_id' => $order->get_user_id( 'edit' ),
                    'pwlpar-view-transactions' => 'true',
                );

                printf(
                    '<a href="%s">%s</a>',
                    esc_url( pwlpara_admin_url( 'members', array( 'id' => $order->get_user_id( 'edit' ) ) ) ),
                    esc_html__( 'View details', 'pw-loyalty-points-and-rewards-for-woocommerce' ) . ' &rarr;'
                );
            ?>
        </div>
        <?php
    } else {
        ?>
        <h4><?php esc_html_e( 'Status', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></h4>
        <span class="pwlpara-not-enrolled">
            <?php esc_html_e( 'Not enrolled.', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
        </span>
        <div style="margin-top: 1em;">
            <?php
                printf(
                    '<a href="%s#pwlpar-member-section">%s</a>',
                    esc_url( add_query_arg( 'user_id', $order->get_user_id( 'edit' ), admin_url( 'user-edit.php' ) ) ),
                    esc_html__( 'View profile', 'pw-loyalty-points-and-rewards-for-woocommerce' ) . ' &rarr;'
                );
            ?>
        </div>
        <?php
    }
?>
</div>
<?php