<?php

class Student
{
    private $db;

    public function __construct($Connection)
    {
        $this->db = $Connection;
    }

    public function getStudentById($student_id)
    {
        $query = "SELECT student_first_name, student_last_name, student_gender,student_email,student_nic_or_passport_id,student_phone_1,student_phone_2,student_address FROM student WHERE student_id = ? ";
        $statement = $this->db->prepare($query);
        $statement->bind_param("i", $student_id);
        $statement->execute();
        $result = $statement->get_result();

        $student = null;
        if ($result->num_rows > 0) {
            $student = $result->fetch_assoc();
            $query = "SELECT education_doc_path ,education_doc_status FROM education WHERE student_id = ? AND education_type_id = '1' AND (education_qual_id= '1' OR education_qual_id= '2') ";
            $statement = $this->db->prepare($query);
            $statement->bind_param("i", $student_id);
            $statement->execute();
            $result = $statement->get_result();
            $documents_ol = [];
            // If documents are found, add them to the student array
            if ($result->num_rows > 0) {

                while ($row = $result->fetch_assoc()) {
                    $documents_ol[] = $row;
                }
                $student['documents_ol'] = $documents_ol;
            }
            $query = "SELECT education_doc_path ,education_doc_status FROM education WHERE student_id = ? AND education_type_id = '1' AND (education_qual_id= '3' OR education_qual_id= '4') ";
            $statement = $this->db->prepare($query);
            $statement->bind_param("i", $student_id);
            $statement->execute();
            $result = $statement->get_result();

            // If documents of type "AL" are found, add them to the student array
            if ($result->num_rows > 0) {
                $documents_al = [];
                while ($row = $result->fetch_assoc()) {
                    $documents_al[] = $row;
                }
                $student['documents_al'] = $documents_al;
            }
            $query = "SELECT education_doc_path ,education_doc_status FROM education WHERE student_id = ? AND education_type_id = '3' AND education_qual_id= '11' ";
            $statement = $this->db->prepare($query);
            $statement->bind_param("i", $student_id);
            $statement->execute();
            $result = $statement->get_result();

            // If documents of type "other" are found, add them to the student array
            if ($result->num_rows > 0) {
                $documents_other = [];
                while ($row = $result->fetch_assoc()) {
                    $documents_other[] = $row;
                }
                $student['documents_other'] = $documents_other;
            }
            $query = "SELECT education_doc_path,education_doc_status FROM education WHERE student_id = ? AND education_type_id = '2' AND (education_qual_id= '5' OR education_qual_id= '6' OR education_qual_id= '7' OR education_qual_id= '8'OR education_qual_id= '9' OR education_qual_id= '10') ";
            $statement = $this->db->prepare($query);
            $statement->bind_param("i", $student_id);
            $statement->execute();
            $result = $statement->get_result();

            // If documents of type "higher education" are found, add them to the student array
            if ($result->num_rows > 0) {
                $documents = [];
                while ($row = $result->fetch_assoc()) {
                    $documents[] = $row;
                }
                $student['documents'] = $documents;
            }
        }
        return $student;
    }



    public function insertEducationDetails(
        $education_created_date,
        $education_created_time,
        $education_updated_date,
        $education_updated_time,
        $education_additional_info,
        $education_institute,
        $education_start_date,
        $education_end_date,
        $education_current_study,
        $educationTypeId,
        $qualification,
        $file_url_campus,
        $education_doc_status, 
        $student_id
    ) {
        // Check if mandatory fields are provided
        if (empty($educationTypeId) || empty($qualification)) {
            // Return a specific error message indicating missing fields
            $missingFields = [];
            if (empty($educationTypeId)) {
                $missingFields[] = "Select Education";
            }
            if (empty($qualification)) {
                $missingFields[] = "Select Qualification";
            }
            $alertMessage = "Please enter the mandatory fields: " . implode(", ", $missingFields) . ".";

            // display the alert message
            echo "<script>alert('$alertMessage');</script>";

            // Log the error
            error_log("Error: " . $alertMessage);

            return false;
        }


        // Ensure that education_current_study is either 1 or 0
        $education_current_study = ($education_current_study === 'yes') ? 1 : 0;

        // Define the query with placeholders for parameters
        $query = "INSERT INTO education (
                    education_created_date,
                    education_created_time,
                    education_updated_date,
                    education_updated_time,
                    education_additional_info,
                    education_institute,
                    education_start_date,
                    education_end_date,
                    education_current_study,
                    education_type_id,
                    education_qual_id,
                    education_doc_path,
                    education_doc_status, 
                    student_id
                  ) VALUES (?,?,?,?,?,?, ?,?, ?, ?, ?, ?, ?, ?)";

        // Prepare the SQL statement
        $statement = $this->db->prepare($query);

        if (!$statement) {
            error_log("Error: Failed to prepare SQL statement.");
            return false;
        }

        // // Null empty fields
        // $education_institute = !empty($education_institute) ? $education_institute : NUll;
        // $education_start_date = !empty($education_start_date) ? $education_start_date : NULL;
        // $education_end_date = !empty($education_end_date) ? $education_end_date : NULL;
        // $education_current_study = !empty($education_current_study) ? $education_current_study : NULL;
        // $file_url_campus = !empty($file_url_campus) ? $file_url_campus : "";

        // Bind the parameters
        $statement->bind_param(
            "ssssssssiissii",
            $education_created_date,
            $education_created_time,
            $education_updated_date,
            $education_updated_time,
            $education_additional_info,
            $education_institute,
            $education_start_date,
            $education_end_date,
            $education_current_study,
            $educationTypeId,
            $qualification,
            $file_url_campus,
            $education_doc_status, 
            $student_id
        );

        // Execute the statement
        $success = $statement->execute();

        if (!$success) {
            // Log any SQL errors
            error_log("Error: Failed to execute SQL statement - " . $statement->error);
        }

        // Close the statement
        $statement->close();
        return $success;
    }




    public function updateGender($student_id, $gender)
    {
        $query = "UPDATE student SET student_gender = ? WHERE student_id = ?";
        $statement = $this->db->prepare($query);

        if (!$statement) {
            return false;
        }

        $statement->bind_param("si", $gender, $student_id);
        $success = $statement->execute();
        $statement->close();

        return $success;
    }


    public function updateContactInfo($student_id, $new_phone)
    {
        $query = "UPDATE student SET student_phone_2 = CONCAT(student_phone_2, ',',?) WHERE student_id = ?";
        $statement = $this->db->prepare($query);

        if (!$statement) {
            return false;
        }

        $statement->bind_param("si", $new_phone, $student_id);
        $success = $statement->execute();
        $statement->close();

        return $success;
    }


    public function selectQualificationByTypeId($selectedTypeId)
    {

        // Fetch qualifications based on selected type_id
        $sqlQuals = "SELECT education_qual_id, education_qual FROM education_qual WHERE education_type_id = ?";
        $stmt = $this->db->prepare($sqlQuals);
        $stmt->bind_param("i", $selectedTypeId);
        $stmt->execute();
        $resultQuals = $stmt->get_result();

        $qualifications = [];
        while ($row = $resultQuals->fetch_assoc()) {
            $qualifications[] = $row;
        }
        $stmt->close();

        // Return the qualifications as JSON
        echo json_encode($qualifications);
        exit();
    }
}