<?php

namespace App\Imports;

use App\Models\Fasttag;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ImportFasttag implements ToModel, WithHeadingRow 
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $exist_data = Fasttag::select('id')->where(['brand_name' => $row['brand_name'],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some fasttag is already available on records!']);
            // return null;
        }else{  
         
            $data = [
                'brand_name'      => $row['brand_name'],
                'amount'          => isset($row['amount']) ? $row['amount'] : null,
                'profit'          => isset($row['profit']) ? $row['profit'] : null,
                'gst_percentage'  => isset($row['gst']) ? $row['gst'] : null,
                'gst'             => isset($row['gst_amount']) ? $row['gst_amount'] : null,
                'total_profit'    => isset($row['total_profit']) ? $row['total_profit'] : null,
                'status'          => 'active',
                'created_at'      => now(),
                'created_by'      => auth()->user()->id,
                'updated_at'      => now(),
                'updated_by'      => auth()->user()->id,
            ];
        }
        $fasttag = [];
        if(!empty($data)){
             $fasttag = Fasttag::create($data);
        }        
        return $fasttag; 
    }
}
