프로젝트 최초 커밋
This commit is contained in:
146
lib/screens/user/controllers/user_form_controller.dart
Normal file
146
lib/screens/user/controllers/user_form_controller.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:superport/models/company_model.dart';
|
||||
import 'package:superport/models/user_model.dart';
|
||||
import 'package:superport/services/mock_data_service.dart';
|
||||
import 'package:superport/utils/constants.dart';
|
||||
import 'package:superport/models/user_phone_field.dart';
|
||||
|
||||
// 사용자 폼의 상태 및 비즈니스 로직을 담당하는 컨트롤러
|
||||
class UserFormController {
|
||||
final MockDataService dataService;
|
||||
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
||||
|
||||
bool isEditMode = false;
|
||||
int? userId;
|
||||
String name = '';
|
||||
int? companyId;
|
||||
int? branchId;
|
||||
String role = UserRoles.member;
|
||||
String position = '';
|
||||
String email = '';
|
||||
|
||||
// 전화번호 관련 상태
|
||||
final List<UserPhoneField> phoneFields = [];
|
||||
final List<String> phoneTypes = ['휴대폰', '사무실', '팩스', '기타'];
|
||||
|
||||
List<Company> companies = [];
|
||||
List<Branch> branches = [];
|
||||
|
||||
UserFormController({required this.dataService, this.userId});
|
||||
|
||||
// 회사 목록 로드
|
||||
void loadCompanies() {
|
||||
companies = dataService.getAllCompanies();
|
||||
}
|
||||
|
||||
// 회사 ID에 따라 지점 목록 로드
|
||||
void loadBranches(int companyId) {
|
||||
final company = dataService.getCompanyById(companyId);
|
||||
branches = company?.branches ?? [];
|
||||
// 지점 변경 시 이전 선택 지점이 새 회사에 없으면 초기화
|
||||
if (branchId != null && !branches.any((b) => b.id == branchId)) {
|
||||
branchId = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 사용자 정보 로드 (수정 모드)
|
||||
void loadUser() {
|
||||
if (userId == null) return;
|
||||
final user = dataService.getUserById(userId!);
|
||||
if (user != null) {
|
||||
name = user.name;
|
||||
companyId = user.companyId;
|
||||
branchId = user.branchId;
|
||||
role = user.role;
|
||||
position = user.position ?? '';
|
||||
email = user.email ?? '';
|
||||
if (companyId != null) {
|
||||
loadBranches(companyId!);
|
||||
}
|
||||
phoneFields.clear();
|
||||
if (user.phoneNumbers.isNotEmpty) {
|
||||
for (var phone in user.phoneNumbers) {
|
||||
phoneFields.add(
|
||||
UserPhoneField(
|
||||
type: phone['type'] ?? '휴대폰',
|
||||
initialValue: phone['number'] ?? '',
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
addPhoneField();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 전화번호 필드 추가
|
||||
void addPhoneField() {
|
||||
phoneFields.add(UserPhoneField(type: '휴대폰'));
|
||||
}
|
||||
|
||||
// 전화번호 필드 삭제
|
||||
void removePhoneField(int index) {
|
||||
if (phoneFields.length > 1) {
|
||||
phoneFields[index].dispose();
|
||||
phoneFields.removeAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
// 사용자 저장 (UI에서 호출)
|
||||
void saveUser(Function(String? error) onResult) {
|
||||
if (formKey.currentState?.validate() != true) {
|
||||
onResult('폼 유효성 검사 실패');
|
||||
return;
|
||||
}
|
||||
formKey.currentState?.save();
|
||||
if (companyId == null) {
|
||||
onResult('소속 회사를 선택해주세요');
|
||||
return;
|
||||
}
|
||||
// 전화번호 목록 준비 (UserPhoneField 기반)
|
||||
List<Map<String, String>> phoneNumbersList = [];
|
||||
for (var phoneField in phoneFields) {
|
||||
if (phoneField.number.isNotEmpty) {
|
||||
phoneNumbersList.add({
|
||||
'type': phoneField.type,
|
||||
'number': phoneField.number,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (isEditMode && userId != null) {
|
||||
final user = dataService.getUserById(userId!);
|
||||
if (user != null) {
|
||||
final updatedUser = User(
|
||||
id: user.id,
|
||||
companyId: companyId!,
|
||||
branchId: branchId,
|
||||
name: name,
|
||||
role: role,
|
||||
position: position.isNotEmpty ? position : null,
|
||||
email: email.isNotEmpty ? email : null,
|
||||
phoneNumbers: phoneNumbersList,
|
||||
);
|
||||
dataService.updateUser(updatedUser);
|
||||
}
|
||||
} else {
|
||||
final newUser = User(
|
||||
companyId: companyId!,
|
||||
branchId: branchId,
|
||||
name: name,
|
||||
role: role,
|
||||
position: position.isNotEmpty ? position : null,
|
||||
email: email.isNotEmpty ? email : null,
|
||||
phoneNumbers: phoneNumbersList,
|
||||
);
|
||||
dataService.addUser(newUser);
|
||||
}
|
||||
onResult(null);
|
||||
}
|
||||
|
||||
// 컨트롤러 해제
|
||||
void dispose() {
|
||||
for (var phoneField in phoneFields) {
|
||||
phoneField.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user