<?php

namespace App\Imports;

use App\Models\Branch;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ImportBranch implements ToModel,WithHeadingRow 
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    // public function startRow(): int
    // {
        
    //     return 0;
    // }
    public function model(array $row)
    {
        $exist_data = Branch::select('id')->where(['name' => $row['name'],'status' => 'active'])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some Branch is already available on records!']);
            // return null;
        }else{  

            $data = [
                'name' => $row['name'],
                'city' => $row['city'],
                'address' =>$row['address'],
                'email' => $row['email'],
                'contact_number' => $row['contact_number'],
                'manager' => $row['manager'],
                'manager_contact_number' => $row['manager_contact_number'],
                'gst' => $row['gstin'],
                'start_value1' => $row['start_value_1'],
                'end_value1' => $row['end_value_1'],
                'tax_percentage1' => $row['tax_percentage_1'],
                'start_value2' => $row['start_value_2'],
                'end_value2' => $row['end_value_2'],
                'tax_percentage2' => $row['tax_percentage_2'],
                '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
                
            ];
        }

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

        
    }
}
