<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToModel;
use Session;

class ImportFinding implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        if ($row[0] != 'Finding Name (required field)' && 
            $row[1] != 'Finding Category (required field)' && 
            $row[2] != 'Description (optional)' ) {

            if ($row[0] != ''){

            	if ($row[1] != ''){

	                $findings_data = Session::get('FindingsData');
	                $exist_finding_name = array_column($findings_data, 0); 

	                if (in_array($row[0], $exist_finding_name)) {
	                    $row['status'] = 'duplicate';
	                    $findings_data[] = $row;

	                    Session::put('FindingsData', $findings_data);
	                } else {
	                    $row['status'] = true;
	                    $findings_data[] = $row;

	                    Session::put('FindingsData', $findings_data);
	                }
	            } else {
	                throw new \Exception('Finding Category is required!');
	            }
            } else {
                throw new \Exception('Finding Name is required!');
            }
        }

        return;
    }
}
