<?php
include('includes/session1.php');
include('includes/dbconnection.php');
include('includes/flash.php');

if (!isset($_SESSION['sid'])) {
    header('Location: logoutteacher.php');
    exit();
}

$teacher_id = (int) $_SESSION['sid'];
$term = $_POST['term'];
$session = $_POST['session'];
$class = $_POST['class'];
$section = $_POST['section'];

// Helper function to sanitize values between 1 and 5
function sanitize_rating($value)
{
    if ($value === '' || !isset($value)) {
        return null;
    }

    $intVal = (int)$value;
    return max(1, min($intVal, 5));
}

foreach ($_POST['fluency'] as $student_id => $value) {
    $student_id = (int)$student_id;

    // Psychomotor domain values
    $fluency = sanitize_rating($_POST['fluency'][$student_id]);
    $handwriting = sanitize_rating($_POST['handwriting'][$student_id]);
    $lab = sanitize_rating($_POST['lab_workshops'][$student_id]);
    $music = sanitize_rating($_POST['musical_skills'][$student_id]);
    $sports = sanitize_rating($_POST['sports'][$student_id]);

    // Affective domain values
    $punctuality = sanitize_rating($_POST['punctuality'][$student_id]);
    $reliability = sanitize_rating($_POST['reliability'][$student_id]);
    $politeness = sanitize_rating($_POST['politeness_honesty'][$student_id]);
    $charisma = sanitize_rating($_POST['charisma'][$student_id]);
    $org_ability = sanitize_rating($_POST['organizational_ability'][$student_id]);
    $self_control = sanitize_rating($_POST['self_control'][$student_id]);

    $hasPsychomotor = $fluency !== null || $handwriting !== null || $lab !== null || $music !== null || $sports !== null;
    $hasAffective = $punctuality !== null || $reliability !== null || $politeness !== null || $charisma !== null || $org_ability !== null || $self_control !== null;

    if (!$hasPsychomotor && !$hasAffective) {
        continue;
    }

    // Check for existing psychomotor record
    if ($hasPsychomotor) {
        $checkPsycho = "SELECT id FROM psychomotor WHERE student_id = ? AND term = ? AND session = ?";
        $stmt = $con->prepare($checkPsycho);
        $stmt->bind_param("iss", $student_id, $term, $session);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $updatePsychomotor = "UPDATE psychomotor SET
                fluency = ?, handwriting = ?, lab_workshops = ?, musical_skills = ?, sports = ?
                WHERE student_id = ? AND term = ? AND session = ?";
            $stmt = $con->prepare($updatePsychomotor);
            $stmt->bind_param("iiiiiiss", $fluency, $handwriting, $lab, $music, $sports, $student_id, $term, $session);
            $stmt->execute();
        } else {
            $insertPsychomotor = "INSERT INTO psychomotor (
                student_id, term, session, fluency, handwriting, lab_workshops, musical_skills, sports
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            $stmt = $con->prepare($insertPsychomotor);
            $stmt->bind_param("issiiiii", $student_id, $term, $session, $fluency, $handwriting, $lab, $music, $sports);
            $stmt->execute();
        }
    }

    // Check for existing affective record
    if ($hasAffective) {
        $checkAffective = "SELECT id FROM affective WHERE student_id = ? AND term = ? AND session = ?";
        $stmt = $con->prepare($checkAffective);
        $stmt->bind_param("iss", $student_id, $term, $session);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $updateAffective = "UPDATE affective SET
                punctuality = ?, reliability = ?, politeness_honesty = ?, charisma = ?, organizational_ability = ?, self_control = ?
                WHERE student_id = ? AND term = ? AND session = ?";
            $stmt = $con->prepare($updateAffective);
            $stmt->bind_param("iiiiiiiss", $punctuality, $reliability, $politeness, $charisma, $org_ability, $self_control, $student_id, $term, $session);
            $stmt->execute();
        } else {
            $insertAffective = "INSERT INTO affective (
                student_id, term, session, punctuality, reliability, politeness_honesty, charisma, organizational_ability, self_control
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $stmt = $con->prepare($insertAffective);
            $stmt->bind_param("issiiiiii", $student_id, $term, $session, $punctuality, $reliability, $politeness, $charisma, $org_ability, $self_control);
            $stmt->execute();
        }
    }
}

flash_alert('Ratings submitted successfully.');
        header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? 'javascript:history.back()')); exit;
exit();
