<?php
namespace App\Http\Controllers;
use App\Models\QuestionBank; use App\Models\QuestionBankOption; use App\Models\QuestionTopic; use Illuminate\Http\Request; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; use Illuminate\Support\Facades\DB;
class QuestionBankController extends Controller {
 public function index(Request $r): View { $q=QuestionBank::with(['topic','options']); if($r->filled('topic_id'))$q->where('question_topic_id',$r->topic_id); if($r->filled('difficulty'))$q->where('difficulty',$r->difficulty); if($r->filled('keyword')){$k=$r->keyword;$q->where(fn($x)=>$x->where('code','like','%'.$k.'%')->orWhere('question_text','like','%'.$k.'%'));} $questions=$q->latest()->paginate(20)->withQueryString(); $topics=QuestionTopic::where('status','Đang sử dụng')->orderBy('name')->get(); return view('question_bank.index',compact('questions','topics')); }
 public function create(): View { $topics=QuestionTopic::where('status','Đang sử dụng')->orderBy('name')->get(); return view('question_bank.create',compact('topics')); }
 public function store(Request $r): RedirectResponse { $r->validate(['question_topic_id'=>'required|exists:question_topics,id','code'=>'required|max:50|unique:question_bank,code','question_text'=>'required','difficulty'=>'required','default_score'=>'required|numeric|min:0','options'=>'required|array|min:2','correct_option'=>'required']); DB::transaction(function()use($r){$q=QuestionBank::create($r->only(['question_topic_id','code','question_text','difficulty','default_score','explanation','status'])); foreach($r->options as $i=>$t){if($t===null||trim($t)==='')continue; QuestionBankOption::create(['question_bank_id'=>$q->id,'option_text'=>$t,'is_correct'=>((string)$i===(string)$r->correct_option),'sort_order'=>$i+1]);}}); return redirect()->route('question-bank.index')->with('success','Đã thêm câu hỏi vào ngân hàng.'); }
 public function show(QuestionBank $questionBank): View { $questionBank->load(['topic','options']); return view('question_bank.show',compact('questionBank')); }
 public function edit(QuestionBank $questionBank): View { $questionBank->load('options'); $topics=QuestionTopic::where('status','Đang sử dụng')->orderBy('name')->get(); return view('question_bank.edit',compact('questionBank','topics')); }
 public function update(Request $r,QuestionBank $questionBank): RedirectResponse { $r->validate(['question_topic_id'=>'required|exists:question_topics,id','code'=>'required|max:50|unique:question_bank,code,'.$questionBank->id,'question_text'=>'required','difficulty'=>'required','default_score'=>'required|numeric|min:0','options'=>'required|array|min:2','correct_option'=>'required']); DB::transaction(function()use($r,$questionBank){$questionBank->update($r->only(['question_topic_id','code','question_text','difficulty','default_score','explanation','status']));$questionBank->options()->delete();foreach($r->options as $i=>$t){if($t===null||trim($t)==='')continue;QuestionBankOption::create(['question_bank_id'=>$questionBank->id,'option_text'=>$t,'is_correct'=>((string)$i===(string)$r->correct_option),'sort_order'=>$i+1]);}}); return redirect()->route('question-bank.index')->with('success','Đã cập nhật câu hỏi.'); }
 public function destroy(QuestionBank $questionBank): RedirectResponse { $questionBank->delete(); return redirect()->route('question-bank.index')->with('success','Đã xóa câu hỏi.'); }
}
