<?php
include_once('../controller/config.php');

	$id=$_GET['id'];
	$grade=$_GET['grade']; 
	$subject=$_GET['subject']; 
	$teacher=$_GET['teacher']; 
	$fee=$_GET['fee']; 

	$sql1="SELECT * FROM affectation_module where anneeId='$grade'";
	$result1=mysqli_query($conn,$sql1);
	$row1=mysqli_fetch_assoc($result1);
	$grade_id=$row1['id'];

	$sql2="SELECT * FROM affectation_module where moduleId='$subject'";
	$result2=mysqli_query($conn,$sql2);
	$row2=mysqli_fetch_assoc($result2);
	$subject_id=$row2['id'];

	$sql3="SELECT * FROM affectation_module where ensignaintId='$teacher'";
	$result3=mysqli_query($conn,$sql3);
	$row3=mysqli_fetch_assoc($result3);
	$teacher_id=$row3['id'];
	
	$sql4="SELECT * FROM affectation_module where anneeId='$grade_id' and moduleId='$subject_id' and ensignaintId='$teacher_id'";
	$result4=mysqli_query($conn,$sql4);
	$row4=mysqli_fetch_assoc($result4);

	$id4=$row4['id'];
	$grade_id4=$row4['grade_id'];	
	$subject_id4=$row4['subject_id'];
	$teacher_id4=$row4['teacher_id'];
	$fee4=$row4['fee'];

	$msg=0;
	$grade1=""; 
	$subject1=""; 
	$teacher1=""; 
	$fee1="";  
	
	if($grade_id == $grade_id4 && $subject_id == $subject_id4 && $teacher_id == $teacher_id4){
		if($id == $id4){//MSK-000143-U-1
		
			if($fee == $fee4){
				$msg+=3;
				//MSK-000143-U-2 You didn't make any of changes.:D	
				
			}else{//MSK-000143-U-3
				$sql5 = "update affectation_module set anneeId='".$grade_id."',moduleId='".$subject_id."',ensignaintId='".$teacher_id."',fee='".$fee."' where id='$id'";
				if(mysqli_query($conn,$sql5)){
					$msg+=1;
					//MSK-000143-U-4 The record has been successfully updated in the database
					
					$sql6="select affectation_module.fee as s_fee,affectation_module.anneeId as g_name,modules.moduleId as s_name,teacher.i_name as t_name
						   from affectation_module
						   inner join modules
						   on affectation_module.moduleId=modules.moduleId 
						   inner join teacher
						   on affectation_module.ensignaintId=teacher.id
						   where affectation_module.id='$id'";
					
					$result6=mysqli_query($conn,$sql6);
					$row6=mysqli_fetch_assoc($result6);//MSK-000143-U-5
					
					$grade1=$row6['g_name']; 
					$subject1=$row6['s_name']; 
					$teacher1=$row6['t_name']; 
					$fee1=$row6['s_fee']; 
						
				}else{
					$msg+=2;
					//MSK-000143-U-6 Connection problem
				}
				
			}
			
		}else{
			$msg+=4;
			//MSK-000143-U-7 The record is duplicated
		}
		
	}else{//MSK-000143-U-8
	
		$sql5 = "update affectation_module set grade_id='".$grade_id."',subject_id='".$subject_id."',teacher_id='".$teacher_id."',fee='".$fee."' where id='$id'";
		
		if(mysqli_query($conn,$sql5)){

			$msg+=1;
			//MSK-000143-U-9 The record has been successfully updated in the database
			
					$sql6="select affectation_module.fee as s_fee,affectation_module.anneeId as g_name,modules.moduleId as s_name,teacher.i_name as t_name
						   from affectation_module
						   inner join modules
						   on affectation_module.moduleId=modules.moduleId 
						   inner join teacher
						   on affectation_module.ensignaintId=teacher.id
						   where affectation_module.id='$id'";
			
			$result6=mysqli_query($conn,$sql6);
			$row6=mysqli_fetch_assoc($result6);//MSK-000143-U-10
			
			$grade1=$row6['g_name']; 
			$subject1=$row6['s_name']; 
			$teacher1=$row6['t_name']; 
			$fee1=$row6['s_fee']; 
				
		}else{
			$msg+=2;
			//MSK-000143-U-11 Connection problem
		}
		
	}

$res=array($grade1,$subject1,$teacher1,$fee1,$msg);
echo json_encode($res);//MSK-000143-U-12


?>