<?php
include('include/entete.php');
include('include/entete_cfa.php');
include('config/config.inc.php');
include('config/lib.inc.php');
$maCleDeCryptage = getCleDeCryptage();	

if(isset($_POST['action']) && $_POST['action']=='go_jpo' && isset($_POST['detailsJpo'])){

	
	foreach($_POST as $cle=>$val) $$cle=$val;
	$detailsJpo = utf8_encode($detailsJpo);
	$detailsJpo=crypter($maCleDeCryptage,$detailsJpo);
	$dateCreation = date('Y-m-d');
	
	
	//Impossible de créer une jpo si une jpo existe déjà pour le CFA/LP
	$query="select * from jpo where id_eta=:etaID";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':etaID', $_SESSION['cfa_num'], PDO::PARAM_STR );
	$queryRequest->execute();
	$test=$queryRequest->rowCount();
	if($test>0){
		$_SESSION['msg']='Cet établissement a déjà renseigné ses journées portes ouvertes';
		header('location:po-cfa.php');
		exit();
	}
	
	
	$query = 'insert into jpo (id_eta,details_jpo,date_create_jpo) values (:num_eta,:details_jpo, :date_create_jpo)';
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':num_eta', $_SESSION['cfa_num'], PDO::PARAM_INT );
	$queryRequest->bindParam( ':details_jpo', $detailsJpo, PDO::PARAM_STR);
	$queryRequest->bindParam( ':date_create_jpo', $dateCreation, PDO::PARAM_STR );
	$queryRequest->execute();
	
	$_SESSION['msg']='Vos journées portes ouvertes ont été créées.';
	header('location:po-cfa.php');
	exit();
	
}

// Modification de la JPO enregistrée
if(isset($_POST['action']) && $_POST['action']=='mo_jpo'){
	foreach($_POST as $cle=>$val) $$cle=$val;
	
	if(!isset($id_jpo) || empty($id_jpo)){
		header('location:po-cfa.php');
		exit();
	}
	
	
	$query="select * from jpo where id_jpo=:id_cap and id_eta=:num_eta";
	$db = connect_base();
	$queryRequest = $db->prepare( $query );
	$queryRequest->bindParam( ':id_cap', $id_jpo, PDO::PARAM_INT );
	$queryRequest->bindParam( ':num_eta', $_SESSION['cfa_num'], PDO::PARAM_INT );
	$queryRequest->execute();
	if($queryRequest->rowCount()==0){
		header('location:po-cfa.phpp');
		exit();
	}else{
		$dataActual=$queryRequest->fetchAll();
		$actualdetails = $dataActual[0]['details_jpo'];
	}
	$newJpoDetails = crypter($maCleDeCryptage,utf8_encode($detailsJpo));
	$newDate = date('Y-m-d');
	
	
	$query='update jpo set details_jpo=:details_jpo, date_create_jpo=:date where id_jpo=:id_jpo';
		$queryRequest = $db->prepare( $query );
		$queryRequest->bindParam( ':details_jpo', $newJpoDetails, PDO::PARAM_STR );
		$queryRequest->bindParam( ':date', $newDate, PDO::PARAM_STR );
		$queryRequest->bindParam( ':id_jpo', $id_jpo, PDO::PARAM_INT );
		$queryRequest->execute();
	
		$_SESSION['msg']='Les journées portes ouvertes ont bien été modifiées.';
		header('location:po-cfa.php');
		exit();
	
	
}
