70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\StationLocator;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
|
|
class CreateBranch extends Component
|
|
{
|
|
use LivewireAlert;
|
|
|
|
public $code = '';
|
|
public $name = '';
|
|
public $details = '';
|
|
public $loading = false;
|
|
public $title = 'Add Branch';
|
|
|
|
protected $rules = [
|
|
'code' => 'required|string|max:255',
|
|
'name' => 'required|string|max:255',
|
|
'details' => 'required|string|max:255',
|
|
];
|
|
|
|
protected $messages = [
|
|
'code.required' => 'Branch code is required',
|
|
'name.required' => 'Branch name is required!',
|
|
'details.required' => 'Branch details is required!',
|
|
];
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
|
|
$this->loading = true;
|
|
|
|
try {
|
|
$response = Http::post('https://api.example.com/branches/add', [
|
|
'code' => $this->code,
|
|
'name' => $this->name,
|
|
'details' => $this->details,
|
|
'created_by' => auth()->user()->name ?? 'user', // Adjust based on your auth
|
|
]);
|
|
|
|
if ($response->successful()) {
|
|
$this->alert('success', 'Branch created successfully! Don\'t forget to sync created branch to system parameters.');
|
|
return redirect('/branches');
|
|
} else {
|
|
$this->alert('error', 'Failed to create branch.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->alert('error', 'An error occurred: ' . $e->getMessage());
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function cancel()
|
|
{
|
|
if (confirm('Are you sure you want to discard changes?')) {
|
|
return redirect('/branches');
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.station-locator.create-branch')->layout('layouts.app', ['title' => $this->title]);
|
|
}
|
|
}
|