import 'package:flutter/material.dart'; import 'package:injectable/injectable.dart'; import '../../../data/models/rent_dto.dart'; import '../../../domain/usecases/rent_usecase.dart'; @injectable class RentController with ChangeNotifier { final RentUseCase _rentUseCase; // 상태 관리 (단순화) bool _isLoading = false; String? _error; List _rents = []; // 단순한 List 구조 RentDto? _selectedRent; // 필터링 상태 String? _selectedStatus; int? _selectedEquipmentHistoryId; RentController(this._rentUseCase); // Getters (단순화) bool get isLoading => _isLoading; String? get error => _error; bool get hasError => _error != null; RentDto? get selectedRent => _selectedRent; String? get selectedStatus => _selectedStatus; int? get selectedEquipmentHistoryId => _selectedEquipmentHistoryId; // 편의 메서드 (백엔드 실제 구조) List get rents => _rents; int get totalRents => _rents.length; // 페이징 관련 getter (UI 호환성) int get currentPage => 1; // 단순화된 페이징 구조 int get totalPages => (_rents.length / 10).ceil(); int get totalItems => _rents.length; void _setLoading(bool loading) { _isLoading = loading; notifyListeners(); } void _setError(String? error) { _error = error; notifyListeners(); } void clearError() { _error = null; notifyListeners(); } /// 임대 목록 조회 Future loadRents({ int page = 1, int pageSize = 10, String? search, bool refresh = false, }) async { try { if (refresh) { _rents.clear(); notifyListeners(); } _setLoading(true); final response = await _rentUseCase.getRents( page: page, pageSize: pageSize, search: search, // status: _selectedStatus, // 삭제된 파라미터 equipmentHistoryId: _selectedEquipmentHistoryId, ); // response를 List로 캐스팅 _rents = response as List; clearError(); } catch (e) { _setError('임대 목록을 불러오는데 실패했습니다: $e'); } finally { _setLoading(false); } } /// 임대 상세 조회 Future loadRent(int id) async { try { _setLoading(true); final response = await _rentUseCase.getRent(id); _selectedRent = response; // response가 직접 RentDto clearError(); } catch (e) { _setError('임대 상세 정보를 불러오는데 실패했습니다: $e'); } finally { _setLoading(false); } } /// 임대 생성 (백엔드 실제 스키마) Future createRent({ required int equipmentHistoryId, required DateTime startedAt, required DateTime endedAt, }) async { try { _setLoading(true); await _rentUseCase.createRent( RentRequestDto( equipmentHistoryId: equipmentHistoryId, startedAt: startedAt, endedAt: endedAt, ), ); // 목록 새로고침 await loadRents(refresh: true); clearError(); return true; } catch (e) { _setError('임대 생성에 실패했습니다: $e'); return false; } finally { _setLoading(false); } } /// 임대 수정 (백엔드 실제 스키마) Future updateRent({ required int id, DateTime? startedAt, DateTime? endedAt, }) async { try { _setLoading(true); await _rentUseCase.updateRent( id, RentUpdateRequestDto( startedAt: startedAt, endedAt: endedAt, ), ); // 목록 새로고침 await loadRents(refresh: true); clearError(); return true; } catch (e) { _setError('임대 수정에 실패했습니다: $e'); return false; } finally { _setLoading(false); } } /// 임대 삭제 Future deleteRent(int id) async { try { _setLoading(true); await _rentUseCase.deleteRent(id); // 목록 새로고침 await loadRents(refresh: true); clearError(); return true; } catch (e) { _setError('임대 삭제에 실패했습니다: $e'); return false; } finally { _setLoading(false); } } // 백엔드에서 반납/연장 처리는 endedAt 수정으로 처리 Future updateRentEndDate(int id, DateTime newEndDate) async { return await updateRent( id: id, endedAt: newEndDate, ); } /// 상태 필터 설정 void setStatusFilter(String? status) { _selectedStatus = status; notifyListeners(); } /// 장비 이력 필터 설정 void setEquipmentHistoryFilter(int? equipmentHistoryId) { _selectedEquipmentHistoryId = equipmentHistoryId; notifyListeners(); } /// 필터 초기화 void clearFilters() { _selectedStatus = null; _selectedEquipmentHistoryId = null; notifyListeners(); } /// 선택된 임대 초기화 void clearSelectedRent() { _selectedRent = null; notifyListeners(); } // 간단한 기간 계산 (클라이언트 사이드) int calculateRentDays(DateTime startDate, DateTime endDate) { return endDate.difference(startDate).inDays; } // 임대 상태 간단 판단 String getRentStatus(RentDto rent) { final now = DateTime.now(); if (rent.startedAt.isAfter(now)) return '예약'; if (rent.endedAt.isBefore(now)) return '종료'; return '진행중'; } // UI 호환성을 위한 상태 표시명 String getRentStatusDisplayName(String status) { switch (status.toLowerCase()) { case '예약': case 'reserved': return '예약'; case '진행중': case 'active': return '진행중'; case '종료': case 'completed': return '종료'; default: return status; } } /// 새로고침 Future refresh() async { await loadRents(refresh: true); } /// 임대 반납 처리 (endedAt를 현재 시간으로 수정) Future returnRent(int id) async { return await updateRent( id: id, endedAt: DateTime.now(), ); } /// 임대 총 비용 계산 (문자열 날짜 기반) double calculateTotalRent(String startDate, String endDate, double dailyRate) { try { final start = DateTime.parse(startDate); final end = DateTime.parse(endDate); final days = end.difference(start).inDays; return days * dailyRate; } catch (e) { return 0.0; } } }