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

        .page-number {
            position: fixed;
            top: -15px;
            right: 0;
            font-size: 10px;
        }

        .page-number:after {
            content: "Page " counter(page);
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 6px;
        }

        .data-table th, .data-table td {
            border: 1px solid #222;
            padding: 2px;
            text-align: center;
            font-size: 7px;
        }

        thead { display: table-header-group; }
            .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; }
    </style>
</head>
<body>

<div class="page-number"></div>

<table class="data-table">
    <tr>
        <td colspan="20" 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="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>
                    {{-- @php
                        $showIsiLogo = $records->every(fn ($record) => $record['isi_model']);
                    @endphp
                    <td style="width:13%; text-align:right; border:none; border-bottom:1px solid #222;">
                        @if ($showIsiLogo)
                            <img src="{{ public_path('images/isi_logo1.PNG') }}" alt="ISI Logo" height="35" style="position: relative; left: -20mm;">
                        @endif
                    </td> --}}
                    <td style="width:13%; text-align:right; border:none; border-bottom:1px solid #222;"> </td>
                </tr>
                <tr>
                    @php
                        $uniqueItemCodes = $records->pluck('Item Code')->unique();
                        $kw = $hp = $phase = '-';
                        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>
        <!-- Row 1: Main Group Headings -->
        <tr>
            {{-- <th rowspan="3">Date</th> --}}
            <th rowspan="3" style="width: 40px; white-space: nowrap;">Date</th>
            <th rowspan="3">Motor SNo</th>
            <th rowspan="3">Item Code</th>
            <th rowspan="3">Motor Type</th>

            <th colspan="8">AFTER FREE RUN</th>
            <th colspan="3">LOCKED ROTOR TEST</th>

            <th rowspan="3">No Load Pickup Voltage</th>
            <th rowspan="3">Room Temp.</th>
            <th rowspan="3">High Voltage Test</th>
            <th rowspan="3">Result</th>
            <th rowspan="3">Remark</th>
        </tr>

        <!-- Row 2: Sub-Headers (Column Names) -->
        <tr>
            <!-- AFTER FREE RUN -->
            <th>Voltage</th>
            <th>Current</th>
            <th>Power</th>
            <th>IR.Hot</th>
            <th>IR.Cool</th>
            <th>Frequency</th>
            <th>Speed</th>
            <th>Leakage Current</th>

            <!-- LOCKED ROTOR TEST -->
            <th>Voltage</th>
            <th>Current</th>
            <th>Power</th>
        </tr>
        <tr>
            <!-- AFTER FREE RUN -->
            <td>(V)</td>
            <td>(A)</td>
            <td>(W)</td>
            <td>(Ohm)</td>
            <td>(Ohm)</td>
            <td>(Hz)</td>
            <td>(Rpm)</td>
            <td>(mA)</td>
            <!-- LOCKED ROTOR TEST -->
            <td>(V)</td>
            <td>(A)</td>
            <td>(W)</td>
        </tr>
    </thead>


    @foreach($records as $record)
    <tr>
        <td>{{ $record['Date'] ?? '' }}</td>
        <td>{{ $record['Motor SNo'] ?? '' }}</td>
        <td>{{ $record['Item Code'] ?? '' }}</td>
        <td style="white-space: nowrap;">{{ $record['Motor Type'] ?? '' }}</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>

        {{-- OTHERS --}}
        <td>{{ $record['No_Load_Pickup_Voltage'] ?? '' }}</td>
        <td>{{ $record['Room_Temp'] ?? '' }}</td>
        <td style="white-space: nowrap;">{{ $record['High_Voltage_Test'] ?? '' }}</td>
        <td>{{ $record['Result'] ?? '' }}</td>
        <td>{{ $record['Remark'] ?? '' }}</td>
    </tr>
    @endforeach
</table>

</body>
</html>
