95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Modules\Writeoff\Livewire\GuaranteeType;
|
||
|
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
use Livewire\Component;
|
||
|
use Modules\Writeoff\Entities\GuaranteeType;
|
||
|
use Modules\Writeoff\Http\Requests\GuaranteeType\StoreGuaranteeTypeRequest;
|
||
|
use Modules\Writeoff\Http\Requests\GuaranteeType\UpdateGuaranteeTypeRequest;
|
||
|
|
||
|
class GuaranteeTypeModal 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.guarantee-type.guarantee-type-modal');
|
||
|
}
|
||
|
|
||
|
protected function rules()
|
||
|
{
|
||
|
if ($this->edit_mode) {
|
||
|
$request = new UpdateGuaranteeTypeRequest();
|
||
|
} else {
|
||
|
$request = new StoreGuaranteeTypeRequest();
|
||
|
}
|
||
|
|
||
|
return $request->rules();
|
||
|
}
|
||
|
|
||
|
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
|
||
|
$guarantee_type = GuaranteeType::find($this->id);
|
||
|
$guarantee_type->update($data);
|
||
|
|
||
|
$this->dispatch('success', __('Guarante Type updated'));
|
||
|
} else {
|
||
|
// Emit a success event with a message
|
||
|
GuaranteeType::create($data);
|
||
|
$this->dispatch('success', __('New Guarante Type created'));
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Reset the form fields after successful submission
|
||
|
$this->reset();
|
||
|
}
|
||
|
|
||
|
public function delete($id)
|
||
|
{
|
||
|
GuaranteeType::destroy($id);
|
||
|
|
||
|
// Emit a success event with a message
|
||
|
$this->dispatch('success', 'Guarante Type successfully deleted');
|
||
|
}
|
||
|
|
||
|
public function update($id)
|
||
|
{
|
||
|
$this->edit_mode = true;
|
||
|
|
||
|
$guarantee_type = GuaranteeType::find($id);
|
||
|
|
||
|
$this->id = $guarantee_type->id;
|
||
|
$this->kode = $guarantee_type->kode;
|
||
|
$this->name = $guarantee_type->name;
|
||
|
}
|
||
|
|
||
|
public function hydrate()
|
||
|
{
|
||
|
$this->resetErrorBag();
|
||
|
$this->resetValidation();
|
||
|
}
|
||
|
}
|
||
|
|