@php
    $recordId = isset($admitcard) ? $admitcard->student_record_id : (isset($admit) ? $admit->student_record_id : null);
    $subjectRows = collect();

    if (isset($admit_subjects_by_record) && $recordId) {
        $subjectRows = collect($admit_subjects_by_record[$recordId] ?? []);
    } elseif (isset($admit_subjects)) {
        $subjectRows = collect($admit_subjects);
    } elseif (isset($exam_routines)) {
        $subjectRows = collect($exam_routines)->map(function ($routine) {
            return (object) [
                'subject_code' => optional($routine->subject)->subject_code,
                'subject_name' => optional($routine->subject)->subject_name,
                'exam_date' => $routine->date,
            ];
        });
    }

    $showRules = !isset($setting->show_rules_section) || (int) $setting->show_rules_section === 1;
    $showSubjectList = !isset($setting->show_subject_list) || (int) $setting->show_subject_list === 1;
    $showSignatures = !isset($setting->enable_signature_columns) || (int) $setting->enable_signature_columns === 1;
@endphp

@once
    <style>
        .admit-card-extra {
            margin-top: 10px;
            color: #222;
            line-height: 1.35;
            page-break-inside: auto;
        }

        .admit-card-rules {
            margin: 8px 0 10px;
            page-break-inside: avoid;
        }

        .admit-card-rules-title,
        .admit-card-subject-title {
            color: #222;
            font-size: 12px;
            font-weight: 600;
            margin: 0 0 5px;
            text-align: left;
        }

        .admit-card-rules ul {
            margin: 0 0 0 16px;
            padding: 0;
        }

        .admit-card-rules li {
            color: #222;
            font-size: 10.5px;
            margin: 2px 0;
        }

        .admit-card-subject-table {
            border-collapse: collapse;
            margin: 6px 0 14px !important;
            page-break-inside: auto;
            table-layout: fixed;
            width: 100%;
        }

        .admit-card-subject-table thead {
            display: table-header-group;
        }

        .admit-card-subject-table tbody {
            display: table-row-group;
        }

        .admit-card-subject-table th,
        .admit-card-subject-table td {
            border: 1px solid #cfd3df;
            color: #222;
            font-size: 10px;
            padding: 5px 6px;
            text-align: left;
            vertical-align: middle;
            word-break: break-word;
        }

        .admit-card-subject-table th {
            background: #f5f6fa;
            font-weight: 600;
            text-transform: uppercase;
        }

        .admit-card-subject-table .signature-cell {
            height: 25px;
        }

        .admit-card-subject-table .signature-line {
            border-bottom: 1px solid #777;
            display: block;
            height: 16px;
            width: 100%;
        }

        .admit_card_wrapper,
        .card-item {
            height: auto !important;
            min-height: 0 !important;
            overflow: visible !important;
        }

        .admid_card_wrapper_body {
            overflow: visible !important;
        }

        @media print {
            .admit_card_wrapper {
                height: auto !important;
                min-height: 0 !important;
                page-break-after: always;
            }

            .card-item {
                height: auto !important;
                page-break-after: always;
                page-break-inside: auto;
            }

            .admid_card_wrapper_body {
                display: block !important;
            }

            .admit-card-subject-table {
                page-break-inside: auto;
            }

            .admit-card-subject-table tr {
                page-break-after: auto;
                page-break-inside: avoid;
            }
        }
    </style>
@endonce

@if($showRules || ($showSubjectList && $subjectRows->isNotEmpty()))
    <div class="admit-card-extra">
        @if($showRules)
            <div class="admit-card-rules">
                <p class="admit-card-rules-title">Rules to be followed by the candidates:</p>
                <ul>
                    <li>Admit card must be collected one day before exam</li>
                    <li>Arrive 15 minutes early</li>
                    <li>Only allowed materials permitted</li>
                    <li>No phones or unauthorized materials</li>
                    <li>Follow official schedule strictly</li>
                </ul>
            </div>
        @endif

        @if($showSubjectList && $subjectRows->isNotEmpty())
            <p class="admit-card-subject-title">To Be Signed by the Respective Lecturer</p>
            <table class="admit-card-subject-table">
                <thead>
                    <tr>
                        <th style="width: 15%;">Unit Code</th>
                        <th>Unit</th>
                        @if($showSignatures)
                            <th style="width: 18%;">Student Signature</th>
                            <th style="width: 18%;">Lecturer Signature</th>
                        @endif
                        <th style="width: 16%;">Exam Date</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($subjectRows as $subjectRow)
                        <tr>
                            <td>{{ data_get($subjectRow, 'subject_code') ?: '-' }}</td>
                            <td>{{ data_get($subjectRow, 'subject_name') ?: '-' }}</td>
                            @if($showSignatures)
                                <td class="signature-cell"><span class="signature-line">&nbsp;</span></td>
                                <td class="signature-cell"><span class="signature-line">&nbsp;</span></td>
                            @endif
                            <td>
                                @php $examDate = data_get($subjectRow, 'exam_date'); @endphp
                                {{ $examDate ? (function_exists('dateConvert') ? dateConvert($examDate) : date('d M Y', strtotime($examDate))) : '-' }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endif
    </div>
@endif
