<?php

namespace App\Imports;

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

class ImportDMS 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 = DMS::select('id')->where(['vin_number' => $row['vin_number']])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some DMS is already available on records!']);
            // return null;
        }else{  
            $data = [
                'date' => $row['date'],
                'dms_invoice_no' => isset($row['dms_invoice_no']) ? $row['dms_invoice_no'] : null,
                'vin_number'          => isset($row['vin_number']) ? $row['vin_number'] : null,
                'engine_number'          => isset($row['engine_number']) ? $row['engine_number'] : null,
                'basic_amount'  => isset($row['basic_amount']) ? $row['basic_amount'] : null,
                'gst_amount' => isset($row['gst_amount']) ? $row['gst_amount'] : null,
                // 'total_amount'    => isset($row['total_amount']) ? $row['total_amount'] : null,
                'getpass_closing_date'    => isset($row['getpass_closing_date']) ? $row['getpass_closing_date'] : null,
                'scheme'    => isset($row['scheme']) ? $row['scheme'] : null,
                'remark'    => isset($row['remark']) ? $row['remark'] : null,
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

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