<?php
if (isset($_POST['delete_conduct_btn'])) {
    $class = $_POST['class'];
    $term = $_POST['term'];
    $session = $_POST['session'];

    // Step 1: Check if records exist
    $check_stmt = $connection->prepare("SELECT id FROM behavioral WHERE class = ? AND term = ? AND session = ?");
    $check_stmt->bind_param("sss", $class, $term, $session);
    $check_stmt->execute();
    $check_stmt->store_result();

    if ($check_stmt->num_rows > 0) {
        $check_stmt->close();

        // Step 2: Delete if found
        $delete_stmt = $connection->prepare("DELETE FROM behavioral WHERE class = ? AND term = ? AND session = ?");
        $delete_stmt->bind_param("sss", $class, $term, $session);

        if ($delete_stmt->execute()) {
            $_SESSION['msg'] = "✅ Behavioral records deleted successfully.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "❌ Failed to delete behavioral records.";
            $_SESSION['type'] = "error";
        }

        $delete_stmt->close();
    } else {
        $_SESSION['msg'] = "⚠️ No behavioral records found for the selected criteria.";
        $_SESSION['type'] = "warning";
        $check_stmt->close();
    }

    // Redirect after handling
    header("Location: view_conducts");
    exit();
}
?>


<div class='page-title'>
    <div>
        <h1><i class='fa fa-laptop'></i> Behavioral Analysis</h1>
        <p>View Uploaded Behavioral</p>
    </div>
</div>

<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>View Uploaded Behavioral Analysis</h4>
        </div>
        <div class='panel-body'>
            <form method='POST' action=''>
                <div class='row'>
                    <div class='col-md-3'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Class:</span>
                            <select class='form-control' name='class'>
                                <option selected><?php echo $select; ?></option>
                                <?php
                                $query = " SELECT * FROM `classes` ";
                                $run_query = mysqli_query($connection, $query);
                                if (mysqli_num_rows($run_query) > 0) {
                                    while ($result = mysqli_fetch_assoc($run_query)) {
                                        $attendance = $result['classes'];
                                        echo "
                                                    <option>{$attendance}</option>
                                                ";
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <br />
                    </div>

                    <div class='col-md-3'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Term:</span>
                            <select class='form-control' name='term'>
                                <option selected><?php echo $select; ?></option>
                                <?php
                                $term_array = array("First Term", "Second Term", "Third Term");

                                foreach ($term_array as $term) {
                                    echo "<option>{$term}</option><br>";
                                }
                                ?>
                            </select>
                        </div>
                        <br />
                    </div>

                    <div class='col-md-3'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Session:</span>
                            <select class='form-control' name='session'>
                                <?php
                                ///////////// POST ACTION TO DISPLAY CURRENT SESSION //////////////
                                $query = " SELECT * FROM `current_season` ";
                                $run_query = mysqli_query($connection, $query);
                                if (mysqli_num_rows($run_query) == 1) {
                                    while ($result = mysqli_fetch_assoc($run_query)) {
                                        $show_current_session = $result['current_session'];
                                        echo "
                                                    <option>{$show_current_session}</option>
                                                ";
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <br />
                    </div>

                    <div class='col-md-3'>
                        <input type='submit' name='view_conduct_btn' value='VIEW' class='btn btn-primary' />
                        <input type='submit' name='delete_conduct_btn' value='DELETE' class='btn btn-danger' />
                    </div>
                </div>
            </form>
            <br />
        </div>
    </div>
    <?php
if (isset($_POST['view_conduct_btn'])) {
    $class = inject_checker($connection, $_POST['class']);
    $term = inject_checker($connection, $_POST['term']);
    $session = inject_checker($connection, $_POST['session']);
    $select = "--select--";

    if ($class == $select || $term === $select || $session === $select) {
        echo "<h4 class='text-danger'><b>!!! Please select all fields</b></h4>";
    } else {
        $stmt = $connection->prepare("SELECT * FROM behavioral WHERE class = ? AND term = ? AND session = ? ORDER BY name ASC");
        $stmt->bind_param("sss", $class, $term, $session);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $i = 0;
            echo "
                <div class='table-responsive'>
                    <table class='table table-hover table-bordered' id='myTable'>
                        <thead>
                            <tr>
                                <th>S/N</th>
                                <th>NAME</th>
                                <th>Punct.</th>
                                <th>Neat.</th>
                                <th>R.W.O</th>
                                <th>R.F.A</th>
                                <th>A.T.W</th>
                                <th>Honesty</th>
                                <th>Initiative</th>
                                <th>F.M.M</th>
                            </tr>
                        </thead>
                        <tbody>
            ";

            while ($row = $result->fetch_assoc()) {
                $i++;
                $name = $row['name'];

                // 🔤 Abbreviate if 3 words and length > 25
                $words = explode(" ", trim($name));
                if (count($words) === 3 && strlen($name) > 25) {
                    $name = ucfirst($words[0]) . " " . strtoupper(substr($words[1], 0, 1)) . ". " . ucfirst($words[2]);
                }

                echo "
                    <tr>
                        <td>{$i}</td>
                        <td>{$name}</td>
                        <td>{$row['puntuality']}</td>
                        <td>{$row['neatness']}</td>
                        <td>{$row['relationship_with_others']}</td>
                        <td>{$row['regards_for_authority']}</td>
                        <td>{$row['attitude_towards_academics']}</td>
                        <td>{$row['honesty']}</td>
                        <td>{$row['initiative']}</td>
                        <td>{$row['form_master_or_mistress']}</td>
                    </tr>
                ";
            }

            echo "
                        </tbody>
                    </table>
                </div>";
        } else {
            echo "<h4 class='text-danger'><b>!!! No Records Found</b></h4>";
        }

        $stmt->close();
    }
}
?>

</div>
