<?php
/**
 * Plugin admin field: Payout list
 *
 * @package YITH\PayPalPayouts\Admin
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$url = wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore

if ( ( isset( $_GET['page'] ) && 'yith_wc_paypal_payouts_panel' === $_GET['page'] ) && ( isset( $_GET['tab'] ) && 'payout-list' === $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	$return_to_list = '';
	$add_new_button = '';
	if ( isset( $_GET['show_payout_details'] ) && '' !== $_GET['show_payout_details'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		$back_to_list_args = array(
			'page' => 'yith_wc_paypal_payouts_panel',
			'tab'  => 'payout-list',
		);

		if ( isset( $_GET['payment_mode'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

			$back_to_list_args['payment_mode'] = wp_unslash( $_GET['payment_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		}

		$url = esc_url( add_query_arg( $back_to_list_args, admin_url( 'admin.php' ) ) );

		$return_to_list = sprintf( '<a href="%1$s" class="yith_payouts_return_link" title="%2$s">%2$s <img draggable="false" class="emoji" alt="⤴" src="https://s.w.org/images/core/emoji/2.3/svg/2934.svg"></a>', $url, __( 'Return to Payouts List', 'yith-paypal-payouts-for-woocommerce' ) );

		if ( ! class_exists( 'YITH_PayOut_Items_List_Table' ) ) {

			require_once YITH_PAYOUTS_INC . 'class.yith-payout-items-list-table.php';
		}

		$payout_batch_id = wp_unslash( $_GET['show_payout_details'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		$payment_id      = str_replace( 'commission_', '', $payout_batch_id );
		$payout_batch_id = 'payment_' . $payment_id;
		$page_title      = __( 'Payout Details', 'yith-paypal-payouts-for-woocommerce' ) . " ($payout_batch_id)";
		$table           = new YITH_PayOut_Items_List_Table(
			array(
				'singular' => _x( 'Payout Item', 'yith-paypal-payouts-for-woocommerce' ),
				// singular name of the listed records.
				'plural'   => _x( 'Payout Items', 'yith-paypal-payouts-for-woocommerce' ),
			)
		);
	} else {

		if ( ! class_exists( 'YITH_PayOuts_List_Table' ) ) {

			require_once YITH_PAYOUTS_INC . 'class.yith-payouts-list-table.php';
		}

		$page_title     = __( 'Payouts List', 'yith-paypal-payouts-for-woocommerce' );
		$table          = new YITH_PayOuts_List_Table(
			array(
				'singular' => _x( 'Payout', 'yith-paypal-payouts-for-woocommerce' ),
				// singular name of the listed records.
				'plural'   => _x( 'Payouts', 'yith-paypal-payouts-for-woocommerce' ),
			)
		);
		$add_new_button = '<a href="#" class="page-title-action add-payouts">' . esc_html__( 'Add payouts', 'yith-paypal-payouts-for-woocommerce' ) . '</a>';
	}
	?>
	<div class="wrap yith-payouts-list-table yith-plugin-ui yith-plugin-ui--classic-wp-list-style yith-plugin-fw-wp-page-wrapper">
		<div class="wrap custom-list-table">
			<div class="list-table-title">
				<h1 class="wp-heading-inline">
					<?php echo esc_html( $page_title ); ?>
					<?php echo $add_new_button; //phpcs:ignore WordPress.Security.EscapeOutput ?>
				</h1>
				<?php echo $return_to_list; //phpcs:ignore WordPress.Security.EscapeOutput ?>
			</div>
			<?php
			yith_payouts_show_messages();
			?>
			<form method="post">
				<?php

				$table->prepare_items();
				$table->views();

				$table->display();
				?>
			</form>
		</div>
	</div>
	<?php
}
