apiService = $apiService; } public function index() { try { $response = $this->apiService->get('/promotions'); if ($response->successful()) { return view('pages.promotions', [ 'promotions' => $response->json()['data'] ]); } return back()->with('error', 'Unable to fetch promotions.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } public function store(Request $request) { $validated = $request->validate([ 'title' => 'required|string|max:255', 'type' => 'required|string', 'startDate' => 'required|date', 'endDate' => 'required|date|after:startDate', 'description' => 'nullable|string', 'terms_conditions' => 'nullable|string', ]); try { $response = $this->apiService->post('/promotions', $validated); if ($response->successful()) { return redirect()->route('promotions') ->with('success', 'Promotion created successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to create promotion.'); } } public function update(Request $request, $id) { $validated = $request->validate([ 'title' => 'required|string|max:255', 'type' => 'required|string', 'startDate' => 'required|date', 'endDate' => 'required|date|after:startDate', 'description' => 'nullable|string', 'terms_conditions' => 'nullable|string', ]); try { $response = $this->apiService->put("/promotions/{$id}", $validated); if ($response->successful()) { return redirect()->route('promotions') ->with('success', 'Promotion updated successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to update promotion.'); } } public function destroy($id) { try { $response = $this->apiService->delete("/promotions/{$id}"); if ($response->successful()) { return redirect()->route('promotions') ->with('success', 'Promotion deleted successfully.'); } return back()->with('error', 'Unable to delete promotion.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } public function batchDestroy(Request $request) { $ids = $request->validate([ 'ids' => 'required|array', 'ids.*' => 'required|integer' ]); try { $response = $this->apiService->post("/promotions/batch-delete", $ids); if ($response->successful()) { return response()->json(['message' => 'Promotions deleted successfully']); } return response()->json(['error' => 'Unable to delete promotions'], 400); } catch (\Exception $e) { return response()->json(['error' => 'Service unavailable'], 503); } } }