<?php 
/*
	osConcert Seat Booking Software
	https://www.osconcert.com
	Copyright (c) 2026 osConcert

	Released under the GNU General Public License 
*/	
// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();
?>
<div class="section-header">
<h2><?php echo HEADING_TITLE; ?></h2>
</div>
<div class="container">
	<div class="card mb-4 box-shadow">
		<div class="card-header"><strong>
		<?php echo sprintf(HEADING_ORDER_NUMBER, $FREQUEST->getvalue('order_id')) . ' <small>(' . $order->info['orders_status'] . ')</small>'; ?>  
		</strong>
		</div>
		<div class="card-body">
        <table width="100%" cellspacing="0" cellpadding="2" class="table-hover">
		<?php
		//PHP8 if (sizeof($order->info['tax_groups']) > 1) {
			if (count($order->info['tax_groups']) > 1) {
		?>
		<tr>
			<td colspan="2"><strong><?php echo HEADING_PRODUCTS; ?></strong></td>
			<td align="right"><strong><?php echo HEADING_TAX; ?></strong></td>
			<td align="right"><strong><?php echo HEADING_TOTAL; ?></strong></td>
		</tr>
		<?php
		} else 
		{
		?>
		<tr>
			<td colspan="2"><strong><?php echo HEADING_PRODUCTS; ?></strong></td>
			<td align="right"><strong><?php echo HEADING_TOTAL; ?></strong></td>
		</tr>
		<?php
		}
		$products_name="";
		$products = array();
		$products_names_p="";
		$pre_qty="";
		$pre_name="";
		$qty_p = "";
		$pre_sign="";
		$first_event = true;
		$first_event_id = 0;
		$jj=0;
		$order_is_printable = 0;

		for ($i = 0, $n = count($order->products); $i < $n; $i++)
		{
			$order_is_printable = $order_is_printable + $order->products[$i]['is_printable'];
			$products_names_p="";
			$qty_p ="";
			$sign_p="";
			
			$products_names_p = $order->products[$i]['name'];
			$qty_p=$order->products[$i]['qty'];
			$sign_p="X";
			$rax_total=tep_add_tax($order->products[$i]['final_price'], $order->products[$i]['tax']) * $order->products[$i]['qty'];
			$event_id = 0;

			$products[] = array( "qty" => $qty_p,
							 "sign"  => $sign_p,
							 "id" => $order->products[$i]['id'],
							 "products_type" => $order->products[$i]['element_type'],
							 "events_type" => $order->products[$i]['events_type'],
							 "p_name"	 => $products_names_p,
							 "event_id" => $event_id,
							 "sku" => $order->products[$i]['sku'],
							 "categories_name" => $heading_name,
							 "concert_venue" => $heading_venue,
							 "concert_date" => $heading_date,
							 "concert_time" => $heading_time,
							 "row_total"=>$rax_total
						   );
	    }

// ----------------- SETUP -----------------
$products = isset($products) && is_array($products)
    ? $products
    : (isset($order->products) ? $order->products : []);

$has_voucher = false; // any SKU 6 or 8
$has_eticket = false; // any SKU 1 or 9

foreach ($products as $p) {
    $sku = isset($p['sku']) ? (int)$p['sku'] : 0;
    if (in_array($sku, [6, 8], true)) { $has_voucher = true; }
    if (in_array($sku, [1, 9], true)) { $has_eticket = true; }
}

$dis_time_format = defined('TIME_FORMAT') ? TIME_FORMAT : '';

// Query delivered/printable status ONCE
$delivered_query = tep_db_query(
    "SELECT osh.orders_status_history_id, osh.orders_status_id
     FROM " . TABLE_ORDERS_STATUS_HISTORY . " osh
     WHERE osh.orders_id = '" . (int)$FREQUEST->getvalue('order_id') . "'
     ORDER BY orders_status_history_id DESC LIMIT 1"
);
$delivered_status = tep_db_fetch_array($delivered_query);
?>

<!-- Products table -->
<?php
for ($i = 0, $n = count($products); $i < $n; $i++) {
    // row total (same for product types P and V)
    $row_total = $products[$i]['row_total'];

    // fields (use $products only for consistency)
    $discount       = (!empty($products[$i]['discount_whole_text'])) ? '<br>' . $products[$i]['discount_whole_text'] : '';
    $event_id       = $products[$i]['event_id'];
    $heading_name   = $products[$i]['categories_name'];
    $heading_venue  = $products[$i]['concert_venue'];
    $heading_date   = $products[$i]['concert_date'];
    $heading_time   = $products[$i]['concert_time'];
    $events_type    = $products[$i]['events_type']; // product_type
    $family         = ($events_type === 'F') ? '<small>' . FAMILY_TICKET . ' x ' . FAMILY_TICKET_QTY . '</small>' : '';

    echo '          <tr>' . "\n" .
         '            <td class="main" align="right" valign="top" width="30">' .
         $products[$i]['qty'] . "&nbsp;&nbsp;" . $products[$i]['sign'] .'&nbsp;&nbsp;</td>' . "\n" .
         '            <td class="main" valign="top">' .
         $heading_name . ' &nbsp;&nbsp; ' .
         $products[$i]['p_name'] . '  ' .
         $heading_date  . ' ' .
         $heading_venue . ' ' .
         $heading_time .' <br>'. $family .
         $discount .
         '</td>' . "\n"; // always close TD

    if (count($order->info['tax_groups']) > 1) {
        echo '            <td class="main" valign="top" align="right">' .
             tep_display_tax_value($products[$i]['tax']) . '%</td>' . "\n";
    }

    echo '<td class="main" align="right" valign="top">&nbsp;&nbsp;&nbsp;&nbsp;' .
         $currencies->format($row_total, true, $order->info['currency'], $order->info['currency_value']) .
         '</td></tr>' . "\n";
}
?>
</table>
</div>

<table width="100%" cellpadding="2" cellspacing="15">
<?php
for ($i = 0, $n = count($order->totals); $i < $n; $i++) {
    echo '              <tr>' . "\n" .
         '                <td class="main" align="right" width="80%">' . $order->totals[$i]['title'] . '</td>' . "\n" .
         '                <td class="main" align="right" width="20%">' . $order->totals[$i]['text'] . '&nbsp;&nbsp;</td>' . "\n" .
         '              </tr>' . "\n";
}
?>
</table>

<div class="card-header">
  <span class="pull-right hidden-xs"><?php echo HEADING_ORDER_TOTAL . ' ' . $order->info['total']; ?></span>
  <?php
  // keep your original date logic (strftime-based)
  $date_purchased = strtotime($order->info['date_purchased']);
  $new_date = utf8_encode(strftime((DATE_FORMAT_LONG), $date_purchased));
  echo HEADING_ORDER_DATE . ' ' . $new_date;
  ?>
</div>
</div>
</div>

<div class="container account">
  <div class="card-deck mb-3 text-left">
    <?php if ($order->delivery != false) { ?>
      <div class="card mb-4 box-shadow">
        <div class="card-header">
          <h4 class="my-0 font-weight-normal"><?php echo HEADING_DELIVERY_ADDRESS; ?></h4>
        </div>
        <div class="card-body">
          <?php if (($order->delivery['format_id']) > 0) { ?>
            <?php echo tep_address_format($order->delivery['format_id'], $order->delivery, 1, ' ', '<br />'); ?>
          <?php } else { ?>
            <?php echo tep_address_format($order->customer['format_id'], $order->customer, 1, ' ', '<br />'); ?>
          <?php } ?>
        </div>
      </div>
    <?php } ?>

    <div class="card mb-4 box-shadow">
      <div class="card-header">
        <h4 class="my-0 font-weight-normal"><?php echo HEADING_BILLING_ADDRESS; ?></h4>
      </div>
      <div class="card-body">
        <?php echo tep_address_format($order->billing['format_id'], $order->billing, 1, ' ', '<br />'); ?>
      </div>
    </div>

    <?php if (tep_not_null($order->info['shipping_method'])) { ?>
      <div class="card mb-4 box-shadow">
        <div class="card-header">
          <h4 class="my-0 font-weight-normal"><?php echo HEADING_SHIPPING_METHOD; ?></h4>
        </div>
        <div class="card-body">
          <?php echo $order->info['shipping_method']; ?>
        </div>
      </div>
    <?php } ?>

    <div class="card mb-4 box-shadow">
      <div class="card-header">
        <h4 class="my-0 font-weight-normal"><?php echo HEADING_PAYMENT_METHOD; ?></h4>
      </div>
      <div class="card-body">
        <?php
        echo '<strong>' . $order->info['payment_method'] . '</strong>';
        if (($order->info['status_id'] == 1) && ($order->info['payment_info'] == 'bank_transfer')) {
            require(DIR_WS_LANGUAGES . $FSESSION->language . '/modules/payment/bank_transfer.php');
            echo '<br><br>';
            echo MODULE_BANK_TRANSFER_INFO;
        }
        ?>
      </div>
    </div>
  </div>
</div>
<?php
$venue_address = '';

$venue_query = tep_db_query("
  SELECT 
    cd.concert_venue,
    cd.concert_address
  FROM " . TABLE_ORDERS_PRODUCTS . " op
  INNER JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd
    ON cd.categories_id = op.events_id
   AND cd.language_id = " . (int)$languages_id . "
  WHERE op.orders_id = '" . (int)$FREQUEST->getvalue('order_id') . "'
  LIMIT 1
");

if ($venue = tep_db_fetch_array($venue_query)) {
  $venue_address = trim(
    $venue['concert_venue'] .
    (!empty($venue['concert_address']) ? ', ' . $venue['concert_address'] : '')
  );
}



$orderId   = (int)($orders['orders_id'] ?? 0);
$icsUrl    = tep_href_link('download_ics.php', 'order_id='.$FREQUEST->getvalue('order_id'));

// Use your real venue variables if you have them
$venueText = $venue_address; //'Theater am Insulaner Berlin';
$mapsUrl   = 'https://www.google.com/maps/search/?api=1&query=' . urlencode($venueText);
?>
<?php //echo  $venue_address; ?>
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
  <a class="btn btn-primary btn-sm d-inline-flex align-items-center gap-2"
     href="<?php echo htmlspecialchars($icsUrl, ENT_QUOTES); ?>">
    <i class="bi bi-calendar-plus"></i> <?php echo TEXT_ADDTO_CALENDAR; ?> <span class="opacity-75">(.ics)</span>
  </a>
<?php if (GET_DIRECTIONS_ENABLE==='true'){ ?>
  <a class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-2"
     href="<?php echo htmlspecialchars($mapsUrl, ENT_QUOTES); ?>"
     target="_blank" rel="noopener">
    <i class="bi bi-map"></i> <?php echo TEXT_GET_DIRECTIONS; ?>
  </a>
  <?php } ?>
</div>


<hr>

<!-- start pdf (eTickets) //-->
<?php
if ($has_eticket) { // only show the tickets section if order has SKU 1 or 9
    $target = " target='_blank'";

    if (E_TICKETS === 'true') {
        if (
            ($order_is_printable > 0) &&
            ($delivered_status['orders_status_id'] == E_TICKET_STATUS) &&
            (DISPLAY_PDF_DELIVERED_ONLY == 'true')
        ) {
            ?>
            <div class="container">
              <div class="row">
                <div class="col-lg-7 text-center text-lg-left">
                  <h3 class="cta-title"><?php echo PDF_INVOICE; ?></h3>
                  <p class="cta-text">
                    <?php
                      echo tep_image(DIR_WS_IMAGES . 'pdf.gif', PRINT_PDF_TICKETS, '', '', '') .
                           sprintf(PDF_DOWNLOAD_LINK,
                             tep_href_link(FILENAME_CUSTOMER_PDF, 'oID=' . $FREQUEST->getvalue('order_id'), 'SSL'));
                    ?>
                  </p>
                </div>
                <div class="col-lg-4 cta-btn-container text-center">
                  <form>
                    <input type="button" class="btn btn-primary btn-lg" value="<?php echo GET_TICKETS_HERE; ?>"
                      onclick="window.open('<?php echo tep_href_link(FILENAME_CUSTOMER_PDF, 'oID=' . $FREQUEST->getvalue('order_id'), 'SSL'); ?>')">
                  </form>
				  
                </div>
              </div>
            </div>
            <?php
        } else {
            // Show your existing "no e-tickets" message (keep or remove as desired)
            echo "<div><p class='red'>" . NO_ETICKETS . "</p></div>";
            echo "<hr>";
        }
    }
} // else: no SKU 1/9; hide entire eTicket block
?>
<!-- end pdf (eTickets) //-->

<!-- start vouchers pdf //-->
<?php
if ($has_voucher) { // only show if order has SKU 6 or 8
    $target = " target='_blank'";

    if (E_TICKETS === 'true') {
        if (
            ($order_is_printable > 0) &&
            ($delivered_status['orders_status_id'] == E_TICKET_STATUS) &&
            (DISPLAY_PDF_DELIVERED_ONLY == 'true')
        ) {
            ?>
            <div class="container">
              <div class="row">
                <div class="col-lg-7 text-center text-lg-left">
                  <h3 class="cta-title"><?php echo PDF_VOUCHERS; ?></h3>
                  <p class="cta-text">
                    <?php
                      echo tep_image(DIR_WS_IMAGES . 'pdf.gif', PRINT_PDF_VOUCHERS, '', '', '') .
                           sprintf(PDF_VOUCHER_LINK,
                             tep_href_link(FILENAME_VOUCHER_PDF, 'oID=' . $FREQUEST->getvalue('order_id'), 'SSL'));
                    ?>
                  </p>
                </div>
                <div class="col-lg-4 cta-btn-container text-center">
                  <form>
                    <input type="button" class="btn btn-primary btn-lg" value="<?php echo GET_VOUCHERS_HERE; ?>"
                      onclick="window.open('<?php echo tep_href_link(FILENAME_VOUCHER_PDF, 'oID=' . $FREQUEST->getvalue('order_id'), 'SSL'); ?>')">
                    <br>
                  </form>
                </div>
              </div>
            </div>
            <?php
        } else {
            // Intentionally quiet (keep same behavior as your original)
        }
    }
}
?>
<!-- end vouchers pdf //-->


	<hr>
		
			<div class="card mb-4 box-shadow">
				<div class="card-header">
				<h4><?php echo HEADING_ORDER_HISTORY; ?></h4>
				</div>
						 <!-- <table colspan="3" width="100%" cellspacing="2" cellpadding="2">
							<tr>
								<td class="main">
									<b><?php //echo HEADING_ORDER_DATE; ?></b>
								</td>
								<td class="main">
									<b><?php //echo HEADING_PAYMENT_STATUS; ?></b>
								</td>
								<td class="main">
									<b><?php //echo NEW_FIELDS_HEADING; ?></b>
								</td>
							</tr>-->		
<div class="card-body">
    <ul class="timeline list-unstyled">
	<?php
  $statuses_query = tep_db_query("select os.orders_status_name, osh.date_added, osh.comments, osh.field_1, osh.field_2, osh.field_3, osh.field_4, osh.other from " . TABLE_ORDERS_STATUS . " os, " . TABLE_ORDERS_STATUS_HISTORY . " osh where osh.orders_id = '" . $FREQUEST->getvalue('order_id','int','0') . "' and osh.orders_status_id = os.orders_status_id and os.language_id = '" . (int)$FSESSION->languages_id . "' order by osh.date_added");
	while ($statuses = tep_db_fetch_array($statuses_query)) 
	{
		if (EXTRA_FIELDS == 'yes')
		{
		echo '
		<div class="card mb-3 shadow-sm">
		  <div class="card-body">
			<div class="row g-3">

			  <div class="col-12 col-md-3">
				<div class="small text-muted">Date Added</div>
				<strong>' . format_date($statuses['date_added']) . '</strong>
			  </div>

			  <div class="col-12 col-md-3">
				<div class="small text-muted">Status</div>
				<span class="badge bg-secondary">' . $statuses['orders_status_name'] . '</span>
			  </div>

			  <div class="col-12 col-md-6">
				<div class="small text-muted mb-1">' . HEADING_COMMENT . '</div>
				<div class="border rounded p-2">
				  ' . (!empty($statuses['comments']) ? '<div><strong>' . HEADING_COMMENT . ':</strong> ' . nl2br(tep_output_string($statuses['comments'])) . '</div>' : '') . '
				  ' . (!empty($statuses['field_1']) ? '<div><strong>' . FIELD_1 . ':</strong> ' . nl2br(tep_output_string($statuses['field_1'])) . '</div>' : '') . '
				  ' . (!empty($statuses['field_2']) ? '<div><strong>' . FIELD_2 . ':</strong> ' . nl2br(tep_output_string($statuses['field_2'])) . '</div>' : '') . '
				  ' . (!empty($statuses['field_3']) ? '<div><strong>' . FIELD_3 . ':</strong> ' . nl2br(tep_output_string($statuses['field_3'])) . '</div>' : '') . '
				  ' . (!empty($statuses['field_4']) ? '<div><strong>' . FIELD_4 . ':</strong> ' . nl2br(tep_output_string($statuses['field_4'])) . '</div>' : '') . '
				  ' . (!empty($statuses['other']) ? '<div><strong>' . FIELD_5 . ':</strong> ' . nl2br(tep_output_string($statuses['other'])) . '</div>' : '') . '
				</div>
			  </div>

			</div>
		  </div>
		</div>' . "\n";
		}
		
		echo '<li>';
			echo '  <div class="timeline-badge"><i class="bi bi-check-square"></i></div>';
			echo '  <div class="timeline-panel">';
			echo '    <div class="timeline-heading">';
			echo '      <p class="pull-right"><small class="text-muted"><i class="bi bi-clock"></i> ' . tep_date_short($statuses['date_added']) . '</small></p><h2 class="h3 timeline-title">' . $statuses['orders_status_name'] . '</h2>';
			echo '    </div>';
			echo '    <div class="timeline-body">';
			//echo '      <p>' . (empty($statuses['comments']) ? '&nbsp;' : '<blockquote>' . nl2br(tep_output_string_protected($statuses['comments'])) . '</blockquote>') . '</p>';
			echo '    </div>';
			echo '  </div>';
			echo '</li>';
	}
	?>    
	</ul>
</div>
</div>
<?php
$order_id = (int)$FREQUEST->getvalue('order_id', 'int', '0');

// Load downloads only when delivered
$order_status_query = tep_db_query("
  SELECT orders_status
  FROM " . TABLE_ORDERS . "
  WHERE orders_id = '" . $order_id . "'
");

$order_status = tep_db_fetch_array($order_status_query);

if (($order_status['orders_status'] ?? 0) == 3) {
  if (DOWNLOAD_ENABLED == 'true') {
    include(DIR_WS_MODULES . 'downloads.php');
  }
}

// Check latest order status for PDF invoice
$show_pdf_button = false;

$pdfdelivered_query = tep_db_query("
  SELECT orders_status_history_id, orders_status_id
  FROM " . TABLE_ORDERS_STATUS_HISTORY . "
  WHERE orders_id = '" . $order_id . "'
  ORDER BY orders_status_history_id DESC
  LIMIT 1
");

$pdfdelivered_status = tep_db_fetch_array($pdfdelivered_query);

if (
  isset($pdfdelivered_status['orders_status_id']) &&
  $pdfdelivered_status['orders_status_id'] == 3 &&
  DISPLAY_PDF_DELIVERED_ONLY == 'true'
) {
  $show_pdf_button = true;
}
?>

<div class="container my-3">

  <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">

    <a class="btn btn-outline-secondary btn-sm"
       href="<?php echo tep_href_link(FILENAME_ACCOUNT, tep_get_all_get_params(array('order_id')), 'SSL'); ?>">
      <i class="bi bi-arrow-left me-1"></i><?php echo IMAGE_BUTTON_BACK; ?>
    </a>

    <?php if ($show_pdf_button) { ?>
      <a class="btn btn-outline-primary btn-sm"
         target="_blank"
         rel="noopener"
         href="<?php echo tep_href_link('pdfinvoice.php', 'oID=' . $order_id, 'SSL'); ?>">
        <i class="bi bi-printer me-1"></i><?php echo IMAGE_BUTTON_PRINT_ORDER; ?>
      </a>
    <?php } ?>

  </div>

  <?php if (!empty($order->info['ip_address'])) { ?>
    <div class="alert alert-light border small mb-3">
      <strong><?php echo HEADING_IP_ADDRESS; ?></strong>
      <?php echo htmlspecialchars($order->info['ip_address']); ?>
    </div>
  <?php } ?>

</div>