'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', 'contact_number.*' => 'regex:/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/|min:7', 'code' => 'required|string', 'fuels' => 'required|array|min:1', 'fuels.*.price' => 'required|numeric|gt:0', ]; 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!', 'contact_number.*.regex' => 'Please enter a valid contact number', 'contact_number.*.min' => 'Minimum character is 7', 'code.required' => 'Station code is required!', 'fuels.required' => 'At least one fuel is required!', 'fuels.*.price.required' => 'Fuel price is required!', 'fuels.*.price.gt' => 'Fuel price must be greater than 0!', ]; public function mount() { $this->fetchBranches(); $this->fetchFuels(); } public function fetchBranches() { try { $response = Http::get('https://api.example.com/branches/noLimit'); if ($response->successful()) { $this->branches = $response->json()['data'] ?? []; } else { $this->alert('error', 'Failed to fetch branches.'); } } catch (\Exception $e) { $this->alert('error', 'An error occurred: ' . $e->getMessage()); } } public function fetchFuels() { try { $response = Http::get('https://api.example.com/fuels'); if ($response->successful()) { $this->fuelsData = $response->json()['data'] ?? []; $this->newFuel = $this->fuelsData; } else { $this->alert('error', 'Failed to fetch fuels.'); } } catch (\Exception $e) { $this->alert('error', 'An error occurred: ' . $e->getMessage()); } } public function updateFuelPrice($index, $price) { $this->fuels[$index]['price'] = $price; } public function addToStationFuel() { $newFuels = array_filter($this->fuelsData, fn($fuel) => in_array($fuel['id'], $this->selectedFuels)); $this->fuels = array_merge($this->fuels, array_map(fn($fuel) => [ 'FuelId' => $fuel['id'], 'name' => $fuel['name'], 'price' => '0.00', ], $newFuels)); $this->newFuel = array_filter($this->newFuel, fn($fuel) => !in_array($fuel['id'], $this->selectedFuels)); $this->selectedFuels = []; $this->fuelModal = false; } public function removeFuel($index, $name) { $this->fuelIndex = $index; $this->fuelName = $name; $this->removeModal = true; } public function confirmRemoveFuel() { $this->fuels = array_filter($this->fuels, fn($_, $i) => $i !== $this->fuelIndex, ARRAY_FILTER_USE_BOTH); $this->removeModal = false; $this->fetchFuels(); } public function save() { $this->validate(); $this->loading = true; try { $response = Http::post('https://api.example.com/stations/add', [ 'name' => $this->name, 'location' => $this->location, 'map_Url' => $this->map_URL, 'lat' => $this->lat, 'long' => $this->long, 'contact_number' => implode(',', $this->contact_number), 'created_by' => auth()->user()->name ?? 'user', // Adjust based on your auth 'BranchId' => $this->BranchId, 'code' => $this->code, 'StationFuels' => array_map(fn($fuel) => [ 'FuelId' => $fuel['FuelId'], 'price' => $fuel['price'], ], $this->fuels), ]); if ($response->successful()) { $this->alert('success', 'Station created successfully!'); return redirect('/stations'); } else { $this->alert('error', 'Failed to create station.'); } } 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 updateMap($lat, $long, $url = '') { $this->lat = $lat; $this->long = $long; $this->map_URL = $url ?: $this->map_URL; } public function render() { return view('livewire.station-locator.station-create', [ 'branches' => $this->branches, 'fuelsData' => $this->fuelsData, 'newFuel' => $this->newFuel, ])->layout('layouts.app', ['title' => $this->title]); } }