import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_record_info/controller.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/field_layouts.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/input_text_field.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/record_tabs_bar.dart';
import 'package:get/get.dart';

class RecordInfoFields extends GetView<LabRecordInfoController> {
  const RecordInfoFields({
    Key? key,
    this.tabHashCode,
  }) : super(key: key);
  final String? tabHashCode;
  @override
  String? get tag => tabHashCode;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        RecordTabsBar(
          tabHashCode: tabHashCode,
        ),
        Expanded(
          child: GetBuilder<LabRecordInfoController>(
              id: LabRecordInfoController.recordInfoFieldId,
              tag: tabHashCode,
              builder: (context) {
                return ListView(
                  padding: EdgeInsets.all(20),
                  children: _buildFields(),
                );
              }),
        ),
      ],
    );
  }

  List<Widget> _buildFields() {
    // 以四个为一组，组装为 _buildFourWidgetField 中间插入 SizedBox
    List<Widget> fields = [];
    controller.fieldsResult.addAll({
      controller.currentTab:
          controller.fieldsResult[controller.currentTab] ?? {}
    });

    for (int i = 0; i < controller.recordInfoFields.length; i += 4) {
      fields.add(FieldLayout(fields: [
        InputTextField(
          label: controller.getValues(
            controller.recordInfoFields[i],
          ),
          intialValue: controller.fieldsResult[controller.currentTab]
                  ?[controller.recordInfoFields[i]] ??
              '',
          onChanged: (value) {
            controller.fieldsResult[controller.currentTab]
                ?[controller.recordInfoFields[i]] = value;
            if (controller.currentTab == "Routine") {
              controller.updateBsaAndBmi();
            }
          },
        ),
        if (controller.recordInfoFields.length > i + 1)
          InputTextField(
            label: controller.getValues(
              controller.recordInfoFields[i + 1],
            ),
            intialValue: controller.fieldsResult[controller.currentTab]
                    ?[controller.recordInfoFields[i + 1]] ??
                '',
            onChanged: (value) {
              controller.fieldsResult[controller.currentTab]
                  ?[controller.recordInfoFields[i + 1]] = value;
              if (controller.currentTab == "Routine") {
                controller.updateBsaAndBmi();
              }
            },
          ),
        if (controller.recordInfoFields.length > i + 2)
          InputTextField(
            label: controller.getValues(
              controller.recordInfoFields[i + 2],
            ),
            intialValue: controller.fieldsResult[controller.currentTab]
                    ?[controller.recordInfoFields[i + 2]] ??
                '',
            onChanged: (value) {
              controller.fieldsResult[controller.currentTab]
                  ?[controller.recordInfoFields[i + 2]] = value;
            },
          ),
        if (controller.recordInfoFields.length > i + 3)
          InputTextField(
            label: controller.getValues(
              controller.recordInfoFields[i + 3],
            ),
            intialValue: controller.fieldsResult[controller.currentTab]
                    ?[controller.recordInfoFields[i + 3]] ??
                '',
            onChanged: (value) {
              controller.fieldsResult[controller.currentTab]
                  ?[controller.recordInfoFields[i + 3]] = value;
            },
          ),
      ]));
      if (i + 4 < controller.recordInfoFields.length) {
        fields.add(SizedBox(height: 20));
      }
    }
    return fields;
  }
}
