    <?php
ini_set('display_errors', 1);

require_once '../../../resources/base_path_include.php';

require_once $base_path_to_learning_resources . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . "vendor/autoload.php";

use \Spipu\Html2Pdf\Html2Pdf;
use \Spipu\Html2Pdf\Exception\Html2PdfException;
use \Spipu\Html2Pdf\Exception\ExceptionFormatter;

function removeBranchIdAndAddBranchName($array) {
    foreach ($array['students_info'] as &$student) {
        if (isset($student['branch_id'])) {
            $student['branch_name'] = $student['branch_id']['name'];
            unset($student['branch_id']);
        }
    }
    return $array;
}

$studentFields = array(
    'name' => 'Name',
    'branch_name' => 'Branch Name',
    'fathername' => 'Father Name',
    'mothername' => 'Mother Name',
    'mobile' => 'Mobile',
    'email' => 'Email',
    'address_line_1' => 'Address',
    'city' => 'City',
    'pincode' => 'Pincode',
    'dob' => 'DOB',
    'gender' => 'Gender',
    'student_profile_image' => 'Image',
    'blood_group' => 'Blood Group',
    'admission_number' => 'Admission Number',
    'class_id' => 'Class',
    'section_id' => 'Section',
    'roll_no' => 'Roll No.',
    'nationality' => 'Nationality',
    'caste' => 'Caste',
);

$data = GetBasicStudentsDetail($conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $search_string, $limit, $offset, $additionaldata);

$result = removeBranchIdAndAddBranchName($data);
$students = $result['students_info'];
$fields_to_remove = ['id', 'institution_id','branch_id', 'admission_id'];

// Assuming removeKeys, getArrayValues, and getArrayKeys are defined elsewhere
$student_data = removeKeys($students, $fields_to_remove);
$values = getArrayValues($student_data);

$keys = getArrayKeys($student_data[0]);
$formattedKeys = $keys;

foreach ($keys as $index => $k) {
    $formattedKeys[$index] = getCorrespondingValue($studentFields, $k);
}

$data = array("values" => $values, "keys" => $formattedKeys);
createPRINT($conn_rjllearning, $conn_rjllearning_specific, $data, $institution_id, $branch_id);

function createPRINT($conn_rjllearning, $conn_rjllearning_specific, $data, $institution_id, $branch_id)
{
    $values = $data['values'];
    $keys = $data['keys'];
    $institutions = getInstitution($conn_rjllearning, $institution_id);
    $additional_data = ['branch_code','pincode','address','contact_number','email'];
    $branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id,$additional_data);

    $html = '<h1>' . htmlspecialchars($institutions['institution_name']) . '</h1>';
    $html .= '<h2>' . htmlspecialchars($branch['name']) . '</h2>';
    $html .= '<table border="1" cellpadding="5">';
    $html .= '<thead><tr>';

    foreach ($keys as $item) {
        $html .= '<th>' . htmlspecialchars($item) . '</th>';
    }

    $html .= '</tr></thead>';
    $html .= '<tbody>';

    foreach ($values as $v) {
        $html .= '<tr>';
        foreach ($v as $index => $item) {
            if ($keys[$index] == 'Image') {
                $profile_pic = !empty($item) ? $item : 'https://media.licdn.com/dms/image/D5603AQGuCXGG6TRzug/profile-displayphoto-shrink_400_400/0/1680016742734?e=1723680000&v=beta&t=Th1zHgyR8xqtlD4-hh1c_Yv--t5gzvJHWc7DYaboPrE';
                $html .= '<td><img class="profile-pic" src="' . htmlspecialchars($profile_pic) . '" alt="Student Profile" width="50" height="50"></td>';
            } else {
                $html .= '<td>' . htmlspecialchars($item) . '</td>';
            }
        }
        $html .= '</tr>';
    }

    $html .= '</tbody></table>';
    
    echo $html;
}
?>
