<?php

namespace App\Imports;

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

class ImportSampleType implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        if ($row[0] != 'Sample Type Name (required field)' && 
            $row[1] != 'Details' && 
            $row[2] != 'Status (Active/Inactive)' ) {

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

                if ($row[2] == '' || $row[2] == 'Active' || $row[2] == 'Inactive'){
                    $sample_type_data = Session::get('SampleTypeData');
                    $exist_sample_type_name = array_column($sample_type_data, 0);

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

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

                        Session::put('SampleTypeData', $sample_type_data);
                    }
                } else {
                    throw new \Exception('Invalid Status.');
                }
            } else {
                throw new \Exception('Sample Type name is required.');
            }
        }

        return;
    }
}