<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

         <style>
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
}



th {
  text-align: center;
  padding: 2px;
}
td {
  text-align: center;
  padding: 8px;
}
#employee {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  border-collapse: collapse;
  width: 100%;
     }

#employee td, #employee th {
  border: 1px solid black;
  padding: 8px;
}

#employee tr:nth-child(even){background-color: #f2f2f2;}

#employee tr:hover {background-color: #ddd;}

#employee th {
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: left;
  background-color: #04AA6D;
  color: white;
}
/*#employee  td:nth-of-type(4)  {display: none;}
#employee  th:nth-of-type(4)  {display: none;}

#employee  td:nth-of-type(12)  {display: none;}
#employee  th:nth-of-type(12)  {display: none;}
 
#employee  td:nth-of-type(11)  {display: none;}
#employee  th:nth-of-type(11)  {display: none;}*/


#employee td {
  padding-top: 10px;
  padding-bottom: 10px;
  text-align: left;
  number-align: right;
  background-color: #;
  color: black;
 border-collapse: collapse;
  border: 1px solid black;
}


</style>

</head>
<div class="main">
<body>



<?php
include("includes/db.php");

echo "<button onclick='exportAsPDF()'>Export as PDF</button>";
echo "<button onclick='window.print()'>Print All Marksheets</button>";

$query = "SELECT * FROM schools LIMIT 1"; // Assuming you only have one school record in the database
$result = $conn->query($query);
$schoolDetails = $result->fetch_assoc();
$logoPath = $schoolDetails['logo'];

    if(isset($_POST['class_id'])) {
    $classId = $_POST['class_id'];
   
      $examId = $_POST['exam_id'];
        $issuedate = $_POST['issuedate'];
        $Chkdby = $_POST['checked_by'];
        $Preparedby = $_POST['prepared_by'];

    // Retrieve class and section names based on class_id
    $classQuery = "SELECT class_name, section FROM classes WHERE class_id = $classId";
    $classResult = $conn->query($classQuery);
    $classDetails = $classResult->fetch_assoc();
    $className = $classDetails['class_name'];
    $sectionName = $classDetails['section'];

    $studentsQuery = "SELECT * FROM students WHERE Class_id = $classId";
    $studentsResult = $conn->query($studentsQuery);
    
    $examquery = "SELECT * FROM examination_type WHERE id = $examId";
        $examresult = $conn->query($examquery);
        $examDetails = $examresult->fetch_assoc();
        $examtype = $examDetails['exam_type'];
        $examyear = $examDetails['exm_year'];

    

    while ($studentRow = $studentsResult->fetch_assoc()) {
        $studentId = $studentRow['student_id'];
        $studentName = $studentRow['name'];
        $stdsym = $studentRow['symbol_no'];
         $stdregd =  $studentRow['regd_id'];
            $stddob =  $studentRow['dob'];
            
            
"<div class=''>";
        echo "<center>";
        echo "<div class='container' style='border:9px double #0f0f0f'>";
        echo "<center>";
        echo "<div>";
        echo "<table>";
        echo "<thead>";
          echo " <th colspan='3' style='text-align:left'>IEMIS CODE: " . $schoolDetails['school_code'] . "</th>";
        
        echo "</thead>";
        echo "<tbody>";

        echo "<td >";
        echo "<img src='$logoPath' alt='Logo' width='100' height='90'>";
        echo "</td>";
        echo "<center>";
        echo "<td >";
        echo "<span style='margin-left:-10%; font-size:30px'><strong><b>" . $schoolDetails['school_name'] . "</b></strong></span> <br>";
        echo "<span style='margin-left:-10%; font-size:25px''>" . $schoolDetails['address'] . "</span> <br>";
        echo "<span style='margin-left:-10%; font-size:20px'> ESTD: " . $schoolDetails['establishment_year'] . "</span> <br>";
        echo "<span style='margin-left:-10%;font-size:18px'>Phone:" . $schoolDetails['phone'] . "</h4>";
        echo "</td>";
        echo "<tr>" ;
        echo "<td colspan='3' style='margin-bottom:-3%;'><h3><b><u> MARKSHEET </u></b></h3> </td>";
        echo "</tr>";
        echo "</center>";
        echo "</tbody>";
    

        echo "<tr> <td colspan='3' style='font-size:16px; text-align:left; padding:10px;'>
                THE MARK(S) SECURED BY <b>$studentName</b>  DATE OF BIRTH <b> $stddob </b> IEMIS REGD. NO. <b> $stdregd </b> 
                SYMBOL NO <b> $stdsym </b>  GRADE : <b>$className </b> SECTION: <b> $sectionName </b> IN THE <b> $examtype  
                </b> CONDUCTED BY SCHOOL IN <b> 2081 B.S. (2024 AD.) </b> ARE GIVEN BELOW. </td>";
                      echo "</tr>";
              echo "<td> </td>";
                echo "<tr>";

            $marksQuery = "SELECT subjects.subject_code, subjects.subject_name, 
                            subjects.theory_full_marks, subjects.internal_full_marks, 
                            subjects.theory_credit_hour, subjects.internal_credit_hour, 
                            marks.student_id, marks.class_id, marks.subject_id, 
                            marks.full_marks, marks.marks
                        FROM subjects
                        INNER JOIN marks ON subjects.id = marks.subject_id
                        WHERE marks.class_id = $classId AND marks.student_id = $studentId order by subject_id";

        $marksResult = $conn->query($marksQuery);

                $totalFullMarks = 0;
                $totalObtMarks = 0;


echo "<div class='container1'>";
        echo "<table clas='employee'>";

        echo "<thead style='border:1px solid black'>";
        echo "<tr style='border:1px solid black'>";
        echo "<th width='140px;' style='border:1px solid black'><strong>SUBJECT CODE</strong> </th>";
        echo "<th width='360px;' style='border:1px solid black'><left><strong>SUBJECT NAME</strong></left> </th>";
        
        // echo "<th style='border:1px solid black'><strong></strong> </th>";
        echo "<th style='border:1px solid black'><strong>FULL MARKS</strong> </th>";
        echo "<th style='border:1px solid black'><strong>PASS MARKS </strong> </th>";
        echo "<th style='border:1px solid black'><strong>OBT. MARKS[TH]</strong> </th>";
        echo "<th style='border:1px solid black'><strong>REMARK</strong> </th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        while ($marksRow = $marksResult->fetch_assoc()) {
            $subjectCode = $marksRow['subject_code'];
            $subjectName = $marksRow['subject_name'];
            $creditHour = $marksRow['theory_credit_hour'];
            $fullMarks = $marksRow['theory_full_marks'];
            $marks = $marksRow['marks'];
            $totalFullMarks += $marksRow['theory_full_marks'];
            $totalObtMarks += $marksRow['marks'];
            $passMarks = $fullMarks * 0.4;
            $percentage1= ($totalObtMarks / $totalFullMarks) * 100 ;
            
            $percentage = number_format($percentage1,2);


            $Obtmarks = $marks;
            $MP = $Obtmarks / $fullMarks;
            $GP = round(($MP * 4), 2);
            $grade = '';
            if ($MP >= 0.9) {
                $grade = 'A+';
            } elseif ($MP >= 0.8) {
                $grade = 'A';
            } elseif ($MP >= 0.7) {
                $grade = 'B+';
            } elseif ($MP >= 0.6) {
                $grade = 'B';
            } elseif ($MP >= 0.5) {
                $grade = 'C+';
            } elseif ($MP >= 0.4) {
                $grade = 'C';
            } elseif ($MP >= 0.35) {
                $grade = 'D';
            } elseif ($MP >= 0.0001) {
                $grade = 'NG';
            } else {
                $grade = 'ABSENT';
            }


            echo "<tr>";


                
                

            echo "<td  style='border:1px solid black'> $subjectCode </td>";
            echo "<td style='text-align:left; border:1px solid black'> $subjectName </td>";

            echo "<td style='border:1px solid black'> $fullMarks</td>";
            echo "<td style='border:1px solid black'> $passMarks</td>";
            echo "<td style='border:1px solid black'>$marks</td>";
            // echo "<td style='border:1px solid black'> </td>";
            // echo "<td style='border:1px solid black'> </td>";
            echo "<td style='border:1px solid black'></td>";

            echo "</tr>";
        }
        echo "<tr> <td colspan=2 style='border:1px solid black; align:right'><strong> TOTAL : </strong> </td>";
        echo "<td colspan=1 style='border:1px solid black'> <b>$totalFullMarks </b></td>";
        echo "<td colspan=1 style='border:1px solid black'> <b> - </b></td>";
        echo "<td colspan=1 style='border:1px solid black'><b>$totalObtMarks </b></td>";
        echo "<td style='border:1px solid black'> </td>";
        
        
        echo "</tr>";
        echo "</tbody>";
        echo "<tfooter>";
        echo "<tr>";
        echo "<td colspan=6> </td>";  //EXTRA CREDIT SUBJECT 
        echo "</tr>";

        echo "<tr>";
        echo "<td colspan=6 style='border:1px solid black' height='20px'> Percentage: $percentage %  </td>";
        // echo "<td style='border:1px solid black'> </td>";
        // echo "<td style='border:1px solid black'> </td>";
        // echo "<td style='border:1px solid black'> </td>";
        // echo "<td style='border:1px solid black'> </td>";
        // // echo "<td style='border:1px solid black'> </td>";
        // echo "<td style='border:1px solid black'> </td>";
        echo "</tr>";


        
         echo "<tr>";
        echo "<td></td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td colspan='2'> ";
        // echo "<td colspan='2'> Prepared By: .....................................";
        echo "<td></td>";
        // echo "<td></td>";
        echo "<td colspan='3' rowspan='2'> <img src='uploads/sign.png' alt='Logo' width='60' height='50'></td>";
      
        echo "</tr>";
        echo "<tr>";
        echo "<td colspan='2'> Checked By: .....................................";
        
        echo "<td colspan='1' style='color:#babcbf'> school seal </td>";
        
        echo "</tr>";
            echo "<tr>";
        echo "<td colspan='2'> Date of Issue: $issuedate B.S.";
      echo "<td></td>";
        echo "<td colspan='3' style='border-top: 2px dotted; font-size:12px'>SHER BAHADUR SHRESTHA<br>HEAD TEACHER </td>";
        
        echo "</tr>";
        echo "<tr >";
        echo "<td colspan=4></td>";
        echo "<td colspan=3></td>";
        echo "</tr>";
            //       echo "</tr>";
            //   echo "<td> </td>";
            //     echo "<tr>";
         echo "<tr>";
        //  echo "<td colspan=7> INTERNAL (IN) : THIS COVERS THE PARTICIPATION, PRACTICAL/PROJECT WORKS, COMMUNITY WORKS, INTERNSHIP, PRESENTATIONS TERMINAL EXAMINATIONS.</td> ";
        echo "</tr>";
                 echo "<tr>";
         echo "<td colspan=6>Email : mmohanmaya@gmail.com <span style='margin-left:20px' >Website : mohanmaya.edu.np </td> ";
        echo "</tr>";
           echo "<tr>";
         echo "<td colspan=2> Abs= ABSENT</td> ";
         echo "<td colspan=2> W=WITHHELD </td> ";

        echo "</tr>";

                                                       
                                                   
                                                       

        echo "</tfooter>";
        echo "</table>";
        echo "</div>";


    echo "</table>";
        echo "</div>";
        echo "</center>";
        echo "</div>";
        echo "</center>";
        echo "</span>";
        
   echo "</div>";

        // Add a page break after each student's marksheet
        echo "<div style='page-break-after: always;'></div>";
        echo "";
    }
}


?>

<script>
    function exportAsPDF() {
        // Code to export marksheets as PDF
        // You can use libraries like jsPDF to generate PDFs
        // For example:
        var doc = new jsPDF();
        doc.text("Marksheet content", 10, 10);
        doc.save('marksheet.pdf');
    }
</script>
</body>
</div>
</html>
