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

// Validation 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;

// Fetch necessary data
$institutions = getInstitution($conn_rjllearning, $institution_id);
$additional_data = ['address','pincode','contact_number','email'];
$branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id,$additional_data);

$data = geStudenIDCardTemplate($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $student_id, $id_card_template_selected);

// Set display filter to control card layout (1 for single, 2 for two cards in a row)
// $display_filter = 2;

// Call the function to generate student ID card HTML
StudentFirstTemplate($institutions, $branch, $data, $primary_color, $secondary_color, $display_filter);

function StudentFirstTemplate($institutions, $branch, $data, $primary_color, $secondary_color, $display_filter)
{
    
    
    $layout_class = $display_filter == 2 ? 'grid-container' : 'flex-container';
    // Start creating the HTML content
    $html = '<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Student ID Card</title>
        <style>
            body {
                padding: 0;
                margin: 0;
                font-family: "Arial", sans-serif;
                
                display: flex;
                /* Center align cards horizontally */
                align-items: flex-start; /* Align cards to the top */
                min-height: 100vh;
                flex-wrap: wrap; /* Allow cards to wrap to next row */
            }
            .id-card {
                width: 300px;
                height: 500px;
                border: 1px solid #000;
                border-radius: 15px;
                overflow: hidden;
                position: relative;
                box-shadow: 0 4px 8px rgba(0,0,0,0.1);
                background-color: #fff;
                margin: 20px;
                display: inline-block; /* Ensure cards stay inline */
                vertical-align: top; /* Align cards to the top of the row */
            }
            .grid-container {
                display: grid;
                justify-content: center; 
                grid-template-columns: auto auto;
                gap: 20px; /* Adjust gap between cards */
                padding: 20px; /* Adjust padding around grid */
                justify-content: center; /* Center align cards horizontally */
            }
            
            
            .flex-container {
                display: flex;
                 flex-direction: column;

            }
            
            
            .header {
                width: 100%;
                height: 102px;
                background-color: ' . $primary_color . ';
                color: #fff;
                text-align: center;
                padding: 30px 0;
                clip-path: polygon(0 0, 100% 0%, 100% 65%, 50% 100%, 0% 65%);
            }
            .rejoice {
                margin-top: -15px;
            }
            .rejoice img {
                margin-top: -7px;
                width: 180px;
                height: 50px;
            }
            .header h2 {
                margin: 2px 0;
                font-size: 25px;
                margin-top: -4px;
            }
            .header p {
                margin: -2px 0;
                font-size: 18px;
            }
            .profile-pic {
                width: 80px;
                height: 80px;
                border-radius: 50%;
                background-color: #fff;
                border: 3px solid ' . $primary_color . ';
                position: absolute;
                top: 100px;
                left: 50%;
                transform: translateX(-50%);
            }
            .body {
                padding: 15px;
                padding-top: 24px;
            }
            .body table {
                width: 100%;
                font-size: 16px;
                margin-top: 4px;
                table-layout: fixed;
            }
            .body table th, .body table td {
                text-align: left;
                padding: 4px; /* Ensure consistent padding */
                word-wrap: break-word;
            }
            .body table th {
                font-weight: bold;
                width: 50%;
            }
            .body table td {
                width: 50%; /* Adjusted width for better layout */
                text-align: left;
                font-size: 15px; /* Default font size */
            }
            .footer {
                position: absolute;
                bottom: 0;
                width: 100%;
                background-color: ' . $secondary_color . ';
                color: #fff;
                text-align: center;
                padding: 5px 0; /* Reduced padding */
                   }

            .footer p {
                margin: 3px;
                font-size: 15px;
            }
        //   @media (min-width: 768px) {
        //         .id-card {
        //             flex: 0 0 20%;
        //             width: 2.56cm;
        //             height: 13.398cm;
        //         }
        //     }
        
        @media print {
                .id-card {
                    page-break-inside: avoid; /* Prevent page break inside cards when printing */
                }
                .grid-container {
                    page-break-inside: avoid; /* Prevent page break inside the grid container */
                }
            }
        </style>
    </head>
    <body>';

    // Loop through each student data to create cards
    // $html .= '<div class="grid-container">';
     $html .= '<div class="' . $layout_class . '">';
    foreach ($data as $student_rows) {
        $html .= '<div class="id-card">
                    <div class="header">
                        <div class="rejoice">
                            <img class="logo" src="' . $institutions['logo'] . '" alt="School Logo">
                        </div>
                        <h2>' . $institutions['institution_name'] . '</h2>
                        <p>' . $branch['name'] . '</p>
                    </div>
                    <div>';

        // Student profile picture
        $profile_pic = isset($student_rows['student']['student_profile_image']) && !empty($student_rows['student']['student_profile_image']) ? $student_rows['student']['student_profile_image'] : 'https://media.licdn.com/dms/image/D5603AQGuCXGG6TRzug/profile-displayphoto-shrink_400_400/0/1680016742734?e=1723680000&v=beta&t=Th1zHgyR8xqtlD4-hh1c_Yv--t5gzvJHWc7DYaboPrE';
        $html .= '<img class="profile-pic" src="' . $profile_pic . '" alt="Student Profile">';

        $html .= '</div>
                    <div class="body">
                        <table>';

        // Define fields to display
        $fields = [
            'Name' => 'name',
            'Father Name' => 'fathername',
            'D.O.B' => 'dob',
            'Adm. No.' => 'admission_number',
            'Grade' => 'class_and_section',
            'Phone' => 'mobile',
            'Blood Group' => 'blood_group',
            'Address' => 'address_line_1'
        ];

        foreach ($fields as $label => $field) {
            // Handle concatenated class_name and section_name
            if ($field == 'class_and_section' && isset($student_rows['student']['class']['class_name']) && isset($student_rows['student']['section']['section_name'])) {
                $html .= '
                    <tr>
                        <th>' . $label . ' :</th>
                        <td>' . $student_rows['student']['class']['class_name'] . ' - ' . $student_rows['student']['section']['section_name'] . '</td>
                    </tr>';
            } elseif (isset($student_rows['student'][$field])) {
                $html .= '
                    <tr>
                        <th>' . $label . ' :</th>
                        <td>' . $student_rows['student'][$field] . '</td>
                    </tr>';
            }
        }

        $html .= '</table>
                </div>
                <div class="footer">
                    <p>' .$branch['address'] . ', ' . $branch['pincode'] . '</p>
                    <p>' . $branch['contact_number'] . ' | ' . $branch['email']. '</p>
                </div>
            </div>'; // End of id-card
    }
    $html .= '</div>'; // End of grid-container
    $html .= '
    </body>
    </html>';

    // Output the HTML content
    echo $html;
}
?>
