'', 'title' => '', 'description' => '', 'image' => null, 'date_start' => '', 'date_end' => '', 'start_time' => '', 'end_time' => '', ]; public $errors = []; public function mount($id) { $this->fetchSlider($id); $this->fetchPromotions(); } public function fetchSlider($id) { try { $response = Http::get("https://api.example.com/photoSlider/{$id}"); $this->slider = $response->json()['data'] ?? null; if (!$this->slider) { throw new \Exception('No data found'); } $this->form = [ 'promotion_uuid' => $this->slider['promotion'] ? $this->slider['promotion']['promotion_uuid'] : '', 'title' => $this->slider['title'] ?? '', 'description' => $this->slider['description'] ?? '', 'image' => $this->slider['image'] ?? null, 'date_start' => Carbon::parse($this->slider['date_start'])->format('Y-m-d'), 'date_end' => Carbon::parse($this->slider['date_end'])->format('Y-m-d'), 'start_time' => Carbon::parse($this->slider['date_start'])->format('H:i'), 'end_time' => Carbon::parse($this->slider['date_end'])->format('H:i'), ]; if ($this->slider['promotion']) { $this->promotionsDefaultValue = [$this->slider['promotion']['title']]; $this->promotionsDefaultKeyValue = [$this->slider['promotion']['promotion_uuid']]; $this->dateStartEnd = [ 'date_start' => $this->slider['promotion']['date_start'], 'date_end' => $this->slider['promotion']['date_end'], ]; } } catch (\Exception $e) { session()->flash('error', 'Failed to load photo slider details: ' . $e->getMessage()); $this->redirect(route('photo-slider.index')); } } public function fetchPromotions() { try { $response = Http::get('https://api.example.com/getPromotions'); $promotions = $response->json()['data'] ?? []; $this->promotionsOptions = array_map(function ($item) { return [ 'label' => $item['title'], 'value' => $item['promotion_uuid'], 'date' => [ 'dateStart' => $item['date_start'], 'dateEnd' => $item['date_end'], ], ]; }, $promotions); } catch (\Exception $e) { session()->flash('error', 'Failed to load promotions: ' . $e->getMessage()); } } public function fetchDate($id) { foreach ($this->promotionsOptions as $option) { if ($option['value'] == $id) { $this->dateStartEnd = [ 'date_start' => $option['date']['dateStart'], 'date_end' => $option['date']['dateEnd'], ]; break; } } } public function autoFillDetails($id) { try { $response = Http::get("https://api.example.com/promotion/{$id}"); $data = $response->json()['data'] ?? []; $this->form = array_merge($this->form, [ 'title' => $data['title'] ?? '', 'description' => $data['description'] ?? '', 'image' => $data['image'] ?? '', 'date_start' => Carbon::parse($data['date_start'])->format('Y-m-d'), 'date_end' => Carbon::parse($data['date_end'])->format('Y-m-d'), 'start_time' => Carbon::parse($data['date_start'])->format('H:i'), 'end_time' => Carbon::parse($data['date_end'])->format('H:i'), ]); } catch (\Exception $e) { session()->flash('error', 'Failed to autofill details: ' . $e->getMessage()); } } public function submit() { $this->validateForm(); $this->loading = true; try { $formData = new \GuzzleHttp\Psr7\MultipartStream([ [ 'name' => 'promotion_uuid', 'contents' => $this->form['promotion_uuid'] ?? '', ], [ 'name' => 'title', 'contents' => $this->form['title'] ?? '', ], [ 'name' => 'description', 'contents' => $this->form['description'] ?? '', ], [ 'name' => 'date_start', 'contents' => Carbon::parse($this->form['date_start'] . ' ' . $this->form['start_time'])->format('Y-m-d\TH:i:s'), ], [ 'name' => 'date_end', 'contents' => Carbon::parse($this->form['date_end'] . ' ' . $this->form['end_time'])->format('Y-m-d\TH:i:s'), ], [ 'name' => 'image', 'contents' => $this->form['image'] instanceof \Livewire\TemporaryUploadedFile ? fopen($this->form['image']->getPathname(), 'r') : $this->form['image'], 'filename' => $this->form['image'] instanceof \Livewire\TemporaryUploadedFile ? $this->form['image']->getClientOriginalName() : '', ], ]); $response = Http::withHeaders(['Content-Type' => 'multipart/form-data; boundary=' . $formData->getBoundary()]) ->withBody($formData) ->post("https://api.example.com/updatePhotoSlider/{$this->slider['photoslider_uuid']}"); if ($response->successful()) { session()->flash('success', 'Record was successfully updated.'); $this->redirect(route('photo-slider.index')); } } catch (\Exception $e) { session()->flash('error', 'Failed to update record: ' . $e->getMessage()); } $this->loading = false; } public function validateForm() { $rules = [ 'title' => 'required|max:128', 'image' => 'required', 'date_start' => 'required|date', 'date_end' => 'required|date', 'start_time' => 'required', 'end_time' => 'required', ]; $messages = [ 'title.required' => 'Title is required!', 'title.max' => 'Maximum character is 128.', 'image.required' => 'Upload Image is required!', 'date_start.required' => 'Start Appearance Date is required!', 'date_end.required' => 'End Appearance Date is required!', 'start_time.required' => 'Start Time is required!', 'end_time.required' => 'End Time is required!', ]; $this->errors = []; try { $this->validate($rules, $messages); } catch (\Exception $e) { $this->errors = collect($e->errors())->flatten()->toArray(); throw $e; } } public function render() { return view('livewire.photo-slider.photo-slider-edit'); } }