<?php
//include "application/scripts/connect.php";
if(isset($_POST['exam_schedule_id'])){
define('BASEPATH', "/smartsms/");
define('ENVIRONMENT', "production");
include "../config/database.php";

$hostname = $db['default']['hostname'];
$username = $db['default']['username'];
$password = $db['default']['password'];
$database = $db['default']['database'];
$dbdriver = $db['default']['dbdriver'];


// Create connection
$conn_db = mysqli_connect($hostname, $username, $password, $database);
// Check connection
if (!$conn_db) {
    die("Connection failed: " . mysqli_connect_error());
}
	//student_session,current_term,this_student_id,type,this_comment
	$exam_schedule_id = $_POST['exam_schedule_id'];
	$csv_content = $_POST['csv_content'];
	
	if($csv_content != ""){
	$explode_csv_content = explode(",",$csv_content);
	$trimmed_csv = $explode_csv_content[1];
	$decode_csv = base64_decode($trimmed_csv);
	
	$csv_rows =  explode("\n",$decode_csv);
	array_pop($csv_rows);
	
	$client_effect = array();
	
	$csv_rows_count  = 0;
	foreach($csv_rows as $key => $result){
		if($key > 0){
			$result_data = explode(",",$result);
			$sn = $result_data[0];
			$admision_no = $result_data[1];
			$student_name = $result_data[2];
			$ca1 = $result_data[3];
			$ca2 = $result_data[4];
			$ca3 = $result_data[5];
			$ca4 = $result_data[6];
			$exam = $result_data[7];
			$att = $result_data[8];
			if(strlen($att) > 1){
			$att = substr($att, 0, 1);
			}

			$attedance = "pre";
			if($att == "1"){
				$attedance = "pre";
			}
			
			if($att == ""){
				$attedance = "ABS";
			}
			
			if($att == "0"){
				$attedance = "ABS";
			}
			

			
			$admision_no = str_replace("@","",$admision_no);
			
			
			$sql = "SELECT * from `students` WHERE admission_no='$admision_no' ORDER BY `id` DESC LIMIT 1;";
			$query = mysqli_query($conn_db, $sql);
			if(mysqli_num_rows($query) != 0){
			$row = mysqli_fetch_assoc($query);
			$student_id = $row['id'];
	
					$sql2 = "SELECT * from `exam_results` WHERE exam_schedule_id='$exam_schedule_id' AND student_id='$student_id' ORDER BY `id` DESC LIMIT 1;";
					$query2 = mysqli_query($conn_db, $sql2);
					if(mysqli_num_rows($query2) == 0){
						$sql3 = "INSERT INTO `exam_results` (`attendence`, `exam_schedule_id`, `student_id`, `ca1`, `ca2`, `ca3`, `ca4`, `get_marks`, `note`, `is_active`, `created_at`, `updated_at`)
								VALUES ('$attedance', '$exam_schedule_id', '$student_id', '$ca1', '$ca2', '$ca3',  '$ca4', '$exam', NULL, 'no', now(), '0000-00-00 00:00:00');";
						mysqli_query($conn_db, $sql3);
						
						$client_effect[] = "client_effect('$exam_schedule_id','$student_id','$ca1','$ca2','$ca3','$ca4','$exam','$att');";
						$csv_rows_count  += 1;
					}else{
						$sql4 = "UPDATE `exam_results` SET
								`attendence` = '$attedance',
								`exam_schedule_id` = '$exam_schedule_id',
								`student_id` = '$student_id',
								`ca1` = '$ca1',
								`ca2` = '$ca2',
								`ca3` = '$ca3',
								`ca4` = '$ca4',
								`get_marks` = '$exam',
								`note` = NULL,
								`is_active` = 'no',
								`updated_at` = now()
								WHERE exam_schedule_id='$exam_schedule_id' AND student_id='$student_id';";
						mysqli_query($conn_db, $sql4);

						$client_effect[] = "client_effect('$exam_schedule_id','$student_id','$ca1','$ca2','$ca3','$ca4','$exam','$att');";
						$csv_rows_count  += 1;
					}
					
				
			}
		}
	}
	
	//$csv_rows_count = count($csv_rows);
	
	//echo "<script>alert('CSV PROCESSIING ' + '$csv_content')</script>";
	//echo "<script>dialog('CSV',\"$decode_csv\");</script>";
	echo "<script>alert('$csv_rows_count students(s) scores uploaded')</script>";
	
	echo "<script>";
	//echo "alert('All is well');";
	foreach($client_effect as $key=>$value){
		$value = str_replace("\n","",$value);
		$value = str_replace("\t","",$value);
	echo "$value";
	}
	echo "</script>";
	
	//$decode_csv
	echo "<div id='updated_csv_div'>$decode_csv</div>";
	echo "
	<script>
		document.getElementById('subject_csv_$exam_schedule_id').innerHTML = document.getElementById('updated_csv_div').innerHTML;
		//alert('Div value updated');
	</script>
	";

	}

}




?>  