|
@@ -8,8 +8,8 @@ use Livewire\Component;
|
|
|
|
|
|
|
|
class TeacherStudentSelector extends Component
|
|
class TeacherStudentSelector extends Component
|
|
|
{
|
|
{
|
|
|
- public ?string $selectedTeacherId = null;
|
|
|
|
|
- public ?string $selectedStudentId = null;
|
|
|
|
|
|
|
+ public ?string $teacherId = null;
|
|
|
|
|
+ public ?string $studentId = null;
|
|
|
public bool $required = false;
|
|
public bool $required = false;
|
|
|
public string $teacherLabel = '选择老师';
|
|
public string $teacherLabel = '选择老师';
|
|
|
public string $studentLabel = '选择学生';
|
|
public string $studentLabel = '选择学生';
|
|
@@ -34,8 +34,8 @@ class TeacherStudentSelector extends Component
|
|
|
?string $teacherHelperText = null,
|
|
?string $teacherHelperText = null,
|
|
|
?string $studentHelperText = null
|
|
?string $studentHelperText = null
|
|
|
): void {
|
|
): void {
|
|
|
- $this->selectedTeacherId = $initialTeacherId;
|
|
|
|
|
- $this->selectedStudentId = $initialStudentId;
|
|
|
|
|
|
|
+ $this->teacherId = $initialTeacherId;
|
|
|
|
|
+ $this->studentId = $initialStudentId;
|
|
|
$this->required = $required;
|
|
$this->required = $required;
|
|
|
$this->teacherLabel = $teacherLabel;
|
|
$this->teacherLabel = $teacherLabel;
|
|
|
$this->studentLabel = $studentLabel;
|
|
$this->studentLabel = $studentLabel;
|
|
@@ -52,11 +52,65 @@ class TeacherStudentSelector extends Component
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
$this->loadTeacherOptions();
|
|
$this->loadTeacherOptions();
|
|
|
- if ($this->selectedTeacherId) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 验证 teacherId 是否在选项中
|
|
|
|
|
+ if ($this->teacherId && !array_key_exists($this->teacherId, $this->teacherOptions)) {
|
|
|
|
|
+ $this->teacherId = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->teacherId) {
|
|
|
$this->loadStudentOptions();
|
|
$this->loadStudentOptions();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function updatedTeacherId($value): void
|
|
|
|
|
+ {
|
|
|
|
|
+ \Illuminate\Support\Facades\Log::info('教师选择已更新', [
|
|
|
|
|
+ 'old_value' => $this->teacherId,
|
|
|
|
|
+ 'new_value' => $value,
|
|
|
|
|
+ 'is_empty' => empty($value)
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 当教师选择变化时,清空之前选择的学生
|
|
|
|
|
+ $this->studentId = null;
|
|
|
|
|
+ $this->loadStudentOptions();
|
|
|
|
|
+
|
|
|
|
|
+ // 发送事件到父组件
|
|
|
|
|
+ $this->dispatch('teacherChanged', teacherId: $value);
|
|
|
|
|
+
|
|
|
|
|
+ // 强制刷新组件视图
|
|
|
|
|
+ $this->dispatch('$refresh');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function updatedStudentId($value): void
|
|
|
|
|
+ {
|
|
|
|
|
+ \Illuminate\Support\Facades\Log::info('学生选择已更新', [
|
|
|
|
|
+ 'teacher_id' => $this->teacherId,
|
|
|
|
|
+ 'student_id' => $value,
|
|
|
|
|
+ 'is_empty' => empty($value),
|
|
|
|
|
+ 'previous_value' => $this->studentId
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 发送事件到父组件
|
|
|
|
|
+ $this->dispatch('studentChanged',
|
|
|
|
|
+ teacherId: $this->teacherId,
|
|
|
|
|
+ studentId: $value
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 同时分发到浏览器窗口,确保父组件能接收到
|
|
|
|
|
+ $this->dispatch('window-student-changed',
|
|
|
|
|
+ teacherId: $this->teacherId,
|
|
|
|
|
+ studentId: $value
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 强制刷新组件视图
|
|
|
|
|
+ $this->dispatch('$refresh');
|
|
|
|
|
+
|
|
|
|
|
+ \Illuminate\Support\Facades\Log::info('学生选择事件已分发', [
|
|
|
|
|
+ 'dispatched' => true
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function loadTeacherOptions(): void
|
|
public function loadTeacherOptions(): void
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
@@ -124,7 +178,7 @@ class TeacherStudentSelector extends Component
|
|
|
|
|
|
|
|
public function loadStudentOptions(): void
|
|
public function loadStudentOptions(): void
|
|
|
{
|
|
{
|
|
|
- if (empty($this->selectedTeacherId)) {
|
|
|
|
|
|
|
+ if (empty($this->teacherId)) {
|
|
|
$this->studentOptions = [];
|
|
$this->studentOptions = [];
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -132,7 +186,7 @@ class TeacherStudentSelector extends Component
|
|
|
try {
|
|
try {
|
|
|
$students = Student::query()
|
|
$students = Student::query()
|
|
|
->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
|
|
->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
|
|
|
- ->where('students.teacher_id', $this->selectedTeacherId)
|
|
|
|
|
|
|
+ ->where('students.teacher_id', $this->teacherId)
|
|
|
->select(
|
|
->select(
|
|
|
'students.student_id',
|
|
'students.student_id',
|
|
|
'students.name',
|
|
'students.name',
|
|
@@ -147,6 +201,13 @@ class TeacherStudentSelector extends Component
|
|
|
->orderBy('students.name')
|
|
->orderBy('students.name')
|
|
|
->get();
|
|
->get();
|
|
|
|
|
|
|
|
|
|
+ \Illuminate\Support\Facades\Log::info('已加载学生列表', [
|
|
|
|
|
+ 'teacher_id' => $this->teacherId,
|
|
|
|
|
+ 'student_count' => count($students)
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果没有找到学生,可能是因为 teacher_id 不在 teachers 表中
|
|
|
|
|
+ // 但学生表中确实有该 teacher_id 的记录,所以直接返回查询结果
|
|
|
$this->studentOptions = $students->mapWithKeys(function ($student) {
|
|
$this->studentOptions = $students->mapWithKeys(function ($student) {
|
|
|
// 构建详细的显示文本,包含所有可用字段
|
|
// 构建详细的显示文本,包含所有可用字段
|
|
|
$displayName = trim($student->name ?? $student->student_id);
|
|
$displayName = trim($student->name ?? $student->student_id);
|
|
@@ -159,86 +220,33 @@ class TeacherStudentSelector extends Component
|
|
|
];
|
|
];
|
|
|
})->toArray();
|
|
})->toArray();
|
|
|
|
|
|
|
|
- \Illuminate\Support\Facades\Log::info('已加载学生列表', [
|
|
|
|
|
- 'teacher_id' => $this->selectedTeacherId,
|
|
|
|
|
- 'student_count' => count($this->studentOptions)
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
\Illuminate\Support\Facades\Log::error('加载学生列表失败', [
|
|
\Illuminate\Support\Facades\Log::error('加载学生列表失败', [
|
|
|
- 'teacher_id' => $this->selectedTeacherId,
|
|
|
|
|
|
|
+ 'teacher_id' => $this->teacherId,
|
|
|
'error' => $e->getMessage()
|
|
'error' => $e->getMessage()
|
|
|
]);
|
|
]);
|
|
|
$this->studentOptions = [];
|
|
$this->studentOptions = [];
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function updatedSelectedTeacherId($value): void
|
|
|
|
|
- {
|
|
|
|
|
- \Illuminate\Support\Facades\Log::info('教师选择已更新', [
|
|
|
|
|
- 'old_value' => $this->selectedTeacherId,
|
|
|
|
|
- 'new_value' => $value,
|
|
|
|
|
- 'is_empty' => empty($value)
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
- // 当教师选择变化时,清空之前选择的学生
|
|
|
|
|
- $this->selectedStudentId = null;
|
|
|
|
|
- $this->loadStudentOptions();
|
|
|
|
|
-
|
|
|
|
|
- // 发送事件到父组件
|
|
|
|
|
- $this->dispatch('teacherChanged', teacherId: $value);
|
|
|
|
|
-
|
|
|
|
|
- // 强制刷新组件视图
|
|
|
|
|
- $this->dispatch('$refresh');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function updatedSelectedStudentId($value): void
|
|
|
|
|
- {
|
|
|
|
|
- \Illuminate\Support\Facades\Log::info('学生选择已更新', [
|
|
|
|
|
- 'teacher_id' => $this->selectedTeacherId,
|
|
|
|
|
- 'student_id' => $value,
|
|
|
|
|
- 'is_empty' => empty($value),
|
|
|
|
|
- 'previous_value' => $this->selectedStudentId
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
- // 发送事件到父组件
|
|
|
|
|
- $this->dispatch('studentChanged',
|
|
|
|
|
- teacherId: $this->selectedTeacherId,
|
|
|
|
|
- studentId: $value
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 同时分发到浏览器窗口,确保父组件能接收到
|
|
|
|
|
- $this->dispatch('window-student-changed',
|
|
|
|
|
- teacherId: $this->selectedTeacherId,
|
|
|
|
|
- studentId: $value
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 强制刷新组件视图
|
|
|
|
|
- $this->dispatch('$refresh');
|
|
|
|
|
-
|
|
|
|
|
- \Illuminate\Support\Facades\Log::info('学生选择事件已分发', [
|
|
|
|
|
- 'dispatched' => true
|
|
|
|
|
- ]);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public function getSelectedTeacherName(): string
|
|
public function getSelectedTeacherName(): string
|
|
|
{
|
|
{
|
|
|
- return $this->teacherOptions[$this->selectedTeacherId] ?? '未选择';
|
|
|
|
|
|
|
+ return $this->teacherOptions[$this->teacherId] ?? '未选择';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function getSelectedStudentName(): string
|
|
public function getSelectedStudentName(): string
|
|
|
{
|
|
{
|
|
|
- return $this->studentOptions[$this->selectedStudentId] ?? '未选择';
|
|
|
|
|
|
|
+ return $this->studentOptions[$this->studentId] ?? '未选择';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function hasSelections(): bool
|
|
public function hasSelections(): bool
|
|
|
{
|
|
{
|
|
|
- return !empty($this->selectedTeacherId) && !empty($this->selectedStudentId);
|
|
|
|
|
|
|
+ return !empty($this->teacherId) && !empty($this->studentId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function isStudentDropdownDisabled(): bool
|
|
public function isStudentDropdownDisabled(): bool
|
|
|
{
|
|
{
|
|
|
- return empty($this->selectedTeacherId);
|
|
|
|
|
|
|
+ return empty($this->teacherId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function hasStudents(): bool
|
|
public function hasStudents(): bool
|
|
@@ -251,7 +259,7 @@ class TeacherStudentSelector extends Component
|
|
|
*/
|
|
*/
|
|
|
public function getStudentDetails(string $studentId): ?array
|
|
public function getStudentDetails(string $studentId): ?array
|
|
|
{
|
|
{
|
|
|
- if (empty($studentId) || empty($this->selectedTeacherId)) {
|
|
|
|
|
|
|
+ if (empty($studentId) || empty($this->teacherId)) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -259,7 +267,7 @@ class TeacherStudentSelector extends Component
|
|
|
$student = Student::query()
|
|
$student = Student::query()
|
|
|
->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
|
|
->leftJoin('users as u', 'students.student_id', '=', 'u.user_id')
|
|
|
->where('students.student_id', $studentId)
|
|
->where('students.student_id', $studentId)
|
|
|
- ->where('students.teacher_id', $this->selectedTeacherId)
|
|
|
|
|
|
|
+ ->where('students.teacher_id', $this->teacherId)
|
|
|
->select(
|
|
->select(
|
|
|
'students.student_id',
|
|
'students.student_id',
|
|
|
'students.name',
|
|
'students.name',
|