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

	Released under the GNU General Public License 
*/	
// Check to ensure this file is included in osConcert!
	defined('_FEXEC') or die();
	require(DIR_WS_INCLUDES.'http.js'); ?>
	<?php echo tep_draw_form('order', tep_href_link(FILENAME_CHECKOUT_SUCCESS, 'action=update', 'SSL')); ?>
	<?php //box office refund 
	//we need an extra /form tag here so that the box office links work
	if($_SESSION['box_office_refund'])
	{
	echo '</form>';
	$FSESSION->remove('box_office_refund');
	}
?>
	<div class="section-header">
	<h2><?php 
	echo HEADING_TITLE;
	?></h2>
	</div>

	<?php
	if ($messageStack->size('checkout_success') > 0) 
	{
	?>
	<div><?php echo $messageStack->output('checkout_success');?></div>
	<?php 
	}   
	?>

	<div><?php echo tep_image(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/images/'.COMPANY_LOGO, HEADING_TITLE,'',''); ?></div>

			<?php
					
					if (sizeof($products_array)>0)
					{
						//echo '<h2>' . SEATS_RESERVED . '</h2><br>';
						$products_displayed = array();
						$order_is_printable = 0;
						for ($i=0, $n=sizeof($products_array); $i<$n; $i++) 
						{
							if (!in_array($products_array[$i]['id'], $products_displayed)) 
							{
								//sort a family ticket
								if(!defined('SET_OF_FAMILY'))
								{
								define('SET_OF_FAMILY', 'Set of ');
								}	
								if($products_array[$i]['type']=='F'){
								//$qty_times=SET_OF_FAMILY .$products_array[$i]['qty'] . ' ';
								$qty_times=SET_OF_FAMILY . FAMILY_TICKET_QTY. ' ';
								}else{
								$qty_times=$products_array[$i]['qty'] . ' x ';
								}
								if ($_GET['type'] == 'canx'){ $qty_times = '';}
						
							echo
							$qty_times . 
							$products_array[$i]['seat'] . ' [ ' . 
							$products_array[$i]['date'] . '] <b>' . 
							$products_array[$i]['categories_name'] . '</b> - ' . 
							$products_array[$i]['concert_date'] . '<br>' ;
							
							}
						$order_is_printable = $order_is_printable + $products_array[$i]['is_printable'];
						}
					}

					?>
					<?php //echo $TEXT_SUCCESS;

					echo '<h1>' . INVOICE . $FREQUEST->getvalue('order_id') .'</h1>';

			$pdfdelivered_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");
			$pdfdelivered_status = tep_db_fetch_array($pdfdelivered_query);

			if ($pdfdelivered_status['orders_status_id'] == 3 && DISPLAY_PDF_DELIVERED_ONLY == 'true')
			{
			echo '<a target="_blank" href="' . tep_href_link('pdfinvoice.php', 'oID=' . $FREQUEST->getvalue('order_id') , 'SSL') . '">' . tep_template_image_button_basic('', IMAGE_BUTTON_PRINT_ORDER) . '</a>';
			}
			?>
			<br class="clearfloat">
		<?php 
			$order_status_query = tep_db_query("select orders_status from " . TABLE_ORDERS . " where orders_id = '" . (int)$FREQUEST->getvalue('order_id') . "'");
			$order_status = tep_db_fetch_array($order_status_query); 	
			if(($order_status['orders_status'])==3)
			{
			if (DOWNLOAD_ENABLED == 'true') 
				include(DIR_WS_MODULES . 'downloads.php'); 
			}
        ?>

<?php
$target = " target='_blank'";
$order_id = (int)$FREQUEST->getvalue('order_id');

// Fetch delivered status
$delivered_query = tep_db_query(
    "SELECT MAX(osh.date_added) AS los, osh.orders_status_history_id, osh.orders_status_id 
     FROM " . TABLE_ORDERS_STATUS_HISTORY . " osh 
     WHERE osh.orders_id = '$order_id' 
     GROUP BY osh.orders_status_id, osh.orders_status_history_id 
     ORDER BY orders_status_history_id DESC 
     LIMIT 1"
);
$delivered_status = tep_db_fetch_array($delivered_query);

// Determine if e-tickets are available
$e_tickets_enabled = (E_TICKETS == 'true');
$order_is_delivered = ($delivered_status['orders_status_id'] == E_TICKET_STATUS);
$display_pdf_delivered_only = (DISPLAY_PDF_DELIVERED_ONLY == 'true');

// Flags to track buttons
$voucher_button_shown = false;
$ticket_button_shown = false;

// Generate buttons based on SKUs
if ($e_tickets_enabled && $order_is_printable > 0 && $order_is_delivered && $display_pdf_delivered_only) {
    foreach ($products_array as $product) {
        // Check for voucher products (SKU 6 or 8)
        if (in_array($product['sku'], ['6', '8']) && !$voucher_button_shown) {
            generateButton(
                GET_VOUCHERS_HERE,
                tep_href_link(FILENAME_VOUCHER_PDF, 'oID=' . $order_id . '&customer_id=' . $customer_number['customers_id'], 'SSL')
            );
            $voucher_button_shown = true; // Mark voucher button as shown
        }

        // Check for ticket products (SKU 1 or 9)
        if (in_array($product['sku'], ['1', '9']) && !$ticket_button_shown) {
            generateButton(
                GET_TICKETS_HERE,
                tep_href_link(FILENAME_CUSTOMER_PDF, 'oID=' . $order_id . '&customer_id=' . $customer_number['customers_id'], 'SSL')
            );
            $ticket_button_shown = true; // Mark ticket button as shown
        }
    }
}


// Function to generate button HTML
function generateButton($label, $url) {
    echo "<div align='center'>
            <input type='button' class='btn btn-primary btn-lg' value='$label' onclick=\"window.open('$url')\">
          </div>";
}

?>

			
			<div>
			<?php 
			if  (DISPLAY_PDF_DELIVERED_ONLY == 'false'){//if it's set at 'Pending' we tell them 
				if ($guest==1)
				{
				echo MESSAGE3 . ' '. $user;
				}else
				{
				echo MESSAGE2;	
				}
			}else
			{
				if ($guest==1)
				{
				echo MESSAGE3 . ' '. $user . ' ' . $log_page;//if PWA 
				}else
				{
				echo MESSAGE2;	
				}
			}
			?>
			</div>
			
			<div>
			</div>
            
			<?php
			//CCGV FOR ORDER_TOTAL CREDIT SYSTEM - Start Addition
			$gv_query=tep_db_query("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id='" . (int)$customer_id . "'");
			if ($gv_result=tep_db_fetch_array($gv_query)) 
			{
				if ($gv_result['amount'] > 0) 
				{
				?>
			<div>
			<?php echo GV_HAS_VOUCHERA; echo tep_href_link(FILENAME_GV_SEND); echo GV_HAS_VOUCHERB; ?>
			</div>
			<?php
				}
			}
			//CCGV ADDED FOR ORDER_TOTAL CREDIT SYSTEM - End Addition
			?>

	  <div class="bs-stepper">
            <div class="bs-stepper-header">
              <div class="step" data-target="#delivery">
                <button type="button" class="btn step-trigger"  disabled="disabled">
                  <span class="bs-stepper-circle">1</span>
                  <span class="bs-stepper-label"><?php echo CHECKOUT_BAR_DELIVERY; ?></span>
                </button>
              </div>
              <div class="line"></div>
              <div class="step" data-target="#payment">
                <button type="button" class="btn step-trigger"  disabled="disabled">
                  <span class="bs-stepper-circle">2</span>
                  <span class="bs-stepper-label"><?php echo CHECKOUT_BAR_PAYMENT; ?></span>
                </button>
              </div>
              <div class="line"></div>
              <div class="step active" data-target="#confirm">
                <button type="button" class="btn step-trigger" disabled="disabled">
                  <span class="bs-stepper-circle">3</span>
                  <span class="bs-stepper-label"><?php echo CHECKOUT_BAR_CONFIRMATION; ?></span>
                </button>
              </div>
            </div>
          </div></form>
		  </div>
