parfum/app/Livewire/Studio/Master/User/Index.php

104 lines
2.7 KiB
PHP

<?php
namespace App\Livewire\Studio\Master\User;
use App\Models\Employee;
use App\Models\User;
use App\Traits\Notification\WithSubscribeNotification;
use App\Traits\WithAuthorization;
use App\Traits\WithConfirmation;
use App\Traits\WithToast;
use Flux\Flux;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Pegawai')]
class Index extends Component
{
use WithAuthorization, WithConfirmation, WithSubscribeNotification, WithToast;
public Collection $employees;
public string $search = '';
public array $status = [];
public function mount(): void
{
$hasNotification = request()->get('notification');
if ($hasNotification) {
$this->js(<<<'JS'
const url = new URL(window.location);
url.search = '';
window.history.replaceState({}, '', url);
JS);
$this->toast($hasNotification);
}
$this->loadEmployees();
}
protected function loadEmployees(): void
{
$this->employees = Employee::with(['user', 'user.referralCode', 'user.outlets'])
->when(! empty($this->search), function ($query) {
return $query->where('full_name', 'like', '%'.$this->search.'%')
->orWhere('code', 'like', '%'.$this->search.'%')
->orWhereHas('user', fn ($q) => $q->where('email', 'like', '%'.$this->search.'%'))
->orWhereHas('user', fn ($q) => $q->where('username', 'like', '%'.$this->search.'%'));
})
->when(! empty($this->status), fn ($query) => $query->whereHas('user', fn ($q) => $q->whereIn('status', $this->status)))
->withoutDeveloper()
->latest()
->get();
}
public function updatedSearch(string $value): void
{
$this->search = $value;
$this->loadEmployees();
}
public function updatedStatus(): void
{
$this->loadEmployees();
}
public function delete(User $user): void
{
$this->canOrAbort('delete user');
$user->delete();
$this->loadEmployees();
$this->toast('Pegawai berhasil dihapus.');
Flux::modals()->close();
}
public function resetPassword(User $user): void
{
$this->canOrAbort('reset password user');
$user->update(['password' => Hash::make(config('myconfig.password_default'))]);
$this->toast('Kata sandi pegawai berhasil direset.');
Flux::modals()->close();
}
public function render(): View
{
return view('livewire.studio.master.user.index', [
'pageTitle' => 'Pegawai',
]);
}
}