<?php
@include('idl_multi.php');
if(@$multi=="ON") {
	if(isset($_POST['etabchoix'])) {
		$_SESSION['etabchoix'] = $_POST['etabchoix'];
	}	
	include('idl_multi.php');
}
else {
	include('idl_connect_data.php');	
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Activer les exceptions

$bdd = null;
$charset = 'utf8mb4';
$fuseau = 'Europe/Paris';
$langue = 'fr';

try {
    if (isset($multi) && $multi === "ON") {
        $bdd = @mysqli_connect($server, $user, $pwd, $base);
    } else {
        $bdd = @mysqli_connect($server, $user, $pwd, $base);
    }

    mysqli_set_charset($bdd, $charset); // Charset par défaut

    // On teste si la table existe en tentant une requête simple
    $check_base = mysqli_query($bdd, "SELECT * FROM {$prefix}_parametres WHERE param_nom = 'freq_jhors'");

    // FUSEAU
    $res_fuseau = mysqli_query($bdd, "SELECT valeur FROM {$prefix}_parametres WHERE param_nom = 'fuseau'");
    if ($res_fuseau && ($tmp = mysqli_fetch_array($res_fuseau))) {
        $fuseau = $tmp['valeur'];
        date_default_timezone_set($fuseau);
    }

    // CHARSET
    $res_charset = mysqli_query($bdd, "SELECT valeur FROM {$prefix}_parametres WHERE param_nom = 'charset'");
    if ($res_charset && ($tmp = mysqli_fetch_array($res_charset))) {
        $charset = $tmp['valeur'];
        mysqli_set_charset($bdd, $charset);
    }

    // LANGUE
    $res_langue = mysqli_query($bdd, "SELECT valeur FROM {$prefix}_parametres WHERE param_nom = 'language'");
    if ($res_langue && ($tmp = mysqli_fetch_array($res_langue))) {
        $langue = $tmp['valeur'];
    }

    // Désactiver les modes stricts en local
    if ($server === "127.0.0.1" || $server === "localhost") {
        mysqli_query($bdd, "SET sql_mode=''");
    }

} catch (mysqli_sql_exception $e) {
    // La base existe, mais pas la table (ou erreur SQL)
    error_log("Erreur SQL : " . $e->getMessage());

    // Valeurs par défaut (charset déjà défini avant)
    date_default_timezone_set($fuseau); // ex: Europe/Paris
    // $langue = 'fr'; déjà mis par défaut
    // La page peut continuer vers login ou autre sans crash
}
$translations = [];
$inverseTranslations = [];
$constantsLoaded = false;
if(!isset($_SESSION['lang'])) {
	$_SESSION['lang'] = 'fr';
	if(isset($langue['valeur'])) {
		$_SESSION['lang'] = $langue['valeur'];
	}
}
$lang = $_SESSION['lang'];
$langFile = BASE_PATH."/include/lang/{$lang}.php";

// Charger les constantes et les traductions
if (file_exists($langFile)) {
    $langData = require_once($langFile);
    if (is_array($langData)) {
        $translations = $langData['translations'];
        $inverseTranslations = $langData['inverseTranslations'];
    }
}
else {
    error_log("Lang file not found: " . $langFile);
}
require_once('idl_variables.php');
?>
