Writeoff/Livewire/LoanType/LoanTypeModal.php

95 lines
2.5 KiB
PHP
Raw Normal View History

2023-11-06 04:57:18 +00:00
<?php
namespace Modules\Writeoff\Livewire\LoanType;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\LoanType;
use Modules\Writeoff\Http\Requests\LoanType\StoreLoanTypeRequest;
use Modules\Writeoff\Http\Requests\LoanType\UpdateLoanTypeRequest;
class LoanTypeModal extends Component
{
public $id;
public $kode;
public $name;
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete',
'update' => 'update',
];
public function render()
{
return view('writeoff::livewire.loan-type.loan-type-modal');
}
public function submit()
{
$this->validate();
// Validate the form input data
DB::transaction(function () {
// Prepare the data for creating a new user
$data = [
'kode' => $this->kode,
'name' => $this->name
];
if ($this->edit_mode) {
// Emit a success event with a message
$loan_type = LoanType::find($this->id);
$loan_type->update($data);
$this->dispatch('success', __('Loan Type updated'));
} else {
// Emit a success event with a message
LoanType::create($data);
$this->dispatch('success', __('New Loan Type created'));
}
});
// Reset the form fields after successful submission
$this->reset();
}
public function update($id)
{
$this->edit_mode = true;
$loan_type = LoanType::find($id);
$this->id = $loan_type->id;
$this->kode = $loan_type->kode;
$this->name = $loan_type->name;
}
public function delete($id)
{
LoanType::destroy($id);
// Emit a success event with a message
$this->dispatch('success', 'Loan Type successfully deleted');
}
public function hydrate()
{
$this->resetErrorBag();
$this->resetValidation();
}
protected function rules()
{
if ($this->edit_mode) {
$request = new UpdateLoanTypeRequest();
} else {
$request = new StoreLoanTypeRequest();
}
return $request->rules();
}
}