si-klinik/sk-klinik.my.id/app/Livewire/Admin/PatientManager.php

244 lines
8.5 KiB
PHP

<?php
namespace App\Livewire\Admin;
use App\Models\Patient;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithPagination;
use Masmerise\Toaster\Toaster;
class PatientManager extends Component
{
use WithPagination;
public $search = '';
public $filterInsurance = '';
public $selectedPatient = null;
protected function validationAttributes()
{
return [
// Alias untuk Form Tambah
'createForm.name' => 'Nama pasien',
'createForm.email' => 'Alamat email',
'createForm.nik' => 'NIK',
'createForm.gender' => 'Jenis kelamin',
'createForm.date_of_birth' => 'Tanggal lahir',
'createForm.place_of_birth' => 'Tempat lahir',
'createForm.phone' => 'Nomor telepon',
'createForm.address' => 'Alamat domisili',
'createForm.insurance_type' => 'Jenis penjaminan',
'createForm.insurance_number' => 'Nomor kartu',
// Alias untuk Form Edit
'editForm.name' => 'Nama pasien',
'editForm.nik' => 'NIK',
'editForm.gender' => 'Jenis kelamin',
'editForm.date_of_birth' => 'Tanggal lahir',
'editForm.place_of_birth' => 'Tempat lahir',
'editForm.phone' => 'Nomor telepon',
'editForm.address' => 'Alamat domisili',
'editForm.insurance_type' => 'Jenis penjaminan',
'editForm.insurance_number' => 'Nomor kartu',
];
}
// Tambahkan properti untuk menangani perubahan UI di modal
public $createForm = [
'name' => '',
'email' => '',
'nik' => '',
'gender' => '',
'blood_type' => '',
'place_of_birth' => '',
'date_of_birth' => '',
'phone' => '',
'address' => '',
'emergency_contact_name' => '',
'emergency_contact_relation' => '',
'emergency_contact_phone' => '',
'insurance_type' => 'Umum',
'insurance_number' => '',
];
public function store()
{
$this->validate([
'createForm.name' => 'required|string|max:255',
'createForm.email' => 'required|email|unique:users,email',
'createForm.nik' => 'required|digits:16|unique:patients,nik',
'createForm.gender' => 'required|in:L,P',
'createForm.date_of_birth' => 'required|date',
'createForm.phone' => 'required',
'createForm.address' => 'required',
'createForm.insurance_type' => 'required',
]);
// DB Transaction direkomendasikan karena insert ke 2 tabel
DB::transaction(function () {
$user = User::create([
'name' => $this->createForm['name'],
'email' => $this->createForm['email'],
'password' => bcrypt($this->createForm['nik']),
]);
$user->patient()->create([
'nik' => $this->createForm['nik'],
'gender' => $this->createForm['gender'],
'blood_type' => $this->createForm['blood_type'],
'place_of_birth' => $this->createForm['place_of_birth'],
'date_of_birth' => $this->createForm['date_of_birth'],
'phone' => $this->createForm['phone'],
'address' => $this->createForm['address'],
'emergency_contact_name' => $this->createForm['emergency_contact_name'],
'emergency_contact_relation' => $this->createForm['emergency_contact_relation'],
'emergency_contact_phone' => $this->createForm['emergency_contact_phone'],
'insurance_type' => $this->createForm['insurance_type'],
'insurance_number' => $this->createForm['insurance_number'],
]);
$user->syncRoles('patient');
});
$this->js("Flux.modal('add-patient').close()");
Toaster::success('Data pasien berhasil didaftarkan. password default adalah NIK pasien.');
$this->reset('createForm');
}
// Fungsi untuk reset form tambah
public function create()
{
$this->reset('createForm');
$this->createForm['insurance_type'] = 'Umum'; // default
$this->js("Flux.modal('add-patient').show()");
}
// Properti untuk Form Edit
public $editForm = [
'id' => null,
'name' => '',
'nik' => '',
'place_of_birth' => '',
'date_of_birth' => '',
'gender' => '',
'blood_type' => '',
'phone' => '',
'address' => '',
'emergency_contact_name' => '',
'emergency_contact_phone' => '',
'emergency_contact_relation' => '',
'insurance_type' => '',
'insurance_number' => '',
];
public function updatedSearch()
{
$this->resetPage();
}
public function updatedFilterInsurance()
{
$this->resetPage();
}
// Fungsi membuka modal detail
public function showDetail($id)
{
$this->selectedPatient = Patient::with('user')->find($id);
$this->js("Flux.modal('patient-detail').show()");
}
// Fungsi memuat data ke modal edit
public function edit($id)
{
$patient = Patient::with('user')->findOrFail($id);
$this->editForm = [
'id' => $patient->id,
'name' => $patient->user->name,
'nik' => $patient->nik,
'place_of_birth' => $patient->place_of_birth,
'date_of_birth' => $patient->date_of_birth,
'gender' => $patient->gender,
'blood_type' => $patient->blood_type,
'phone' => $patient->phone,
'address' => $patient->address,
'emergency_contact_name' => $patient->emergency_contact_name,
'emergency_contact_phone' => $patient->emergency_contact_phone,
'emergency_contact_relation' => $patient->emergency_contact_relation,
'insurance_type' => $patient->insurance_type,
'insurance_number' => $patient->insurance_number,
];
$this->js("Flux.modal('edit-patient').show()");
}
// Fungsi simpan perubahan
public function update()
{
$this->validate([
'editForm.name' => 'required|string|max:255',
'editForm.nik' => 'required|digits:16|unique:patients,nik,' . $this->editForm['id'],
'editForm.place_of_birth' => 'required',
'editForm.date_of_birth' => 'required|date',
'editForm.gender' => 'required|in:L,P',
'editForm.phone' => 'required',
'editForm.address' => 'required',
'editForm.insurance_type' => 'required',
]);
$patient = Patient::findOrFail($this->editForm['id']);
$patient->user->update(['name' => $this->editForm['name']]);
$patient->update([
'nik' => $this->editForm['nik'],
'place_of_birth' => $this->editForm['place_of_birth'],
'date_of_birth' => $this->editForm['date_of_birth'],
'gender' => $this->editForm['gender'],
'blood_type' => $this->editForm['blood_type'],
'phone' => $this->editForm['phone'],
'address' => $this->editForm['address'],
'emergency_contact_name' => $this->editForm['emergency_contact_name'],
'emergency_contact_phone' => $this->editForm['emergency_contact_phone'],
'emergency_contact_relation' => $this->editForm['emergency_contact_relation'],
'insurance_type' => $this->editForm['insurance_type'],
'insurance_number' => $this->editForm['insurance_number'],
]);
$this->js("Flux.modal('edit-patient').close()");
Toaster::success('Data pasien berhasil diperbarui.');
}
public function delete($id)
{
Patient::findOrFail($id)->delete();
Toaster::success('Data pasien berhasil dihapus.');
}
public function render()
{
$query = Patient::with('user');
if ($this->search) {
$query->where(function ($q) {
$q->where('nik', 'like', '%' . $this->search . '%')
->orWhere('medical_record_number', 'like', '%' . $this->search . '%')
->orWhereHas('user', function ($userQuery) {
$userQuery->where('name', 'like', '%' . $this->search . '%');
});
});
}
if ($this->filterInsurance) {
$query->where('insurance_type', $this->filterInsurance);
}
return view('livewire.admin.patient-manager', [
'patients' => $query->latest()->paginate(10)
]);
}
}