import 'package:cs_resources/constants/color_constants.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:plugin_basic/basic_export.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/picker/option_pick_util.dart';
import 'package:widgets/shatter/round_my_text_field.dart';
import 'package:domain/entity/agency_need_number.dart';

/*
 * 代理商选择人数的控件封装
 * 选择性别限制的数量或者不限制性别限制的数量
 */
class AgencyNeedNumberWidget extends StatefulWidget {
  final String? agencyId;
  final String? agencyName;

  AgencyNeedNumberWidget({
    this.agencyId,
    this.agencyName,
    Key? key,
  }) : super(key: key ?? GlobalKey<AgencyNeedNumberState>()); // 使用新的 GlobalKey

  @override
  State<AgencyNeedNumberWidget> createState() => AgencyNeedNumberState();
}

class AgencyNeedNumberState extends State<AgencyNeedNumberWidget> {
  int limitType = 0;
  List<String> limitTypeList = [
    "Gender Unlimited".tr,
    "Gender Limited".tr,
  ];

  final Map<String, Map<String, dynamic>> formData = {
    'need_male': {
      'value': '',
      'controller': TextEditingController(),
      'focusNode': FocusNode(),
      'hintText': 'Male'.tr,
      'obsecure': false,
    },
    'need_female': {
      'value': '',
      'controller': TextEditingController(),
      'focusNode': FocusNode(),
      'hintText': 'Female'.tr,
      'obsecure': false,
    },
    'need_no': {
      'value': '',
      'controller': TextEditingController(),
      'focusNode': FocusNode(),
      'hintText': 'Needs Num'.tr,
      'obsecure': false,
    },
  };

  @override
  void initState() {
    super.initState();
    limitType = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        //中介性别
        MyTextView(
          widget.agencyName ?? "-",
          fontSize: 15,
          isFontRegular: true,
          marginBottom: 7,
          textColor: Colors.white,
          marginTop: 15,
        ),

        Row(
          children: [
            Container(
              padding: const EdgeInsets.only(left: 16, right: 10),
              margin: const EdgeInsets.only(right: 12),
              height: 45,
              decoration: BoxDecoration(
                color: const Color(0xFF4DCFF6).withOpacity(0.2),
                borderRadius: const BorderRadius.all(Radius.circular(5)),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  MyTextView(
                    limitTypeList[limitType],
                    fontSize: 14,
                    textHintColor: ColorConstants.textGrayAECAE5,
                    isFontRegular: true,
                    textColor: ColorConstants.white,
                  ).expanded(),
                  //下拉选图标
                  const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                ],
              ),
            ).onTap(() {
              FocusScope.of(context).unfocus();
              pickLimitType();
            }).expanded(flex: 55),

            //输入框-不限制性别
            Visibility(
              visible: limitType == 0,
              child: CustomTextField(
                formKey: "need_no",
                marginLeft: 0,
                marginRight: 0,
                paddingTop: 0,
                paddingBottom: 0,
                height: 45,
                fillBackgroundColor: const Color(0xFF4DCFF6).withOpacity(0.2),
                inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                textInputType: TextInputType.number,
                formData: formData,
                textInputAction: TextInputAction.done,
                onSubmit: (key, value) {
                  FocusScope.of(context).unfocus();
                },
              ).expanded(flex: 55),
            ),

            //输入框组-限制性别
            Visibility(
              visible: limitType != 0,
              child: Row(
                children: [
                  Row(
                    children: [
                      MyTextView(
                        "M",
                        fontSize: 15,
                        paddingLeft: 10,
                        isFontRegular: true,
                        textColor: ColorConstants.white,
                      ),
                      CustomTextField(
                        formKey: "need_male",
                        marginLeft: 0,
                        marginRight: 0,
                        paddingTop: 0,
                        paddingBottom: 0,
                        paddingLeft: 10,
                        paddingRight: 10,
                        height: 45,
                        cornerRadius: 0,
                        fillBackgroundColor: Colors.transparent,
                        inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                        textInputType: TextInputType.number,
                        formData: formData,
                        textInputAction: TextInputAction.done,
                        onSubmit: (key, value) {
                          FocusScope.of(context).unfocus();
                        },
                      ).expanded(),
                    ],
                  )
                      .decorated(
                        color: const Color(0xFF4DCFF6).withOpacity(0.2),
                        borderRadius: const BorderRadius.all(Radius.circular(5)),
                      )
                      .expanded(),
                  const SizedBox(width: 12),
                  Row(
                    children: [
                      MyTextView(
                        "F",
                        fontSize: 15,
                        paddingLeft: 10,
                        isFontRegular: true,
                        textColor: ColorConstants.white,
                      ),
                      CustomTextField(
                        formKey: "need_female",
                        marginLeft: 0,
                        marginRight: 0,
                        paddingTop: 0,
                        paddingBottom: 0,
                        paddingLeft: 10,
                        paddingRight: 10,
                        height: 45,
                        cornerRadius: 0,
                        fillBackgroundColor: Colors.transparent,
                        inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                        textInputType: TextInputType.number,
                        formData: formData,
                        textInputAction: TextInputAction.done,
                        onSubmit: (key, value) {
                          FocusScope.of(context).unfocus();
                        },
                      ).expanded(),
                    ],
                  )
                      .decorated(
                        color: const Color(0xFF4DCFF6).withOpacity(0.2),
                        borderRadius: const BorderRadius.all(Radius.circular(5)),
                      )
                      .expanded(),
                ],
              ).expanded(flex: 55),
            ),
          ],
        ),
      ],
    );
  }

  //选择分类
  void pickLimitType() {
    OptionPickerUtil.showCupertinoOptionPicker(
      items: limitTypeList,
      initialSelectIndex: limitType,
      onPickerChanged: (_, index) {
        setState(() {
          limitType = index;
        });
      },
    );
  }

  // 返回 AgencyNeedNumberEntity 对象
  AgencyNeedNumberEntity getAgencyNeedNumberEntity() {
    AgencyNeedNumberEntity entity = AgencyNeedNumberEntity();
    entity.isInHouse = widget.agencyId != null && (widget.agencyId == "0");
    entity.agencyId = widget.agencyId;
    entity.agencyName = widget.agencyName;
    entity.sexLimit = limitType; // 设置性别限制
    if (limitType == 0) {
      // 不限制性别的总人数
      entity.needNum = int.tryParse(formData['need_no']!['controller']!.text) ?? 0;
    } else {
      // 限制性别
      entity.maleLimit = int.tryParse(formData['need_male']!['controller']!.text) ?? 0;
      entity.femaleLimit = int.tryParse(formData['need_female']!['controller']!.text) ?? 0;
    }
    return entity;
  }
}
