<!DOCTYPE html>
<html>
<head>
    <style>
        @page { size: A3 landscape;  margin: 30px 8px 8px 8px; }
        body { font-family: Arial, sans-serif; font-size: 8px; }
        body {
            font-family: Arial, sans-serif;
            font-size: 8px;
            counter-reset: page;
        }

          /* Page number fixed at top-right, slightly above the content */
          .page-number {
            position: fixed;
            top: -15px; /* Adjust as needed to appear above the border */
            right: 0;
            font-size: 10px;
        }

        .page-number:after {
            content: "Page " counter(page);
        }
        /* .data-table {
    width: auto;
    table-layout: auto;
    } */

        .header-table { width: 100%;  border-collapse: collapse;}
        .header-table td { vertical-align: middle;  border-collapse: collapse; border: 1px solid #222; }
        .company-title { font-size: 16px; font-weight: bold; text-align: center; }
        .company-subtitle { font-size: 12px; text-align: center; }
        .company-address { font-size: 10px; text-align: center; }
        .register-title { font-size: 12px; font-weight: bold; text-align: center; }
        .data-table { width: 100%; border-collapse: collapse; margin-top: 6px; }
        .data-table th, .data-table td { border: 1px solid #222; padding: 2px 2px; text-align: center; font-size: 7px; }
        .data-table td:last-child {border-right: 1px solid #222 !important;}
        /* .highlight { color: #0072c6; font-weight: bold; } */
        thead { display: table-header-group; }
    </style>
</head>
<body>

    <div class="page-number"></div>
    <!-- Header Row with Logos and Company Info -->
    <table class="data-table">
        <tr>
            <td colspan="33" style="padding:0;">
                <table width="100%" style="border-collapse:collapse;">
                    <tr>
                        <td style="width:13%; text-align:left; border:none; border-bottom:1px solid #222;">
                            <img src="{{ public_path('images/cripumps.logo.png') }}" alt="Left Logo" height="45" style="position: relative; left: 5mm;">
                        </td>
                        <td style="width:74%; border:none; border-bottom:1px solid #222;">
                            <div class="company-title">C.R.I. Pumps Private Limited</div>
                            <div class="company-subtitle">Unit : {{ $plant?->name ?? '' }}</div>
                            <div class="company-address">{{ $plant?->address ?? '' }}</div>
                            <div class="register-title">MOTOR FREE RUN TEST REGISTER</div>
                        </td>
                        {{-- <td style="width:13%; text-align:right; border:none; border-bottom:1px solid #222;">
                            <img src="{{ public_path('images/isi_logo1.PNG') }}" alt="ISI Logo" height="35" style="position: relative; left: -20mm;">
                        </td> --}}
                        {{-- @php
                        $showIsiLogo = $records->every(fn ($record) => $record['isi_model']);
                        @endphp

                        @if ($showIsiLogo)
                            <td style="width:13%; text-align:right; border:none; border-bottom:1px solid #222;">
                                <img src="{{ public_path('images/isi_logo1.PNG') }}" alt="ISI Logo" height="35" style="position: relative; left: -20mm;">
                            </td>
                        @else
                            <td style="width:13%; border:none; border-bottom:1px solid #222;"></td>
                        @endif --}}
                        <td style="width:13%; border:none; border-bottom:1px solid #222;"></td>
                    </tr>
                    <tr>
                        {{-- @php
                            $firstRecord = $records->first();
                            $kw = $firstRecord['kw'] ?? '-';
                            $hp = $firstRecord['hp'] ?? '-';
                            $phase = $firstRecord['phase'] ?? '-';
                        @endphp --}}

                        @php
                            $uniqueItemCodes = $records->pluck('Item Code')->unique();

                            //Default values
                            $kw = '-';
                            $hp = '-';
                            $phase = '-';

                            // If there's exactly one unique item code, extract values from the first record
                            if ($uniqueItemCodes->count() === 1) {
                                $firstRecord = $records->first();
                                $kw = $firstRecord['kw'] ?? '-';
                                $hp = $firstRecord['hp'] ?? '-';
                                $phase = $firstRecord['phase'] ?? '-';
                            }
                        @endphp
                        <td colspan="3" style="text-align:left; font-size:10px; font-weight:bold; border:none; padding-left:300mm;">
                            MOTOR KW / HP : {{ $kw }} / {{ $hp }} and PHASE : {{ $phase }}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <thead>
            <tr>
                <!-- Combine first 5 columns into one cell with rowspan 4 -->
                {{-- <th rowspan="4">Date</th> --}}
                <th rowspan="4" style="width: 40px; white-space: nowrap;">Date</th>
                <th rowspan="4">Output</th>
                <th rowspan="4">Motor SNo</th>
                <th rowspan="4">Item Code</th>
                {{-- <th rowspan="4">Motor Type</th> --}}
                <th rowspan="4" style="white-space: nowrap;">Motor Type</th>

                <!-- Grouped columns -->
                <th colspan="9">BEFORE FREE RUN</th>
                <th colspan="8">AFTER FREE RUN</th>
                <th colspan="3">LOCKED ROTOR TEST</th>

                <!-- Last 8 columns with rowspan 4 -->
                <th rowspan="4">No Load Pickup Voltage</th>
                <th rowspan="4">Room Temp.</th>
                <th rowspan="4">High Voltage Test</th>
                <th rowspan="4">Batch Number</th>
                <th rowspan="4">Batch Count</th>
                <th rowspan="4">Result</th>
                <th rowspan="4">Remark</th>
                <th rowspan="4">Tested By</th>
            </tr>

            <tr>
                <!-- BEFORE FREE RUN -->
                <th rowspan="2">Voltage</th>
                <th rowspan="2">Current</th>
                <th rowspan="2">Power</th>
                <th colspan="3">Resistance</th>
                <th rowspan="2">Insulation Resistance</th>
                <th rowspan="2">Frequency</th>
                <th rowspan="2">Speed</th>

                <!-- AFTER FREE RUN -->
                <th rowspan="2">Voltage</th>
                <th rowspan="2">Current</th>
                <th rowspan="2">Power</th>
                <th rowspan="2">IR.Hot</th>
                <th rowspan="2">IR.Cool</th>
                <th rowspan="2">Frequency</th>
                <th rowspan="2">Speed</th>
                <th rowspan="2">Leakage Current</th>

                <!-- LOCKED ROTOR TEST -->
                <th rowspan="2">Voltage</th>
                <th rowspan="2">Current</th>
                <th rowspan="2">Power</th>
            </tr>

            <tr>
                <th>RY</th>
                <th>YB</th>
                <th>BR</th>
            </tr>
            <tr>
                <th>(V)</th>
                <th>(A)</th>
                <th>(W)</th>
                <th>(Ohm)</th>
                <th>(Ohm)</th>
                <th>(Ohm)</th>
                <th>(Ohm)</th>
                <th>(Hz)</th>
                <th>(Rpm)</th>

                <th>(V)</th>
                <th>(A)</th>
                <th>(W)</th>
                <th>(Ohm)</th>
                <th>(Ohm)</th>
                <th>(Hz)</th>
                <th>(Rpm)</th>
                <th>(mA)</th>

                <th>(V)</th>
                <th>(A)</th>
                <th>(W)</th>
            </tr>
        </thead>
        @foreach($records as $record)
            <tr>

                <td>{{ $record['Date'] ?? '' }}</td>
                <td>{{ $record['Output'] ?? '' }}</td>
                <td>{{ $record['Motor SNo'] ?? '' }}</td>
                <td>{{ $record['Item Code'] ?? '' }}</td>
                {{-- <td>{{ $record['Motor Type'] ?? '' }}</td> --}}
                <td style="white-space: nowrap;">{{ $record['Motor Type'] ?? '' }}</td>


                {{-- BEFORE FREE RUN --}}

                <td>{{ $record['Voltage_Before'] ?? '' }}</td>
                <td>{{ $record['Current_Before'] ?? '' }}</td>
                <td>{{ $record['Power_Before'] ?? '' }}</td>
                <td>{{ $record['Resistance_RY'] ?? '' }}</td>
                <td>{{ $record['Resistance_YB'] ?? '' }}</td>
                <td>{{ $record['Resistance_BR'] ?? '' }}</td>
                <td>{{ $record['Insulation_BBR'] ?? '' }}</td>
                <td>{{ $record['Frequency_Before'] ?? '' }}</td>
                <td>{{ $record['Speed_Before'] ?? '' }}</td>

                {{-- AFTER FREE RUN --}}
                <td>{{ $record['Voltage_After'] ?? '' }}</td>
                <td>{{ $record['Current_After'] ?? '' }}</td>
                <td>{{ $record['Power_After'] ?? '' }}</td>
                <td>{{ $record['IR_Hot'] ?? '' }}</td>
                <td>{{ $record['IR_Cool'] ?? '' }}</td>
                <td>{{ $record['Frequency_After'] ?? '' }}</td>
                <td>{{ $record['Speed_After'] ?? '' }}</td>
                <td>{{ $record['Leakage_Current'] ?? '' }}</td>

                {{-- LOCKED ROTOR TEST --}}
                <td>{{ $record['Voltage_Locked'] ?? '' }}</td>
                <td>{{ $record['Current_Locked'] ?? '' }}</td>
                <td>{{ $record['Power_Locked'] ?? '' }}</td>

                {{-- Other Info --}}
                <td>{{ $record['No_Load_Pickup_Voltage'] ?? '' }}</td>
                <td>{{ $record['Room_Temp'] ?? '' }}</td>
                <td style="white-space: nowrap;">{{ $record['High_Voltage_Test'] ?? '' }}</td>
                <td>{{ $record['Batch_Number'] ?? '' }}</td>
                <td>{{ $record['Batch_Count'] ?? '' }}</td>
                <td>{{ $record['Result'] ?? '' }}</td>
                <td>{{ $record['Remark'] ?? '' }}</td>
                <td>{{ $record['Tested_By'] ?? '' }}</td>
            </tr>
        @endforeach
    </table>
</body>
</html>
