<?php

function tzp_thankyou_page($order_id)
{
  global $woocommerce;

  $order = wc_get_order($order_id);
  $paymentMethod = $order->get_payment_method();

  if ($paymentMethod == 'tazapay') {

    $orderStatus = $order->get_status();
    $getTransactionDetails = tzp_get_checkout_api($order_id);
    if ($getTransactionDetails->status == 'success') {

      ?>
      <script>
        jQuery(document).ready(function () {
          jQuery('.entry-header .entry-title').html('<?php esc_html_e('Order Status', 'tazapay') ?>');
          jQuery('.woocommerce-thankyou-order-received').hide();
          jQuery('.woocommerce-thankyou-order-details').hide();
          jQuery('.woocommerce-customer-details').hide();

          show_tazapay_status();
        });

        function show_tazapay_status() {
          <?php if ($getTransactionDetails->data->payment_status == 'paid') { ?>
            jQuery('.woocommerce-thankyou-order-received').show();
            jQuery('.woocommerce-thankyou-order-details').show();
            jQuery('.woocommerce-customer-details').show();
          <?php } ?>
        }
      </script>
      
      <?php
      if ('processing' === $orderStatus || 'completed' === $orderStatus) {
        tzp_TxnDetailsTable($order_id);

        WC()->session->__unset('token');
        WC()->session->__unset('complete_url');
        WC()->session->__unset('abort_url');
      } elseif ($getTransactionDetails->data->payment_status == 'unpaid') {
        // Don't clear the cart
        // Keep the session tokens in case needed for retry
      }
    }
  }
}
