<?php

function getIDCardTemplateDataSecond($institutions, $branch, $academic_session, $students_info, $grid = 1) {
    
    // Initialize $html as an empty string
    $html = '
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student ID Card</title>
    <style>' . studentProfileStylessecond($grid) . '</style>
</head>
<body>
    <div class="card-grid">';

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"; 
    }

    $fields = [
        "Name" => $student_info['student_name'],
        "Father_Name" => $student_info['fathername'],
        "Date of Birth" => $student_info['dob'],
        "Adm. No." => $student_info['admissionnumber'],
        "Grade" => $student_info['class']['class_name'] . ' ' . $student_info['section']['section_name'],
        "Phone" => $student_info['phone'],
        "Address" => $student_info['address'],
        "student_images" => $student_info['student_images']
    ];

    $html .= '
        <div class="id-card">
            <div class="header">
                <div class="rejoice">
                    <img
                        src="' . $institutions['logo'] . '"
                        alt="School Logo"
                    />
                </div>
                <h2>' . htmlspecialchars($institutions['institution_name'], ENT_QUOTES, 'UTF-8') . '</h2>
                <p>Let\'s Make Learning Joyful</p>
            </div>
            <div>
                <img class="profile-pic" 
                     src="' . (!empty($fields['student_images']) 
                                ? htmlspecialchars($fields['student_images'], ENT_QUOTES, 'UTF-8') 
                                : $url) . '" 
                     alt="Profile Picture" />
            </div>
            <div class="body">
                <table style="width: 100%; border-collapse: collapse;">';
               $html .= ' <tr>
                <th colspan="3" style="font-size: 8pt;text-align:center;">Session 2023-24</th>
              </tr>';
              foreach ($fields as $label => $value) {
                if ($label === "Name") {
                    // Special case for "Name"
                    $html .= '
                        <tr>
                            <th>Name</th>
                            <td>:</td>
                            <td style="font-size: 7pt; font-weight: 550;">' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</td>
                        </tr>';
                } elseif ($label !== "student_images") {
                    // General case for other fields (excluding "student_images")
                    $html .= '
                        <tr>
                            <th style="width: 35%; text-align: left;">' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '</th>
                            <td style="width: 2%; text-align: center;">:</td>
                            <td style="text-align: left;">' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</td>
                        </tr>';
                }
            }
            

    $html .= '
                </table>
                 
                 <div class="cont">
        <p style="font-size: 7.5pt; font-family: Calibri; font-weight: 600;position: absolute;bottom: 47px;">Principal Signature</p>
        </div>
            </div>
            <div class="footer">
                <p>' . htmlspecialchars($branch['address'], ENT_QUOTES, 'UTF-8') . ', ' . htmlspecialchars($branch['pincode'], ENT_QUOTES, 'UTF-8') . '</p>
                <p>' . htmlspecialchars($branch['contact_number'], ENT_QUOTES, 'UTF-8') . ' | ' . htmlspecialchars($branch['email'], ENT_QUOTES, 'UTF-8') . '</p>
            </div>
        </div>';
}


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

return $html;
}

function studentProfileStylessecond($grid) {
    return '
    <style>
      body {
        padding: 0;
        margin: 0;
        font-family: "Arial", sans-serif;
        background-color: #f0f0f0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }

      .card-grid {
        display: grid;
        grid-template-columns: repeat(' . $grid . ', 1fr);
        gap: 20px;
        padding: 20px;
        justify-content: center;
        align-items: center;
      }

      .id-card {
        width: 5.3975cm;
        height: 8.57cm;
        border: .5px solid #f0f0f0;
        border-radius: 15px;
        overflow: hidden;
        position: relative;
        background-color: #fff;
      }

      /* Rest of the existing styles remain the same */
      .header {
        width: 100%;
        height: 100px;
        background-color: #2c7c57;
        color: #fff;
        text-align: center;
        padding: 5px 0;
        clip-path: polygon(0 0, 100% 0%, 100% 65%, 50% 100%, 0% 65%);
      }

      .rejoice img {
        width: 60px; /* Adjust the width of the logo */
        height: 35px; /* Adjust the height of the logo */
      }

      .header h2 {
        font-size: 10pt;
        margin-top: -2px;
      }

      .header p {
        margin: -6px 0;
        font-size: 10px;
      }

      .profile-pic {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: #fff;
        border: 1px solid #1b2a41;
        position: absolute;
        top: 75px;
        left: 50%;
        transform: translateX(-50%);
      }

 .body {
        padding: 5px;
        /*padding-top: 25px;*/
        margin-top: 15px;
      }

      .body table th {
        text-align: left;
        padding-left: 8px;
        word-wrap: break-word;
        vertical-align: top;
        font-weight: 550;
        font-size: 7pt;
      }

      .body table td {
        /*text-align: left; */
        font-size: 7pt;
        vertical-align: top;
        font-weight: 400;
        vertical-align: top;
        padding-left:10px;
      }
        
      .cont{
          display:flex;
          justify-content:center;
      }
      .footer {
        position: absolute;
        bottom: 0px;
        width: 100%;
        height: auto; /* Adjust height automatically based on content */
        background-color: #2c7c57;
        color: #fff;
        text-align: center;
        padding: 5px 0; /* Increased padding slightly for better spacing */
    }

    .footer p {
        font-size: 6.6pt; 
        margin: 2px 0; /* Uniform margin for even spacing */
    }
      @media print {
        .card-grid {
          grid-template-columns: repeat(3, 1fr);
        }
      }
    </style>
    ';
}

function get_IDCard_TemplateData_2($institutions, $branch, $academic_session, $students_info, $grid = 1) {
    // Ensure $students_info is always an array
    if (!isset($students_info[0])) {
        $students_info = [$students_info];
    }
    
    // Initialize default values
    $institution_logo = isset($institutions['logo']) ? $institutions['logo'] : '';
    $institution_name = isset($institutions['institution_name']) ? $institutions['institution_name'] : '';
    $branch_address = isset($branch['address']) ? $branch['address'] : '';
    $branch_pincode = isset($branch['pincode']) ? $branch['pincode'] : '';
    $branch_contact = isset($branch['contact_number']) ? $branch['contact_number'] : '';
    $branch_email = isset($branch['email']) ? $branch['email'] : '';
    
    $html = '
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student ID Card</title>
    <style>' . studentProfileStylessecond($grid) . '</style>
</head>
<body>
    <div class="card-grid">';

    foreach ($students_info as $student_info) {
        // Ensure student_info has all required fields with defaults
        $student_info = array_merge([
            'gender' => 'M',
            'student_name' => '',
            'fathername' => '',
            'dob' => '',
            'admissionnumber' => '',
            'class' => ['class_name' => ''],
            'section' => ['section_name' => ''],
            'phone' => '',
            'address' => '',
            'student_images' => null
        ], $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";

        $fields = [
            "Name" => $student_info['student_name'],
            "Father_Name" => $student_info['fathername'],
            "Date of Birth" => $student_info['dob'],
            "Adm. No." => $student_info['admissionnumber'],
            "Grade" => ($student_info['class']['class_name'] ?? '') . ' ' . ($student_info['section']['section_name'] ?? ''),
            "Phone" => $student_info['phone'],
            "Address" => $student_info['address'],
            "student_images" => $student_info['student_images']
        ];

        $html .= '
        <div class="id-card">
            <div class="header">
                <div class="rejoice">
                    <img src="' . htmlspecialchars($institution_logo) . '" alt="School Logo" />
                </div>
                <h2>' . htmlspecialchars($institution_name) . '</h2>
                <p>Let\'s Make Learning Joyful</p>
            </div>
            <div>
                <img class="profile-pic" 
                     src="' . (!empty($fields['student_images']) 
                                ? htmlspecialchars($fields['student_images']) 
                                : htmlspecialchars($url)) . '" 
                     alt="Profile Picture" />
            </div>
            <div class="body">
                <table style="width: 100%; border-collapse: collapse;">';
        
        $html .= '<tr>
                <th colspan="3" style="font-size: 8pt;text-align:center;">Session ' . 
                (is_array($academic_session) ? htmlspecialchars($academic_session['name'] ?? '2023-24') : htmlspecialchars($academic_session)) . '</th>
              </tr>';
        
        foreach ($fields as $label => $value) {
            if ($label === "Name") {
                $html .= '
                    <tr>
                        <th>Name</th>
                        <td>:</td>
                        <td style="font-size: 7pt; font-weight: 550;">' . htmlspecialchars($value) . '</td>
                    </tr>';
            } elseif ($label !== "student_images") {
                $html .= '
                    <tr>
                        <th style="width: 35%; text-align: left;">' . htmlspecialchars($label) . '</th>
                        <td style="width: 2%; text-align: center;">:</td>
                        <td style="text-align: left;">' . htmlspecialchars($value) . '</td>
                    </tr>';
            }
        }

        $html .= '
                </table>
                <div class="cont">
                    <p style="font-size: 7.5pt; font-family: Calibri; font-weight: 600;position: absolute;bottom: 47px;">Principal Signature</p>
                </div>
            </div>
            <div class="footer">
                <p>' . htmlspecialchars($branch_address) . ', ' . htmlspecialchars($branch_pincode) . '</p>
                <p>' . htmlspecialchars($branch_contact) . ' | ' . htmlspecialchars($branch_email) . '</p>
            </div>
        </div>';
    }

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

    return $html;
}
?>

