import 'package:superport/models/company_model.dart'; /// Company와 Branch를 통합 관리하기 위한 래퍼 모델 /// 리스트에서 본사와 지점을 동일한 구조로 표시하기 위해 사용 class CompanyItem { final bool isBranch; final Company? company; // 본사인 경우에만 값 존재 final Branch? branch; // 지점인 경우에만 값 존재 final String? parentCompanyName; // 지점인 경우 본사명 final int? parentCompanyId; // 지점인 경우 본사 ID CompanyItem({ required this.isBranch, this.company, this.branch, this.parentCompanyName, this.parentCompanyId, }) : assert( (isBranch && branch != null && parentCompanyName != null) || (!isBranch && company != null), 'CompanyItem must have either company (for headquarters) or branch+parentCompanyName (for branch)' ); /// 본사 생성자 CompanyItem.headquarters(Company company) : isBranch = false, company = company, branch = null, parentCompanyName = null, parentCompanyId = null; /// 지점 생성자 CompanyItem.branch(Branch branch, String parentCompanyName, int parentCompanyId) : isBranch = true, company = null, branch = branch, parentCompanyName = parentCompanyName, parentCompanyId = parentCompanyId; /// 표시용 이름 (계층적 구조) String get displayName { if (isBranch) { return '$parentCompanyName > ${branch!.name}'; } else { return company!.name; } } /// 실제 이름 (본사명 또는 지점명) String get name { return isBranch ? branch!.name : company!.name; } /// ID (본사 ID 또는 지점 ID) int? get id { return isBranch ? branch!.id : company!.id; } /// 주소 String get address { if (isBranch) { return branch!.address.toString(); } else { return company!.address.toString(); } } /// 담당자명 String? get contactName { if (isBranch) { return branch!.contactName; } else { return company!.contactName; } } /// 담당자 직급 (지점은 null) String? get contactPosition { if (isBranch) { return null; // 지점은 직급 정보 없음 } else { return company!.contactPosition; } } /// 연락처 String? get contactPhone { if (isBranch) { return branch!.contactPhone; } else { return company!.contactPhone; } } /// 이메일 (지점은 null) String? get contactEmail { if (isBranch) { return null; // 지점은 이메일 정보 없음 } else { return company!.contactEmail; } } /// 회사 유형 (본사만, 지점은 빈 리스트) List get companyTypes { if (isBranch) { return []; // 지점은 회사 유형 없음 } else { return company!.companyTypes; } } /// 비고 String? get remark { if (isBranch) { return branch!.remark; } else { return company!.remark; } } /// 생성일 DateTime? get createdAt { if (isBranch) { return null; // 지점은 생성일 정보 없음 } else { return company!.createdAt; } } /// 수정일 DateTime? get updatedAt { if (isBranch) { return null; // 지점은 수정일 정보 없음 } else { return company!.updatedAt; } } /// 활성 상태 bool get isActive { if (isBranch) { return true; // 지점은 기본적으로 활성 } else { return company!.isActive; } } /// 파트너사 플래그 bool get isPartner { if (isBranch) { return false; // 지점은 파트너 플래그 없음 } else { return company!.isPartner; } } /// 고객사 플래그 bool get isCustomer { if (isBranch) { return false; // 지점은 고객 플래그 없음 } else { return company!.isCustomer; } } /// JSON 직렬화 Map toJson() { if (isBranch) { return { 'isBranch': true, 'branch': branch!.toJson(), 'parentCompanyName': parentCompanyName, 'parentCompanyId': parentCompanyId, }; } else { return { 'isBranch': false, 'company': company!.toJson(), }; } } /// JSON 역직렬화 factory CompanyItem.fromJson(Map json) { final isBranch = json['isBranch'] as bool; if (isBranch) { return CompanyItem.branch( Branch.fromJson(json['branch']), json['parentCompanyName'] as String, json['parentCompanyId'] as int, ); } else { return CompanyItem.headquarters( Company.fromJson(json['company']), ); } } @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is CompanyItem && other.isBranch == isBranch && other.id == id; } @override int get hashCode { return Object.hash(isBranch, id); } @override String toString() { if (isBranch) { return 'CompanyItem.branch(${branch!.name} of $parentCompanyName)'; } else { return 'CompanyItem.headquarters(${company!.name})'; } } }