<?php
session_start();
require_once "../models/Operations.php";
$pdo = Databases::connect();

$ord_det_code = $_POST["ord_det_code"];

if (empty($ord_det_code)) {
	exit();
}

$sqlHeader = $pdo->query("SELECT
	ord_det_pos_id
	,pos_business_category
	,CONCAT(ord_det_sale_id,'-',ord_det_number) AS orderNumber
	,ord_det_sale_type
	,table_description
	,cus_full_name AS cus_name
	,(SELECT CONCAT(user_firstname,' ',user_lastname) FROM tb_users WHERE user_id = ord_det_emp_id) AS emp_full_name
	,CONCAT(user_firstname,' ',user_lastname) AS user_full_name
	,ent_name
	,pos_name
	FROM tb_orders_request
	LEFT JOIN tb_enterprises ON ent_id = ord_det_ent_id
	LEFT JOIN tb_pos ON pos_id = ord_det_pos_id
	LEFT JOIN tb_tables ON table_id = ord_sale_table_id
	LEFT JOIN tb_customers ON cus_id = ord_det_cus_id
	LEFT JOIN tb_users ON user_id = ord_det_created_by
	WHERE ord_det_code = '{$ord_det_code}';")->fetch(PDO::FETCH_ASSOC);

if ($sqlHeader == false) {
	exit();
}

// USER INTERFACE TRANSLATE//POS TRANSLATE
$posInfo = getBranchData($sqlHeader['ord_det_pos_id']);
$posLang = $posInfo['pos_lang'];

if ($posLang == "es") {
	$getPosLanguage = file_get_contents("../lang/pos_es.json");
} else if ($posLang == "fr") {
	$getPosLanguage = file_get_contents("../lang/pos_fr.json");
} else {
	$getPosLanguage = file_get_contents("../lang/pos_en.json");
}
$posTranslate = json_decode($getPosLanguage, true);


if ($sqlHeader['ord_det_sale_type'] == "Local" && ($sqlHeader['pos_business_category'] == "Restaurant")) {
	$order_type = $posTranslate["local_sale_type"];
} else {
	$order_type = $sqlHeader['ord_det_sale_type'];
}

$customer_name = $sqlHeader['cus_name'];
$emp_full_name = $sqlHeader["emp_full_name"];

if (empty(trim((string) $customer_name))) {
	$customer_name = $emp_full_name;
}

//Establecemos la fecha actual de la impresion
$timeZone = $posInfo['pos_time_zone'] ?? "America/New_York";
date_default_timezone_set($timeZone);

$currentDateTimeByZone = mdlGetDateTimeByZone($timeZone);
?>

<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>

<body onload="window.print();">

	<style type="text/css">
		.tk_container {
			font-family: sans-serif;
		}

		.tx_center {
			text-align: center;
		}

		.tx_left {
			text-align: left;
		}

		.tx_right {
			text-align: right;
		}

		.first_line {
			font-size: 0.9em;
		}

		.big_text {
			font-size: 1.5em;
		}

		.medium_text {
			font-size: 1.0em;
		}

		.second_line {
			font-size: 0.7em;
		}

		.table_text_size td {
			font-size: 1em;
			text-align: center;
		}

		.italic {
			font-style: italic;
		}

		table thead td {
			border-top: 1.5px dashed;
			border-bottom: 1.5px dashed;
		}
	</style>

	<div class="tk_container col-lg-12 col-md-12 col-xs-12">

		<div class="medium_text tx_center"><b><?= $sqlHeader['ent_name']; ?></b></div>
		<div class="first_line tx_center"><b>POS: <?= $sqlHeader['pos_name']; ?></b></div>
		<div class="first_line tx_center"><b><?= "{$posTranslate["printed_date"]} {$currentDateTimeByZone}"; ?></b></div>

		<div style="margin-bottom: 10px;"></div>

		<div class="big_text tx_center" style="text-transform: uppercase;"><b><?= $order_type; ?></b></div>
		<div class="big_text tx_center"><b><?= "{$posTranslate["order_number"]} #{$sqlHeader['orderNumber']} / {$sqlHeader['table_description']}" ?></b></div>

		<div style="margin-bottom: 10px;"></div>

		<div class="first_line tx_center"><b><?= "{$posTranslate["customer"]}: {$customer_name}"; ?></b></div>
		<div class="first_line tx_center"><b><?= "{$posTranslate["attended_by"]}: {$sqlHeader['user_full_name']}"; ?></b></div>

		<div style="margin-bottom: 10px;"></div>

		<div>
			<table style="width:100%;" class="table_text_size">

				<thead>
					<tr style="height: 30px; text-transform: uppercase;">
						<td><b><?= $posTranslate["quantity"]; ?></b></td>
						<td><b><?= $posTranslate["product"]; ?></b></td>
					</tr>
				</thead>

				<tbody>

					<!-- START FOREACH DETAILS ENTRIES -->

					<?php

					//Obtenemos todos los detalles del pedido actual
					$sqlBody = "SELECT
					sale_det_pro_qty
					,pro_description
					FROM tb_sales_details
					LEFT JOIN tb_products ON sale_det_pro_id = pro_id
					WHERE sale_det_ord_det_code = '{$ord_det_code}'";

					$answer = executePDOFetchAll($sqlBody, "");

					foreach ($answer as $key => $value) {

						echo "<tr>";
						echo "<td><b>" . $value['sale_det_pro_qty'] . "</b></td>";
						echo "<td><b>" . $value['pro_description'] . "</b></td>";
						echo "</tr>";
					}

					?>
					<!-- END FOREACH DETAILS ENTRIES -->
				</tbody>
			</table>
		</div>
	</div>


</body>

</html>