<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  
// include database and object files
include_once '../config/database.php';
include_once '../objects/identite.php';
  
// get database connection
$database = new Database();
$db = $database->getConnection();
  
// prepare product object
$identite = new Identite($db);
  
// get id of product to be edited
$data = json_decode(file_get_contents("php://input"));
  
// set ID property of product to be edited
$identite->id = $data->id;
  
// set product property values
$identite->login = $data->login;
$identite->password = $data->password;
$identite->pseudo = $data->pseudo;
$identite->age = $data->age;
$identite->poids = $data->poids;
$identite->taille = $data->taille;
$identite->sexe = $data->sexe;
$identite->niveaudusport = $data->niveaudusport;
    if($data->sexe=="Homme" && $data->niveaudusport=="Bas"){
        $identite->icalories = (13.707*$data->poids+4.923*$data->taille+77.607-6.673*$data->age)*1.3;
    }
    else if($data->sexe=="Homme" && $data->niveaudusport=="Moyen"){
        $identite->icalories = (13.707*$data->poids+4.923*$data->taille+77.607-6.673*$data->age)*1.55;
    }
    else if($data->sexe=="Homme" && $data->niveaudusport=="Elevé"){
        $identite->icalories = (13.707*$data->poids+4.923*$data->taille+77.607-6.673*$data->age)*1.75;
    }
    else if($data->sexe=="Femme" && $data->niveaudusport=="Bas"){
        $identite->icalories = (9.740*$data->poids+1.729*$data->taille+667.051-4.737*$data->age)*1.3;
    }
    else if($data->sexe=="Femme" && $data->niveaudusport=="Moyen"){
        $identite->icalories = (9.740*$data->poids+1.729*$data->taille+667.051-4.737*$data->age)*1.55;
    }
    else if($data->sexe=="Femme" && $data->niveaudusport=="Elevé"){
        $identite->icalories = (9.740*$data->poids+1.729*$data->taille+667.051-4.737*$data->age)*1.75;
    }

  
// update the product
if($identite->update()){
  
    // set response code - 200 ok
    http_response_code(200);
  
    // tell the user
    echo json_encode(array("message" => "Les modifications ont été prises en compte."));
}
  
// if unable to update the product, tell the user
else{
  
    // set response code - 503 service unavailable
    http_response_code(503);
  
    // tell the user
    echo json_encode(array("message" => "Impossible de mettre à jour"));
}
?>
