85 lines
2.2 KiB
PHP
85 lines
2.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Modules\Writeoff\Livewire\Branch;
|
||
|
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
use Livewire\Component;
|
||
|
use Modules\Writeoff\Entities\Branch;
|
||
|
|
||
|
class BranchModal extends Component
|
||
|
{
|
||
|
public $kode;
|
||
|
public $name;
|
||
|
|
||
|
public $edit_mode = false;
|
||
|
|
||
|
protected $rules = [
|
||
|
'kode' => 'required|string|max:255|unique:branches,kode',
|
||
|
'name' => 'required|string',
|
||
|
];
|
||
|
|
||
|
protected $listeners = [
|
||
|
'delete_branch' => 'deleteBranch',
|
||
|
'update_branch' => 'updateBranch',
|
||
|
];
|
||
|
|
||
|
public function render()
|
||
|
{
|
||
|
return view('writeoff::livewire.branch.branch-modal');
|
||
|
}
|
||
|
|
||
|
public function submit()
|
||
|
{
|
||
|
// Validate the form input data
|
||
|
$this->validate();
|
||
|
|
||
|
DB::transaction(function () {
|
||
|
// Prepare the data for creating a new user
|
||
|
$data = [
|
||
|
'name' => $this->name,
|
||
|
];
|
||
|
|
||
|
// Create a new user record in the database
|
||
|
$user = Branch::updateOrCreate([
|
||
|
'kode' => $this->kode,
|
||
|
], $data);
|
||
|
|
||
|
if ($this->edit_mode) {
|
||
|
// Emit a success event with a message
|
||
|
$this->dispatch('success', __('Branch updated'));
|
||
|
} else {
|
||
|
// Emit a success event with a message
|
||
|
$this->dispatch('success', __('New Branch created'));
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Reset the form fields after successful submission
|
||
|
$this->reset();
|
||
|
}
|
||
|
|
||
|
public function deleteBranch($id)
|
||
|
{
|
||
|
Branch::destroy($id);
|
||
|
|
||
|
// Emit a success event with a message
|
||
|
$this->dispatch('success', 'Branch successfully deleted');
|
||
|
}
|
||
|
|
||
|
public function updateBranch($id)
|
||
|
{
|
||
|
$this->edit_mode = true;
|
||
|
|
||
|
$branch = Branch::find($id);
|
||
|
|
||
|
$this->kode = $branch->kode;
|
||
|
$this->name = $branch->name;
|
||
|
}
|
||
|
|
||
|
public function hydrate()
|
||
|
{
|
||
|
$this->resetErrorBag();
|
||
|
$this->resetValidation();
|
||
|
}
|
||
|
}
|
||
|
|