@@ -130,6 +130,137 @@ class AcademyHomeView extends StatelessWidget {
130130 ),
131131 ],
132132 ),
133+ <<<<<< < HEAD
134+ ====== =
135+ const Divider (height: 1 , indent: 16 , endIndent: 16 ),
136+ Padding (
137+ padding: const EdgeInsets .symmetric (horizontal: 8.0 , vertical: 4 ),
138+ child: TextButton .icon (
139+ icon: const Icon (Icons .add_circle_outline, size: 18 ),
140+ label: const Text ("Asignar Materia" ),
141+ style: TextButton .styleFrom (foregroundColor: AppTheme .bluePrimary),
142+ onPressed: onAssign,
143+ ),
144+ )
145+ ]),
146+ );
147+ }
148+ }
149+
150+ class _AssignmentForm extends StatefulWidget {
151+ final UserModel student;
152+ const _AssignmentForm ({required this .student});
153+
154+ @override
155+ State <_AssignmentForm > createState () => _AssignmentFormState ();
156+ }
157+
158+ class _AssignmentFormState extends State <_AssignmentForm > {
159+ final _scheduleCtrl = TextEditingController ();
160+ final _salonCtrl = TextEditingController ();
161+ SubjectModel ? _selectedSubject;
162+ ProfessorModel ? _selectedProfessor;
163+ bool _isSaving = false ;
164+
165+ @override
166+ void initState () {
167+ super .initState ();
168+ // Auto-select the subject if there's only one available
169+ final vm = context.read <AcademyViewModel >();
170+ if (vm.availableSubjectsForStudent.length == 1 ) {
171+ _selectedSubject = vm.availableSubjectsForStudent.first;
172+ }
173+ }
174+
175+ @override
176+ void dispose () {
177+ _scheduleCtrl.dispose ();
178+ _salonCtrl.dispose ();
179+ super .dispose ();
180+ }
181+
182+ void _submit () async {
183+ final vm = context.read <AcademyViewModel >();
184+ if (_selectedSubject == null || _selectedProfessor == null || _salonCtrl.text.isEmpty) {
185+ if (mounted) ScaffoldMessenger .of (context).showSnackBar (const SnackBar (content: Text ("Todos los campos son obligatorios" )));
186+ return ;
187+ }
188+
189+ setState (() => _isSaving = true );
190+ final success = await vm.assignSubject (
191+ studentId: widget.student.id,
192+ subjectName: _selectedSubject! .name,
193+ professorName: _selectedProfessor! .name,
194+ schedule: _selectedProfessor! .schedule,
195+ salon: _salonCtrl.text,
196+ );
197+
198+ if (mounted && success){
199+ Navigator .pop (context);
200+ } else if (mounted) {
201+ setState (() => _isSaving = false );
202+ }
203+ }
204+
205+ @override
206+ Widget build (BuildContext context) {
207+ final vm = context.watch <AcademyViewModel >();
208+ final bottomPadding = MediaQuery .of (context).viewInsets.bottom;
209+
210+ return SingleChildScrollView (
211+ padding: EdgeInsets .fromLTRB (24 , 24 , 24 , 24 + bottomPadding),
212+ child: Column (
213+ mainAxisSize: MainAxisSize .min,
214+ crossAxisAlignment: CrossAxisAlignment .start,
215+ children: [
216+ Text ("Asignar Materia y Profesor" , style: Theme .of (context).textTheme.titleLarge),
217+ Text ("Alumno: ${widget .student .name }" , style: const TextStyle (color: Colors .grey)),
218+ const SizedBox (height: 20 ),
219+
220+ // --- FIX: Use the filtered list of subjects ---
221+ DropdownButtonFormField <SubjectModel >(
222+ key: ValueKey (_selectedSubject),
223+ initialValue: _selectedSubject,
224+ decoration: const InputDecoration (labelText: "Materia" , border: OutlineInputBorder ()),
225+ items: vm.availableSubjectsForStudent.map ((s) => DropdownMenuItem (value: s, child: Text (s.name))).toList (),
226+ onChanged: (val) => setState (() {
227+ _selectedSubject = val;
228+ _selectedProfessor = null ;
229+ _scheduleCtrl.clear ();
230+ }),
231+ ),
232+ const SizedBox (height: 15 ),
233+
234+ DropdownButtonFormField <ProfessorModel >(
235+ key: ValueKey (_selectedProfessor),
236+ decoration: const InputDecoration (labelText: "Profesor" , border: OutlineInputBorder ()),
237+ items: _selectedSubject? .professors.map ((p) => DropdownMenuItem (value: p, child: Text (p.name))).toList (),
238+ onChanged: (val) => setState (() {
239+ _selectedProfessor = val;
240+ _scheduleCtrl.text = val? .schedule ?? '' ;
241+ }),
242+ ),
243+ const SizedBox (height: 15 ),
244+
245+ Row (children: [
246+ Expanded (child: TextField (controller: _scheduleCtrl, readOnly: true , decoration: const InputDecoration (labelText: "Horario" , border: OutlineInputBorder ()))),
247+ const SizedBox (width: 10 ),
248+ Expanded (child: TextField (controller: _salonCtrl, decoration: const InputDecoration (labelText: "Salón" , hintText: "Ej. A-04" , border: OutlineInputBorder ()))),
249+ ]),
250+ const SizedBox (height: 25 ),
251+ SizedBox (
252+ width: double .infinity,
253+ height: 50 ,
254+ child: _isSaving
255+ ? const Center (child: CircularProgressIndicator ())
256+ : ElevatedButton (
257+ style: ElevatedButton .styleFrom (backgroundColor: AppTheme .bluePrimary, foregroundColor: Colors .white),
258+ onPressed: _submit,
259+ child: const Text ("Guardar Asignación" ),
260+ ),
261+ )
262+ ],
263+ >>>>>> > b02e3f38a199391d13e8c793264fe648935ec0f9
133264 ),
134265 );
135266 }
0 commit comments