<?php
    if(isset($_POST['delete'])){
        $id = $_POST['delete_studentresult'];

        $all_studentresult = "DELETE FROM studentresult WHERE stresult_id=$id";

        if($db->query($all_studentresult) == TRUE){
            echo "<script>alert('Student Result Deleted')</script>";
        }else{
            echo "<script>alert('Student Result not deleted')</script>";
        }

    }

?>
            <!-- Recent Sales Start -->
            <div class="container-fluid pt-4 px-4">
                <div class="bg-secondary text-center rounded p-4">
                    <div class="d-flex align-items-center justify-content-between mb-4">
                        <h6 class="mb-0">Add ASS FT Shipping Management Result</h6>
                        
                    </div>
                    <div class="table-responsive">
                      <div class="container">
                        <div class="row">
                            <div class="col-md-2"></div>
                            <div class="col-md-8">
                                <form method="POST" action="index.php?env=ASSFTShippingManagementresult" enctype="multipart/form-data">
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                            
                                                <select name="sname" value="<?php if(isset($_POST['sname'])){echo $_POST['sname']; } ?>" class="form-control" required>
                                                    <option value="">Select Student</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM portal where Dept = 'ASS FT Shipping Management'");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['sname']); ?>"><?php echo htmlentities($row['sname']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select><br/>
                                               
                                            </div>
                                            <div class="col-md-6">
                                            <select name="sessiond" id="" class="form-control" required>
                                                <option value="Select session">Select session</option>
                                                <option value="2023/2024">2023/2024</option>
                                            </select> <br/>  
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                            <select name="Dept" value="<?php if(isset($_POST['Dept'])){echo $_POST['Dept']; } ?>" class="form-control" required>
                                                    
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT DISTINCT Dept FROM portal where Dept = 'ASS FT Shipping Management'");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['Dept']); ?>"><?php echo htmlentities($row['Dept']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select><br/> 
                                            </div>
                                            <div class="col-md-6">
                                            <select name="semesta" value="<?php if(isset($_POST['leveld_name'])){echo $_POST['leveld_name']; } ?>" class="form-control" required>
                                                    <option value="">Select Semesta</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM leveld ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['leveld_name']); ?>"><?php echo htmlentities($row['leveld_name']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                               
                                                <input type="text" name="cgpa" class="form-control" placeholder="Student CGPA" required><br/>
                                            </div>
                                            <div class="col-md-6">
                                                
                                                <input type="text" name="gpa" class="form-control" placeholder="Student GPA" required><br/>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                                
                                                <input type="file" name="resultfile" class="form-control" placeholder="Student Result" required><br/>
                                            </div>
                                            <div class="col-md-6">
                                               
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <button type="submit" name="addstudentresult" class="btn btn-outline-primary btn-block">Add Student Result</button>
                                    </div>
                                </form>

                               
                            </div>
                            <div class="col-md-2"></div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <!-- view all student result -->
                               <table class="table text-start align-middle table-bordered table-hover mb-0" style="margin-top:50px;">
                                    <thead>
                                        <tr>
                                            <!--<th scope="col">S/N</th>-->
                                            <th scope="col">Student Name</th>
                                            <th scope="col">Session</th>
                                            <th scope="col">Semesta</th>
                                            <th scope="col">CGPA</th>
                                            <th scope="col">GPA</th>
                                            <th scope="col">Result</th>
                                            <th scope="col">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <?php
    // Fetch all student results
    $all_studentresult = mysqli_query($db, "SELECT * FROM studentresult where Dept = 'ASS FT Shipping Management'");

    // Check if there are any student results
    if(mysqli_num_rows($all_studentresult) > 0) {
        // Loop through each student result
        foreach($all_studentresult as $studentresult) {
?>
            <tr>
                <!--<td><?php echo $studentresult['stresult_id']; ?></td>-->
                <td><?php echo $studentresult['sname']; ?></td>
                <td><?php echo $studentresult['sessiond']; ?></td>
                <td><?php echo $studentresult['semesta']; ?></td>
                <td><?php echo $studentresult['cgpa']; ?></td>
                <td><?php echo $studentresult['gpa']; ?></td>
                <td>
                    <!-- View button to display the PDF document -->
                    <a href="index.php?env=ASSFTShippingManagementresult&stresult_id=<?php echo $studentresult['stresult_id']; ?>" target="_blank">View</a>
                </td>
                <td>
                    <!-- Delete button to delete the student result -->
                    <form method="POST">
                        <input type='hidden' name='delete_studentresult' value='<?php echo $studentresult['stresult_id']; ?>'>
                        <input type='submit' class='btn btn-sm btn-danger' name='delete' value='Delete'>
                    </form>
                </td>
            </tr>
<?php
        }
    } else {
        // Display a message if there are no student results
        echo "<tr><td colspan='8'>No student results found.</td></tr>";
    }
?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                      </div>
                    </div>
                </div>
            </div>
            <!-- Recent Sales End -->

            <!-- footer -->
           <?php include("footer.php"); ?>

                    <?php
if(isset($_POST['addstudentresult'])){
   $sname = mysqli_real_escape_string($db, $_POST['sname']);
   $sessiond = mysqli_real_escape_string($db, $_POST['sessiond']);
   $Dept = mysqli_real_escape_string($db, $_POST['Dept']);
   $semesta = mysqli_real_escape_string($db, $_POST['semesta']);
   $cgpa = mysqli_real_escape_string($db, $_POST['cgpa']);
   $gpa = mysqli_real_escape_string($db, $_POST['gpa']);

  // name of the uploaded file
  $result = $_FILES['resultfile']['name'];

   // destination of the file on the server
   $destination = 'upload/' .$result;

      // get the file extension
      $extension = pathinfo($result, PATHINFO_EXTENSION);

      // the physical file on a temporary uploads directory on the server
      $file = $_FILES['resultfile']['tmp_name'];
      $size = $_FILES['resultfile']['size'];

      if(!in_array($extension, ['pdf'] )){
        echo "<script>alert('Your file extension must be .pdf')</script>";
      }elseif($_FILES['resultfile']['size'] > 1500000){
        echo "<script>alert('File too large!')</script>";
      }else{
        // move the uploaded (temporary) file to the specified destination
        if(move_uploaded_file($file, $destination)){
            $query = "INSERT INTO studentresult(sname, sessiond, Dept, semesta, cgpa, gpa, resultfile) 
            VALUES('$sname','$sessiond', '$Dept', '$semesta', '$cgpa', '$gpa', '$result')";

            if(mysqli_query($db, $query)){
                header("Location: index.php?env=ASSFTShippingManagementresult");
            }else{
                echo "<script>alert('Record Not Inserted Successfully')</script>"; 
            }
        }
      }
}



//view student result
if (isset($_GET['stresult_id'])) {
    // Sanitize and validate the input
    $id = mysqli_real_escape_string($db, $_GET['stresult_id']);

    // Fetch file to download from the database
    $sql = "SELECT * FROM studentresult WHERE stresult_id=$id";
    $result = mysqli_query($db, $sql);

    // Check if a record with the given ID exists
    if ($result && mysqli_num_rows($result) > 0) {
        $file = mysqli_fetch_assoc($result);
        $filepath = 'upload/' . $file['resultfile'];

        // Check if the file exists
        if (file_exists($filepath)) {
            // Set appropriate headers
            header('Content-Description: File Transfer');
            header('Content-Type: application/pdf');
            header('Content-Disposition: inline; filename="' . basename($filepath) . '"');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));

            // Stream the file content
            ob_clean();
            flush();
            readfile($filepath);

            // Update downloads count
            $newCount = $file['downloads'] + 1;
            $updateQuery = "UPDATE studentresult SET downloads=$newCount WHERE stresult_id=$id";
            mysqli_query($db, $updateQuery);
            exit;
        } else {
            // File not found
            echo 'File not found.';
        }
    } else {
        // Record with the given ID not found
        echo 'Record not found.';
    }
}
 
?>

