<?php

namespace App\Imports;

use App\Models\rto_master;
use Maatwebsite\Excel\Concerns\ToModel;
use App\Models\Carmodel;
use App\Models\Carvarient;
use App\Models\Branch;
use App\Models\Pricelist;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithProgressBar;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use DB;
use Illuminate\Support\Arr;
class ImportRTO implements ToModel , WithStartRow , SkipsOnError, SkipsOnFailure ,WithProgressBar, WithHeadingRow 
{
    use Importable ,SkipsErrors ,SkipsFailures ;
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 2;
    }
    public function model(array $row)
    {
        //dd($row['branch']);
        //Carmodel
        $Carmodel = (object)'';
        if(isset($row['car_model_name']) && !empty($row['car_model_name'])){
            $Carmodel = Carmodel::select('id')->where(['name' => $row['car_model_name']])->first();
            //dd($Carmodel);
            if(!$Carmodel){
                session(['error' => 'No Carmodel Found on records!']);
                return null;
            }
        }else{
            $Carmodel->id = null;
        }
        //dd($Carmodel);
         //Carvarient
        $Carvarient = (object)'';
        if(isset($row['car_varient_name']) && !empty($row['car_varient_name'])){
            $Carvarient = Carvarient::select('id')->where(['name' => $row['car_varient_name']])->first();
            if(!$Carvarient){
                session(['error' => 'No Carvarient Found on records!']);
                return null;
            }
        }else{
            $Carvarient->id = null;
        }

        //branch_id
        $branch = (object)'';
        if(isset($row['branch']) && !empty($row['branch'])){
            $branch = Branch::select('id')->where(['name' => $row['branch']])->first();
            if(!$branch){
                session(['error' => 'No Branch Found on records!']);
                return null;
            }
        }else{
            $branch->id = null;
        }

        //pricelist_id
        $pricelist =(object)'';
        if(isset($row['price_list_name']) && !empty($row['price_list_name'])){
            $pricelist =  Pricelist::select('id')->where(['price_list_name'=>$row['price_list_name']])->first();
            if(!$pricelist){
                session(['error'=>'No pricelist Found on records']);
                return null;
            }
        }else{
            $pricelist->id = null;
        }
        //dd($row[4]);
        $data = [
            'amount'=>$row['amount'],
            'branch_id'=>$branch->id,
            'car_model_id'=>$Carmodel->id,
            'car_varient_id'=>$Carvarient->id,
            'pricelist_id'=>$pricelist->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
        ];
        //dd($data);
        $rto = rto_master::create($data);
        session(['success' => 'Record Imported Succesfully']);
        return $rto;
    }
}
