<?php

namespace App\Imports;

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

class ImportCustomerTypeMaster 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 = CustomerTypeMaster::select('id')->where(['name' => $row['name'],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some CustomerTypeMaster 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
            ];
        }

        /*$insurance = CustomerTypeMaster::create($data);
        return $insurance;*/


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