import 'package:flutter/material.dart';
import '../controllers/branch_form_controller.dart';
import 'package:superport/models/address_model.dart';
import 'package:superport/screens/company/widgets/contact_info_form.dart';
import 'package:superport/screens/common/widgets/address_input.dart';
import 'package:superport/screens/common/widgets/remark_input.dart';

/// 지점 입력 폼 위젯
///
/// BranchFormController를 받아서 입력 필드, 드롭다운, 포커스, 전화번호 등 UI/상태를 관리한다.
class BranchFormWidget extends StatelessWidget {
  final BranchFormController controller;
  final int index;
  final void Function()? onRemove;
  final void Function(Address)? onAddressChanged;

  const BranchFormWidget({
    Key? key,
    required this.controller,
    required this.index,
    this.onRemove,
    this.onAddressChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      key: controller.cardKey,
      margin: const EdgeInsets.symmetric(vertical: 8),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: TextFormField(
                    controller: controller.nameController,
                    focusNode: controller.focusNode,
                    decoration: const InputDecoration(labelText: '지점명'),
                    onChanged: (value) => controller.updateField('name', value),
                  ),
                ),
                if (onRemove != null)
                  IconButton(
                    icon: const Icon(Icons.delete, color: Colors.red),
                    onPressed: onRemove,
                  ),
              ],
            ),
            const SizedBox(height: 8),
            // 주소 입력: 회사와 동일한 AddressInput 위젯 사용
            AddressInput(
              initialZipCode: controller.branch.address.zipCode,
              initialRegion: controller.branch.address.region,
              initialDetailAddress: controller.branch.address.detailAddress,
              isRequired: false,
              onAddressChanged: (zipCode, region, detailAddress) {
                controller.updateAddress(
                  Address(
                    zipCode: zipCode,
                    region: region,
                    detailAddress: detailAddress,
                  ),
                );
                if (onAddressChanged != null) {
                  onAddressChanged!(
                    Address(
                      zipCode: zipCode,
                      region: region,
                      detailAddress: detailAddress,
                    ),
                  );
                }
              },
            ),
            const SizedBox(height: 8),
            // 담당자 정보 입력: ContactInfoForm 위젯으로 대체 (회사 담당자와 동일 UI)
            ContactInfoForm(
              contactNameController: controller.contactNameController,
              contactPositionController: controller.contactPositionController,
              contactPhoneController: controller.contactPhoneController,
              contactEmailController: controller.contactEmailController,
              positions: controller.positions,
              selectedPhonePrefix: controller.selectedPhonePrefix,
              phonePrefixes: controller.phonePrefixes,
              onPhonePrefixChanged: (value) {
                controller.updatePhonePrefix(value);
              },
              onNameSaved: (value) {
                controller.updateField('contactName', value ?? '');
              },
              onPositionSaved: (value) {
                controller.updateField('contactPosition', value ?? '');
              },
              onPhoneSaved: (value) {
                controller.updateField('contactPhone', value ?? '');
              },
              onEmailSaved: (value) {
                controller.updateField('contactEmail', value ?? '');
              },
            ),
            const SizedBox(height: 8),
            // 비고 입력란
            RemarkInput(controller: controller.remarkController),
          ],
        ),
      ),
    );
  }
}
