<?php

defined( 'ABSPATH' ) or exit;

$transactions = pwlpar_get_transactions( array(
    'user_id' => $member->get_id(),
    'status__not_in' => array( 'trash' ),
) );

?>
<table class="pwlpara-table">
    <thead>
        <tr>
            <th><?php esc_html_e( 'Date', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Note', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th style="white-space: nowrap; text-align: right;"><?php esc_html_e( 'Debit (-)', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th style="white-space: nowrap; text-align: right;"><?php esc_html_e( 'Credit (+)', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th><?php esc_html_e( 'Status', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></th>
            <th style="text-align: right;"><?php esc_html_e( 'Balance', '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( $transactions ) ) {
                $running_balance = $member->get_balance( 'edit' );

                foreach ( $transactions as $transaction ) {
                    ?>
                    <tr class="pwlpara-member-transactions-table-transaction" data-id="<?php echo esc_attr( $transaction->get_id() ); ?>">
                        <td style="white-space: nowrap;"><?php echo esc_html( $transaction->get_create_date() ); ?></td>
                        <td>
                            <?php
                                $url = '';
                                if ( !empty( $transaction->get_earned_order_id() ) ) {
                                    $url = get_edit_post_link( $transaction->get_earned_order_id() );
                                } else if ( !empty( $transaction->get_redeemed_reward_id() ) ) {
                                    $reward = pwlpar_get_reward( $transaction->get_redeemed_reward_id() );
                                    if ( is_a( $reward, 'PWLPAR_Reward' ) && !empty( $reward->get_offer_id() ) ) {
                                        $url = pwlpara_admin_url( 'offers', array( 'id' => $reward->get_offer_id() ) );
                                    }
                                }

                                if ( !empty( $url ) ) {
                                    ?>
                                    <a href="<?php echo esc_attr( $url ); ?>"><?php echo esc_html( $transaction->get_note() ); ?></a>
                                    <?php
                                } else {
                                    echo esc_html( $transaction->get_note() );
                                }
                            ?>
                        </td>
                        <td style="white-space: nowrap; text-align: right;">
                            <?php
                                if ( $transaction->get_points( 'edit' ) <= 0 ) {
                                    echo esc_html( pwlpar_format_points( $transaction->get_points( 'edit' ) * -1 ) );
                                }
                            ?>
                        </td>
                        <td style="white-space: nowrap; text-align: right;">
                            <?php
                                if ( $transaction->get_points( 'edit' ) > 0 ) {
                                    echo esc_html( $transaction->get_points() );
                                }
                            ?>
                        </td>
                        <td><?php echo esc_html( $transaction->get_status() ); ?></td>
                        <td style="white-space: nowrap; text-align: right;"><?php echo esc_html( pwlpar_format_points( $running_balance ) ); ?></td>
                        <td style="white-space: nowrap;">
                            <a href="#" class="pwlpara-delete-transaction-button button pwlpara-button-danger"><?php esc_html_e( 'Delete', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?></a>
                        </td>
                    </tr>
                    <?php

                    if ( $transaction->get_status( 'edit' ) != 'pending' ) {
                        $running_balance -= $transaction->get_points( 'edit' );
                    }
                }
            } else {
                ?>
                <tr>
                    <td colspan="99">
                        <?php esc_html_e( 'No activity found.', 'pw-loyalty-points-and-rewards-for-woocommerce' ); ?>
                    </td>
                </tr>
                <?php
            }
        ?>
    </tbody>
</table>
<?php
