si-klinik/sk-klinik.my.id/app/Livewire/Patient/PatientProfile.php

81 lines
3.0 KiB
PHP

<?php
namespace App\Livewire\Patient;
use Livewire\Component;
use Masmerise\Toaster\Toaster;
class PatientProfile extends Component
{
public $patient;
// Properti Form Lengkap
public $name, $nik, $place_of_birth, $date_of_birth, $gender, $blood_type;
public $phone, $address;
public $insurance_type, $insurance_number;
public $emergency_contact_name, $emergency_contact_phone, $emergency_contact_relation;
public function mount()
{
// Ambil user yang login beserta relasi patient-nya
$user = auth()->user()->load('patient');
if ($user->patient) {
$this->patient = $user->patient;
// Mapping data dari database ke properti form
$this->name = $user->name;
$this->nik = $this->patient->nik;
$this->place_of_birth = $this->patient->place_of_birth;
$this->date_of_birth = $this->patient->date_of_birth;
$this->gender = $this->patient->gender;
$this->blood_type = $this->patient->blood_type;
$this->phone = $this->patient->phone;
$this->address = $this->patient->address;
$this->insurance_type = $this->patient->insurance_type;
$this->insurance_number = $this->patient->insurance_number;
$this->emergency_contact_name = $this->patient->emergency_contact_name;
$this->emergency_contact_phone = $this->patient->emergency_contact_phone;
$this->emergency_contact_relation = $this->patient->emergency_contact_relation;
} else {
// Jika user punya role patient tapi data di tabel patients hilang
return redirect()->route('patient.complete-profile')->with('error', 'Data profil Anda tidak ditemukan. Silakan lengkapi profil Anda terlebih dahulu.');
}
}
public function updateProfile()
{
// Validasi data
$this->validate([
'phone' => 'required|numeric',
'address' => 'required|min:10',
'place_of_birth' => 'required',
'date_of_birth' => 'required|date',
'insurance_type' => 'required',
'insurance_number' => 'required_if:insurance_type,BPJS,Asuransi Swasta',
]);
// Update data di tabel patients
$this->patient->update([
'place_of_birth' => $this->place_of_birth,
'date_of_birth' => $this->date_of_birth,
'gender' => $this->gender,
'blood_type' => $this->blood_type,
'phone' => $this->phone,
'address' => $this->address,
'insurance_type' => $this->insurance_type,
'insurance_number' => $this->insurance_number,
'emergency_contact_name' => $this->emergency_contact_name,
'emergency_contact_phone' => $this->emergency_contact_phone,
'emergency_contact_relation' => $this->emergency_contact_relation,
]);
Toaster::success('Profil berhasil diperbarui.');
}
public function render()
{
return view('livewire.patient.patient-profile');
}
}