<?php

namespace App\Http\Livewire\Pages\Role;

use Livewire\Component;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Role;
use App\Models\Permission;
use Livewire\WithPagination;

class RoleIndex extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';
    public $perPage = 10;
    public $url;
    public $searchSelected = 'name';
    public $editRoleId;
    public $deleteRoleId;
    public $keyword = '';
    public $route = '';
    public $selectedOption = 'name';
    public $searchBy;
    public $menu;
    public $action = 'list';
    public $message;
    public $selectedRoles ;
    public $permissionLists= [];
    public $showDeleteListModal = false;
    public $showNoPermissionModal = false;
    public $showMessage = false;
    protected $listeners = [ 'showRoleList', 'deleteItem' , 'deleteSelected'];

    public function mount()
    {
        $this->searchBy = [
            'name' => 'Name',
        ];
        $this->message = session('message');
    }

    public function render()
    {
        $results = $this->searchSelected && $this->keyword
            ? Role::where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%')->paginate($this->perPage)
            : Role::paginate($this->perPage);

    
        $permissions = Permission::orderBy('permission_group_name', 'asc')->get();

        return view('livewire.pages.role.role-index', [
            'results' => $results,
            'route' => $this->route,
            'url' => $this->url,
            'permissions' => $permissions,
            'selectedRoles' => $this->selectedRoles,
            'showDeleteListModal' => $this->showDeleteListModal
        ]);
    }

    public function showRoleCreateForm()
    {
        if (!\Auth::user()->hasPermissions(['add-role'])) {
            $this->showNoPermissionModal = true;
            return;
        }
        $this->action = 'create';
    }

    public function showRoleEditForm($roleId)
    {
        if (!\Auth::user()->hasPermissions(['edit-role'])) {
            $this->showNoPermissionModal = true;
            return;
        }
        $this->action = 'edit';
        $this->editRoleId = $roleId;
    }
    public function showRoleList($message)
    {
        $this->action = 'list';
        $this->resetPage();
        $this->message = $message;
    
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }

    public function paginationView()
    {
        return 'paginate-custom';
    }
    public function deleteItem($deleteRoleId)
    {
        if (!\Auth::user()->hasPermissions(['delete-role'])) {
            $this->showNoPermissionModal = true;
            return;
        }
        $role = Role::find($deleteRoleId);

        if ($role) {
            $role->delete();
            $message = "Deleted Successfully";
            $this->message = $message;
            if ($this->message) {
                $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
            }
        }
    }
    public function deleteSelected($selectedRoles)
    {
        if (!\Auth::user()->hasPermissions(['delete-role'])) {
            $this->showNoPermissionModal = true;
            return;
        }
        $roleDeleted= Role::whereIn("id", $selectedRoles)->pluck('name')->toArray();
        $roleStr = implode("," ,$roleDeleted);
        Role::destroy($selectedRoles);
        $message = "Deleted : (". $roleStr." )Successfully";

        $this->message = $message;
        $this->selectedRoles = [];
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }
    
}
