364 lines
13 KiB
Dart
364 lines
13 KiB
Dart
// 👥 교사용 학생 계정 관리 화면. 신규 학생 계정 추가와 계정 강제 삭제를 담당한다.
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config.dart';
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// 👥 [서브 화면 2] 학생 계정 관리 란 (추가 양식 폼 + 강제 삭제 다이얼로그 완전 내장)
|
|
// -----------------------------------------------------------------------------
|
|
class TeacherStudentManagementPage extends StatefulWidget {
|
|
const TeacherStudentManagementPage({super.key});
|
|
|
|
@override
|
|
State<TeacherStudentManagementPage> createState() =>
|
|
_TeacherStudentManagementPageState();
|
|
}
|
|
|
|
class _TeacherStudentManagementPageState
|
|
extends State<TeacherStudentManagementPage> {
|
|
final TextEditingController _addIdController = TextEditingController();
|
|
final TextEditingController _addNameController = TextEditingController();
|
|
final TextEditingController _addPwController = TextEditingController();
|
|
bool _isWorking = false;
|
|
|
|
// ➕ [학생 추가 API 연동용 함수]
|
|
|
|
Future<void> _addStudentAccount() async {
|
|
final sId = _addIdController.text.trim();
|
|
final sName = _addNameController.text.trim();
|
|
final sPw = _addPwController.text.trim();
|
|
|
|
if (sId.isEmpty || sName.isEmpty || sPw.isEmpty) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(const SnackBar(content: Text('⚠️ 모든 입력란을 채워주세요.')));
|
|
return;
|
|
}
|
|
|
|
setState(() => _isWorking = true);
|
|
try {
|
|
final url = Uri.parse('$baseUrl/api/users/register-student');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {"Content-Type": "application/json"},
|
|
body: jsonEncode({"studentId": sId, "name": sName, "password": sPw}),
|
|
);
|
|
final result = jsonDecode(utf8.decode(response.bodyBytes));
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
_addIdController.clear();
|
|
_addNameController.clear();
|
|
_addPwController.clear();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('✅ 계정 생성 완료: ${result['message'] ?? '성공'}')),
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('❌ 생성 실패: ${result['message'] ?? '오류 발생'}')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('🚨 네트워크 에러: $e')));
|
|
} finally {
|
|
setState(() => _isWorking = false);
|
|
}
|
|
}
|
|
|
|
// ❌ [학생 삭제 API 연동용 함수]
|
|
Future<void> _deleteStudentAccount(String studentId) async {
|
|
setState(() => _isWorking = true);
|
|
final url = Uri.parse('$baseUrl/api/users/delete/$studentId');
|
|
|
|
try {
|
|
final response = await http.delete(url);
|
|
if (response.statusCode == 200) {
|
|
final resData = jsonDecode(utf8.decode(response.bodyBytes));
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('✅ ${resData['message']}')));
|
|
} else {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(const SnackBar(content: Text('❌ 삭제 실패하였습니다.')));
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(const SnackBar(content: Text('❌ 서버 에러 발생')));
|
|
} finally {
|
|
setState(() => _isWorking = false);
|
|
}
|
|
}
|
|
|
|
// 🚨 계정 삭제 확인 팝업창 모달
|
|
void _showDeleteDialog() {
|
|
final TextEditingController deleteIdController = TextEditingController();
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(24),
|
|
),
|
|
title: Row(
|
|
children: [
|
|
Icon(Icons.warning_amber_rounded, color: Colors.orange[700]),
|
|
const SizedBox(width: 10),
|
|
const Text(
|
|
'학생 계정 강제 삭제',
|
|
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
|
|
),
|
|
],
|
|
),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Text(
|
|
'영구 삭제할 학생의 학번을 정확하게 입력하세요.',
|
|
style: TextStyle(color: Colors.black54, fontSize: 13),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: deleteIdController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: InputDecoration(
|
|
labelText: '학번 입력',
|
|
labelStyle: TextStyle(color: Colors.orange[700]),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
borderSide: BorderSide(
|
|
color: Colors.orange[700]!,
|
|
width: 2,
|
|
),
|
|
),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
prefixIcon: const Icon(Icons.no_accounts_rounded),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('취소', style: TextStyle(color: Colors.grey)),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
final inputId = deleteIdController.text.trim();
|
|
if (inputId.isNotEmpty) {
|
|
Navigator.pop(context);
|
|
_deleteStudentAccount(inputId);
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.orange[700],
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
child: const Text(
|
|
'삭제 확정',
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey[100],
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
'⚙️ 학생 통합 관리 센터',
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
backgroundColor: Colors.orange,
|
|
foregroundColor: Colors.white,
|
|
elevation: 0,
|
|
),
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// 🏷️ 인디케이터 바 1 (추가 메뉴)
|
|
Row(
|
|
children: [
|
|
Container(
|
|
width: 4,
|
|
height: 16,
|
|
decoration: BoxDecoration(
|
|
color: Colors.orange,
|
|
borderRadius: BorderRadius.circular(2),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Text(
|
|
'신규 학생 계정 추가',
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 📝 학생 추가 컨테이너 폼
|
|
Container(
|
|
padding: const EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(24),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withValues(alpha: 0.04),
|
|
blurRadius: 16,
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
controller: _addIdController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: const InputDecoration(
|
|
labelText: '학번',
|
|
prefixIcon: Icon(Icons.badge),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _addNameController,
|
|
decoration: const InputDecoration(
|
|
labelText: '이름',
|
|
prefixIcon: Icon(Icons.person),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _addPwController,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(
|
|
labelText: '초기 비밀번호',
|
|
prefixIcon: Icon(Icons.lock),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 50,
|
|
child: ElevatedButton(
|
|
onPressed: _isWorking ? null : _addStudentAccount,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.orange,
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
child: _isWorking
|
|
? const CircularProgressIndicator(color: Colors.white)
|
|
: const Text(
|
|
'학생 등록 완료',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 15,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 36),
|
|
|
|
// 🏷️ 인디케이터 바 2 (삭제 권한 제어메뉴)
|
|
Row(
|
|
children: [
|
|
Container(
|
|
width: 4,
|
|
height: 16,
|
|
decoration: BoxDecoration(
|
|
color: Colors.red,
|
|
borderRadius: BorderRadius.circular(2),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Text(
|
|
'위험 구역 (Account Reset)',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.redAccent,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 🚨 학생 삭제 트리거 카드
|
|
InkWell(
|
|
onTap: _showDeleteDialog,
|
|
borderRadius: BorderRadius.circular(24),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.red[50],
|
|
borderRadius: BorderRadius.circular(24),
|
|
border: Border.all(color: Colors.red.shade200),
|
|
),
|
|
child: const Row(
|
|
children: [
|
|
Icon(
|
|
Icons.delete_sweep_rounded,
|
|
color: Colors.red,
|
|
size: 32,
|
|
),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'학생 계정 강제 원격 삭제',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.red,
|
|
),
|
|
),
|
|
SizedBox(height: 2),
|
|
Text(
|
|
'인증 초기화 및 DB 제거용',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.redAccent,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Icon(
|
|
Icons.arrow_forward_ios_rounded,
|
|
color: Colors.red,
|
|
size: 16,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|