<?php

namespace App\Imports;

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

class ImportTestItemOption implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
    	if ($row[0] != 'Option Name (required)' &&
		    $row[1] != 'Style Type (Normal, Low & High)' &&
		    $row[2] != 'Status (Active/Inactive)' ) {

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

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

    				if ($row[1] == 'Normal' ||
			            $row[1] == 'Low' ||
			            $row[1] == 'High') {

		            	if ($row[2] == '' || $row[2] == 'Active' || $row[2] == 'Inactive') {

		            		$test_item_options_data = Session::get('TestItemOptionsData');
		                    $exist_option_name = array_column($test_item_options_data, 0);

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

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

		                        Session::put('TestItemOptionsData', $test_item_options_data);
		                    }
	            		} else {
	                        throw new \Exception('Invalid Status.');
	                    }
	            	} else {
			            throw new \Exception('Invalid Style Type.');
			        }
				} else {
			        throw new \Exception('Style Type is required.');
			    }
    		} else {
		        throw new \Exception('Option Name is required.');
		    }
		}
    }
}