147 lines
3.6 KiB
PHP
147 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Doctor;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use Livewire\WithFileUploads;
|
|
use Masmerise\Toaster\Toaster;
|
|
|
|
class DoctorManager extends Component
|
|
{
|
|
use WithPagination, WithFileUploads;
|
|
|
|
public $search = '';
|
|
public $filterSpecialization = '';
|
|
|
|
public $doctorId;
|
|
public $name, $specialization, $phone, $email, $schedule, $photo;
|
|
public $oldPhoto;
|
|
|
|
public $isModalOpen = false;
|
|
|
|
public function updatedSearch()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function updatedFilterSpecialization()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->resetForm();
|
|
$this->isModalOpen = true;
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$doctor = Doctor::findOrFail($id);
|
|
|
|
$this->doctorId = $doctor->id;
|
|
$this->name = $doctor->name;
|
|
$this->specialization = $doctor->specialization;
|
|
$this->phone = $doctor->phone;
|
|
$this->email = $doctor->email;
|
|
$this->oldPhoto = $doctor->photo;
|
|
|
|
$this->schedule = $doctor->schedule ?? [
|
|
'Senin' => '',
|
|
'Selasa' => '',
|
|
'Rabu' => '',
|
|
'Kamis' => '',
|
|
'Jumat' => '',
|
|
'Sabtu' => '',
|
|
'Minggu' => ''
|
|
];
|
|
|
|
$this->isModalOpen = true;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate([
|
|
'name' => 'required|string|max:255',
|
|
'specialization' => 'required|string|max:255',
|
|
'phone' => 'required|string|max:20',
|
|
'email' => 'nullable|email',
|
|
'photo' => 'nullable|image|max:2048', // 2MB
|
|
]);
|
|
|
|
// Upload foto jika ada
|
|
$photoPath = $this->oldPhoto;
|
|
|
|
if ($this->photo) {
|
|
$photoPath = $this->photo->store('doctors', 'public');
|
|
}
|
|
|
|
Doctor::updateOrCreate(
|
|
['id' => $this->doctorId],
|
|
[
|
|
'name' => $this->name,
|
|
'specialization' => $this->specialization,
|
|
'phone' => $this->phone,
|
|
'email' => $this->email,
|
|
'schedule' => $this->schedule,
|
|
'photo' => $photoPath,
|
|
]
|
|
);
|
|
|
|
$this->isModalOpen = false;
|
|
$this->resetForm();
|
|
|
|
Toaster::success('Data dokter berhasil disimpan.');
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
Doctor::findOrFail($id)->delete();
|
|
Toaster::success('Data dokter berhasil dihapus.');
|
|
}
|
|
|
|
private function resetForm()
|
|
{
|
|
$this->doctorId = null;
|
|
$this->name = '';
|
|
$this->specialization = '';
|
|
$this->phone = '';
|
|
$this->email = '';
|
|
$this->photo = null;
|
|
$this->oldPhoto = null;
|
|
|
|
$this->schedule = [
|
|
'Senin' => '',
|
|
'Selasa' => '',
|
|
'Rabu' => '',
|
|
'Kamis' => '',
|
|
'Jumat' => '',
|
|
'Sabtu' => '',
|
|
'Minggu' => '',
|
|
];
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$query = Doctor::query();
|
|
|
|
if ($this->search) {
|
|
$query->where(function ($q) {
|
|
$q->where('name', 'like', "%{$this->search}%")
|
|
->orWhere('specialization', 'like', "%{$this->search}%");
|
|
});
|
|
}
|
|
|
|
if ($this->filterSpecialization) {
|
|
$query->where('specialization', $this->filterSpecialization);
|
|
}
|
|
|
|
return view('livewire.admin.doctor-manager', [
|
|
'doctors' => $query->latest()->paginate(10),
|
|
'specializations' => Doctor::select('specialization')->distinct()->pluck('specialization')
|
|
]);
|
|
}
|
|
}
|