<?php

namespace App\Imports;
use App\Models\Fuletype;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithProgressBar;
use DB;
use Illuminate\Support\Arr;

class ImportFuletype implements ToModel , WithStartRow , SkipsOnError, SkipsOnFailure ,WithProgressBar
{
    use Importable ,SkipsErrors ,SkipsFailures ;
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function startRow(): int
    {
        return 2;
    }
    public function model(array $row)
    {      
        $exist_data = Fuletype::select('id')->where(['title' => $row[0],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some Fuletype is already available on records!']);
            // return null;
        }else{  

            $data = [            
                'title' => $row[0],         
                '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
            ];
        }

       /* var_dump($data);
        die('s');*/

        $Fuletype = [];
        if(!empty($data)){
             $Fuletype = Fuletype::create($data);
        }        
        return $Fuletype;         
        /*$obf = Fuletype::create($data);
        session(['success' => 'Record Imported Succesfully']);
        return $obf;*/
    }


    
    public function rules(): array
    {
        return [
            '*.10' => ['email' ,'unique:users,email']
        ];
    }
} 
