<?php

namespace App\Imports;

use App\Models\Finance;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ImportFinance implements ToModel,WithHeadingRow 
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $exist_data = Finance::select('id')->where(['name' => $row['name'],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some Finance is already available on records!']);
            // return null;
        }else{
            $data = [
                'name' => $row['name'],
                'branch_id' => $row['branch_name'],
                'dsa_or_broker' => $row['dsa_or_broker'],
                '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
            ];
        }
        $Finance = [];
        if(!empty($data)){
             $Finance = Finance::create($data);
        }        
        return $Finance;
    }
}
