<?php

if (!isset($_SESSION["user_id"]))
	require_once "server_files/403.php";

/** @var User $user */
$user = $_SESSION["user"];

if (isset($_POST["submitted"])) {
    $error_message = updateAccount();
} elseif (isset($_POST["submitted_password"])) {
    $error_message = updatePassword();
}

function updateAccount()
{
    global $URL_BASE, $MAIL_ADDRESS, $user;

    $surname = htmlspecialchars($_POST["surname"]);
    if (isset($surname) && $surname != "")
        $user->setSurname($surname);

    $first_name = htmlspecialchars($_POST["firstname"]);
    if (isset($first_name) && $first_name != "")
        $user->setFirstName($first_name);

    $birth_date = htmlspecialchars($_POST["birth_date"]);
    if (isset($birth_date) && $birth_date != "")
        $user->setBirthDate($birth_date);

    if (isset($_POST["gender"])) {
        $gender = htmlspecialchars($_POST["gender"]);
        if (isset($gender) && ($gender == "M" || $gender == "F"))
            $user->setGender($gender);
    }

    $address = htmlspecialchars($_POST["address"]);
    if (isset($address) && $address != "")
        $user->setAddress($address);

    $postal_code = htmlspecialchars($_POST["postal_code"]);
    if (isset($postal_code) && $postal_code != "")
        $user->setPostalCode($postal_code);

    $city = htmlspecialchars($_POST["city"]);
    if (isset($city) && $city != "")
        $user->setCity($city);

    $country = htmlspecialchars($_POST["country"]);
    if (isset($country) && $country != "")
        $user->setCountry($country);

    $phone_number = htmlspecialchars($_POST["phone_number"]);
    if (isset($phone_number) && $phone_number != "")
        $user->setPhoneNumber($phone_number);

    if (isset($_POST["school"])) {
        $school = htmlspecialchars($_POST["school"]);
        if (isset($school) && $school != "")
            $user->setSchool($school);
    }

    if (isset($_POST["class"])) {
        $class = htmlspecialchars($_POST["class"]);
        if (isset($class) && ($class == "terminale" || $class == "premiere" || $class == "seconde"))
            $user->setClass($class);
    }

	if (isset($_POST["responsible_name"])) {
		$responsible_name = htmlspecialchars($_POST["responsible_name"]);
		if (isset($responsible_name) && $responsible_name != "")
			$user->setResponsibleName($responsible_name);
	}

	if (isset($_POST["responsible_phone"])) {
		$responsible_phone = htmlspecialchars($_POST["responsible_phone"]);
		if (isset($responsible_phone) && $responsible_phone != "")
			$user->setResponsiblePhone($responsible_phone);
	}

	if (isset($_POST["responsible_email"])) {
		$responsible_email = htmlspecialchars($_POST["responsible_email"]);
		if (isset($responsible_email) && $responsible_email != "")
			$user->setResponsibleEmail($responsible_email);
	}

    if (isset($_POST["description"])) {
        $description = htmlspecialchars($_POST["description"]);
        if (isset($description) && $description != "")
            $user->setDescription($description);
    }

    $email = htmlspecialchars($_POST["email"]);
    if (isset($email) && $email != "" && filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $confirm_email_token = uniqid();
        $user->setConfirmEmailToken($confirm_email_token);

        $msg = "Vous venez de changer votre adresse mail. Veuillez désormais confirmer votre adresse mail en cliquant ici : $URL_BASE/confirmer_mail/$confirm_email_token";
        mail($email, "Changement d'adresse mail - TFJM²", $msg, "From: $MAIL_ADDRESS\r\n");
    }

    return false;
}

function updatePassword()
{
	global $user;

    $old = htmlspecialchars($_POST["old_password"]);
    $new = htmlspecialchars($_POST["new_password"]);
    $confirm = htmlspecialchars($_POST["confirm_password"]);

    if (!$user->checkPassword($old))
        return "L'ancien mot de passe est incorrect.";

    if (strlen($new) < 8)
        return "Le mot de passe doit comporter au moins 8 caractères.";

    if ($new != $confirm)
        return "Les deux mots de passe sont différents.";

    $user->setPassword($new);

    return false;
}

require_once "server_files/views/mon_compte.php";