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

$query = "SELECT * FROM schools LIMIT 1";
$result = $conn->query($query);
$schoolDetails = $result->fetch_assoc();
$logoPath = $schoolDetails['logo'];

if (isset($_POST['class_id'])) {
    $classId = $_POST['class_id'];
    $duplicateissuedate = $_POST['duplicateissuedate'];
    $Chkdby = $_POST['checked_by'];
    $Preparedby = $_POST['prepared_by'];
    $examTypeId = $_POST['exam_id'];
    $issuedate = $_POST['result_date'];
    $sectionId = $_POST['section_id'];
    // $session_id = $_POST['session_id'];

    $classQuery = "
        SELECT 
            c.class AS class_name,
            s.section_name
        FROM class_section cs
        INNER JOIN classes c ON c.class_id = cs.class_id
        INNER JOIN sections s ON s.section_id = cs.section_id
        WHERE cs.class_id = $classId
          AND cs.section_id = '$sectionId'
          AND cs.status = 1
        LIMIT 1
    ";

    $classResult = $conn->query($classQuery);
    $classDetails = $classResult->fetch_assoc();

    $className   = $classDetails['class_name'];
    $sectionName = $classDetails['section_name'];

    // Fetch school working days details
    $daysQuery = "SELECT working_days, school_days FROM school_days WHERE exam_type = $examTypeId";
    $daysResult = $conn->query($daysQuery);

    if ($daysResult && $daysResult->num_rows > 0) {
        $daysRow = $daysResult->fetch_assoc();
        $workingdays = $daysRow['working_days'];
        $schooldays = $daysRow['school_days'];
    } else {
        $workingdays = 0;
        $schooldays = 0;
    }

    // Fetch exam type details
    $examQuery = "SELECT exam_type, exm_year FROM examination_type WHERE id = $examTypeId";
    $examResult = $conn->query($examQuery);
    $examDetails = $examResult->fetch_assoc();
    $examType = strtoupper($examDetails['exam_type']);
    $examYear = $examDetails['exm_year'];

    // Modified query to filter students by section
    if ($sectionId !== 'all') {
        $studentsQuery = "
            SELECT DISTINCT s.*, sd.symbol_no 
            FROM students s
            INNER JOIN symbolno_data sd ON s.student_id = sd.student_id
            INNER JOIN student_class_section_data scsd ON s.student_id = scsd.student_id
            WHERE s.Class_id = $classId
            AND sd.exam_id = $examTypeId
            AND scsd.section_id = '$sectionId'
            AND scsd.status = 'active'
            ORDER BY sd.symbol_no ASC
        ";
    } else {
        // If section is 'all', get all students from the class
        $studentsQuery = "
            SELECT DISTINCT s.*, sd.symbol_no 
            FROM students s
            INNER JOIN symbolno_data sd ON s.student_id = sd.student_id
            WHERE s.Class_id = $classId
            AND sd.exam_id = $examTypeId
            ORDER BY sd.symbol_no ASC
        ";
    }
    
    $studentsResult = $conn->query($studentsQuery);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student Results - <?php echo $className; ?> - <?php echo $sectionName; ?></title>

    <style>
        body {
            font-size: 14px;
        }
        table th, table td {
            font-size: 13px;
        }
        button {
            font-size: 12px;
        }
        .icon-btn {
            display: inline-flex;
            align-items: center;
        }
        .icon-btn i {
            margin-right: 5px;
        }
        .page-header {
            background-color: #f8f9fa;
            padding: 5px;
            border-bottom: 2px solid #dee2e6;
            margin-bottom: 20px;
        }
        .filter-info {
            background-color: #e9ecef;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <!-- Page header with filter information -->
        <div class="page-header">
            <h4>Student Results</h4>
            <div class="filter-info">
                <strong>Class:</strong> <?php echo $className; ?> | 
                <strong>Section:</strong> <?php echo $sectionName; ?> | 
                <strong>Exam:</strong> <?php echo $examType . ' - ' . $examYear; ?>
            </div>
        </div>
        
        <div class="text-right mb-3">
            <button class="btn btn-secondary fa print" 
                onclick="getResultAll(
                    '<?php echo $examTypeId; ?>', 
                    '<?php echo $classId; ?>', 
                    '<?php echo $Chkdby; ?>', 
                    '<?php echo $Preparedby; ?>', 
                    '<?php echo $issuedate; ?>', 
                    '<?php echo $duplicateissuedate; ?>',
                    '<?php echo $sectionId; ?>')">
                <i class="fa fa-print"></i> Print All Results
            </button>
        </div>
        
        <table class="table table-bordered table-striped">
            <thead class="thead-dark">
                <tr>
                    <th>S.N.</th>
                    <th>Student Name</th>
                    <th>Date of Birth</th>
                    <th>Registration No.</th>
                    <th>Symbol No.</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $sn = 1;
                if ($studentsResult && $studentsResult->num_rows > 0) {
                    while ($studentRow = $studentsResult->fetch_assoc()) { 
                        $studentId = $studentRow['student_id'];
                        $studentName = $studentRow['name'];
                        $stdsym = $studentRow['symbol_no'];
                        $stdregd = $studentRow['regd_id'];
                        $stddob = $studentRow['dob'];
                ?>
                    <tr>
                        <td><?php echo $sn++; ?></td>
                        <td><?php echo $studentName; ?></td>
                        <td><?php echo $stddob; ?></td>
                        <td><?php echo $stdregd; ?></td>
                        <td><?php echo $stdsym; ?></td>
                        <td> 
                            <button class="btn btn-primary btn-sm fa print" 
                                onclick="getResult(
                                    '<?php echo $studentId; ?>', 
                                    '<?php echo $examTypeId; ?>', 
                                    '<?php echo $classId; ?>', 
                                    '<?php echo $Chkdby; ?>', 
                                    '<?php echo $Preparedby; ?>', 
                                    '<?php echo $issuedate; ?>', 
                                    '<?php echo $duplicateissuedate; ?>',
                                    '<?php echo $sectionId; ?>')">
                                <i class="fa fa-print"></i> Print Result
                            </button>
                        </td>
                    </tr>
                <?php 
                    }
                } else {
                    echo '<tr><td colspan="6" class="text-center">No students found for the selected class and section.</td></tr>';
                }
                ?>
            </tbody>
        </table>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.2/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    
    <script>
    function getResult(studentId, examTypeId, classId, Chkdby, Preparedby, issuedate, duplicateissuedate, sectionId) {
        var form = document.createElement('form');
        form.method = 'POST';
        form.action = 'get_gradesheet_ind.php';

        // Create hidden inputs for each parameter
        var inputs = [
            // { name: 'session_id', value: session_id },
            { name: 'std_id_visible', value: studentId },
            { name: 'exam_id', value: examTypeId },
            { name: 'class_id', value: classId },
            { name: 'checked_by', value: Chkdby },
            { name: 'prepared_by', value: Preparedby },
            { name: 'issuedate', value: issuedate },
            { name: 'duplicateissuedate', value: duplicateissuedate },
            { name: 'section_id', value: sectionId }
        ];

        // Append each input to the form
        inputs.forEach(function(inputData) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = inputData.name;
            input.value = inputData.value;
            form.appendChild(input);
        });

        // Append the form to the body and submit it
        document.body.appendChild(form);
        form.submit();
    }
    
    function getResultAll(examTypeId, classId, Chkdby, Preparedby, issuedate, duplicateissuedate, sectionId) {
        var form = document.createElement('form');
        form.method = 'POST';
        form.action = 'get_gradesheet.php';

        // Create hidden inputs for each parameter
        var inputs = [
            // { name: 'session_id', value: session_id },
            { name: 'exam_id', value: examTypeId },
            { name: 'class_id', value: classId },
            { name: 'checked_by', value: Chkdby },
            { name: 'prepared_by', value: Preparedby },
            { name: 'result_date', value: issuedate },
            { name: 'duplicateissuedate', value: duplicateissuedate },
            { name: 'section_id', value: sectionId }
        ];

        // Append each input to the form
        inputs.forEach(function(inputData) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = inputData.name;
            input.value = inputData.value;
            form.appendChild(input);
        });

        // Append the form to the body and submit it
        document.body.appendChild(form);
        form.submit();
    }
    </script>
</body>
</html>

<?php } ?>