<?php

namespace App\Imports;

use App\Models\Teammaster;
use App\Models\Carmodel;
use App\Models\Branch;
use App\Models\Department;
use App\Models\Interiorcolor;
use App\Models\Colortone;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;

class ImportInteriorcolor implements ToModel ,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 6;
    }
    public function model(array $row)
    {
        $tone_selection = (object)'';
        if(isset($row['color_tone']) && !empty($row['color_tone'])){
            $tone_selection = DB::table('colortone')->where(['name' => $row['color_tone']])->first();
            if(!$tone_selection){
                /*session(['error' => 'No tone selection Found on records!']);
                return null;*/
                $crud = [
                    'name' => $row['color_tone'],
                    'status' => 'active',
                    'created_at' => date('Y-m-d H:i:s'),
                    'created_by' => auth()->user()->id,
                    'updated_at' => date('Y-m-d H:i:s'),
                    'updated_by' => auth()->user()->id
                ];
                $tone_selection = (object)'';
                $tone_selection->id = Colortone::insertGetId($crud);     
            }
        }else{
            $tone_selection->id = null;
        }

        $exist_data = Interiorcolor::select('id')->where(['name' => $row['interior_color_name'],'tone_selection' => $tone_selection->id,'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some Interiorcolor is already available on records!']);
            // return null;
        }else{  
            $data = [
                'name' => $row['interior_color_name'],
                'tone_selection' => $tone_selection->id,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];
        }

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

        /*$insurance = Interiorcolor::create($data);
        return $insurance;*/
    }
}
