<? include ("../settings.php"); ?>
<? include("session.php"); ?>
<?

    //$output = "\"SN\",\"Full Name\",\"Organisation\",\"Email\",\"RSVP\",\"Category\",\"Date Time Registered\",\"Registrant SN\",\"Replaced\",\"Replacing\",\"Attended\"\n";
    $results = $db->run("select * from tbl_registrant  limit 1");
    $result = $results[0];
    $output = "";
    foreach (array_keys($result) as $key){
        if ($output=="")
            $output = "\"".$key."\"";
        else
            $output .= ",\"".$key."\"";
    }
    $output .= ",\"Attended 1\",\"Attended 2\",\"Attended 3\",\"Attended 4\",\"Attended 5\",\"Attended 6\",\"Attended 7\",\"Attended 8\"\n";

	$file = $_SERVER['DOCUMENT_ROOT']."/MRC2024/csv/MRC2024.csv";
	$fp = fopen($file, 'w');
	fwrite($fp, $output);
    $results = $db->run("select * from tbl_registrant ORDER by registrant_id ASC");
	foreach ($results as $result){

        $bind = array(
            ":search1" => $result['registrant_id']
        );
        $attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=1", $bind);
        if (count($attend)>0)
            $attended1 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended1 = "No";
        $attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=2", $bind);
        if (count($attend)>0)
            $attended2 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended2 = "No";
		$attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=3", $bind);
        if (count($attend)>0)
            $attended3 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended3 = "No";
		$attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=4", $bind);
        if (count($attend)>0)
            $attended4 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended4 = "No";
		$attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=5", $bind);
        if (count($attend)>0)
            $attended5 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended5 = "No";
        $attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=6", $bind);
        if (count($attend)>0)
            $attended6 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended6 = "No";
		$attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=7", $bind);
        if (count($attend)>0)
            $attended7 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended7 = "No";
        $output = "";
		$attend = $db->select("tbl_registrant_attendance", "registrant_id = :search1 AND session_id=8", $bind);
		if (count($attend)>0)
            $attended8 = "Yes (".$attend[0]['attendance_datetime'].")- Admin ".$attend[0]['user_id'];
        else
            $attended8 = "No";
        $output = "";
        foreach ($result as $record){
            if ($output=="")
                $output = "\"".$record."\"";
            else
                $output .= ",\"".$record."\"";
        }
        
        $output .= ",\"$attended1\",\"$attended2\",\"$attended3\",\"$attended4\",\"$attended5\",\"$attended6\",\"$attended7\",\"$attended8\"\n";
        //echo $output;
		fwrite($fp, $output);
	}

	fclose($fp);

     // Set headers
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=MRC2024.csv");
     header("Content-Type: application/zip");
     header("Content-Transfer-Encoding: binary");

     // Read the file from disk
     readfile($file);

?>