arService = $arService; } public static function middleware(): array { return [ //new Middleware('permission:/setting/ar'), ]; } public function index(Request $request) { $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.index',$data); } // public function show(){ // dd('a'); // } public function create() { $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.create',$data); } public function store(Request $request) { $request->validate([ 'code' => [ 'required', 'string', 'max:255', new UniqueInSchema('reference', 'ar', 'code'), ], 'name' => 'required|string|max:255', 'description' => 'nullable|string', 'row_num' => 'required|numeric', ]); try { $data = array_merge($request->all(), ['active_status' => 0]); $this->arService->create($data); return redirect()->route($this->route.'.index')->with('success', 'AR berhasil ditambahkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'AR gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $data['ar'] = $this->arService->find($id); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.edit', $data); } public function update(Request $request, ReferenceAr $ar) { $request->validate([ 'code' => [ 'required', 'string', 'max:255', new UniqueInSchema('reference', 'ar', 'code', $ar->id), ], 'name' => 'required|string|max:255', 'description' => 'nullable|string', 'row_num' => 'required|numeric', ]); try { $this->arService->update($ar, $request->all()); return redirect()->route($this->route.'.index')->with('success', 'AR berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'AR gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy(ReferenceAr $ar) { try { $this->arService->delete($ar); return redirect()->route($this->route.'.index')->with('success', 'AR berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'AR gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function setAktif($id) { try { $this->arService->setAktif($id); return redirect()->route($this->route.'.index')->with('success', 'AR berhasil diaktifkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Gagal mengaktifkan AR. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function grid() { $data = $this->arService->getAll(); $_data = []; foreach ($data as $key => $row) { $btn = 'Edit'; $btn .= '
'; if ($row->active_status == 0) { $btn .= ' '; } if ($row->active_status == 1) { $status = 'Aktif'; } else { $status = 'Tidak Aktif'; } $_data[] = [ 'no' => $key+1, 'no_baris' => @$row->row_num, 'code' => $row->code, 'name' => $row->name, 'description' => $row->description, 'no_baris' => $row->row_num, 'status' => $status, 'action' => @$btn, ]; } return response()->json($_data); } }