<?php // BEGIN PHP

//session_set_cookie_params(['lifetime' => 0, 'path' => '/', 'domain' => '', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax');
session_start(); // start session

require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/categories.lib.php';
include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";

$tag = GETPOST('tag', 'alpha');
$cat_id = GETPOST('cat', 'alpha');
$search = GETPOST('search_query', 'alpha');
$fulltag = GETPOST('fulltag', 'alpha');
$param_order_id = preg_match('/ORD=([^\.]+)/', $fulltag, $reg_order) ? $reg_order[1] : 0;
$param_customer_id = preg_match('/CUS=([^\.]+)/', $fulltag, $reg_customer) ? $reg_customer[1] : 0;

dol_syslog("The page paymentok.php is called with tag=".$tag." fulltag=".$fulltag);

// If not login redirect to login
if (empty($_SESSION['user']['id_user'])) {
    redirectToContainer('login-register');
}

// Check if all necessary parameters are passed.
if (empty($fulltag) || empty($param_order_id) || empty($param_customer_id)) {
	print 'An error has occurred. Bad value for some parameters.';
	exit;
}

// Check if the customer_id passed via GET is the same as the one stored in the session.
$user_id = $_SESSION['user']['id_user'];
$customer = new SocieteAccount($db);
$result = $customer->fetch($user_id);
$customer_id = $customer->fk_soc;
if ($customer_id != $param_customer_id) {
	print 'An error has occurred. Id of login account in session is not of the same thirdparty than the thirdparty ID received in parameter CUS.';
	exit;
}

// Check if there is a paid order for this customer with the sent information.
$sql_find_order = "SELECT c.rowid, c.ref, c.fk_statut, c.fk_soc, c.date_creation, c.module_source ";
$sql_find_order .= "FROM ".MAIN_DB_PREFIX."commande as c ";
$sql_find_order .= "WHERE c.fk_soc = " . $customer_id . " and c.rowid = " . $param_order_id . " and c.fk_statut= 1 and c.facture = 1 and c.module_source = 'marketplace' ";
$sql_find_order .= "limit 1";
$find_order = $db->query($sql_find_order);
$obj_order = $db->fetch_object($find_order);

if (empty($obj_order)) {
    print 'An error has occurred. Order with ID received '.$param_order_id.' and thirdparty found from session '.$customer_id.' was not found in database.';
    print $sql_find_order;
	exit;
} else {
    $order_ref = $obj_order->ref;
    $order_id = $obj_order->rowid;
    if(isset($_SESSION['products'])){
        unset($_SESSION['products']);
    }
}
// END PHP 
?>
<div id="page">
	<div class="header-container">
		<?php 
		    includeContainer('header');
		    $__PAGE__TITLE__ = $weblangs->trans("paymentOkPageTitle");
		?>
		<?php
            marketplaceNotifySellersBuyerByEmail($order_id);
		?>
	</div>
	<div class="columns-container">
		<div id="columns" class="container">
			<!--  Breadcrumb  -->
			<div class="breadcrumb clearfix">
				<a class="home" href="index.php" title="Return to Home"><i class="icon-home"></i></a>
				<span class="navigation-pipe">&gt;</span> Payment Success 
			</div>
			<!--  Breadcrumb  -->
			<div id="slider_row" class="row">
				<div id="top_column" class="center_column col-xs-12 col-sm-12"></div>
			</div>
			<div class="row">
				<!-- include lefet sidebar container -->
				<?php includeContainer('sidebar'); ?>
				<div id="center_column" class="center_column col-xs-12 col-sm-9">
					<div class="clearfix payment_accepted">
					    <h1><?php print $weblangs->trans("paymentAccepted"); ?></h1>
                        <p><?php print $weblangs->trans("paymentSuccessProcessed"); ?></p>
                        <p><?php print $weblangs->trans("youWillBeRedirectedToOrderPage"); ?></p>
                        <div class="countdown-container">
                            <div id="countdown">5</div>
                        </div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- .columns-container -->
	<div class="footer-container">
		<?php includeContainer('footer'); ?>
	</div>
</div>

<?php
if (!defined('USEDOLIBARRSERVER') && $website->virtualhost) {
	$url_order_details = $website->virtualhost . '/order-details.php?ref='.urlencode($order_ref);
} else {
	$url_order_details = DOL_URL_ROOT.'/public/website/index.php?website='.urlencode($website->ref).'&pageref=order-details&ref='.urlencode($order_ref);
}
?>

<script>
$(document).ready(function() {
  var urlOrderDetails = "<?php echo $url_order_details; ?>";
  var count = 5;

  var countdownInterval = setInterval(function() {
    count--;
    $("#countdown").text(count);
    
    if (count <= 0) {
      clearInterval(countdownInterval);
      window.location.href = urlOrderDetails;
    }
  }, 1000);
});
</script>

