<?php

namespace App\Imports;

use App\Models\User;
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;
use DB;

class ImportUser implements ToModel,WithHeadingRow 
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    // public function startRow(): int
    // {
        
    //     return 0;
    // }
    public function model(array $row)
    {

        $Role = (object)'';
        if(isset($row['role']) && !empty($row['role'])){
          $Role = DB::table('roles')->where(['name' => $row['role']])->first();
          if(!$Role){
              session(['error' => 'No Role Found on records!']);
              return null;
          }
        }else{
          $Role->id = null;
        }

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

        $Team_master = (object)'';
        if(isset($row['team_selection']) && !empty($row['team_selection'])){
           $Team_master = DB::table('Team_master')->where(['team_name' => $row['team_selection']])->first();
           if(!$Team_master){
               session(['error' => 'No team selection Found on records!']);
               return null;
           }
        }else{
           $Team_master->id = null;
        }

        $branch = (object)'';
        $branch_data_get = null;
        if(isset($row['branch']) && !empty($row['branch'])){

             $row['branch'] = explode(',', $row['branch']);

             $branch_arr = [];
             if(!empty($row['branch'])){
                 foreach ($row['branch'] as $value_new) {
                     $branch = DB::table('branches')->where(['name' => $value_new])->first();
                     if(!empty($branch)){
                       array_push($branch_arr, @$branch->id);
                     }
                 }
                 $branch_data_get = implode(' ,', $branch_arr);
             }else{
                 $branch_data_get = null;     
             }
           
        }else{
            $branch_data_get = null;
        }


        $exist_data = User::select('id')->where(['email' => $row['email'],'status' => 'active'])->first();

        $data = [];
        if($exist_data){
             session(['error' => 'Some User is already available on records!']);
        }else{
            $data = [
                'first_name' => $row['first_name'],
                'last_name' => $row['last_name'],
                'contact_number' =>$row['contact_number'],
                'email' => $row['email'],
                'password' => bcrypt($row['password']),
                'branch' =>  $branch_data_get,
                'department_id' =>  $department->id,
                'team_name' =>  $Team_master->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,
            ];
            /*echo "<pre>";
            print_r($data);
            die('ss');*/
        }

        $user = [];
        if(!empty($data)){
             $user = User::create($data);
             $user->assignRole($Role->id);
        }        
        return $user;

        /*echo "<pre>";
        print_r($data);
        die('ss');


        $user = User::create($data);
        return $user;*/

       // $user->assignRole($row[5]);
    }
}
