<?php

namespace App\Exports;

use Illuminate\Support\Facades\View as FacadesView;
use Illuminate\View\View as ViewView;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
// use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Excel;


use Maatwebsite\Excel\Events\BeforeExport;

use PhpOffice\PhpSpreadsheet\IOFactory;

// {
//     public function __construct(
//         public $records,
//     ) {}

//     // public function view(): View
//     // {
//     //     return view('exports.export-isi-pdf', [
//     //         'records' => $this->records,
//     //     ]);
//     // }

//        public function collection()
//     {
//         return $this->records;
//     }

//     public function headings(): array
//     {
//         // Top two rows (Company info + Motor KW/HP)
//         return [
//             ['C.R.I. Pumps Private Limited', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Unit: MOTOR FREE RUN TEST REGISTER'],
//             ['Motor KW/HP : - / -', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Phase : -'],
//             // Column headers
//             [
//                 'Date', 'Motor SNo', 'Item Code', 'Motor Type',
//                 'Voltage', 'Current', 'Power', 'IR.Hot', 'IR.Cool', 'Frequency', 'Speed', 'Leakage Current', // AFTER FREE RUN
//                 'Voltage', 'Current', 'Power', // LOCKED ROTOR TEST
//                 'No Load Pickup Voltage', 'Room Temp.', 'High Voltage Test', 'Result', 'Remark'
//             ]
//         ];
//     }

//     public function registerEvents(): array
//     {
//         return [
//             AfterSheet::class => function (AfterSheet $event) {
//                 $sheet = $event->sheet->getDelegate();

//                 // Merge top rows for company info
//                 $sheet->mergeCells('A1:S1'); // Company Name
//                 $sheet->mergeCells('T1:T1'); // Unit/Register info
//                 $sheet->mergeCells('A2:S2'); // Motor KW/HP row
//                 $sheet->mergeCells('T2:T2'); // Phase

//                 // Merge headers for grouped columns (AFTER FREE RUN & LOCKED ROTOR TEST)
//                 $sheet->mergeCells('E3:L3');  // AFTER FREE RUN
//                 $sheet->mergeCells('M3:O3');  // LOCKED ROTOR TEST

//                 // Optional: style headers
//                 $sheet->getStyle('A1:T3')->getFont()->setBold(true);
//                 $sheet->getStyle('A1:T3')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
//                 $sheet->getStyle('A1:T3')->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);

//                 // Set column widths (optional for better visibility)
//                 $columns = range('A', 'T');
//                 foreach ($columns as $col) {
//                     $sheet->getColumnDimension($col)->setAutoSize(true);
//                 }
//             },
//         ];
//     }


// }

// class MotorFreeRunExport implements FromCollection, WithMapping, WithStartRow
// {
//     protected Collection $records;

//     public function __construct(Collection $records)
//     {
//         $this->records = $records;
//     }

//     public function collection()
//     {
//         return $this->records;
//     }

//     public function startRow(): int
//     {
//         return 3; // insert data starting from row 3
//     }

//     public function map($record): array
//     {
//         return [
//             $record['Date'] ?? '',
//             $record['Output'] ?? '',
//             $record['Motor SNo'] ?? '',
//             $record['Item Code'] ?? '',
//             $record['Motor Type'] ?? '',
//             $record['kw'] ?? '',
//             $record['hp'] ?? '',
//             $record['phase'] ?? '',
//             $record['isi_model'] ?? '',
//             $record['Voltage_Before'] ?? '',
//             $record['Current_Before'] ?? '',
//             $record['Power_Before'] ?? '',
//             $record['Resistance_RY'] ?? '',
//             $record['Resistance_YB'] ?? '',
//             $record['Resistance_BR'] ?? '',
//             $record['Insulation_Resistance'] ?? '',
//             $record['Frequency_Before'] ?? '',
//             $record['Speed_Before'] ?? '',
//             $record['Voltage_After'] ?? '',
//             $record['Current_After'] ?? '',
//             $record['Power_After'] ?? '',
//             $record['IR_Hot'] ?? '',
//             $record['IR_Cool'] ?? '',
//             $record['Leakage_Current'] ?? '',
//             $record['Frequency_After'] ?? '',
//             $record['Speed_After'] ?? '',
//             $record['Voltage_Locked'] ?? '',
//             $record['Current_Locked'] ?? '',
//             $record['Power_Locked'] ?? '',
//             $record['No_Load_Pickup_Voltage'] ?? '',
//             $record['Room_Temp'] ?? '',
//             $record['High_Voltage_Test'] ?? '',
//             $record['Batch_Number'] ?? '',
//             $record['Batch_Count'] ?? '',
//             $record['Result'] ?? '',
//             $record['Remark'] ?? '',
//             $record['Tested_By'] ?? '',
//         ];
//     }
// }


class MotorFreeRunExport implements  WithCustomStartCell, WithMapping, WithStartRow, WithChunkReading
{
    protected $records;
    protected $templatePath;

    public function __construct($records)
    {
        $this->records = $records;
      //  dd($this->records);
    }

    public function collection()
    {
        return $this->records;
    }

    public function startRow(): int
    {
        return 7; // Start inserting data from row 7
    }

    public function startCell(): string
    {
        return 'A7'; // Start inserting data from row 7 column A
    }

    public function map($record): array
    {
        return [
            $record['Date'] ?? '',
            $record['Output'] ?? '',
            $record['Motor SNo'] ?? '',
            $record['Item Code'] ?? '',
            $record['Motor Type'] ?? '',
            $record['kw'] ?? '',
            $record['hp'] ?? '',
            $record['phase'] ?? '',
            $record['isi_model'] ?? '',
            $record['Voltage_Before'] ?? '',
            $record['Current_Before'] ?? '',
            $record['Power_Before'] ?? '',
            $record['Resistance_RY'] ?? '',
            $record['Resistance_YB'] ?? '',
            $record['Resistance_BR'] ?? '',
            $record['Insulation_Resistance'] ?? '',
            $record['Frequency_Before'] ?? '',
            $record['Speed_Before'] ?? '',
            $record['Voltage_After'] ?? '',
            $record['Current_After'] ?? '',
            $record['Power_After'] ?? '',
            $record['IR_Hot'] ?? '',
            $record['IR_Cool'] ?? '',
            $record['Leakage_Current'] ?? '',
            $record['Frequency_After'] ?? '',
            $record['Speed_After'] ?? '',
            $record['Voltage_Locked'] ?? '',
            $record['Current_Locked'] ?? '',
            $record['Power_Locked'] ?? '',
            $record['No_Load_Pickup_Voltage'] ?? '',
            $record['Room_Temp'] ?? '',
            $record['High_Voltage_Test'] ?? '',
            $record['Batch_Number'] ?? '',
            $record['Batch_Count'] ?? '',
            $record['Result'] ?? '',
            $record['Remark'] ?? '',
            $record['Tested_By'] ?? '',
        ];
    }

    public function chunkSize(): int
    {
        return 1000; // load 1000 records at a time
    }
}
