OCRResults.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Livewire\UploadExam;
  3. use Livewire\Component;
  4. use App\Models\OCRRecord;
  5. class OCRResults extends Component
  6. {
  7. public ?int $ocrRecordId = null;
  8. public ?OCRRecord $ocrRecord = null;
  9. public array $ocrData = [];
  10. public bool $showResults = false;
  11. protected $listeners = [
  12. 'showOCRResults' => 'showResults',
  13. 'refreshResults' => '$refresh',
  14. ];
  15. #[On('showOCRResults')]
  16. public function showResults(int $ocrRecordId)
  17. {
  18. $this->ocrRecordId = $ocrRecordId;
  19. $this->loadOCRResults();
  20. $this->showResults = true;
  21. }
  22. public function loadOCRResults()
  23. {
  24. if (!$this->ocrRecordId) {
  25. return;
  26. }
  27. $this->ocrRecord = OCRRecord::find($this->ocrRecordId);
  28. if ($this->ocrRecord) {
  29. $this->ocrData = $this->ocrRecord->ocr_data ?? [];
  30. }
  31. }
  32. public function getQuestionsProperty()
  33. {
  34. return $this->ocrData['questions'] ?? [];
  35. }
  36. public function getPaperInfoProperty()
  37. {
  38. return [
  39. 'name' => $this->ocrData['paper_name'] ?? '',
  40. 'type' => $this->ocrData['paper_type'] ?? '',
  41. 'total_questions' => count($this->questions),
  42. ];
  43. }
  44. public function acceptResults()
  45. {
  46. if ($this->ocrRecord) {
  47. $this->ocrRecord->update(['status' => 'completed']);
  48. $this->dispatch('ocrResultsAccepted', [
  49. 'ocrData' => $this->ocrData,
  50. ]);
  51. }
  52. }
  53. public function rejectResults()
  54. {
  55. if ($this->ocrRecord) {
  56. $this->ocrRecord->update(['status' => 'rejected']);
  57. $this->dispatch('ocrResultsRejected');
  58. }
  59. $this->showResults = false;
  60. }
  61. public function render()
  62. {
  63. return view('livewire.upload-exam.ocr-results');
  64. }
  65. }