116 lines
3.3 KiB
PHP
116 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\StationLocator;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
|
|
class StationCreate extends Component
|
|
{
|
|
use LivewireAlert;
|
|
|
|
public $name = '';
|
|
public $location = '';
|
|
public $map_URL = '';
|
|
public $lat = '';
|
|
public $long = '';
|
|
public $BranchId = '';
|
|
public $contact_number = [];
|
|
public $code = '';
|
|
public $fuels = [];
|
|
public $loading = false;
|
|
public $title = 'Add Station';
|
|
|
|
protected $rules = [
|
|
'name' => 'required|string|max:128',
|
|
'location' => 'required|string|max:128',
|
|
'map_URL' => 'required|url',
|
|
'lat' => 'required|string',
|
|
'long' => 'required|string',
|
|
'BranchId' => 'required|string',
|
|
'contact_number' => 'required|array|min:1',
|
|
'code' => 'required|string',
|
|
];
|
|
|
|
protected $messages = [
|
|
'name.required' => 'Station name is required!',
|
|
'location.required' => 'Station location is required!',
|
|
'map_URL.required' => 'Url is required!',
|
|
'lat.required' => 'Lat is required!',
|
|
'long.required' => 'Long is required!',
|
|
'BranchId.required' => 'Branch is required!',
|
|
'contact_number.required' => 'Contact number is required!',
|
|
'code.required' => 'Station code is required!',
|
|
];
|
|
|
|
public function mount()
|
|
{
|
|
$this->fetchBranches();
|
|
$this->fetchFuels();
|
|
}
|
|
|
|
public function fetchBranches()
|
|
{
|
|
$response = Http::get('https://api.example.com/branches/noLimit');
|
|
if ($response->successful()) {
|
|
$this->branches = $response->json()['data'];
|
|
}
|
|
}
|
|
|
|
public function fetchFuels()
|
|
{
|
|
$response = Http::get('https://api.example.com/fuels');
|
|
if ($response->successful()) {
|
|
$this->fuelsData = $response->json()['data'];
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
|
|
$this->loading = true;
|
|
|
|
try {
|
|
$response = Http::post('https://api.example.com/stations', [
|
|
'name' => $this->name,
|
|
'location' => $this->location,
|
|
'map_URL' => $this->map_URL,
|
|
'lat' => $this->lat,
|
|
'long' => $this->long,
|
|
'BranchId' => $this->BranchId,
|
|
'contact_number' => implode(',', $this->contact_number),
|
|
'code' => $this->code,
|
|
'fuels' => $this->fuels,
|
|
]);
|
|
|
|
if ($response->successful()) {
|
|
$this->alert('success', 'Station saved successfully!');
|
|
return redirect('/stations');
|
|
} else {
|
|
$this->alert('error', 'Failed to save station. Please try again.');
|
|
}
|
|
} 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('/stations');
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.station-locator.station-create', [
|
|
'branches' => $this->branches ?? [],
|
|
'fuelsData' => $this->fuelsData ?? [],
|
|
])->layout('layouts.app', ['title' => $this->title]);
|
|
}
|
|
}
|