@extends('layouts.app')
@section('title','Tạo câu hỏi trực tiếp')
@section('content')
<div class="d-flex justify-content-between align-items-center mb-4"><div><h3 class="fw-bold mb-1">Tạo câu hỏi trực tiếp</h3><div class="text-muted">{{ $quiz->title }}</div></div><a href="{{ route('quizzes.questions.index',$quiz->id) }}" class="btn btn-outline-secondary">Quay lại</a></div>
@if($errors->any())<div class="alert alert-danger"><strong>Vui lòng kiểm tra lại thông tin.</strong><ul class="mb-0 mt-2">@foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul></div>@endif
<div class="card shadow-sm"><div class="card-body"><form action="{{ route('quizzes.questions.store',$quiz->id) }}" method="POST">@csrf

<div class="row g-3"><div class="col-md-8"><label class="form-label">Chủ đề câu hỏi <span class="text-danger">*</span></label><select name="question_topic_id" class="form-select" required><option value="">-- Chọn chủ đề câu hỏi --</option>@foreach($topics as $topic)<option value="{{ $topic->id }}" {{ old('question_topic_id',$question->question_topic_id ?? '') == $topic->id ? 'selected' : '' }}>{{ $topic->code }} - {{ $topic->name }}</option>@endforeach</select>@if($topics->isEmpty())<div class="form-text text-danger">Chưa có chủ đề. Hãy tạo chủ đề câu hỏi trước.</div>@endif</div><div class="col-md-4"><label class="form-label">Điểm câu hỏi</label><input type="number" step="0.01" name="score" class="form-control" value="{{ old('score',$question->score ?? 1) }}" required></div><div class="col-md-12"><label class="form-label">Nội dung câu hỏi <span class="text-danger">*</span></label><textarea name="question_text" class="form-control" rows="4" required>{{ old('question_text',$question->question_text ?? '') }}</textarea></div></div>
@php $existingOptions=isset($question)?$question->options->values():collect(); $correctIndex=0; if(isset($question)){ foreach($existingOptions as $idx=>$option){ if($option->is_correct){$correctIndex=$idx;} } } @endphp
<h5 class="fw-bold mt-4">Các phương án trả lời</h5>
@for($i=0;$i<4;$i++) @php $optionText=old('options.'.$i,$existingOptions[$i]->option_text ?? ''); @endphp <div class="input-group mb-2"><span class="input-group-text">{{ chr(65+$i) }}</span><input type="text" name="options[{{ $i }}]" class="form-control" value="{{ $optionText }}" placeholder="Nhập đáp án {{ chr(65+$i) }}"><span class="input-group-text"><input class="form-check-input mt-0" type="radio" name="correct_option" value="{{ $i }}" {{ old('correct_option',$correctIndex)==$i?'checked':'' }}></span><span class="input-group-text">Đáp án đúng</span></div>@endfor

<div class="mt-4 text-end"><a href="{{ route('quizzes.questions.index',$quiz->id) }}" class="btn btn-outline-secondary">Hủy</a> <button class="btn btn-primary" {{ $topics->isEmpty() ? 'disabled' : '' }}>Lưu câu hỏi</button></div></form></div></div>
@endsection
