<?php

namespace App\Http\Livewire\Pages\Group;

use Livewire\Component;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Group;
use App\Models\Role;
use App\Models\User;
use App\Models\Company;
use Livewire\WithPagination;

class GroupIndex extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';
    public $perPage = 10;
    public $url;
    public $searchSelected = 'name';
    public $editGroupId;
    public $deleteGroupId;
    public $keyword = '';
    public $route = '';    
    public $selectedOption = 'name';
    public $searchBy;
    public $menu;
    public $action = 'list';
    public $message;
    public $selectedGroups = [];
    public $showDeleteListModal = false;
    public $showNoPermissionModal = false;
    public $showMessage = false;
    protected $listeners = [ 'showGroupList', 'deleteItem' , 'deleteSelected'];

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

    public function render()
    {
        // \Log::info('searchSelected:', ['searchSelected' => $this->searchSelected]);
        $results = $this->searchSelected && $this->keyword
            ? Group::where($this->searchSelected, 'LIKE', '%' . $this->keyword . '%')->paginate($this->perPage)
            : Group::paginate($this->perPage);

    
        $roles = Role::all();
        $users = User::all();
        $companies = Company::all();

        return view('livewire.pages.group.group-index', [
            'results' => $results,
            'route' => $this->route,
            'url' => $this->url,
            'roles' => $roles,
            'users' => $users,
            'companies' => $companies,
            'selectedGroups' => $this->selectedGroups,
            'showDeleteListModal' => $this->showDeleteListModal
        ]);
    }

    public function showGroupCreateForm()
    {
        // if (!\Auth::user()->hasPermissions(['add-group'])) {
        //     $this->showNoPermissionModal = TRUE;
        //     return;
        // }
        $this->action = 'create';
    }

    public function showGroupEditForm($groupId)
    {
        if (!\Auth::user()->hasPermissions(['edit-group'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $this->action = 'edit';
        $this->editGroupId = $groupId;
    }
    public function showGroupList($message)
    {
        $this->action = 'list';
        $this->keyword = '';
        $this->message = $message;
        $this->resetPage();
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }

    public function paginationView()
    {
        return 'paginate-custom';
    }
    public function deleteItem($deleteGroupId)
    {
        if (!\Auth::user()->hasPermissions(['delete-group'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $group = Group::find($deleteGroupId);

        if ($group) {
            $group->delete();
            $message = "Deleted Successfully";
            $this->message = $message;
            if ($this->message) {
                $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
            }
        }
    }
    public function deleteSelected($selectedGroups)
    {
        if (!\Auth::user()->hasPermissions(['delete-group'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }

        $groupDeleted= Group::whereIn("id", $selectedGroups)->pluck('name')->toArray();
        $groupStr = implode("," ,$groupDeleted);
        Group::destroy($selectedGroups);
        $message = "Deleted : (". $groupStr." )Successfully";

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

    }
}
