// 회사/지점 드롭다운 공통 위젯
// 여러 도메인에서 재사용 가능
import 'package:flutter/material.dart';
import '../../../models/company_model.dart';

class CompanyBranchDropdown extends StatelessWidget {
  final List<Company> companies;
  final int? selectedCompanyId;
  final int? selectedBranchId;
  final List<Branch> branches;
  final void Function(int? companyId) onCompanyChanged;
  final void Function(int? branchId) onBranchChanged;

  const CompanyBranchDropdown({
    super.key,
    required this.companies,
    required this.selectedCompanyId,
    required this.selectedBranchId,
    required this.branches,
    required this.onCompanyChanged,
    required this.onBranchChanged,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 회사 드롭다운
        DropdownButtonFormField<int>(
          value: selectedCompanyId,
          decoration: const InputDecoration(hintText: '소속 회사를 선택하세요'),
          items:
              companies
                  .map(
                    (company) => DropdownMenuItem<int>(
                      value: company.id,
                      child: Text(company.name),
                    ),
                  )
                  .toList(),
          onChanged: onCompanyChanged,
          validator: (value) {
            if (value == null) {
              return '소속 회사를 선택해주세요';
            }
            return null;
          },
        ),
        const SizedBox(height: 12),
        // 지점 드롭다운 (지점이 있을 때만)
        if (branches.isNotEmpty)
          DropdownButtonFormField<int>(
            value: selectedBranchId,
            decoration: const InputDecoration(hintText: '소속 지점을 선택하세요'),
            items:
                branches
                    .map(
                      (branch) => DropdownMenuItem<int>(
                        value: branch.id,
                        child: Text(branch.name),
                      ),
                    )
                    .toList(),
            onChanged: onBranchChanged,
            validator: (value) {
              if (branches.isNotEmpty && value == null) {
                return '소속 지점을 선택해주세요';
              }
              return null;
            },
          ),
      ],
    );
  }
}
