<?php

namespace App\Imports;

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

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

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