<?php
    // Start the session
    session_start();
    
    //form values
    $storage = array();
    
    //returns the name of the array's index
    function rowName($index, $name){
        return "ID".$index."_".$name;
    }
    
    
    //after the submit 
    if(isset($_POST['ID1_devobjective']) && isset($_POST['ID1_actioncall70']) && isset($_POST['ID1_deadline70']) && 
        isset($_POST['ID1_resutlnote70']) && isset($_POST['ID1_actioncall20']) && isset($_POST['ID1_deadline20']) && 
        isset($_POST['ID1_resutlnote20']) && isset($_POST['ID1_actioncall10']) && isset($_POST['ID1_deadline10']) && 
        isset($_POST['ID1_resutlnote10']) && isset($_SESSION['idSession'])){
            
            //Array contains all the informations submitted by the user
        $storage = array();
        $development = array();
        
        //taking the current values
       $str = file_get_contents($_SESSION['idSession'].".json");
       $storage = json_decode($str, true);
       
       //here is the index of the objective field 
       $count = 1;
       
       while($_POST[rowName($count,"devobjective")] != null){
           
           $developmentStorage = array();
           
           $developmentStorage['Development Objective'] = $_POST[rowName($count,"devobjective")];
           
           $developmentStorage['Action70'] = $_POST[rowName($count,"actioncall70")];
           
           $developmentStorage['Deadline70'] = $_POST[rowName($count,"deadline70")];
           
           $developmentStorage['Note70'] = $_POST[rowName($count,"resutlnote70")];
           
           $developmentStorage['Action20'] = $_POST[rowName($count,"actioncall20")];
           
           $developmentStorage['Deadline20'] = $_POST[rowName($count,"deadline20")];
           
           $developmentStorage['Note20'] = $_POST[rowName($count,"resutlnote20")];
           
           $developmentStorage['Action10'] = $_POST[rowName($count,"actioncall10")];
           
           $developmentStorage['Deadline10'] = $_POST[rowName($count,"deadline10")];
           
           $developmentStorage['Note10'] = $_POST[rowName($count,"resutlnote10")];
           
           $count++;
           
           array_push($development,$developmentStorage);
       }
       
    $storage['Development plan'] = $development;
       
       //store elements in a json file
       $fp = fopen($_SESSION['idSession'].".json", 'w');
       fwrite($fp, json_encode($storage));
       fclose($fp);
       header('location: success.php');
    }
    
?>
<!doctype html>
<html class="fixed">
	<head>
	<head>
		<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="js/script_form_developement_plan.js"></script>
	</head>
	</head>
	<body>
	    <section class="body-sign">
			<div class="center-sign">
				<div id="wrapper">
					<form action="form_developement_plan.php" method="post" id="sign-up_area" role="form">
						<div id="entry1" class="clonedInput">
							<h2 id="reference" name="reference" class="heading-reference">Entry #1</h2>
							<fieldset>
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_devobjective">Objectifs de développement:</label>
    								<textarea id="ID1_devobjective" name="ID1_devobjective" class="form-control" placeHolder = "Entrez les objectifs ici"></textarea>
    							</div>
    
                                <label class="label_fn control-label" for="mesure">Actions à prendre :</label>
                                
                                <div class="input-group">
                                  <span class="input-group-addon" id="basic-addon2">70%</span>
                                  <input type="text" class="form-control" id = "ID1_actioncall70" name = "ID1_actioncall70" placeholder="Actions à entreprendre" aria-describedby="basic-addon2">
                                  <input type="text" class="form-control" id = "ID1_deadline70" name = "ID1_deadline70" placeholder="Échéancier" aria-describedby="basic-addon2">
                                  <input type="text" class="form-control" id = "ID1_resutlnote70" name = "ID1_resutlnote70" placeholder="Résultats et commentaires" aria-describedby="basic-addon2">
                                </div>
                                <br>
    
    							<!-- Text input-->
    							<div class="input-group">
                                  <span class="input-group-addon" id="basic-addon2">20%</span>
                                  <input type="text" class="form-control" id = "ID1_actioncall20" name = "ID1_actioncall20" placeholder="Actions à entreprendre" aria-describedby="basic-addon2">
                                  <input type="text" class="form-control" id = "ID1_deadline20" name = "ID1_deadline20" placeholder="Échéancier" aria-describedby="basic-addon2">
                                  <input type="text" class="form-control" id = "ID1_resutlnote20" name = "ID1_resutlnote20" placeholder="Résultats et commentaires" aria-describedby="basic-addon2">
                                </div>
                                <br>
                                
    							<!-- Text input-->
    							<div class="input-group">
                                    <span class="input-group-addon" id="basic-addon2">10%</span>
                                    <input type="text" class="form-control" id = "ID1_actioncall10" name = "ID1_actioncall10" placeholder="Actions à entreprendre" aria-describedby="basic-addon2">
                                    <input type="text" class="form-control" id = "ID1_deadline10" name = "ID1_deadline10" placeholder="Échéancier" aria-describedby="basic-addon2">
                                    <input type="text" class="form-control" id = "ID1_resutlnote10" name = "ID1_resutlnote10" placeholder="Résultats et commentaires" aria-describedby="basic-addon2">
                                </div>
                                <br>
    
                                </div><!-- end #entry1 -->
    							<!-- Button (Double) -->
    							<div>
    							    <button type="button" id="btnAdd" name="btnAdd" class="btn btn-info">add section</button>
    								<button type="button" id="btnDel" name="btnDel" class="btn btn-danger">remove section above</button>
    							</div>
    							<br>
    							<!-- Button -->
    							<div>
    								<button id="submit_button" name="submit_button" class="btn btn-primary">Submit</button>
    							</div>

						    </fieldset>
						 </div>
					</form>
    		    </div>
		    </div>
	    </section>
	</body>
</html>