<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\AdminResetPasswordNotification;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';
    private   $permissions = [];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new AdminResetPasswordNotification($token));
    }


    public function getFullNameAttribute()
    {
        return "{$this->name} {$this->last_name}";
    }


    public function roles() {
        return $this->belongsToMany('App\Role', 'role_admin', 'admin_id', 'role_id');
    }

    public function department() {
        return $this->belongsTo('App\Department', 'dep_id', 'dep_id');
    }


    public function isActive(){

        return $this->status;
    }

    public function isSuperAdmin(){

        return $this->is_super_admin;
    }




    public function getPermissions() {

        $aid = auth('admin')->user()->id;

        $permissions = \DB::table('admins')
            ->join('role_admin','admins.id','=','role_admin.admin_id')
            ->join('roles','role_admin.role_id','=','roles.id')
            ->join('permission_role','roles.id','=','permission_role.role_id')
            ->join('permissions','permission_role.permission_id','=','permissions.id')
            ->whereRaw('admins.id = ?', $aid)
            ->distinct()
            ->pluck(\DB::raw("CONCAT(permissions.object,'.',permissions.method)  AS ability"))
            ->toArray()
        ;

        //echo print_r($permissions,true); exit;

        return $permissions;
    }



    public  function hasPermission($permission){

        if(auth('admin')->user()->isSuperAdmin()){
            return true;
        } else {



            $permissionsData = $this->getPermissions();
            //print_r($permissionsData); exit;
            if(is_array($permissionsData) && !empty($permissionsData)){
                $this->permissions = $permissionsData;
            }


            $ifHavePermission = (in_array($permission, $this->permissions)) ? true : false;

            //echo $ifHavePermission.' '.$permission.' '.print_r($this->permissions,true); exit;

            return $ifHavePermission;

        }

    }

    public  function navData(){

        $aid = $this->id;

        if($this->isSuperAdmin()){
            $data = \DB::table('permissions')
                ->select('permissions.name', 'permissions.object_display_name','permissions.route', 'permissions.object_ico_class')
                ->where('permissions.in_nav', '=', '1')
                ->orderBy('permissions.ord','ASC')
                ->get()
                ->toArray();

        } else {

            $data = \DB::table('permissions')
                ->join('permission_role','permissions.id','=','permission_role.permission_id')
                ->join('role_admin','permission_role.role_id','=','role_admin.role_id')
                ->whereRaw('role_admin.admin_id = ?', $aid)
                ->where('permissions.in_nav', '=', '1')
                ->select('permissions.name', 'permissions.object_display_name','permissions.route', 'permissions.object_ico_class')
                ->distinct()
                ->orderBy('permissions.ord','ASC')
                ->get()
                ->toArray()
            ;
        }

        /*$data = \DB::table('permissions')
            ->select('permissions.name', 'permissions.object_display_name','permissions.route', 'permissions.object_ico_class')
            ->where('permissions.in_nav', '=', '1')
            ->orderBy('permissions.ord','ASC')
            ->get()
            ->toArray();*/

        return $data;

    }



}
