<?php
/**
 * File: last-order-widget.php
 * Year: 2023
 */

?>
<div class="tami-card tami-rounded-4 tami-p-4 tami-py-2 tami-bg-tami tami-wave-green tami-mb-3">
                <span class="tami-lead tami-text-white tami-w-75">
                    <i class="fa fa-shopping-bag"></i> <?php esc_attr_e( 'Last Completed Order', 'tami-payment' ) ?></span>
    <div class="tami-text-white tami-mt-2 tami-pb-3">
		<?php
		$orderListArgs  = array(
			'meta_key'   => 'tami_payment_completed_time', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
			'meta_value' => '0', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
            'meta_compare' => '>',
            'return' => 'objects'
		);
		$orderList      = wc_get_orders( $orderListArgs );
		$noOrderFound   = ! is_array( $orderList ) || ! isset( $orderList[0] ) || ! method_exists($orderList[0],'get_id');
		if ( $orderList ) :
			$lastOrder = $orderList[0];
			$orderTotal = get_woocommerce_currency_symbol( $lastOrder->get_currency() ) . '' . $lastOrder->get_total();
			$tz         = new DateTimeZone( 'Europe/Istanbul' );
			$tsOfOrder  = $lastOrder->get_meta( 'tami_payment_completed_time', true );
			if ( ! is_numeric( $tsOfOrder ) ) {
				$tsOfOrder = 0;
			}
			$orderLines = [
				(object) [
					'title'   => __( 'Order Number', 'tami-payment' ),
					'content' => '#' . $lastOrder->get_order_number()
				],
				(object) [
					'title'   => __( 'Payment Time', 'tami-payment' ),
					'content' => ( new DateTime() )->setTimestamp( $tsOfOrder )->setTimezone( $tz )->format( 'd/m/Y H:i' )
				],
				(object) [
					'title'   => __( 'Order Total', 'tami-payment' ),
					'content' => $orderTotal
				]
			];
			?>
			<?php
			foreach ( $orderLines as $line ) {
				echo sprintf( "<div class='tami-row tami-py-2'>
<div class='tami-col'><h6 class='tami-m-0'>%s</h6></div>
<div class='tami-col'>%s</div>
</div>", esc_attr($line->title), esc_attr($line->content) );
			}
			?>
            <div class="tami-row">
                <div class="tami-col">
                    <a
                            target="_blank"
                            href="<?php
                            $buttonUrl = tami_isHposDisabled() ? admin_url( 'edit.php?post_type=shop_order' ) : admin_url('admin.php?page=wc-orders');
                            echo esc_url($buttonUrl); ?>"
                            class="tami-btn tami-btn-tami-green tami-mt-2"
                    ><?php esc_attr_e( 'List Orders', 'tami-payment' ) ?> <i class="fa fa-arrow-right-long"></i></a></div>
            </div>
		<?php else: ?>
            <p class="tami-text-center tami-my-5"><i class="fa fa-exclamation-triangle"></i> <?php esc_attr_e('No order found!','tami-payment') ?></p>
		<?php endif; ?>
    </div>
</div>
