<?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';

//valdation file
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 . 'admin_api/academic_session/functions/validate_faculty.php';
require_once $base_path_to_learning . 'admin_api/academic_session/functions/functions.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

if (!isset($student_id) || empty($student_id)) {
    errorreturn("Student ID is mandatory and cannot be empty.");
}

$institutions = getInstitution($conn_rjllearning, $institution_id);
//  echo $student_id,$institution_id,$branch_id,$admission_id;diE();
$additional_data = ['branch_code','pincode','address','contact_number','email'];
$branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id,$additional_data);
// $sql_class ="SELECT id,classname FROM class_info WHERE id = ? and institution_id=? ";
// $stmt_class = $conn_rjllearning_specific->prepare($sql_class);   
$sql = "SELECT students.*,student_admission.*
            FROM students
            JOIN student_admission ON students.admission_id = student_admission.id
            WHERE  students.id= ? and students.institution_id = ? and students.branch_id =  ? AND students.admission_id = ?";
            // echo $sql;die();
$array_params = [$student_id,$institution_id,$branch_id,$admission_id];
$stmt = $conn_rjllearning_specific->prepare($sql);
$stmt->execute($array_params);
$result = $stmt->fetchAll();
// print_R($result);die();
if(count($result) != 1){
    errorreturn("Student Not found.");
}
        
    if ($result[0]['student_profile_image'] != null) {
        if ($result[0]['student_profile_image'] != null && !is_array($result[0]['student_profile_image'])) {
            $data_attachment = json_decode($result[0]['student_profile_image'], true);
            $filePath = $data_attachment['filepath'];
            $student_profile_image = displayFileFromS3($GLOBALS['s3_learning_bucket'], $GLOBALS['learning_bucketName'], $filePath);
        }
    }else{
        $student_profile_image = null;   
    }
    $class_id = $result[0]['class_id'];
     
    
    // $stmt_class->execute(array($class_id, $institution_id));
    // $result_class = $stmt_class->fetchAll();
   
    // if(count($result_class) >= 1) {
    //     $class_name = $result_class[0]['classname'];
    // }else{
        
    //     $class_name = '';
    // }
   
    $student_data = array("name"=>$result[0]['name'],"fathername"=>$result[0]['fathername'],"mothername"=>$result[0]['mothername'],"mobile"=>$result[0]['mobile'] ,
        "email"=>$result[0]['email']
        ,"city"=>$result[0]['city'],"pincode"=>$result[0]['pincode'],"dob"=>$result[0]['dob'],"gender"=>$result[0]['gender'],"blood_group"=>$result[0]['blood_group'],
        "roll_no"=>$result[0]['name'],"roll_no"=>$result[0]['roll_no'],"class"=>$class_id,"section_id"=>$result[0]['section_id']
        ,"nationality"=>$result[0]['nationality'],"caste"=>$result[0]['caste'],"admission_number"=>$result[0]['admission_number'],"last_school"=>$result[0]['last_school'],
        "date_admission"=>$result[0]['date_admission'],"student_profile_image"=>$student_profile_image,"address_line_1"=>$result[0]['address_line_1']);
    // print_r($student_data);die();
    ExportStudentAdmisson($institutions,$branch,$student_data);
 
   
function ExportStudentAdmisson($institutions, $branch, $student_data){
    
    if ($student_data['gender'] == 'F') {
            $gender = 'Female';
    } elseif ($student_data['gender'] == 'M') {
            $gender =  'Male';
    }
 
    $class=getClass($GLOBALS['conn_rjllearning_specific'], $GLOBALS['institution_id'],$student_data['class']);
    $html = ''; // Initialize the $html variable

    $html .= '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Admission </title>
   <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 2px;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            max-width: 900px;
            margin: auto;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .header {
           float:left;
/*            border-bottom: 2px solid #000;*/
            padding-bottom: 10px;
            margin-bottom: 10px;
        }

        .header .logo {
            width: 100px;
            height: auto;
            margin-bottom: 10px;
        }
        
         .header2  {
           float: right;
        }
		.header2 .logo2{
		  width: 150px;
		  height: 170px;
		  border:1px solid #ddd;
		}


        .header h1 {
            margin: 0;
            font-size: 24px;
        }

        .header h2 {
            margin: 5px 0;
            font-size: 16px;
            color: #555;
        }

        .header p {
            margin: 5px 0;
            font-size: 14px;
            color: #333;
        }

        .cert-title {
            margin-top: 10px;
        }

        .cert-title button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: default;
            font-size: 16px;
        }

        .content table {
            width: 100%;
            border-collapse: collapse;
        }

        .content table, .content td, .content th {
            border-bottom: 1px solid #ddd;
        }

        .content td, .content th {
            padding: 1px;
            text-align: left;
        }

        /*.content tr:nth-child(even) {
            background-color: #f2f2f2;
        }*/

        .content tr:hover {
            background-color: #ddd;
        }

        .content th {
            background-color: #f4f4f4;
            color: black;
        }
        .content td{
            font-size: 12px;
            padding:8px;
            font-weight: 600;
            color: #333;
        }

        .signatures {
            display: flex;
            justify-content: space-between;
            margin-top: 40px;
        }

        .sign {
            text-align: center;
            flex: 1;
            font-size: 12px;
        }

        .sign:not(:last-child) {
            margin-right: 20px;
        }
    </style>
</head>
<body>';
       $html .= '<div class="container">
        <div class="header">';
        if($institutions['logo'] != null){
                  $html .= '<img class="logo" src="' . htmlspecialchars($institutions['logo']) . '" alt="School Logo">';

         
                    }
            $html .= '<h1>'. $institutions['institution_name'] .'</h1>
            <h2>'. $branch['name'].'&nbsp;&nbsp; (Code No.'. $branch['branch_code'].' )</h2>
            <p>'.$branch['address'].' ,&nbsp;&nbsp '.$branch['pincode'].'</p>
            <p>'.$branch['contact_number'].' | '.$branch['email'].'</p>
            <div class="cert-title">
                <button>Admission From</button>
            </div>
        </div>
         <div class="header2">';
       if (isset($student_data['profile_image']) && !empty($student_data['profile_image'])) {
          $html .= '<img class="logo2" src="' . $student_data['profile_image']['filepath'] . '" alt="Student Logo">';
        }else {
            // Check gender and set default image
            $gender = $student_data['gender']; // Assuming 'gender' field is available and contains 'male' or 'female'
            if ($gender === 'Male') {
                $defaultImage = 'https://rejoicebilldesk.in/rjllearning_development/resources/images/student-m.png';
            } else if ($gender === 'Female') {
                $defaultImage = 'https://rejoicebilldesk.in/rjllearning_development/resources/images/student-f.png';
            } else {
                $defaultImage = 'https://rejoicebilldesk.in/rjllearning_development/resources/images/student-f.png'; // A generic default image
            }
            $html .= '<img class="logo2" src="' . $defaultImage . '" alt="Default Student Logo">';
        }
          $html .= '</div>    
        <div class="content">
            <table>
                <tr>
                    <td>Admission Number</td>
                    <td>'.$student_data['admission_number'].'</td>
                </tr>
                <tr>
                    <td>Name of the pupil</td>
                    <td>'.$student_data['name'].'</td>
                </tr>
                <tr>
                    <td>Fathers / Guardians Name</td>
                    <td>'.$student_data['fathername'].' </td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td>'.$gender.'</td>
                </tr>
                <tr>
                    <td>Date of first admission in the school</td>
                    <td>'.date("d-m-Y", strtotime($student_data['date_admission'])).'</td>
                </tr>
                <tr>
                    <td>Date of Birth</td>
                    <td>'.date("d-m-Y", ($student_data['dob'])).'</td>
                </tr>
            
                <tr>
                    <td>Email</td>
                    <td>'.$student_data['email'].'</td>
                </tr>
                 <tr>
                    <td>Mobile</td>
                    <td>'.$student_data['mobile'].'</td>
                </tr>
                 <tr>
                    <td>Admission for Class</td>
                    <td>'.$class['class_name'].'</td>
                </tr>
                 <tr>
                    <td>Last School Attended</td>
                    <td>'.$student_data['last_school'].'</td>
                </tr>
                
                <tr>
                    <td>Blood Group</td>';
                     if($student_data['blood_group'] !=  null){  
                    $html .= '<td>'.$student_data['blood_group'].'</td>';
                     }
                $html .= '</tr>
                <tr>
                    <td>Address</td>
                    <td>'.$student_data['address_line_1'].' ,&nbsp;'.$student_data['pincode'].'</td>
                </tr>
                <tr>
                    <td>City</td>
                    <td>'.$student_data['city'].'</td>
                </tr>
                <tr>
                    <td>Nationality</td>
                    <td>'.$student_data['nationality'].'</td>
                </tr>
                <tr>
                    <td>Caste</td>
                    <td>'.$student_data['caste'].'</td>
                </tr>
               
            </table>
        </div>
        <div class="signatures">
            <div class="sign">
                <p>Principal</p>
            </div>
            <div class="sign">
                <p>Class Incharge</p>
            </div>
            <div class="sign">
                <p>Accountant</p>
            </div>
        </div>
    </div>
</body>
</html>';

    // Output the HTML content
    echo $html;
}
