parfum/app/Livewire/Forms/Studio/Setting/ProfileForm.php

82 lines
2.2 KiB
PHP

<?php
namespace App\Livewire\Forms\Studio\Setting;
use App\Enums\Gender;
use App\Models\Employee;
use App\Rules\PhoneNumber;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Livewire\Form;
class ProfileForm extends Form
{
public ?Employee $employee = null;
public string $full_name = '';
public string $phone_number = '';
public string $base_salary = '';
public string $birthdate = '';
public string $hire_date = '';
public ?string $address = null;
public string $gender = '';
public string $status = '';
public function rules(): array
{
return [
'full_name' => ['required', 'string', 'max:100'],
'phone_number' => ['required', 'string', new PhoneNumber],
'birthdate' => ['required', 'date', 'before:'.Carbon::now()->subYears(18)->format('Y-m-d')],
'address' => ['nullable', 'string'],
'gender' => ['required', Rule::in(Gender::cases())],
];
}
public function validationAttributes(): array
{
return [
'full_name' => 'nama lengkap',
'phone_number' => 'nomor telepon',
'base_salary' => 'gaji pokok',
'birthdate' => 'tanggal lahir',
'address' => 'alamat',
'gender' => 'jenis kelamin',
];
}
public function setProfile(Employee $employee): void
{
$this->employee = $employee;
$this->full_name = $employee->full_name;
$this->phone_number = $employee->phone_number;
$this->base_salary = $employee->base_salary;
$this->birthdate = $employee->birthdate;
$this->hire_date = formatDateLocalized($employee->hire_date);
$this->address = $employee->address;
$this->gender = $employee->gender->value;
$this->status = $employee->status->label();
}
public function update(): void
{
$this->validate();
$this->employee->update([
'full_name' => $this->full_name,
'phone_number' => $this->phone_number,
'birthdate' => $this->birthdate,
'address' => $this->address,
'gender' => $this->gender,
]);
}
}