<?php

namespace App\Imports;

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

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

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

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

	                $operations_data = Session::get('OperationsData');
	                $exist_operation_name = array_column($operations_data, 0);

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

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

	                    Session::put('OperationsData', $operations_data);
	                }
	            } else {
	                throw new \Exception('Operation Category is required!');
	            }
            } else {
                throw new \Exception('Operation Name is required!');
            }
        }

        return;
    }
}
