<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        form {
            background-color: #ffffff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            margin: auto;
        }

        label {
            display: block;
            margin: 10px 0 5px;
        }

        input[type="text"], input[type="file"], input[type="submit"], select {
            width: 100%;
            padding: 10px;
            margin: 5px 0 15px;
            border: 1px solid #cccccc;
            border-radius: 4px;
        }

        input[type="submit"] {
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<?php
include 'connection/config.php';
$host = 'localhost'; // Hostname
$dbname = 'oscotech_cbt'; // Database name
$username = 'oscotech_oscopt'; // Database username
$password = 'oscopt'; // Database password
$pdo = new PDO('mysql:host=localhost;dbname=oscotech_cbt', 'oscotech_oscopt', 'oscopt');

// Fetch score tables from question_log
$stmt = $pdo->query("SELECT DISTINCT question_level_score FROM question_log");
$scoreTables = $stmt->fetchAll(PDO::FETCH_COLUMN);

// Handle score submission
if (isset($_POST['print_score'])) {
    $selectedTable = $_POST['question_level_score'];
    $dept=$_POST['dept'];

    // Fetching details for the selected score table
    $stmt = $pdo->prepare("SELECT matric, fname, score, dept FROM $selectedTable WHERE dept LIKE '%$dept%' ORDER BY matric");
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Fetch additional details from question_log for headings
    $stmtLog = $pdo->prepare("SELECT level, dept, course_code, course_title FROM question_log WHERE question_level_score = ?");
    $stmtLog->execute([$selectedTable]);
    $logDetails = $stmtLog->fetch(PDO::FETCH_ASSOC);

    // Start printing
    echo "<div id='printableArea'>";
 
    echo "<h2><center>OSUN STATE COLLEGE OF TECHNOLOGY, ESAOKE</center></h2><br>";
    // echo "src=images/logo.jpg";
    echo "<h3><center>Semester: First Semester  Session: 2025/2026 </center></h3> <br>" ;

    echo "<h3><center>Computer Based Examination Result</center></h3> <br>" ;

      echo "<h3><center>Re-Runners Result</center></h3> <br>" ;
    echo "<p>Level: " . htmlspecialchars(strtoupper($logDetails['level'])) . " </p> " ;

    echo "<p>Department: " . htmlspecialchars($_POST['dept']) . "</p>"   ;
   
     echo "<p>Course Code: " . htmlspecialchars(strtoupper($logDetails['course_code'])) . "</p>";
    echo "<p>Course Title: " . htmlspecialchars(strtoupper($logDetails['course_title'])) . "</p> <br>";
    echo "<table border='1' style='width: 100%; border-collapse: collapse;'>
            <tr>
                <th>S/N</th>
                <th>Matric Number</th>
                <th>Full Name</th>
                <th>Score (60)</th>
		<!--<th>dept</th>-->
            </tr>";

                 $sn = 1;
    foreach ($results as $row) {
		$apaopa =$row['score'] * 2;
//echo $apaopa;
        echo "<tr>
                <td> ". $sn++ ." </td>
                <td>" . htmlspecialchars($row['matric']) . "</td>
                <td>" . htmlspecialchars(strtoupper(($row['fname']))) . "</td>
 		<td>   $apaopa  </td>
               <!-- <td>" . htmlspecialchars($row['score']) . "</td>
 		<td>" . htmlspecialchars($row['dept']) . "</td> -->
              </tr>" ;
    }
    echo "</table></div>";

    echo "<button onclick='printDiv()'>Print</button>";
    echo "<script>
        function printDiv() {
            var printContents = document.getElementById('printableArea').innerHTML;
            var originalContents = document.body.innerHTML;

            document.body.innerHTML = printContents;
            window.print();
            document.body.innerHTML = originalContents;
        }
    </script>";
    exit; // Stop further processing
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Score Sheet</title>
    <style>
        @media print {
            body * {
                display: none; /* Hide everything */
            }
            #printableArea, #printableArea * {
                display: block; /* Show only the printable area */
            }
        }
    </style>
</head>
<body>

<h2>Score Sheet View</h2>

<!-- Dropdown to select score table -->
<form method="POST">
    <label for="question_level_score">Select Score Table:</label>
    <select name="question_level_score" id="question_level_score" required>
        <option value="">-- Select Score Table --</option>
        <?php foreach ($scoreTables as $table): ?>
            <option value="<?php echo htmlspecialchars($table); ?>"><?php echo htmlspecialchars($table); ?></option>
        <?php endforeach; ?>
    </select>

<label>Department:</label>
        <select name="dept" id="dept" required>
        <option value="">--Select Department--</option>
         <option value="Accountancy">Accountancy</option>
                <option value="Banking and Finance">Banking and Finance</option>
                <option value="Business Administration and Management">Business Administration and Management</option>
                <option value="Civil Engineering">Civil Engineering</option>
                <option value="Building Technology">Building Technology</option>
                <option value="URBAN AND REGIONAL PLANNING">URBAN AND REGIONAL PLANNING</option>
                
                 <option value="QUANTITY SURVEYING">QUANTITY SURVEYING</option>
                <option value="Surveying and Geo-informatics">Surveying and Geo-informatics</option>
                <option value="Electrical Electronic Engineering">Electrical Electronic Engineering</option>
                
                 <option value="Welding Engineering Technology">Welding Engineering Technology</option>  
                 
                <option value="CIVIL ENGINEERING TECHNOLOGY">CIVIL ENGINEERING TECHNOLOGY</option>     
                <option value="Statistics">Statistics</option>
                <option value="Estate Management">Estate Management</option>
                <option value="MECHANICAL ENGINEERING TECHNOLOGY (PLANT) (PLANT)">MECHANICAL ENGINEERING TECHNOLOGY (PLANT) (PLANT)</option>
               <option value="MECHANICAL ENGINEERING TECHNOLOGY (MANUFACTURING) (MANUFACTURING)">MECHANICAL ENGINEERING TECHNOLOGY (MANUFACTURING) (MANUFACTURING)</option>  
                
                MECHANICAL ENGINEERING TECHNOLOGY (MANUFACTURING) (MANUFACTURING)
                <option value="Architectural Technology">Architectural Technology</option>
                <option value="Science Laboratory Technology">Science Laboratory Technology</option>
                
                <option value="Mechanical Engineering">Mechanical Engineering</option>
                <option value="Electrical Electronics Engineering Technology (Electronics/Telecommunication)">Electrical Electronics Engineering Technology (Electronics/Telecommunication)</option>
                <option value="Electrical Electronics Engineering Technology (Power/Machine)">Electrical Electronics Engineering Technology (Power/Machine)</option>
                
                 <option value="Applied Science (Microbiology Option)">Applied Science (Microbiology Option)</option>
                <option value="Applied Science Chemistry (Option)">Applied Science Chemistry (Option)</option>
                <option value="Applied Science (Biochemistry Option)">Applied Science (Biochemistry Option)</option>
                
	 <option value="Applied Science (Physics with Electronics Option)">Applied Science (Physics with Electronics Option)</option>

   <option value="Applied Science (Environmental Biology Option)">Applied Science (Environmental Biology Option)</option>

                <option value="Marketing">Marketing</option>
                <option value="Taxation">Taxation</option>
                
		<option value="Mechatronics Engineering">Mechatronics Engineering</option>               
			<option value="Welding Engineering Technology">Welding Engineering Technology</option>

                <option value="Photography">Photography</option>
                <option value="Arts and Design">Arts and Design</option>
                 <option value="Computer Science">Computer Science</option>
                
                <option value="Computer Engineering">Computer Engineering</option>
                <option value="Bio-chemistry">Biochemistry</option>
                
 <option value="Public Administration">Public Administration</option>
 <option value="Computer Science (Software and Web Development Option)">Computer Science (Software and Web Development Option)</option>
 <option value="NETWORKING AND CLOUD COMPUTING">NETWORKING AND CLOUD COMPUTING</option>

              
                <option value="Agricultural Technology">Agricultural Technology</option>
                <option value="Library and Information Science">Library and Information Science</option>
                
                <option value="Office Technology and Management">Office Technology and Management</option>
                <option value="Multimedia Technology">Multimedia Technology</option>
                <option value="WELDING AND FABRICATION ENGINEERING TECHNOLOGY">WELDING AND FABRICATION ENGINEERING TECHNOLOGY</option>
                <option value="Water Resources Engineering Technology">Water Resources Engineering Technology</option>                 
          </select>
    <br>

    <Input type="submit" name="print_score">
</form>


</body>
</html>
