 <?php
// namespace App\Imports;
// use App\Models\Products;
// use Illuminate\Support\Collection;
// use Maatwebsite\Excel\Concerns\ToCollection;
// use Maatwebsite\Excel\Concerns\ToModel;
// use Maatwebsite\Excel\Concerns\WithHeadingRow;
// use Maatwebsite\Excel\Concerns\WithValidation;
// use Maatwebsite\Excel\Concerns\SkipsOnError;
// use Illuminate\Support\Str;
// use PhpOffice\PhpSpreadsheet\IOFactory;
// use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
// use Illuminate\Support\Facades\Validator;
// use Maatwebsite\Excel\Concerns\Importable;

// use Illuminate\Support\Facades\Storage;
// use Throwable;




// class UpdateProduct implements ToCollection
// {
//     use Importable;

//     /**
//      * @param Collection $collection
//      */




    // public function model(array $row)
    // {
    //     foreach ($row as $eachRow) {
    //         $getProduct = Products::query()->where('product_code', $eachRow[0])->first();
    //         if (!is_null($getProduct)) {
    //             $getProduct->product_code = $eachRow['product_code'];
    //             $getProduct->catalogue_code=$eachRow['catelogue_code'];
    //             $getProduct->name=$eachRow['name'];
    //             $getProduct->description=$eachRow['description'];
    //            // $getProduct->image=$eachRow['image'];
    //            $getProduct->avg_purchase_price=$eachRow['avg_purchase_price'];
    //            $getProduct->avg_selling_price=$eachRow['avg_selling_price'];
    //            $getProduct->min_stock_level=$eachRow['min_stock_level'];
    //            $getProduct->max_stock_level=$eachRow['max_stock_level'];
    //            $getProduct->save();
    //         }
    //     }
    // }
//}
//     public function collection(Collection $rows)
//     {
//         $spreadsheet = IOFactory::load(request()->file('file'));
//         $i = 0;
//         $listImages = [];
//         foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
//             dd($drawing);
//             if ($drawing instanceof MemoryDrawing) {

//                 ob_start();
//                 call_user_func(
//                     $drawing->getRenderingFunction(),
//                     $drawing->getImageResource()
//                 );
//                 $imageContents = ob_get_contents();
//                 ob_end_clean();
//                 switch ($drawing->getMimeType()) {
//                     case MemoryDrawing::MIMETYPE_PNG :
//                         $extension = 'png';
//                         break;
//                     case MemoryDrawing::MIMETYPE_GIF:
//                         $extension = 'gif';
//                         break;
//                     case MemoryDrawing::MIMETYPE_JPEG :
//                         $extension = 'jpg';
//                         break;
//                 }
//             } else {
//                 $zipReader = fopen($drawing->getPath(), 'r');
//                 $imageContents = '';
//                 while (!feof($zipReader)) {
//                     $imageContents .= fread($zipReader, 1024);
//                 }
//                 fclose($zipReader);
//                 $extension = $drawing->getExtension();
//             }

//             $folderName = 'productimage';
//             $date = date('Y-m-d_H-i-s'); // Example date and time format
//             $myFileName = $i . '.' . $date . '.' . $extension;
//             $imagePath = $folderName . '/' . $myFileName;
//             if (env('APP_ENV') == 'prod') {
//                 $destinationPath = public_path($imagePath);
//             } else {
//                 $destinationPath = storage_path('app/public/' . $imagePath);
//             }
//             file_put_contents($destinationPath, $imageContents);
//             array_push($listImages, $imagePath);
//         // dd($listImages);
//     function importProductImages($filePath)
// {
//     $i=0;
//     // Load the spreadsheet file
//     $spreadsheet = IOFactory::load($filePath);
//     $sheet = $spreadsheet->getActiveSheet();

//     // Loop through each row starting from the second row (assuming the first row is the header)
//     foreach ($sheet->getRowIterator(2) as $row) {
//         $productId = $row->getCell('product_code')->getValue();
//         $imagePath = $row->getCell('image')->getValue();

//         // Move the image file to the public folder
//         $folderName = 'productimage';
//                 $date = date('Y-m-d_H-i-s'); // Example date and time format
//                 $myFileName = $i . '.' . $date ;
//                 $imagePath = $folderName . '/' . $myFileName;
//                 if (env('APP_ENV') == 'prod') {
//                     $destinationPath = public_path($imagePath);
//                 } else {
//                     $destinationPath = storage_path('app/public/' . $imagePath);
//                 }
//                 file_put_contents($destinationPath);
//                 array_push($listImages, $imagePath);

// public function importProductImages($filePath)
// {
//     $spreadsheet = IOFactory::load($filePath);
//     $sheet = $spreadsheet->getActiveSheet();

//     $folderName = 'productimage';
//     $date = date('Y-m-d_H-i-s');
//     $listImages = [];

//     // Loop through each row starting from the second row (assuming the first row is the header)
//     foreach ($sheet->getRowIterator(2) as $row) {
//         $productId = $row->getCell('A')->getValue();
//         $imagePath = $row->getCell('B')->getValue();

//         // Move the image file to the public folder
//         $myFileName = $productId . '_' . $date;
//         $imageFileName = $myFileName . '.' . pathinfo($imagePath, PATHINFO_EXTENSION);
//         $imageDestinationPath = $folderName . '/' . $imageFileName;

//         if (env('APP_ENV') == 'prod') {
//             $destinationPath = public_path($imageDestinationPath);
//         } else {
//             $destinationPath = storage_path('app/public/' . $imageDestinationPath);
//         }

//         // Download the image file
//         file_put_contents($destinationPath, file_get_contents($imagePath));

//         // Update the products table with the new image path
//         $product = Products::where('id', $productId)->first();

//         if (!$product) {
//             \Log::error('Could not find product with ID '.$productId);
//             continue;
//         }

//         $product->image_path = $imageDestinationPath;
//         $product->save();

//         // Store the image path in the list
//         $listImages[$productId] = $imageDestinationPath;
//     }

//     // Perform any additional operations or return the list of images if needed
//     return $listImages;
// }

//         foreach ($row as $key=>$eachrow) {
//             if($key==0 && $eachrow[0]=="product_code"){
//                 continue;
//             }
//             // if (!isset($eachrow['product_code'])) {
//             //     // If the product code is missing, skip this row
//             //     continue;
//             // }
//                // dd($eachrow['product_code']);
//             //    dd($eachrow);
//             $product = Products::where('product_code', $eachrow[0])->first();

//             if (!$product) {

//                 \Log::error('Could not find product with code '.$eachrow[0]);
//                 continue;
//             }


//             $product->image = $listImages[$key-1];
//             $product->save();
//             // dd($product);
//         }
//     }
// }
//    }
// }
// public function onError(Throwable $e)
// {
//     // TODO: Implement onError() method.
// }

