si-klinik/sk-klinik.my.id/app/Livewire/Guest/CompleteProfile.php

86 lines
2.9 KiB
PHP

<?php
namespace App\Livewire\Guest;
use App\Models\Patient;
use Livewire\Component;
use Masmerise\Toaster\Toaster;
use Illuminate\Support\Facades\DB;
class CompleteProfile extends Component
{
public function mount()
{
// Proteksi di sini agar user yang sudah jadi 'patient' tidak bisa akses halaman ini lagi
if (auth()->user()->patient()->exists()) {
return redirect()->route('dashboard');
}
}
// Properties sesuai kolom database
public $nik, $place_of_birth, $date_of_birth, $gender, $blood_type;
public $phone, $address;
public $emergency_contact_name, $emergency_contact_phone, $emergency_contact_relation;
public $insurance_type = 'Umum'; // Default Umum
public $insurance_number;
protected $rules = [
'nik' => 'required|digits:16|unique:patients,nik',
'place_of_birth' => 'required|string',
'date_of_birth' => 'required|date',
'gender' => 'required|in:L,P',
'blood_type' => 'nullable|in:A,B,AB,O,-',
'phone' => 'required|numeric|min_digits:10',
'address' => 'required|min:10',
'emergency_contact_name' => 'nullable|string',
'emergency_contact_phone' => 'nullable|numeric',
'emergency_contact_relation' => 'nullable|string',
'insurance_type' => 'required|in:Umum,BPJS,Asuransi Swasta',
'insurance_number' => 'required_if:insurance_type,BPJS,Asuransi Swasta',
];
public function save()
{
$this->validate();
try {
DB::transaction(function () {
// 1. Buat data pasien
Patient::create([
'user_id' => auth()->id(),
'nik' => $this->nik,
'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,
]);
// 2. Berikan role 'patient'
$user = auth()->user();
$user->syncRoles(['patient']); // Gunakan assignRole atau syncRoles
// 3. Hapus role 'guest' jika ada
$user->removeRole('guest');
});
Toaster::success('Profil berhasil dilengkapi!');
// Redirect ke dashboard agar session di-refresh
return redirect()->route('dashboard');
} catch (\Exception $e) {
// Tampilkan error asli agar kamu tahu apa yang salah (misal: kolom kurang)
logger($e->getMessage());
Toaster::error('Gagal simpan: ' . $e->getMessage());
}
}
public function render()
{
return view('livewire.guest.complete-profile');
}
}