'required|string|max:128', 'description' => 'required|string|max:32000', 'image' => 'nullable|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.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($id) { $this->id = $id; $this->fetchPromotion(); $this->fetchBranches(); $this->fetchPromoTypes(); $this->checkTopPromotion(); } public function fetchPromotion() { try { $response = Http::get("https://api.example.com/promotion/{$this->id}"); if ($response->successful()) { $this->promotion = $response->json()['data']; $this->title = $this->promotion['title'] ?? ''; $this->description = $this->promotion['description'] ?? ''; $this->station_uuid = array_column($this->promotion['stations'] ?? [], 'station_uuid'); $this->date_start = \Carbon\Carbon::parse($this->promotion['date_start'])->format('Y-m-d'); $this->date_end = \Carbon\Carbon::parse($this->promotion['date_end'])->format('Y-m-d'); $this->start_time = \Carbon\Carbon::parse($this->promotion['date_start'])->format('H:i'); $this->end_time = \Carbon\Carbon::parse($this->promotion['date_end'])->format('H:i'); $this->is_toppromotion = $this->promotion['is_toppromotion'] ?? 0; $this->is_gps = $this->promotion['is_gps'] ?? 0; $this->promo_type = $this->promotion['promo_type']['id'] ?? ''; } else { $this->alert('error', 'Failed to fetch promotion data.'); return redirect('/promotions'); } } catch (\Exception $e) { $this->alert('error', 'An error occurred: ' . $e->getMessage()); return redirect('/promotions'); } } 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'); } } catch (\Exception $e) { $this->alert('error', 'Failed to fetch branches.'); } } 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'] ?? []); } } catch (\Exception $e) { $this->alert('error', 'Failed to fetch promo types.'); } } public function checkTopPromotion() { try { $response = Http::get("https://api.example.com/promotionDisableTopTwo?promotion_uuid={$this->id}"); 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, 'updated_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/updatePromotion/{$this->id}", $data); if ($response->successful()) { $this->alert('success', 'Promotion updated successfully!'); return redirect('/promotions'); } else { $this->alert('error', 'Failed to update 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-edit', [ 'branches' => $this->branches, 'branchesOptionsTwo' => $this->branchesOptionsTwo, 'promoTypes' => $this->promoTypes, ])->layout('layouts.app', ['title' => 'Update Promotion']); } }