<?php

namespace App\Imports;

use App\Models\CarExchangeCategory;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ImportCarExchangeCategory implements ToModel ,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 6;
    }
    
    public function model(array $row)
    {
       $exist_data = CarExchangeCategory::select('id')->where(['name' => $row['name'],'status' => 'active'])->first();
               
       $data = [];
       if($exist_data){
           session(['error' => 'Some CarExchangeCategory is already available on records!']);
           // return null;
       }else{  
        $data = [
            'name' => $row['name'],
            'status' => 'active',
            'created_at' => date('Y-m-d H:i:s'),
            'created_by' => auth()->user()->id,
            'updated_at' => date('Y-m-d H:i:s'),
            'updated_by' => auth()->user()->id
        ];
       }

        /*$CarExchange = CarExchangeCategory::create($data);
        return $CarExchange;*/

        $CarExchangeCategory = [];
        if(!empty($data)){
             $CarExchangeCategory = CarExchangeCategory::create($data);
        }        
        return $CarExchangeCategory;     
    }
}
