<div id="affwp-affiliate-dashboard-payouts" class="affwp-tab-content">

	<h4><?php _e( 'Referral Payouts', 'affiliate-wp' ); ?></h4>

	<?php
	$affiliate_id            = affwp_get_affiliate_id();
	$payouts_service_enabled = (bool) affiliate_wp()->settings->get( 'enable_payouts_service', false );

	$per_page = 30;
	$page     = affwp_get_current_page_number();
	$count    = affiliate_wp()->affiliates->payouts->count( array( 'affiliate_id' => $affiliate_id ) );
	$pages    = absint( ceil( $count / $per_page ) );
	$payouts  = affiliate_wp()->affiliates->payouts->get_payouts(
		array(
			'number'       => $per_page,
			'status'       => array( 'processing', 'paid' ),
			'offset'       => $per_page * ( $page - 1 ),
			'affiliate_id' => $affiliate_id,
		)
	);
	?>

	<?php
	$affwp_unpaid_earnings   = affwp_get_affiliate_unpaid_earnings( $affiliate_id, true );
	$affwp_lifetime_earnings = affwp_get_affiliate_earnings( $affiliate_id, true );
	?>
	<div class="affwp-payout-summary">
		<div class="affwp-payout-summary__stat">
			<span class="affwp-payout-summary__label"><?php esc_html_e( 'Unpaid earnings', 'affiliate-wp' ); ?></span>
			<span class="affwp-payout-summary__value"><?php echo esc_html( $affwp_unpaid_earnings ); ?></span>
		</div>
		<div class="affwp-payout-summary__stat">
			<span class="affwp-payout-summary__label"><?php esc_html_e( 'Lifetime paid', 'affiliate-wp' ); ?></span>
			<span class="affwp-payout-summary__value"><?php echo esc_html( $affwp_lifetime_earnings ); ?></span>
		</div>
		<?php
		/**
		 * Fires inside the affiliate payouts summary stat row, after the core stats. Payout methods
		 * (e.g. Stripe) hook in a matching `.affwp-payout-summary__stat` card such as "Next payout".
		 *
		 * @since 2.34.0
		 *
		 * @param int $affiliate_id Affiliate ID.
		 */
		do_action( 'affwp_payouts_dashboard_summary_stats', $affiliate_id );
		?>
	</div>

	<?php
	/**
	 * Fires right before displaying the affiliate payouts dashboard table.
	 *
	 * @since 1.9.4
	 *
	 * @param int $affiliate_id Affiliate ID.
	 */
	do_action( 'affwp_payouts_dashboard_before_table', $affiliate_id ); ?>

	<table id="affwp-affiliate-dashboard-payouts" class="affwp-table affwp-table-responsive" aria-describedby="affwp-table-summary">
		<thead>
			<tr>
				<th class="payout-date"><?php _e( 'Date', 'affiliate-wp' ); ?></th>
				<th class="payout-amount"><?php _e( 'Amount', 'affiliate-wp' ); ?></th>
				<th class="payout-method"><?php _e( 'Payout', 'affiliate-wp' ); ?></th>
				<th class="payout-status"><?php _e( 'Status', 'affiliate-wp' ); ?></th>
				<?php
				/**
				 * Fires right after displaying the last affiliate payouts dashboard table header.
				 *
				 * @since 1.9.4
				 *
				 * @param int $affiliate_id Affiliate ID.
				 */
				do_action( 'affwp_payouts_dashboard_th' ); ?>
			</tr>
		</thead>

		<tbody>
			<?php if ( $payouts ) : ?>

				<?php foreach ( $payouts as $payout ) : ?>
					<tr>
						<td data-th="<?php _e( 'Date', 'affiliate-wp' ); ?>">
							<?php echo esc_html( $payout->date_i18n( 'datetime' ) ); ?>
						</td>
						<td data-th="<?php _e( 'Amount', 'affiliate-wp' ); ?>">
							<?php echo affwp_currency_filter( affwp_format_amount( $payout->amount ) ); ?>
						</td>
						<td data-th="<?php _e( 'Payout', 'affiliate-wp' ); ?>">
							<?php
							if ( function_exists( 'affwp_get_payout_method_display_label' ) ) {
								echo esc_html( affwp_get_payout_method_display_label( $payout->payout_method ) );
							} else {
								echo esc_html( $payout->payout_method );
							}

							// Destination detail shown beneath the method: Payouts Service bank, PayPal email,
							// or (via the filter) the affiliate's Stripe bank last four.
							$payout_account = '';
							switch ( $payout->payout_method ) {
								case 'payouts-service':
								case 'payouts_service':
									$payout_account = isset( $payout->service_account ) ? $payout->service_account : '';
									break;
								case 'paypal':
									$payout_account = affwp_get_affiliate_payment_email( $payout->affiliate_id );
									break;
							}

							/**
							 * Filters the destination label shown beneath the method in the affiliate
							 * dashboard "Payout" column. Lets each method supply a recipient-facing
							 * destination (e.g. Stripe returns the connected bank's last four).
							 *
							 * @since 2.34.0
							 *
							 * @param string $payout_account Default label (Payouts Service / PayPal).
							 * @param object $payout         Payout object.
							 */
							$payout_account = (string) apply_filters( 'affwp_payouts_dashboard_account_label', $payout_account, $payout );

							if ( '' !== $payout_account ) {
								echo '<br /><small class="affwp-payout-account-detail">' . esc_html( $payout_account ) . '</small>';
							}
							?>
						</td>
						<td data-th="<?php _e( 'Status', 'affiliate-wp' ); ?>">
							<?php printf( '<span class="affwp-badge affwp-badge--%s">%s</span>', esc_attr( affwp_get_status_badge_variant( $payout->status ) ), esc_html( affwp_get_payout_status_label( $payout ) ) ); ?>
						</td>
						<?php
						/**
						 * Fires right after displaying the last affiliate payouts dashboard table data.
						 *
						 * @since 1.9.4
						 *
						 * @param object $payout Payout object.
						 */
						do_action( 'affwp_payouts_dashboard_td', $payout ); ?>
					</tr>
				<?php endforeach; ?>

			<?php else : ?>

				<tr>
					<td class="affwp-table-no-data" colspan="4"><?php _e( 'None of your referrals have been paid out yet.', 'affiliate-wp' ); ?></td>
				</tr>

			<?php endif; ?>
		</tbody>
	</table>

	<?php
	/**
	 * Fires right after displaying the affiliate payouts dashboard table.
	 *
	 * @since 1.9.4
	 *
	 * @param int $affiliate_id Affiliate ID.
	 */
	do_action( 'affwp_payouts_dashboard_after_table', $affiliate_id ); ?>

	<?php if ( $pages > 1 ) : ?>

		<p class="affwp-pagination">
			<?php
			echo paginate_links(
				array(
					'current'      => $page,
					'total'        => $pages,
					'add_fragment' => '#affwp-affiliate-dashboard-payouts',
					'add_args'     => array(
						'tab' => 'payouts',
					),
				)
			);
			?>
		</p>

	<?php endif; ?>

</div>
