// πŸ“‹ μ„ μƒλ‹˜μš© "슀마트기기 반좜 λŒ€μž₯" ν™”λ©΄μ˜ κΈ°λŠ₯(μ„œλ²„ 톡신/μƒνƒœ) λ‹΄λ‹Ή 컨트둀러. import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import '../config.dart'; class DeviceCheckoutLedgerController extends ChangeNotifier { List _requests = []; bool _isLoading = true; bool _isWorking = false; Timer? _timer; bool _disposed = false; List get requests => _requests; bool get isLoading => _isLoading; bool get isWorking => _isWorking; void _safeNotify() { if (!_disposed) notifyListeners(); } void init() { fetchAll(); _timer = Timer.periodic(const Duration(seconds: 5), (_) => fetchAll()); } @override void dispose() { _disposed = true; _timer?.cancel(); super.dispose(); } Future fetchAll() async { try { final response = await http.get( Uri.parse('$baseUrl/api/device-checkout/list'), ); if (response.statusCode == 200) { final data = jsonDecode(utf8.decode(response.bodyBytes)); _requests = data['requests'] ?? []; } } catch (_) { // μƒˆλ‘œκ³ μΉ¨ μ‹€νŒ¨λŠ” 쑰용히 λ¬΄μ‹œν•˜κ³  λ§ˆμ§€λ§‰μœΌλ‘œ λ°›μ•„μ˜¨ λͺ©λ‘μ„ μœ μ§€ν•œλ‹€. } finally { _isLoading = false; _safeNotify(); } } // βœ… 승인 μ‹œ μŠΉμΈν•œ μ„ μƒλ‹˜ 계정을 ν•¨κ»˜ 보낸닀 (λŒ€μž₯에 "OOO μ„ μƒλ‹˜μ΄ ν—ˆμš©ν–ˆμŠ΅λ‹ˆλ‹€" ν‘œμ‹œ + // λ―Έλ°˜λ‚© μ•Œλ¦Όμ„ κ·Έ μ„ μƒλ‹˜μ—κ²Œλ§Œ 보내기 μœ„ν•¨). Future<(bool success, String message)> approve( int requestId, { required String teacherId, required String teacherName, }) async { return _postAction('/api/device-checkout/approve', { "requestId": requestId, "teacherId": teacherId, "teacherName": teacherName, }); } Future<(bool success, String message)> reject(int requestId) async { return _postAction('/api/device-checkout/reject', {"requestId": requestId}); } // πŸ–οΈ [ν•˜λ“œμ›¨μ–΄ μžλ™ 감지 μ „κΉŒμ§€ μž„μ‹œ] μ„ μƒλ‹˜μ΄ κΈ°κΈ°λ₯Ό μ‹€μ œλ‘œ λŒλ €λ°›μ•˜μ„ λ•Œ λˆ„λ₯΄λŠ” λ²„νŠΌ. Future<(bool success, String message)> confirmReturn(int requestId) async { return _postAction('/api/device-checkout/return', {"requestId": requestId}); } Future<(bool success, String message)> _postAction( String path, Map body, ) async { _isWorking = true; _safeNotify(); try { final response = await http.post( Uri.parse('$baseUrl$path'), headers: {"Content-Type": "application/json"}, body: jsonEncode(body), ); final result = jsonDecode(utf8.decode(response.bodyBytes)); if (response.statusCode == 200 && result['status'] == 'success') { await fetchAll(); return (true, 'βœ… ${result['message'] ?? 'μ²˜λ¦¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€.'}'); } return (false, '❌ ${result['message'] ?? '처리 μ‹€νŒ¨'}'); } catch (e) { return (false, '🚨 λ„€νŠΈμ›Œν¬ μ—λŸ¬: $e'); } finally { _isWorking = false; _safeNotify(); } } }