'required|string|max:128', 'description' => 'required|string|max:32000', 'image' => 'required|image|mimes:jpg,png,gif|max:100', // 100KB 'station_uuid' => 'nullable|array', 'date_start' => 'required|date', 'date_end' => 'required|date|after_or_equal:date_start', 'start_time' => 'required', 'end_time' => 'required', 'is_toppromotion' => 'in:0,1', 'is_gps' => 'in:0,1', 'promo_type' => 'required|string', ]; protected $messages = [ 'title.required' => 'Title is required!', 'title.max' => 'Maximum character is 128.', 'description.required' => 'Description is required!', 'description.max' => 'Maximum character is 32,000.', 'image.required' => 'Upload Image is required!', 'image.max' => 'Maximum file size is 100KB.', 'date_start.required' => 'Start Date is required!', 'date_end.required' => 'End Date is required!', 'date_end.after_or_equal' => 'End Date must be on or after Start Date!', 'start_time.required' => 'Start Time is required!', 'end_time.required' => 'End Time is required!', 'promo_type.required' => 'Promo Type is required!', ]; public function mount() { $this->fetchBranches(); $this->fetchPromoTypes(); $this->checkTopPromotion(); } public function fetchBranches() { try { $response = Http::get('https://api.example.com/getStations'); if ($response->successful()) { $stations = $response->json()['data'] ?? []; $this->branches = array_map(function ($station) { return [ 'value' => $station['station_uuid'], 'label' => $station['description'], ]; }, $stations); $this->branchesOptionsTwo = array_column($stations, 'description'); } else { $this->alert('error', 'Failed to fetch branches.'); } } catch (\Exception $e) { $this->alert('error', 'An error occurred: ' . $e->getMessage()); } } public function fetchPromoTypes() { try { $response = Http::get('https://api.example.com/promoTypes'); if ($response->successful()) { $this->promoTypes = array_map(function ($key, $value) { return [ 'value' => $key, 'label' => $value, ]; }, array_keys($response->json()['data'] ?? []), $response->json()['data'] ?? []); } else { $this->alert('error', 'Failed to fetch promo types.'); } } catch (\Exception $e) { $this->alert('error', 'An error occurred: ' . $e->getMessage()); } } public function checkTopPromotion() { try { $response = Http::get('https://api.example.com/promotionDisableTopTwo'); if ($response->successful()) { $this->is_toppromotion_disabled = !($response->json()['data']['is_toppromotion'] === 'disable'); } } catch (\Exception $e) { $this->alert('error', 'Failed to check top promotion status.'); } } public function save() { $this->validate(); $this->loading = true; try { $data = [ 'title' => $this->title, 'description' => $this->description, 'station_uuid' => json_encode($this->station_uuid), 'date_start' => \Carbon\Carbon::parse($this->date_start . ' ' . $this->start_time)->format('Y-m-d\TH:i:s'), 'date_end' => \Carbon\Carbon::parse($this->date_end . ' ' . $this->end_time)->format('Y-m-d\TH:i:s'), 'is_toppromotion' => $this->is_toppromotion, 'is_gps' => $this->is_gps, 'promo_type' => $this->promo_type, 'created_by' => auth()->user()->name ?? 'user', ]; $response = Http::asMultipart(); if ($this->image) { $response = $response->attach('image', file_get_contents($this->image->getRealPath()), $this->image->getClientOriginalName()); } $response = $response->post('https://api.example.com/promotion', $data); if ($response->successful()) { $this->alert('success', 'New record added.'); return redirect('/promotions'); } else { $this->alert('error', 'Failed to create promotion.'); } } 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('/promotions'); } } public function render() { return view('livewire.promotions.promotions-create', [ 'branches' => $this->branches, 'branchesOptionsTwo' => $this->branchesOptionsTwo, 'promoTypes' => $this->promoTypes, ])->layout('layouts.app', ['title' => 'Add Promotion']); } }