<?php

namespace App\Imports;

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

class ImportCarCompany 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 = carcompany::select('id')->where(['company_name' => $row['company_name'],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some account is already available on records!']);
            // return null;
        }else{        
            $data = [
                'company_name' => $row['company_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
            ];
        }

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