<?php
/**
 * PeachPay Square order payment info.
 *
 * @var WC_Order $order
 *
 * @package PeachPay
 */

defined( 'PEACHPAY_ABSPATH' ) || exit;

$square_payment_id   = PeachPay_Square_Order_Data::get_payment( $order, 'id' );
$square_mode         = PeachPay_Square_Order_Data::get_payment( $order, 'mode' );
$square_status       = PeachPay_Square_Order_Data::get_payment( $order, 'status' );
$square_source_type  = PeachPay_Square_Order_Data::get_payment( $order, 'source_type' );
$square_card_details = PeachPay_Square_Order_Data::get_payment( $order, 'card_details' );
$square_total_money  = PeachPay_Square_Order_Data::get_payment( $order, 'total_money' );
$square_currency     = null !== $square_total_money ? strtoupper( $square_total_money['currency'] ) : strtoupper( $order->get_currency() );
?>

<div class="peachpay">
	<div>
		<h3><?php esc_html_e( 'Transaction details', 'peachpay-for-woocommerce' ); ?></h3>
		<a href="#transaction-info"><?php esc_html_e( 'View', 'peachpay-for-woocommerce' ); ?></a>
	</div>

	<div id="transaction-info" class="modal-window">
		<a href="#" title="Close" class="outside-close"> </a>

		<div>
			<h2><?php esc_html_e( 'Transaction details', 'peachpay-for-woocommerce' ); ?></h2>
			<hr>
			<a href="#" title="Close" class="modal-close"><?php esc_html_e( 'Close', 'peachpay-for-woocommerce' ); ?></a>

			<h3><?php esc_html_e( 'Square Payment', 'peachpay-for-woocommerce' ); ?></h3>
			<dl class="inline-dl">
				<dt><?php esc_html_e( 'Mode', 'peachpay-for-woocommerce' ); ?></dt>
				<dd><?php echo esc_html( ucfirst( (string) $square_mode ) ); ?></dd>

				<dt><?php esc_html_e( 'Status', 'peachpay-for-woocommerce' ); ?></dt>
				<dd><?php echo esc_html( (string) $square_status ); ?></dd>

				<dt><?php esc_html_e( 'Payment ID', 'peachpay-for-woocommerce' ); ?></dt>
				<dd>
					<?php if ( null !== $square_payment_id && null !== $square_mode ) { ?>
						<a href="<?php echo esc_url( PeachPay_Square::dashboard_url( $square_mode, $square_payment_id ) ); ?>" target="_blank" rel="noopener noreferrer">
							<?php echo esc_html( (string) $square_payment_id ); ?>
						</a>
					<?php } else { ?>
						<?php echo esc_html( (string) $square_payment_id ); ?>
					<?php } ?>
				</dd>

				<dt><?php esc_html_e( 'Source', 'peachpay-for-woocommerce' ); ?></dt>
				<dd>
					<?php
					if ( 'CARD' === $square_source_type && null !== $square_card_details ) {
						$card_brand = isset( $square_card_details['card']['card_brand'] ) ? $square_card_details['card']['card_brand'] : '';
						$last_4     = isset( $square_card_details['card']['last_4'] ) ? $square_card_details['card']['last_4'] : '';
						echo esc_html( $card_brand ) . ( $last_4 ? ' •••• ' . esc_html( $last_4 ) : '' );
					} else {
						echo esc_html( (string) $square_source_type );
					}
					?>
				</dd>

				<?php if ( null !== $square_total_money ) { ?>
					<dt><?php esc_html_e( 'Amount', 'peachpay-for-woocommerce' ); ?></dt>
					<dd>
						<?php
						$amount = PeachPay_Square::display_amount( $square_total_money['amount'], $square_currency );
						// PHPCS:ignore
						echo wc_price( $amount, array( 'currency' => $square_currency ) );
						?>
					</dd>

					<dt><?php esc_html_e( 'Refunds', 'peachpay-for-woocommerce' ); ?></dt>
					<dd>
						<?php
						$refunds = -PeachPay_Square::display_amount( PeachPay_Square_Order_Data::total_refunds( $order ), $square_currency );
						// PHPCS:ignore
						echo wc_price( $refunds, array( 'currency' => $square_currency ) );
						?>
					</dd>

					<?php $square_fees = PeachPay_Square_Order_Data::total_fees( $order ); ?>
					<?php if ( null !== $square_fees ) { ?>
						<dt><?php esc_html_e( 'Fee', 'peachpay-for-woocommerce' ); ?></dt>
						<dd>
							<?php
							$fee = PeachPay_Square::display_amount( $square_fees, $square_currency );
							// PHPCS:ignore
							echo wc_price( $fee, array( 'currency' => $square_currency ) );
							?>
						</dd>
						<hr>
						<dt><?php esc_html_e( 'Net', 'peachpay-for-woocommerce' ); ?></dt>
						<dd>
							<?php
							$net = PeachPay_Square::display_amount( PeachPay_Square_Order_Data::total_payout( $order ), $square_currency );
							// PHPCS:ignore
							echo wc_price( $net, array( 'currency' => $square_currency ) );
							?>
						</dd>
					<?php } ?>
				<?php } ?>
			</dl>
		</div>
	</div>
</div>
