<?php
$select = "--select--";
$current_session = date('Y') . "/" . (date('Y') + 1);
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Result Management</h1>
    <p>View Published Annual Result</p>
  </div>
</div>

<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>View Annual Result Summary</h4>
        </div>
        <div class='panel-body'>
            <form method='POST' action=''>
                <div class='row'>
                    <div class='col-md-4'>
                        <div class='input-group'>
                            <span class='input-group-addon'>Class:</span>
                            <select class='form-control' name='show_summary_class'>
                                <option selected><?php echo $select; ?></option>
                                <?php
                                    $query = "SELECT * FROM `classes`";
                                    $run_query = mysqli_query($connection, $query);
                                    while ($result = mysqli_fetch_assoc($run_query)) {
                                        echo "<option>{$result['classes']}</option>";
                                    }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class='col-md-4'>
                        <div class='input-group'>
                            <span class='input-group-addon'>Session:</span>
                            <select class='form-control' name='show_summary_session'>
                                <option selected><?php echo $current_session; ?></option>
                            </select>
                        </div>
                    </div>
                    <div class='col-md-4'>
                        <input type='submit' name='view_annual_summary_btn' value='VIEW SUMMARY' class='btn btn-primary btn-sm' />
                        <input type='submit' name='delete_annual_summary_btn' value='DELETE' class='btn btn-danger btn-sm' />
                    </div>
                </div>
            </form>
            <br />

<?php
if (isset($_POST['view_annual_summary_btn'])) {
    $class = $_POST['show_summary_class'];
    $session = $_POST['show_summary_session'];

    if ($class == $select || $session == $select) {
        $_SESSION['msg'] = "Please select class and session.";
        $_SESSION['type'] = "error";
        header("Location: view_annual_published_result");
        exit();
    }

    $query = "SELECT * FROM annual_result_summary WHERE class = ? AND session = ? ORDER BY position ASC";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("ss", $class, $session);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        echo "<div class='table-responsive'><table class='table table-bordered table-hover'><thead><tr>
                <th>SN</th><th>NAME</th><th>REG NO.</th><th>CLASS</th><th>SESSION</th>
                <th>1st TERM AVG</th><th>2nd TERM AVG</th><th>3rd TERM AVG</th>
                <th>TOTAL</th><th>AVG</th><th>POS</th><th>REMARK</th></tr></thead><tbody>";
        $sn = 0;
        while ($row = $result->fetch_assoc()) {
            $sn++;
            echo "<tr>
                    <td>{$sn}</td>
                    <td>{$row['name']}</td>
                    <td>{$row['reg_number']}</td>
                    <td>{$row['class']}</td>
                    <td>{$row['session']}</td>
                    <td>{$row['first_term_avg']}</td>
                    <td>{$row['second_term_avg']}</td>
                    <td>{$row['third_term_avg']}</td>
                    <td>{$row['total_annual_summary']}</td>
                    <td>{$row['overall_avg_marks']}</td>
                    <td>" . getOrdinal($row['position']) . "</td>
                    <td>{$row['remarks']}</td>
                  </tr>";
        }
        echo "</tbody></table></div>";
    } else {
        $_SESSION['msg'] = "No summary found for this selection.";
        $_SESSION['type'] = "info";
        header("Location: view_annual_published_result");
        exit();
    }
}

if (isset($_POST['delete_annual_summary_btn'])) {
    $class = $_POST['show_summary_class'];
    $session = $_POST['show_summary_session'];

    if ($class == $select || $session == $select) {
        $_SESSION['msg'] = "Please select class and session to delete.";
        $_SESSION['type'] = "error";
    } else {
        $query = "DELETE FROM annual_result_summary WHERE class = ? AND session = ?";
        $stmt = $connection->prepare($query);
        $stmt->bind_param("ss", $class, $session);
        if ($stmt->execute()) {
            $_SESSION['msg'] = "Annual summary deleted successfully.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Failed to delete summary.";
            $_SESSION['type'] = "error";
        }
    }
    header("Location: view_annual_published_result");
    exit();
}
?>

        </div>
    </div>
</div>

