<div style="width: 50vw">


    <table style="width:100%">
        <tr>
            <th style="text-transform: capitalize; text-align:center; border: 1px solid black;" >Transmittal</th>
            <th style="text-transform: capitalize; text-align:center; border: 1px solid black;" >File</th>
            <th style="text-transform: capitalize; text-align:center; border: 1px solid black;" >Notes</th>
            <th style="text-transform: capitalize; text-align:center; border: 1px solid black;" >Remarks</th>
        </tr>
        @foreach ($transmittals as $transmittal)
            @php
                $fileRowCounter = 0;


                foreach ($transmittal->files as $file) {
                    $fileNoteCount = $file->notes_count;
                    $fileRemarkCount = $file->remarks_count;

                    $fileRowCount = $fileNoteCount;
                    if($fileRemarkCount > $fileNoteCount) {
                        $fileRowCount = $fileRemarkCount;
                    }

                    $fileRowCounter += $fileRowCount;

                }



                $transmittalRowCount = $fileRowCounter;

                if($transmittalRowCount < $transmittal->files_count) {
                    $transmittalRowCount = $transmittal->fiels_count;
                }

                $fileNoteCount = $transmittal->files[0]->notes_count;
                $fileRemarkCount = $transmittal->files[0]->remarks_count;

                $firstFileRowCount = $fileNoteCount;

                if($fileRemarkCount > $fileNoteCount)
                {
                    $firstFileRowCount = $fileRemarkCount;
                }


            @endphp
            <tr>
                <td style="border: 1px solid black;" rowspan="{{ $transmittalRowCount }}">{{ $transmittal->series  }} </td>
                <td style="border: 1px solid black;" rowspan="{{ $firstFileRowCount ?: 1  }}">{{ $transmittal->files[0]->description  }} </td>
                <td style="border: 1px solid black;" >{{ $transmittal->files[0]->notes->first()?->comment  }}</td>
                <td style="border: 1px solid black;" >{{ $transmittal->files[0]->remarks->first()?->remark  }} </td>
            </tr>

            @foreach($transmittal->files as $file)
                @php
                    $fileNoteCount = $file->notes_count;
                    $fileRemarkCount = $file->remarks_count;
                    $fileRowCount = $fileNoteCount;
                    if($fileRemarkCount > $fileNoteCount) {
                        $fileRowCount = $fileRemarkCount;
                    }

                    $notes = $file->notes->pluck('comment');
                    $remarks = $file->remarks->pluck('remark');
                @endphp
                @if(!$loop->parent->first)
                    @if($loop->first)
                        @for($i = 1 ; $i < $fileRowCount ; $i++)
                            <tr>
                                <td style="border: 1px solid black;" >{{ $notes[$i] ?? ''  }} </td>
                                <td style="border: 1px solid black;" >{{ $remarks[$i] ?? ''  }}</td>
                            </tr>
                        @endfor
                    @else
                        @for($i = 0 ; $i < $fileRowCount ; $i++)
                            <tr>
                                @if($i == 0)
                                    <td style="border: 1px solid black;" rowspan="{{ $fileRowCount  }}">{{ $file->description  }} </td>
                                @endif
                                <td style="border: 1px solid black;" >{{ $notes[$i] ?? ''  }} </td>
                                <td style="border: 1px solid black;" >{{ $remarks[$i] ?? ''  }}</td>
                            </tr>
                        @endfor
                    @endif

                @endif

            @endforeach
        @endforeach


{{--        @foreach($transmittals as $transmittal)--}}
{{--            @php $rowspan = 0; @endphp--}}
{{--            @foreach($transmittal->files as $file)--}}
{{--                @php $rowspan += max(count($file->notes), count($file->remarks)); @endphp--}}
{{--            @endforeach--}}
{{--            @foreach($transmittal->files as $file)--}}
{{--                @php $fileRowspan = max(count($file->notes), count($file->remarks)); @endphp--}}
{{--                <tr>--}}
{{--                    @if($loop->first)--}}
{{--                        <td style="border: 1px solid black;" rowspan="{{ $rowspan }}">{{ $transmittal->series }}</td>--}}
{{--                    @endif--}}
{{--                    <td style="border: 1px solid black;" rowspan="{{ $fileRowspan }}">{{ $file->description }}</td>--}}
{{--                    @if(count($file->notes) > 0)--}}
{{--                        <td style="border: 1px solid black;">{{ $file->notes[0]->comment }}</td>--}}
{{--                    @else--}}
{{--                        <td style="border: 1px solid black;"></td>--}}
{{--                    @endif--}}
{{--                    @if(count($file->remarks) > 0)--}}
{{--                        <td style="border: 1px solid black;">{{ $file->remarks[0]->remark }}</td>--}}
{{--                    @else--}}
{{--                        <td style="border: 1px solid black;"></td>--}}
{{--                    @endif--}}
{{--                </tr>--}}
{{--                @for($i = 1; $i < $fileRowspan; $i++)--}}
{{--                    <tr>--}}
{{--                        @if($loop->first)--}}
{{--                            <td style="border: 1px solid black;">--first--</td>--}}
{{--                        @endif--}}
{{--                        @if(isset($file->notes[$i]))--}}
{{--                            <td style="border: 1px solid black;">{{ $file->notes[$i]->comment }}</td>--}}
{{--                        @else--}}
{{--                            <td style="border: 1px solid black;"></td>--}}
{{--                        @endif--}}
{{--                        @if(isset($file->remarks[$i]))--}}
{{--                            <td style="border: 1px solid black;">{{ $file->remarks[$i]->remark }}</td>--}}
{{--                        @else--}}
{{--                            <td style="border: 1px solid black;"></td>--}}
{{--                        @endif--}}
{{--                    </tr>--}}
{{--                @endfor--}}
{{--            @endforeach--}}
{{--        @endforeach--}}
    </table>
</div>
