<?php

function getIDCardTemplateData($institutions, $branch, $academic_session, $students_info, $grid = 1)
{
   


// Output the image in HTML


    $html = '
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Student Profiles</title>';

    $html .= studentProfileStyles($grid);

    $html .= '
    </head>
  <body>
    <div class="card-grid">';

    // Loop through each student's data
    foreach ($students_info as $student_info) {
        if ($student_info['gender'] == 'M') {
    $url = "https://rejoicebilldesk.in/rjllearning_development/resources/idcard_template/common_assets/avatar1.png";
    } else {
    $url = "https://rejoicebilldesk.in/rjllearning_development/resources/idcard_template/common_assets/avatar4.png"; 
    }
        $html .= '
        <div class="student-profile py-4" style="padding: 10px;">
            <div class="id_card_container">
                <div class="card shadow-sm">
                    <header class="flex-container">
                        <div style="padding: 10px; width:20%">
                            <img class="logo" src="' . $institutions['logo'] . '" alt="School Logo" style="width: 55px; height: 55px;">
                        </div>
                        <div style="padding: 2px; width: 70%; align-items: center; display: flex; flex-direction: column;">
                            <span style="font-weight: 600; font-size: 14pt; color: #fff; font-family: Calibri; justify-content: end;">' . $institutions['institution_name'] . '</span>
                            <span style="font-weight: 400; font-size: 8.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . $branch['name'] . '</span>
                            <span style="font-weight: 400; font-size: 7.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . $branch['contact_number'] . ' | ' . $branch['email'] . '</span>
                            <span style="font-weight: 400; font-size: 8.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . $branch['address'] . ', ' . $branch['pincode'] . '</span>
                        </div>
                    </header>
                    <div class="card-header bg-transparent text-center" style="height: 115px; margin-bottom: 0px;">
                        <div style="display: flex; flex-direction: column; justify-content: space-around;">';
                          if (!empty($student_info['student_images'])) {
                                  $html .= ' <img class="profile_img" src="'. htmlspecialchars($student_info['student_images'], ENT_QUOTES, 'UTF-8').'" alt=""> ';
                         } else {
                                  $html .= ' <img class="profile_img" src="'. $url.'" alt=""> ';
                 
                         }
                    $html .= '<p style="font-size: 6.5pt; font-family: Calibri; font-weight: 600; text-align: center;">
                        Principal Signature
                     </p>
                    </div>

                        <div style="">
                            <table class="table table-bordered">
                                <tr>
                                    <th colspan="3" style="font-size: 8pt; padding-bottom: 14px;">Session ' . $academic_session['name'] . '</th>
                                </tr>';

        // Create an array for student details
        $fields = [
            "Name" => $student_info['student_name'],
            "Father_Name" => $student_info['fathername'],
            "Adm. No." => $student_info['admissionnumber'],
            "Grade" => $student_info['class']['class_name'] . ' ' . $student_info['section']['section_name'],
            "Phone" => $student_info['phone'],
            "Address" => $student_info['address']
        ];

        // Use foreach to dynamically generate table rows for the student
        foreach ($fields as $label => $value) {
            $html .= '
                                <tr>
                                    <th style="width:35%">' . $label . '</th>
                                    <td width="2%">:</td>
                                    <td>' . $value . '</td>
                                </tr>';
        }
        $html .= '</table>
                        </div>
                    </div>
                </div>
            </div>
        </div>';
    }

    $html .= '
    </div>
    </body>
    </html>';

    return $html;
}

function get_IDCardTemplate1_Data($institutions, $branch, $academic_session, $students_info, $grid = 1)
{
    // Ensure $students_info is always an array
    if (!isset($students_info[0])) {
        $students_info = [$students_info];
    }

    // Ensure academic session is properly formatted
    if (is_array($academic_session) && isset($academic_session['name'])) {
        $session_name = $academic_session['name'];
    } else {
        $session_name = (string)$academic_session;
    }

    $html = '
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Student Profiles</title>';

    $html .= studentProfileStyles($grid);
    $html .= '
    </head>
    <body>
    <div class="card-grid">';

    // Loop through each student's data
    foreach ($students_info as $student_info) {
        // Make sure student_info has all required fields
        $student_info = array_merge([
            'gender' => 'M',
            'student_images' => null,
            'student_name' => '',
            'fathername' => '',
            'admissionnumber' => '',
            'class' => ['class_name' => ''],
            'section' => ['section_name' => ''],
            'phone' => '',
            'address' => ''
        ], $student_info);

        $url = ($student_info['gender'] == 'M') ?
            "https://rejoicebilldesk.in/rjllearning_development/resources/idcard_template/common_assets/avatar1.png" :
            "https://rejoicebilldesk.in/rjllearning_development/resources/idcard_template/common_assets/avatar4.png";

        $html .= '
        <div class="student-profile py-4" style="padding: 10px;">
            <div class="id_card_container">
                <div class="card shadow-sm">
                    <header class="flex-container">
                        <div style="padding: 10px; width:20%">';

        // Safely access institution logo
        $institution_logo = isset($institutions['logo']) ? $institutions['logo'] : '';
        $institution_name = isset($institutions['institution_name']) ? $institutions['institution_name'] : '';

        $html .= '<img class="logo" src="' . htmlspecialchars($institution_logo) . '" alt="School Logo" style="width: 55px; height: 55px;">
                        </div>
                        <div style="padding: 2px; width: 70%; align-items: center; display: flex; flex-direction: column;">
                            <span style="font-weight: 600; font-size: 14pt; color: #fff; font-family: Calibri; justify-content: end;">' . htmlspecialchars($institution_name) . '</span>';

        // Safely access branch info
        $branch_name = isset($branch['name']) ? $branch['name'] : '';
        $branch_contact = isset($branch['contact_number']) ? $branch['contact_number'] : '';
        $branch_email = isset($branch['email']) ? $branch['email'] : '';
        $branch_address = isset($branch['address']) ? $branch['address'] : '';
        $branch_pincode = isset($branch['pincode']) ? $branch['pincode'] : '';

        $html .= '<span style="font-weight: 400; font-size: 8.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . htmlspecialchars($branch_name) . '</span>
                            <span style="font-weight: 400; font-size: 7.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . htmlspecialchars($branch_contact) . ' | ' . htmlspecialchars($branch_email) . '</span>
                            <span style="font-weight: 400; font-size: 8.5pt; color: #fff; font-family: Calibri; justify-content: end;">' . htmlspecialchars($branch_address) . ', ' . htmlspecialchars($branch_pincode) . '</span>
                        </div>
                    </header>
                    <div class="card-header bg-transparent text-center" style="height: 115px; margin-bottom: 0px;">
                        <div style="display: flex; flex-direction: column; justify-content: space-around;">';

        if (!empty($student_info['student_images'])) {
            $html .= '<img class="profile_img" src="' . htmlspecialchars($student_info['student_images'], ENT_QUOTES, 'UTF-8') . '" alt="">';
        } else {
            $html .= '<img class="profile_img" src="' . htmlspecialchars($url) . '" alt="">';
        }

        $html .= '<p style="font-size: 6.5pt; font-family: Calibri; font-weight: 600; text-align: center;">
                        Principal Signature
                     </p>
                    </div>

                        <div style="">
                            <table class="table table-bordered">
                                <tr>
                                    <th colspan="3" style="font-size: 8pt; padding-bottom: 14px;">Session ' . htmlspecialchars($session_name) . '</th>
                                </tr>';

        // Create an array for student details
        $fields = [
            "Name" => $student_info['student_name'],
            "Father_Name" => $student_info['fathername'],
            "Adm. No." => $student_info['admissionnumber'],
            "Grade" => ($student_info['class']['class_name'] ?? '') . ' ' . ($student_info['section']['section_name'] ?? ''),
            "Phone" => $student_info['phone'],
            "Address" => $student_info['address']
        ];

        // Use foreach to dynamically generate table rows for the student
        foreach ($fields as $label => $value) {
            $html .= '
                                <tr>
                                    <th style="width:35%">' . htmlspecialchars($label) . '</th>
                                    <td width="2%">:</td>
                                    <td>' . htmlspecialchars($value) . '</td>
                                </tr>';
        }
        $html .= '</table>
                        </div>
                    </div>
                </div>
            </div>
        </div>';
    }

    $html .= '
    </div>
    </body>
    </html>';

    return $html;
}

function studentProfileStyles($grid)
{
    return '
    <style>
        body {
            padding: 0;
            margin: 0;
            font-family: "Lato", sans-serif;
            color: #000;

        }

        .student-profile .card {
            border-radius: 10px;
        }

        .student-profile .card .card-header .logo {
             width: 150px;
             height: 200px;
             object-fit: cover;
             margin: 0px auto;
             border: 1px solid #ccc;
         }
        .student-profile .card .card-header .profile_img {
            width: 75px;
            height: 90px;
            object-fit: cover;
            border: 1px solid #ccc;
        }

        .student-profile .card h3 {
            font-weight: 700;
            font-size: 20pt;
            font-weight: 700;
        }

        .student-profile .card p {
            font-size: 16px;
            color: #000;
            margin: 0px;
            padding: 0px;
        }

        .student-profile .table th {
            font-size: 7pt;
            font-weight: 550;
            
            padding: .15px 8px;
            color: #000;
            text-align: left;
            vertical-align: top;
        }

    .student-profile .table td {
      font-size: 7pt;
      font-weight: 400;
      /* Keep values normal */
      padding: .15px 8px;
      color: #000;
      vertical-align: top;
    }

        .student-profile .table tr {
            text-align: left;
        }

        .student-profile .card {
            border-radius: 10px;
        }

        .card-header:first-child {
            border-radius: calc(.25rem - 1px) calc(.25rem - 1px) 0 0;
        }

        .shadow-sm {
            box-shadow: 0 .125rem .25rem rgba(0, 0, 0, .075) !important;
        }

        .card {
            position: relative;
            display: -ms-flexbox;
            display: flex;
            -ms-flex-direction: column;
            flex-direction: column;
            min-width: 0;
            word-wrap: break-word;
            background-color: #fff;
            background-clip: border-box;
            border: 1px solid rgba(0, 0, 0, .125);
            border-radius: .25rem;
        }

        .bg-transparent {
            background-color: transparent !important;
        }

        .card-header {
            display: flex;
            /*padding: .60rem 1rem;*/
            padding: .20rem .61rem;
            margin-bottom: 0;
            background-color: rgba(0, 0, 0, .03);
            border-bottom: 1px solid rgba(0, 0, 0, .125);
        }

        .card-body {
            flex: 1 1 auto;
            padding: 1.25rem;
        }

        .student-profile .card p {
            font-size: 16px;
            color: #000;
        }

        .mb-0,
        .my-0 {
            margin-bottom: 0 !important;
        }

        .pr-1,
        .px-1 {
            padding-right: .25rem !important;
        }

        .student-profile .card p {
            font-size: 16px;
            color: #000;
        }

        .id_card_container {
            width: 8.57cm;
            height: 5.3975cm;
            border: 1px solid gray;
            overflow: hidden;
        }

        .flex-container {
            display: flex;
            background-color: #2c7c57;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }

        .card-header,
        .card-body {
            background-color: #00B2A2;
        }
        
        
        .card-grid {
            display: grid;
            grid-template-columns: repeat(' . $grid . ', 1fr);
            gap: 20px;
            padding: 20px;
        }
        @media print {
            .card-grid {
                grid-template-columns: repeat(3, 1fr);
            }
        }
    </style>
    ';
}
