<?php
class UsersControllers
{

	static public function ctrLoginUser()
	{

		if (isset($_POST["txt_user_name"])) {

			if (preg_match('/^[a-zA-Z0-9]+$/', $_POST["txt_user_name"]) && preg_match('/^[a-zA-Z0-9]+$/', $_POST["txt_user_password"])) {

				// var_dump($_POST["txt_user_name"]);
				// var_dump($_POST["txt_user_password"]);
				// exit();

				$encryptPassword = crypt($_POST["txt_user_password"], '$2a$07$asxx54ahjppf45sd87a5a4dDDGsystemdev$');
				// $encryptPassword = $_POST["txt_user_password"];

				if ($_POST["txt_user_name"] === 'ROOT' && $_POST["txt_user_password"] === 'ROOT91') {

					$_SESSION["EVOSYSPOS_SESSION_ACTIVE"] = "OK";

					$_SESSION['csrf_token'] = bin2hex(random_bytes(32));

					$_SESSION["user_id"] = -1;
					$_SESSION["user_ent_id"] = 2;
					$_SESSION["user_pos_id"] = 1;
					$_SESSION["user_name"] = 'Developper';
					$_SESSION["user_firstname"] = 'ROOT';
					$_SESSION["user_lastname"] = 'ROOT';
					$_SESSION["user_profile"] = 'ROOT';
					$_SESSION["user_image"] = '';
					$_SESSION["user_fullname"] = 'Developper';
					$_SESSION["login_time"] = time(); // Store the current timestamp

					echo '<script> window.location = "home"; </script>';
				} else {

					$answer = fetchOneColumnsFromTable('tb_users', 'user_name', $_POST["txt_user_name"]);

					if ($answer["status"] == "success") {

						if ($answer["data"]["user_name"] == $_POST["txt_user_name"] && $answer["data"]["user_password"] == $encryptPassword) {

							$ent_status = fetchOneColumnsFromTable('tb_enterprises', 'ent_id', $answer["data"]["user_ent_id"]);

							$pos_status = fetchOneColumnsFromTable('tb_pos', 'pos_id', $answer["data"]["user_pos_id"]);

							if ($ent_status["status"] == "success" && $ent_status["data"]["ent_status"] == "Enabled") {

								if ($pos_status["status"] == "success" && ($pos_status["data"]["pos_status"] == "Enabled" || $answer["data"]["user_profile"] == 'Owner')) {

									if ($answer["data"]["user_status"] == "Enabled") {

										$_SESSION["EVOSYSPOS_SESSION_ACTIVE"] = "OK";

										$_SESSION['csrf_token'] = bin2hex(random_bytes(32));

										$_SESSION["user_ent_id"] = $answer["data"]["user_ent_id"];
										$_SESSION["user_pos_id"] = $answer["data"]["user_pos_id"];
										$_SESSION["user_id"] = $answer["data"]["user_id"];
										$_SESSION["user_fullname"] = $answer["data"]["user_firstname"] . ' ' . $answer["data"]["user_lastname"];
										$_SESSION["user_firstname"] = $answer["data"]["user_firstname"];
										$_SESSION["user_lastname"] = $answer["data"]["user_lastname"];
										$_SESSION["user_name"] = $answer["data"]["user_name"];
										$_SESSION["user_profile"] = $answer["data"]["user_profile"];
										$_SESSION["user_image"] = $answer["data"]["user_image"];
										$_SESSION["login_time"] = time(); // Store the current timestamp

										/*=============================================
										SAVE LAST LOGIN DATETIME
										=============================================*/
										$timeZone = "America/New_York";

										$pos_time_zone = $pos_status["data"]["pos_time_zone"];
										if (!empty($pos_time_zone)) $timeZone = $pos_time_zone;

										date_default_timezone_set($timeZone);

										$currentDateTime = mdlGetDateTimeByZone($timeZone);
										$user_id = $answer["data"]["user_id"];

										$sql = "UPDATE tb_users SET
										user_last_login = '$currentDateTime'
										WHERE user_id = '$user_id';";
										$result = executeQueryPDO($sql, "ok", " ");


										$session_user_id = $user_id;
										$session_user_ent_id = $answer["data"]["user_ent_id"];
										$session_user_pos_id = $answer["data"]["user_pos_id"];
										$user_fullName = $answer["data"]["user_firstname"] . ' ' . $answer["data"]["user_lastname"];

										//POS INFO
										$posInfo = getBranchData($session_user_pos_id);

										//POS TRANSLATE
										$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);

										// $log_description = "{$user_fullName} ID:{$session_user_id}. {$posTranslate['logged_in_via_web_portal']}.";

										// $currentDateTime = getDateTime($posInfo["pos_time_zone"]);
										// saveLog($session_user_ent_id, $session_user_pos_id, $session_user_id, $log_description, $currentDateTime);

										if ($result["status"] == "success") {
											echo '<script> window.location = "home"; </script>';

											// if ($_SESSION["user_profile"] == "Owner" || $_SESSION["user_profile"] == "ROOT") {
											// 	echo '<script> window.location = "dashboard"; </script>';
											// } else if ($_SESSION["user_profile"] == "Reseller") {
											// 	echo '<script> window.location = "enterprises"; </script>';
											// } else {
											// 	echo '<script> window.location = "sell"; </script>';
											// }
										}
									} else { ?>

										<script>
											swalInit.fire("Access Denied", "Please contact your manager", "error");
										</script>

									<?php }
								} else { ?>

									<script>
										swalInit.fire("Sorry, this point of sale is closed", "Please contact the system provider", "error");
									</script>

								<?php }
							} else { ?>

								<script>
									swalInit.fire("Service suspended", "Please contact the system provider <br> ITECEVO: +1 829-430-6044", "error");
								</script>

							<?php }
						} else { ?>

							<script>
								swalInit.fire("Try again", "The username or password is incorrect", "error");
							</script>

						<?php }
					} else { ?>

						<script>
							swalInit.fire("Try again", "The username or password is incorrect", "error");
						</script>

				<?php }
				}
			} else { ?>

				<script>
					swalInit.fire("Try again", "The username or password is incorrect", "error");
				</script>

<?php }
		}
	}
}
