validate([ 'nama' => 'required|string|max:255', 'handphone' => 'required|string|max:255', 'email' => 'required|email|max:255', 'nik' => 'required|string|max:255', 'kategori' => 'required|string|max:255', 'tujuan' => 'required|string', 'created_by_id' => 'required|exists:users,id', 'operation_id' => 'nullable|exists:operations,id', ]); $validated['status'] = 'Pending'; $validated['keterangan'] = ''; // Check if operation_id is present in the request if ($request->has('operation_id')) { $validated['operation_id'] = $request->operation_id; } else { // If operation_id is not in the request, set it to null $validated['operation_id'] = null; } DataRequest::create($validated); if ($validated['operation_id']) { return redirect()->route('operations.show', ['id' => $validated['operation_id']]) ->with('success', 'Your request has been submitted successfully.'); } else { return redirect()->route('browse_data') ->with('success', 'Your request for all data has been submitted successfully.'); } } // public function index() // { // return view('request_log'); // } // public function getData(Request $request) // { // $query = DataRequest::query(); // if ($request->filled('category')) { // $query->where('kategori', $request->category); // } // if ($request->filled('status') && $request->status !== '') { // $query->where('status', $request->status); // } // // Remove the else clause that was setting the default to 'Pending' // if ($request->filled('email')) { // $query->where('email', 'like', '%' . $request->email . '%'); // } // if ($request->filled('name')) { // $query->where('nama', 'like', '%' . $request->name . '%'); // } // $requests = $query->orderBy('created_at', 'desc')->paginate(1); // return view('partials.requests_table', compact('requests'))->render(); // } public function index(Request $request) { $query = DataRequest::query(); if ($request->filled('category')) { $query->where('kategori', $request->category); } if ($request->filled('status') && $request->status !== 'all') { $query->where('status', $request->status); } if ($request->filled('email')) { $query->where('email', 'like', '%' . $request->email . '%'); } if ($request->filled('name')) { $query->where('nama', 'like', '%' . $request->name . '%'); } $requests = $query->orderBy('created_at', 'desc')->simplePaginate(15)->appends($request->all()); return view('request_log', compact('requests')); } public function show($id) { $request = DataRequest::findOrFail($id); return view('request_log_specific_user', compact('request')); } public function destroy($id) { $request = DataRequest::findOrFail($id); $request->delete(); return redirect()->route('request_log')->with('success', 'Request deleted successfully'); } public function approve($id) { $request = DataRequest::findOrFail($id); $request->update([ 'status' => 'Approved', 'keterangan' => 'Request approved' ]); return redirect()->route('requests.show', $id)->with('success', 'Request has been approved.'); } public function reject(Request $httpRequest, $id) { $request = DataRequest::findOrFail($id); $request->update([ 'status' => 'Rejected', 'keterangan' => $httpRequest->keterangan ]); return redirect()->route('requests.show', $id)->with('success', 'Request has been rejected.'); } public function checkLatestRequest($operationId) { $request = DataRequest::where('operation_id', $operationId) ->where('created_by_id', Auth::id()) ->latest() ->first(); return response()->json(['request' => $request]); } public function getRejectionReason($operationId) { $request = DataRequest::where('operation_id', $operationId) ->where('created_by_id', Auth::id()) ->where('status', 'Rejected') ->latest() ->first(); return response()->json(['keterangan' => $request ? $request->keterangan : 'No reason provided']); } public function cancelRequest($operationId) { $user = Auth::user(); $request = DataRequest::where('operation_id', $operationId) ->where('created_by_id', $user->id) ->where('status', 'Pending') ->latest() ->first(); if ($request) { $request->delete(); return redirect()->route('operations.show', ['id' => $operationId]) ->with('success', 'Request cancelled successfully'); } return redirect()->route('operations.show', ['id' => $operationId]) ->with('error', 'No pending request found to cancel'); } public function reqall() { return view('request_all_data'); } public function checkAllDataRequest() { $user = Auth::user(); return DataRequest::where('created_by_id', $user->id) ->whereNull('operation_id') ->latest() ->first(); } public function cancelAllRequest() { $user = Auth::user(); $request = DataRequest::where('created_by_id', $user->id) ->whereNull('operation_id') ->where('status', 'Pending') ->latest() ->first(); if ($request) { $request->delete(); return redirect()->route('browse_data') ->with('success', 'All data request cancelled successfully'); } return redirect()->route('browse_data') ->with('error', 'No pending request found to cancel'); } public static function checkAllDataRequestforOp() { $user = Auth::user(); return DataRequest::where('created_by_id', $user->id) ->whereNull('operation_id') ->latest() ->first(); } }