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



require_once $base_path_to_learning .'admin_api/modules/left_students/left_students_functions.php';

function reversePrintDate($date) {
    // Explode the date into parts
    $parts = explode('-', $date);

    // Reverse the array
    $reversed = array_reverse($parts);

    // Join with desired separator and print
    return implode('-', $reversed);
}

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

// $institution_id = $_GET['institution_id'] ?? null;
// $branch_id = $_GET['branch_id'] ?? null;
// $left_students_id = $_GET['left_students_id'] ?? null;

// if (!$institution_id || !$branch_id || !$left_students_id) {
//     die("Required parameters are missing.");
// }

$institutions = getInstitution($conn_rjllearning, $institution_id);

$additional_data = ['branch_code','pincode','address','contact_number','email', 'affiliation_code'];
$branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id, $additional_data);



$data = fetch_specific_manage_alumni($conn_rjllearning_specific, $alumni_id, $institution_id, $branch_id);

$student_data = array_merge($data['personal_info'], $data['other_info']);




ExportStudentAdmisson($institutions, $branch, $student_data);

function ExportStudentAdmisson($institutions, $branch, $student_data)
{

    //  $student_data = $data[0]['$response'];
    //  $student_data = $data[0]['other_info'];

    if ($student_data['gender'] == 'F') {
        $gender = 'Female';
    } elseif ($student_data['gender'] == 'M') {
        $gender = 'Male';
    }
    $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>Transfer Certificate</title>
    <style>
       body {
            font-family: Arial, sans-serif;
            background-color: #fff;
            margin: 0;
            padding: 2px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            background-color: #fff;
            // padding: 20px;
            width: 100%;
            height: 100%;
            border-radius: 8px;
            overflow: auto;
            box-shadow: none;
        }

        .header {
            text-align: center;
            padding-bottom: 8px;
            margin-bottom: 4px;
            margin-top: 12px;
        }

        .header .logo {
            width: 120px;
            height: auto;
            margin-bottom: 6px;
        }

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

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

        .header p {
            margin: 4px 0;
            font-size: 18px;
            color: #333;
            font-weight:3rem;
        }

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

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

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

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

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

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

        .content th {
            width:70%;
            font-size: 11px;
            padding:5px;
            font-weight: normal;
            color: #000;
          background-color: transparent; 
            text-align: left;
        }
        .content td{
            font-size: 11px;
            padding:5px;
        }

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

        .sign {
            text-align: center;
            flex: 1;
            padding: 1px;
        }

        .sign:not(:last-child) {
            margin-right: 20px;
        }
         .content tr:nth-of-type(odd) {
        background-color: #f9f9f9; /* Light grey strip for odd rows */
    }

    @media print {
        .container {
            page-break-inside: avoid; /* Avoid page breaks inside container */
        }

        .content table {
            page-break-inside: auto; /* Allow table to break but minimize it */
        }

        .content tr {
            page-break-inside: avoid; /* Avoid page breaks inside table rows */
            page-break-after: auto; /* Allow page breaks after rows if needed */
        }

        .signatures {
            page-break-before: avoid; /* Avoid page break before signatures section */
        }
        
        .no_print {
            display: none !important;
        }
        
        
        .header p {
            font-size: 12px;
        }
        
        .header {
            margin-top: 40px;
        }
        
        .cert-title button {
            background: transparent;
            font-weight: bold;
            color:black;
        }
    }

    </style>

</head>
<body>
    <div class="container">
        <div class="header">
            <img src="' . $institutions['logo'] . '" alt="School Logo" class="logo no_print">
            <h1 class="no_print">' . $institutions['institution_name'] . '</h1>
            <p class="no_print">' . $branch['name'] . '</p>';
            if($branch['affiliation_code'] != null and $branch['affiliation_code'] != '') {
                $html .= '<p> CBSE Affiliated (Code No. : '.$branch['affiliation_code'].') </p>';
            }
            $html .= '<p>' . $branch['address'] . ' ,&nbsp' . $branch['pincode'] . '</p>
            <p>' . $branch['contact_number'] . ' | ' . $branch['email'] . '</p>
         
            <div class="cert-title">
                <button>Transfer Certificate</button>
            </div>
        </div>
        <div class="content">
            <table>
                <tr>
                <th>Admission Number</th><td>' . $student_data['admission_number'] . '</td>
                </tr>

                <tr>
                <th>Name of the pupil</th><td>' . $student_data['name'] . '</td>
                </tr>
                <tr>
                <th>Father\'s / Guardian\'s Name</th><td>' . $student_data['fathername'] . '</td>
                </tr>
                <tr>
                <th>Nationality</th><td>' . $student_data['nationality'] . '</td>
                </tr>
                <tr>
                <th>Whether the candidate belongs to Scheduled Caste / Scheduled Tribe / OBC</th>
                <td>' . $student_data['caste'] . '</td>
                </tr>
                <tr>
                <th>Date of first admission in the school</th>
                <td>' . (!empty($student_data['date_admission']) && DateTime::createFromFormat('Y-m-d', $student_data['date_admission'])
        ? DateTime::createFromFormat('Y-m-d', $student_data['date_admission'])->format('F j, Y')
        : '') . '</td>

                </tr>
                <tr>
                <th>Date of Birth (in Christian Era) according to the admission register</th>
                <td>' . (!empty($student_data['dob']) && DateTime::createFromFormat('Y-m-d', $student_data['dob'])
        ? DateTime::createFromFormat('Y-m-d', $student_data['dob'])->format('F j, Y')
        : '') . '</td>

                </tr>
                <tr>
                <th>Date of Birth (in words)</th>
        <td>' . dateToWords($student_data['dob']) . '</td>

                </tr>
                <tr>
                <th>Class in which pupil last studied (In fig)</th><td>' . ($student_data['last_class']['class_name'] ?? '') . '</td>

                </tr>
                <tr>
                <th>(In words)</th><td>' . ($student_data['last_class']['class_name'] ?? '') . '</td>
                </tr>
                <tr>
                <th>School / Board annual examination last taken with result</th><td>' . strtoupper((string) ($student_data['school_last_examination'] ?? '')) . '</td>
                </tr>
                <tr>
                <th>Whether failed once/twice in the same class</th><td>' . ucwords((string) ($student_data['failed_same_class'] ?? '')) . '</td>
               </tr>';

    // Display subjects studied dynamically
    if (!empty($student_data['subject_studied']) && is_array($student_data['subject_studied'])) {
        foreach ($student_data['subject_studied'] as $index => $subject) {
            if (is_array($subject) && array_key_exists('value', $subject)) {
                $subject_value = $subject['value'];
                $html .= '<tr><th>Subject ' . ($index + 1) . '</th><td>' . ucwords($subject_value) . '</td></tr>';
            }
        }
    }


    $html .= '<tr>
                <th>Whether qualified for promotion to the higher class</th><td>' . ucwords((string) ($student_data['promotion_higher_class'] ?? '')) . '</td>
                </tr>
                <tr><th>If so to which class (in fig)</th><td>' . ucwords((string) ($student_data['class_in_figure'] ?? '')) . '</td>
                </tr>
                <tr>
                <th>Months upto which the (Pupil has paid) school dues paid</th>
                <td>' . ucwords($student_data['paid'] ? $student_data['paid'] : '-') . '</td>
                </tr>
                <tr>
                <th>Any fee concession availed of if so, the nature of concession</th>
                <td>' . ucwords($student_data['fee_concession'] ? $student_data['fee_concession'] : '-') . '</td>
                </tr>
                <tr>
                <th>Total number of working days</th><td>' . ($student_data['working_days'] ?? '') . '</td>

                </tr>
                <tr>
                <th>Total number of working days when the pupil was present</th><td>' . ($student_data['present_days'] ?? '') . '</td>
                </tr>
                <tr>
                <th>Whether NCC cadet, Boy scout, Girl guide (detail may be given)</th>
                <td>' . ucwords((string)($student_data['ncc'] ?? NA)) . '</td>
                </tr>
                <tr>
                <th>Games played or extra curricular activities in which the pupil usually took part (Mention achievement level therein)</th>
                <td>' . ucwords((string)($student_data['extra_curricular_activities'] ?? '')) . '</td>
                </tr>
                <tr>
                <th>Reasons for leaving the school</th><td>' . ucwords(isset($student_data['reasons_leaving_school']) ? $student_data['reasons_leaving_school'] : '') . '</td>
                </tr>
                <tr>
                <th>General conduct</th><td>' . ucwords(($student_data['conduct'] ? $student_data['conduct'] : '--')) . '</td>
                </tr>
                <tr>
                <th>Date of application for certificate</th><td>' . (reversePrintDate($student_data['date_application_certificate']) ?? '') . '</td>
                </tr>
                <tr>
                <th>Date of issue of certificate</th><td>' . (reversePrintDate($student_data['date_issue_certificate']) ?? '') . '</td>
                </tr>
                <tr>
                <th>Any other remarks</th><td>' . ucwords(($student_data['remarks'] ? $student_data['remarks'] : '--')) . '</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>';
    echo $html;
}
