import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/main/registration_form_controller.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/step/officer-information/unit_info_controller.dart';
import 'package:sigap/src/shared/widgets/dropdown/custom_dropdown.dart';
import 'package:sigap/src/shared/widgets/form/form_section_header.dart';
import 'package:sigap/src/shared/widgets/text/custom_text_field.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/validators/validation.dart';

class UnitInfoStep extends StatelessWidget {
  const UnitInfoStep({super.key});

  @override
  Widget build(BuildContext context) {
    final formKey = GlobalKey<FormState>();
    final controller = Get.find<UnitInfoController>();
    final mainController = Get.find<FormRegistrationController>();
    mainController.formKey = formKey;

    return Form(
      key: formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const FormSectionHeader(
            title: 'Unit Information',
            subtitle: 'Please provide your unit details',
          ),

          const SizedBox(height: TSizes.spaceBtwItems),

          // Position field
          Obx(
            () => CustomTextField(
              label: 'Position',
              controller: controller.positionController,
              validator: TValidators.validatePosition,
              errorText: controller.positionError.value,
              textInputAction: TextInputAction.next,
              hintText: 'e.g., Commander',
              onChanged: (value) {
                controller.positionController.text = value;
                controller.positionError.value = '';
              },
            ),
          ),

          // Unit Dropdown
          Obx(
            () => CustomDropdown(
              label: 'Unit',
              items:
                  controller.availableUnits
                      .map(
                        (unit) => DropdownMenuItem(
                          value: unit.codeUnit,
                          child: Text(unit.name),
                        ),
                      )
                      .toList(),
              value:
                  controller.unitIdController.text.isEmpty
                      ? null
                      : controller.unitIdController.text,
              onChanged: (value) {
                if (value != null) {
                  controller.unitIdController.text = value.toString();
                  controller.unitIdError.value = '';
                }
              },
              validator: (value) => TValidators.validateUnitId(value),
              errorText: controller.unitIdError.value,
            ),
          ),
        ],
      ),
    );
  }
}
