<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Traits\Role;
use Spatie\Permission\Traits\Permission;
use DB;

class User extends Authenticatable{
    use HasFactory, Notifiable, HasApiTokens , HasRoles;

    public function guardName(){
        return "web";
    }

    protected $fillable = [
        'first_name',
        'last_name',
        'branch',
        'contact_number',
        'email',
        'name_of_company',
        'pan_no',
        'password',
        'department_id',
        'team_name',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];

    protected $hidden = ['password', 'remember_token'];

    protected $casts = ['email_verified_at' => 'datetime'];

    public function index(){

        $users = User::with('role')->get();
        return $users;
    }

    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch', 'id');
    }


    public function export($slug){



        

        // foreach ($users as $value) {
            
        // }
       // / die('stp[');

        $collection = DB::table('users as u')
                        ->select('u.first_name', 'u.last_name', 'u.contact_number','u.email', 'u.pan_no', 'name_of_company','u.password','department.name as department_name', 'Team_master.team_name', 'u.status','u.branch','u.id')
                        ->leftjoin('department' ,'department.id' ,'u.department_id')
                        ->leftjoin('Team_master' ,'Team_master.id' ,'u.team_name');
                        // ->leftjoin('roles' ,'roles.id' ,'u.id');

        if($slug != 'all')
            $collection->where(['u.status' => $slug]);
        
        $data = $collection->get();

        foreach ($data as $value) {      

            $users_dd = User::where('id',$value->id)->first();

            $value->role_name = $users_dd->roles->first()->name;


            unset($value->id);
            $department_name = $value->department_name;
            unset($value->department_name);
            $value->department_name = $department_name;

            $team_name = $value->team_name;
            unset($value->team_name);
            $value->team_name = $team_name;

            $value->branch = explode(',', $value->branch);
            $data_new = DB::table('branches')->select(DB::raw('group_concat(name) as names'))->whereIn('id',$value->branch)->get();
            $branch_set = '';
            if(!empty($data_new)){
                $branch_set = $data_new[0]->names;
            }
            $value->branch_name = $branch_set;
            unset($value->branch);  

            $status_new = $value->status;
            unset($value->status);

            $value->status = $status_new;


            // var_dump($value->id);

            
            // die('ss');
        }

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

        if($data->isNotEmpty()){
            return $data;
        }else{
            return null;
        }
    }
}
