<?php

	session_start ();
	
	include('database.php');
	DB_connexion();
 	header( 'content-type: text/html; charset=utf-8' );
	
	if (!isset($_SESSION['nom']) && !isset($_SESSION['password']))
	{  
			header("Location: login.php");   
	}
/*	
CREATE TABLE IF NOT EXISTS configuration (
  id int(11) NOT NULL AUTO_INCREMENT,
  tarifCE varchar(30) NOT NULL,
  tarifnormal varchar(30) NOT NULL,
  seuilcreneau varchar(30) NOT NULL,
  PRIMARY KEY (id)
)*/
/*
CREATE TABLE IF NOT EXISTS `configuration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tarifCE` varchar(30) NOT NULL,
  `tarifnormal` varchar(30) NOT NULL,
  `tarif30` varchar(30) NOT NULL,
  `nb_creneau` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) 
*/
	
	if(isset($_POST['submit']))
	{
			if(isset($_POST['verrou']) && $_POST['verrou'] == "ok")
			{
			
				$tarifCE = $_POST['tarifCE'];		
				$tarifnormal = $_POST['tarifnormal'];
				$tarif30 = $_POST['tarif30'];
				$nb_creneau = $_POST['nb_creneau'];	
				$mode_debug = $_POST['mode_debug'];	
				
				// rafraichir 
				$_SESSION['tarifCE'] = $tarifCE;	
				$_SESSION['tarifnormal'] = $tarifnormal; 
				$_SESSION['tarif30'] = $tarif30; 
				$_SESSION['nb_creneau'] = $nb_creneau; 
				$_SESSION['mode_debug'] = $mode_debug; 
				
				$id = 1;
				$sql = "UPDATE configuration_v3  SET tarifCE = '$tarifCE',  tarifnormal ='$tarifnormal' , tarif30 ='$tarif30' , nb_creneau = '$nb_creneau' , mode_debug = '$mode_debug' WHERE id = '$id' ";
				//echo $sql;
				$result = mysqli_query($connexion, $sql );	
				if(!$result)
				{
					die('Impossible d\'exécuter la requête :' . mysql_error());
				}		
				
				echo "Paramétres pris en compte";
				echo '<br><br>';
			}	
	}
	 
	$sql = 'SELECT * FROM configuration_v3';
	mysqli_query($connexion, "SET NAMES 'utf8'");
	$result = mysqli_query($connexion, $sql );
	$data = mysqli_fetch_assoc($result);
	$tarifCE = $data['tarifCE'];		
	$tarifnormal = $data['tarifnormal'];
	$tarif30 = $data['tarif30'];
	$nb_creneau = $data['nb_creneau'];	
	$mode_debug = $data['mode_debug'];	
	
	

	
	//ALTER TABLE configuration ADD tarif30 VARCHAR(30) AFTER tarifnormal;
	//ALTER TABLE configuration ADD mode_debug VARCHAR(30) AFTER nb_creneau;
	// UPDATE copy_gestion SET formule = courte
	
/*	
	CREATE TABLE IF NOT EXISTS configuration (
  id int(11) NOT NULL AUTO_INCREMENT,
  tarifCE varchar(30) NOT NULL,
  tarifnormal varchar(30) NOT NULL,
  PRIMARY KEY (id)
)
	 */
	 
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tarification</title>
</head>		

<link rel="stylesheet" href="menu_style.css" type="text/css">

<body>

<script type="text/javascript">

function allLetter(inputtxt)  
          {   
          var letters = /^[A-Za-z]+$/;  
          if(inputtxt.value.match(letters))  
          {  
          alert('Your name have accepted : you can try another');  
          return true;  
          }  
          else  
          {  
          alert('Please input alphabet characters only');  
          return false;  
          }  
}  

function valide_champs()
{	
	var champ1 = document.getElementById('tarifCE').value;
	var champ2 = document.getElementById('tarifnormal').value;
	var champ3 = document.getElementById('tarif30').value;
	var champ4 = document.getElementById('mode_debug').value;
	
	var verif = true;
	var msg_erreur = new Array;
	
	if(isNaN(champ1) == true ||  isNaN(champ2) == true || isNaN(champ3) == true )
	{
			msg_erreur[0] = "Uniquement des chiffres !";
			verif = false;
	}
	
	if(champ1 == '' ||  champ2 == '' ||  champ3 == '' ||  champ4 == '' )
	{
			msg_erreur[0] = "Pas de champs vide !";
			verif = false;
	}
	
	var letters = /^[A-Za-z]+$/;  
    if(!champ4.match(letters))
	{
		msg_erreur[0] = "Mode debug : yes or no !";
		verif = false;
	}
	
	
	if(verif == true)
 	{ document.getElementById('verrou').value="ok";
 	}
 	
	if(verif == false)
 	{ 	var affiche = msg_erreur.join("\n");
		alert(affiche);
		document.getElementById('verrou').value="erreur";
 	}	
	
} 

</script>

	<link rel="stylesheet" href="menu_style.css" type="text/css">

	<div id='cssmenu'>
	<ul>
		<li><a href='home.php'><span>Home</span></a></li>
		<li><a href='selection_planning_mois.php'><span>Planning</span></a></li>
		<li><a href='selection_gestion_mois.php'><span>Gestion</span></a></li>
		<li><a href='selection_statistique_mois.php'><span>Statistiques</span></a></li>
<?php
		if($_SESSION['nom'] == "admin")
		{ 	
			echo '<li><a href="deplacer_un_rendezvous_admin.php"><span>Déplacer</span></a></li>';
			echo '<li><a href="configuration.php"><span>Configuration</span></a></li>';
		}
		else
		{
			echo '<li><a href="deplacer_un_rendezvous_user.php"><span>Déplacer</span></a></li>';
		}
?>
		<li><a href='change_password.php'><span>Changer password</span></a></li>
		<li><a href='logout.php'><span>Déconnexion</span></a></li>
		<li><a href='#'><span><?php echo $_SESSION['nom'] .' ' .$_SESSION['prenom'] .' connecté' ?></span></a></li>
		<li><a href='#'><span><?php echo $_SESSION['year']; ?></span></a></li>
	</ul>
	</div>
	<br>
	<br>
	
	
	<form action="" method="post" id="editer_tarif" name="editer_tarif" >
	<input type="hidden" name="verrou" id="verrou" value="">

	<table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="600">
	<tr bgcolor="#F1F3F5">
	<td colspan="2">Paramétres : </td>
	</tr>
	</tr>
		
	</td>
	</tr>
		
	<tr bgcolor="#FFFFFF">
	<td align="left" valign="top">Tarif CE (subvention) :</td>
	<td><input type="text" id="tarifCE" = name="tarifCE" size="5" maxlength="5"  value="<?php echo $tarifCE; ?>" id="annee" >&nbsp;</td>
	</tr>

	<tr bgcolor="#FFFFFF">
	<td align="left" valign="top">Tarif normal :</td>
	<td><input type="text" id="tarifnormal" name="tarifnormal" size="5" maxlength="5"  value="<?php echo $tarifnormal; ?>" id="date" >&nbsp;</td>
	</tr>
	
	<tr bgcolor="#FFFFFF">
	<td align="left" valign="top">Tarif 30mins :</td>
	<td><input type="text" id="tarif30" name="tarif30" size="5" maxlength="5"  value="<?php echo $tarif30; ?>" id="date" >&nbsp;</td>
	</tr>
	
	<tr bgcolor="#FFFFFF">
	<td align="left" valign="top">Nombre de créneaux par semaine (sans supplément):</td>
	<td><input type="text" id="nb_creneau" name="nb_creneau" size="5" maxlength="5"  value="<?php echo $nb_creneau; ?>" id="date" >&nbsp;</td>
	</tr>	
				
	<tr bgcolor="#FFFFFF">
	<td align="left" valign="top">Mode debug:</td>
	<td><input type="text" id="mode_debug" name="mode_debug" size="5" maxlength="5"  value="<?php echo $mode_debug; ?>" id="date" >&nbsp;</td>
	</tr>	
							
				
	<tr bgcolor="#F1F3F5">
	<td colspan="2" align="center">
	
    <input type="submit" name="submit" value="Enregister" onclick="valide_champs()"/>
	</td>
	</tr>
		
	</table>
	</div>
	</div>
		
	</form>
	
	</body>
	</html>