<?php

namespace App\Http\Livewire\Pages\User;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use Livewire\WithPagination;

class UserIndex extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';
    public $perPage = 10;
    public $url;
    public $searchSelected = 'username';
    public $editUserId;
    public $deleteUserId;
    public $keyword = '';
    public $route = '';
    public $selectedOption = 'username';
    public $searchBy;
    public $menu;
    public $action = 'list';
    public $message;
    public $selectedUsers = [];
    public $showDeleteListModal = false;
    public $showNoPermissionModal = false;
    public $showMessage = false;
    public $totalItems;

    protected $listeners = ['showUserList', 'deleteItem', 'deleteSelected', 'setShowMessageFalse'];

    public function mount()
    {
        $this->searchBy = [
            'username' => 'Username',
        ];
        // $this->message = session('message');
        // $this->showMessage = true;
    }
    public function updatedPerPage($value)
    {
        $this->adjustPageForNewPerPage();
    }

    public function adjustPageForNewPerPage()
    {
        $lastPage = ceil($this->totalItems / $this->perPage);
        if ($this->page > $lastPage) {
            $this->setPage($lastPage);
        }
    }
    public function search()
    {
        $this->resetPage();
    }
    public function render()
    {

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


        $this->totalItems = $results->total();
        return view('livewire.pages.user.user-index', [
            'results' => $results,
            'route' => $this->route,
            'url' => $this->url,
            'selectedUsers' => $this->selectedUsers,
            'showDeleteListModal' => $this->showDeleteListModal
        ]);
    }

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

    public function showUserEditForm($UserId)
    {

        if (!Auth::user()->hasPermissions(['edit-user'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $this->action = 'edit';
        $this->editUserId = $UserId;
    }

    public function showConfigManualResponseForm($UserId)
    {

        if (!Auth::user()->hasPermissions(['edit-user'])) {
            $this->showNoPermissionModal = TRUE;
            return;
        }
        $this->action = 'configManualResponse';
        $this->editUserId = $UserId;
    }

    public function hideMessage()
    {
        $this->showMessage = false;
    }
    public function showUserList($message = null)
    {
        $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($deleteUserId)
    {
        // if (!Auth::user()->hasPermissions(['delete-user'])) {
        //     $this->showNoPermissionModal = TRUE;
        //     return;
        // }
        $user = User::find($deleteUserId);

        if ($user) {
            $user->delete();
            $message = "Deleted Successfully";
            $this->message = $message;
            if ($this->message) {
                $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
            }
        }
    }
    public function deleteSelected($selectedUsers)
    {
        // if (!Auth::user()->hasPermissions(['delete-user'])) {
        //     $this->showNoPermissionModal = TRUE;
        //     return;
        // }
        $userDeleted = User::whereIn("id", $selectedUsers)->pluck('name')->toArray();
        $userStr = implode(",", $userDeleted);
        User::destroy($selectedUsers);
        $message = "Deleted : (" . $userStr . " )Successfully";
        $this->message = $message;
        $this->selectedUsers = [];
        if ($this->message) {
            $this->dispatchBrowserEvent('show-message', ['message' => $this->message]);
        }
    }
}
