<?php

namespace App\Imports;

use App\Models\Teammaster;
use App\Models\Carmodel;
use App\Models\Branch;
use App\Models\Department;
use App\Models\Asset;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;

class ImportAsset 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 = Asset::select('id')->where(['item_name' => $row['item']])->first();
                
        $data = [];
        if($exist_data){
            session(['error' => 'Some Asset is already available on records!']);
            // return null;
        }else{  
            $data = [
                'item_name' => $row['item'],
                'amount' => $row['amount'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        /*echo "<pre>";
        print_r($data);
         die();*/

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

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