<?php
	date_default_timezone_set('Europe/Madrid'); 
	require("../config/config.php");
	if (!isset($_SESSION)){
		session_name(SESSION);
		session_start();
	}
	require("../config/functions.php");
	header('Content-Type: text/html; charset=UTF-8');
	include("../config/languages/lang.".GetLang().".php");

	if (!empty($_POST["type"])){
		$type = GetConexion()->real_escape_string($_POST["type"])-1;
		$langs = ["pt","ro","en","es"];

		if (!file_exists("../config/languages/lang.".$langs[$type].".php")){
			echo '<script>SendNotification("'.$lang["idioma3"].'", "warning", "check");</script>';
			return;
		}

		if ($_SESSION["user_lang"] == $langs[$type]){
			echo '<script>SendNotification("'.$lang["idioma4"].'", "warning", "check");</script>';
			return;
		}

		if ($type >= 0 && $type <= 3){
			if (mysqli_query(GetConexion(), "update ".DB_ACCOUNT.".account set ".LANGUAGE_FIELD." = '".$langs[$type]."' where id = ".$_SESSION["user_id"]." limit 1;")){
				echo '<script>SendNotification("'.$lang["idioma1"].'", "success", "check");</script>';
				$_SESSION["user_lang"] = $langs[$type];
			}
			else{
				echo '<script>SendNotification("'.$lang["idioma2"].'", "danger", "check");</script>';
			}
		}
		echo '<meta http-equiv="Refresh" content="1;url=home/" />';
	}
?>