// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_record_info/controller.dart';
import 'package:flyinsono/lab/pages/lab_record_info/widgets/index.dart';
import 'package:flyinsono/utils/datetime.dart';

import 'package:get/get.dart';

class BaseInfoFields extends GetView<LabRecordInfoController> {
  BaseInfoFields({
    Key? key,
    this.tabHashCode,
    this.isPerson,
  }) : super(key: key);
  final String? tabHashCode;

  bool? isPerson = false;

  @override
  String? get tag => tabHashCode;
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabRecordInfoController>(
      id: LabRecordInfoController.baseInfoFieldId,
      tag: tabHashCode,
      builder: (context) {
        if (isPerson!)
          return _buildPerson();
        else
          return _buildAnimals();
      },
    );
  }

  Widget _buildPerson() {
    return ListView(
      key: GlobalKey(),
      padding: EdgeInsets.all(10),
      children: [
        FieldLayout(
          layoutType: LayoutType.OOOOOOO,
          fields: [
            InputTextField(
              label: "姓名",
              editable: false,
              intialValue: controller.initPatientName,
            ),
          ],
        ),
        // SizedBox(height: 20),
        FieldLayout(
          layoutType: LayoutType.OOO_OOO,
          fields: [
            InputTextField(
              label: "病人ID",
              editable: false,
              intialValue: controller.initPatientId,
            ),
            InputTextField(
              label: "副ID",
              intialValue: "",
              editable: false,
            ),
          ],
        ),
        FieldLayout(
          layoutType: LayoutType.OOO_O_O,
          fields: [
            Obx(
              () => DateSelectorField(
                label: "出生日期",
                editable: false,
                initialValue: controller.state.birthday,
                onChanged: (value) {
                  controller.state.birthday = value;
                  controller.state.birthdayAge = DataTimeUtils.calculateAge(
                    controller.state.birthday ?? DateTime.now(),
                  );
                },
              ),
            ),
            Obx(
              () => InputTextField(
                label: "年龄",
                intialValue: controller.state.birthdayAge,
                editable: false,
              ),
            ),
            Obx(
              () => ValueSelectorField(
                label: "性别",
                intialValue: controller.state.sex ?? '未选择',
                dataSource: ["未选择", "Male", "Female"],
                onChanged: (value) {
                  controller.state.sex = value;
                },
              ),
            )
          ],
        ),
      ],
    );
  }

  Widget _buildAnimals() {
    return ListView(
      key: GlobalKey(),
      padding: EdgeInsets.all(10),
      children: [
        FieldLayout(
          layoutType: LayoutType.OOO_OOO,
          fields: [
            Obx(
              () => InputTextField(
                label: "主人",
                editable: false,
                intialValue: controller.state.owner ?? '',
              ),
            ),
            InputTextField(
              label: "姓名",
              editable: false,
              intialValue: controller.initPatientName,
            ),
          ],
        ),
        // SizedBox(height: 20),
        FieldLayout(
          layoutType: LayoutType.OOO_O_O,
          fields: [
            InputTextField(
              label: "病人ID",
              editable: false,
              intialValue: controller.initPatientId,
            ),
            InputTextField(
              label: "副ID",
              intialValue: "",
              editable: false,
            ),
            Obx(
              () => InputTextField(
                label: "年龄",
                intialValue: controller.state.birthdayAge,
                editable: false,
              ),
            ),
          ],
        ),
        FieldLayout(
          layoutType: LayoutType.OOO_O_O,
          fields: [
            Obx(
              () => ValueSelectorField(
                label: "性别",
                intialValue: controller.state.sex ?? '未选择',
                dataSource: ["未选择", "AnimalInfoMale", "AnimalInfoFemale"],
                onChanged: (value) {
                  controller.state.sex = value;
                },
              ),
            ),
            Obx(
              () => ValueSelectorField(
                label: "种类",
                intialValue: controller.state.species ?? "未选择",
                dataSource: [
                  "未选择",
                  "Canidae",
                  "Equidae",
                  "Felidae",
                  "Caprinae",
                  "Suidae",
                  "Bovine",
                  "Mouse",
                  "Rat",
                ],
                editable: false,
                onChanged: (value) {
                  controller.state.species = value;
                },
              ),
            ),
            Obx(
              () => InputTextField(
                label: "品种",
                intialValue: controller.state.breed ?? '',
                editable: false,
              ),
            )
          ],
        ),
      ],
    );
  }
}
