get() ->pluck('gejala') ->unique(); $kondisi = Kondisi::all(); return view('user.diagnosa', compact('gejala', 'kondisi')); } public function prosesDiagnosa(Request $request) { $gejalaInput = $request->input('gejala'); $userId = auth()->id(); $penyakitList = Penyakit::with('rules')->get(); $hasilDiagnosa = null; $cfCombineTertinggi = -1; foreach ($penyakitList as $penyakit) { $cfCombine = null; $hasMatchingRule = false; foreach ($penyakit->rules as $rule) { if (isset($gejalaInput[$rule->kode_gejala])) { $hasMatchingRule = true; $mb = $rule->mb; $md = $rule->md; $userCondition = $gejalaInput[$rule->kode_gejala]; // Perhitungan CF menggunakan (MB - MD) terlebih dahulu sebelum dikalikan $cf = ($mb - $md) * $userCondition; if (is_null($cfCombine)) { $cfCombine = $cf; } else { // Formula Certainty Factor Combine $cfCombine = $cfCombine + $cf * (1 - $cfCombine); } } } if ($hasMatchingRule && $cfCombine > $cfCombineTertinggi) { // Simpan hanya hasil diagnosa dengan CF tertinggi $cfCombineTertinggi = $cfCombine; $hasilDiagnosa = [ 'penyakit' => $penyakit->nama_penyakit, 'cf_combine' => $cfCombine, // Menyimpan nilai asli sebelum dikali 100% 'penjelasan' => $penyakit->penjelasan, 'penanganan' => $penyakit->penanganan, 'gambar' => $penyakit->gambar, ]; } } // Jika tidak ditemukan penyakit dengan kecocokan if (!$hasilDiagnosa) { return redirect()->back()->with('error', 'Tidak ditemukan penyakit yang sesuai dengan gejala yang dipilih.'); } // Simpan hasil diagnosa ke dalam database dengan nilai CF Combine asli Diagnosa::create([ 'user_id' => $userId, 'penyakit_id' => Penyakit::where('nama_penyakit', $hasilDiagnosa['penyakit'])->value('id'), 'penanganan' => $hasilDiagnosa['penanganan'], 'cf' => $cfCombineTertinggi, 'tanggal_diagnosa' => now(), 'gambar' => $hasilDiagnosa['gambar'] ]); // Simpan hasil diagnosa di session agar bisa digunakan di view session(['hasilDiagnosa' => $hasilDiagnosa]); return view('user.hasil-diagnosa', compact('hasilDiagnosa')); } public function downloadPdf() { $user = auth()->user(); $hasilDiagnosa = session('hasilDiagnosa') ?? []; $diagnosaTertinggi = $hasilDiagnosa; $pdf = Pdf::loadView('user.hasil-diagnosa-pdf', compact('user', 'hasilDiagnosa', 'diagnosaTertinggi')); $pdf->setPaper('A4', 'portrait'); return $pdf->download('hasil_diagnosa.pdf'); } public function riwayatDiagnosa() { $diagnosa = Diagnosa::where('user_id', auth()->id())->with('penyakit')->get(); return view('user.riwayat-diagnosa', compact('diagnosa')); } public function destroy($id) { $diagnosa = Diagnosa::where('id', $id)->where('user_id', auth()->id())->first(); if (!$diagnosa) { return response()->json(['success' => false, 'message' => 'Data tidak ditemukan!'], 404); } $diagnosa->delete(); return response()->json(['success' => true, 'message' => 'Riwayat diagnosa berhasil dihapus!']); } }